source: trunk/tools/database/db.cpp@ 6651

Last change on this file since 6651 was 6651, checked in by bird, 24 years ago

Some more fixes.

File size: 77.1 KB
Line 
1/* $Id: db.cpp,v 1.24 2001-09-05 13:59:03 bird Exp $ *
2 *
3 * DB - contains all database routines.
4 *
5 * Copyright (c) 1999-2000 knut st. osmundsen
6 *
7 */
8
9/*******************************************************************************
10* Defined Constants *
11*******************************************************************************/
12#define CheckLogContinue(sprintfargs) \
13 if (rc < 0) \
14 { \
15 if (pszError[1] == '\xFE') \
16 { \
17 strcat(pszError, "\n\t"); \
18 pszError += 2; \
19 } \
20 sprintf sprintfargs; \
21 ulRc++; \
22 pszError += strlen(pszError); \
23 pszError[1] = '\xFE'; \
24 } \
25 rc=rc
26
27
28#define CheckFKError(table, msg) \
29 pres2 = mysql_store_result(pmysql);\
30 if (rc < 0 || pres2 == NULL || \
31 mysql_num_rows(pres2) == 0) \
32 { \
33 if (pszError[1] == '\xFE') \
34 { \
35 strcat(pszError, "\n\t"); \
36 pszError += 2; \
37 } \
38 sprintf(pszError, table ":" \
39 msg \
40 " (refcode=%s) " \
41 "(sql=%s)", \
42 row1[0], \
43 pszQuery); \
44 ulRc++; \
45 pszError += strlen(pszError); \
46 pszError[1] = '\xFE'; \
47 } \
48 if (pres2 != NULL) \
49 mysql_free_result(pres2)
50
51
52/*******************************************************************************
53* Header Files *
54*******************************************************************************/
55#define INCL_DOSMISC
56#include <os2.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <memory.h>
61#include <signal.h>
62#include <assert.h>
63#include <limits.h>
64#include <mysql.h>
65
66#include "db.h"
67
68
69/*@Global***********************************************************************
70* Global Variables *
71*******************************************************************************/
72static MYSQL mysql;
73static MYSQL *pmysql = NULL;
74
75
76/*@IntFunc**********************************************************************
77* Internal Functions *
78*******************************************************************************/
79static long getvalue(int iField, MYSQL_ROW pRow);
80static unsigned long CheckAuthorError(char * &pszError, const char *pszFieldName, const char *pszFieldValue, const char *pszQuery);
81static unsigned long logDbError(char * &pszError, const char *pszQuery);
82static char *sqlstrcat(char *pszQuery, const char *pszBefore, const char *pszStr, const char *pszAfter = NULL);
83
84#ifndef DLL
85 extern "C" void dbHandler(int sig);
86#endif
87
88
89/**
90 * Gets the descriptions of the last database error.
91 * @returns Readonly string.
92 */
93char * _System dbGetLastErrorDesc(void)
94{
95 return mysql_error(&mysql);
96}
97
98
99/**
100 * Connects to local database.
101 * @returns Success indicator, TRUE / FALSE.
102 * @param pszDatabase Name of database to use.
103 */
104BOOL _System dbConnect(const char *pszHost, const char *pszUser, const char *pszPassword, const char *pszDatabase)
105{
106 BOOL fRet = FALSE;
107 #ifndef DLL
108 static fHandler = FALSE;
109 /* signal handler */
110 if (!fHandler)
111 {
112 if ( SIG_ERR == signal(SIGBREAK, dbHandler)
113 || SIG_ERR == signal(SIGINT, dbHandler)
114 || SIG_ERR == signal(SIGTERM, dbHandler)
115 || SIG_ERR == signal(SIGABRT, dbHandler)
116 || SIG_ERR == signal(SIGSEGV, dbHandler)
117 || SIG_ERR == signal(SIGILL, dbHandler)
118 )
119 fprintf(stderr, "Error installing signalhandler...");
120 else
121 fHandler = TRUE;
122 }
123 #endif
124
125 /* connect to server */
126 memset(&mysql, 0, sizeof(mysql));
127 pmysql = mysql_connect(&mysql, pszHost, pszUser, pszPassword);
128 if (pmysql != NULL)
129 {
130 /* connect to database */
131 fRet = mysql_select_db(pmysql, pszDatabase) >= 0;
132 if (fRet)
133 mysql_refresh(pmysql, REFRESH_TABLES);
134 }
135
136 return fRet;
137}
138
139
140/**
141 * Disconnects from database.
142 * @returns Success indicator. TRUE / FALSE.
143 */
144BOOL _System dbDisconnect(void)
145{
146 if (pmysql != NULL)
147 {
148 mysql_refresh(pmysql, REFRESH_TABLES);
149 mysql_close(pmysql);
150 pmysql = NULL;
151 }
152 return TRUE;
153}
154
155/**
156 * Gets the refid for the give dll name.
157 * @returns Dll refid. -1 on error.
158 * @param pszDllName Dll name.
159 */
160signed long _System dbGetDll(const char *pszDllName)
161{
162 int rc;
163 char szQuery[256];
164 MYSQL_RES * pres;
165
166 sprintf(&szQuery[0], "SELECT refcode FROM dll WHERE name = '%s'\n", pszDllName);
167 rc = mysql_query(pmysql, &szQuery[0]);
168 pres = mysql_store_result(pmysql);
169
170 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
171 rc = (int)getvalue(0, mysql_fetch_row(pres));
172 else
173 rc = -1;
174 mysql_free_result(pres);
175 return (signed long)rc;
176}
177
178
179/**
180 * Count the function in a given dll.
181 * @returns Number of functions. -1 on error.
182 * @param lDll Dll refcode.
183 * @param fNotAliases TRUE: don't count aliased functions.
184 */
185signed long _System dbCountFunctionInDll(signed long lDll, BOOL fNotAliases)
186{
187 signed long rc;
188 char szQuery[256];
189 MYSQL_RES * pres;
190
191 if (lDll >= 0)
192 {
193 sprintf(&szQuery[0], "SELECT count(refcode) FROM function WHERE dll = %ld\n", lDll);
194 if (fNotAliases)
195 strcat(&szQuery[0], " AND aliasfn < 0");
196 rc = mysql_query(pmysql, &szQuery[0]);
197 pres = mysql_store_result(pmysql);
198
199 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
200 rc = (int)getvalue(0, mysql_fetch_row(pres));
201 else
202 rc = -1;
203 mysql_free_result(pres);
204 }
205 else
206 rc = -1;
207 return rc;
208}
209
210
211
212/**
213 * Checks if dll exists. If not exists the dll is inserted.
214 * @returns Dll refcode. -1 on errors.
215 * @param pszDll Dll name.
216 * @remark This search must be case insensitive.
217 * (In the mysql-world everything is case insensitive!)
218 */
219signed long _System dbCheckInsertDll(const char *pszDll, char fchType)
220{
221 int rc;
222 char szQuery[256];
223 MYSQL_RES * pres;
224
225 /* try find match */
226 sprintf(&szQuery[0], "SELECT refcode, name FROM dll WHERE name = '%s'\n", pszDll);
227 rc = mysql_query(pmysql, &szQuery[0]);
228 pres = mysql_store_result(pmysql);
229
230 /* not found? - insert dll */
231 if (rc < 0 || pres == NULL || mysql_num_rows(pres) == 0)
232 {
233 mysql_free_result(pres);
234
235 sprintf(&szQuery[0], "INSERT INTO dll(name, type) VALUES('%s', '%c')\n", pszDll, fchType);
236 rc = mysql_query(pmysql, &szQuery[0]);
237 if (rc < 0)
238 return -1;
239
240 /* select row to get refcode */
241 sprintf(&szQuery[0], "SELECT refcode, name FROM dll WHERE name = '%s'\n", pszDll);
242 rc = mysql_query(pmysql, &szQuery[0]);
243 pres = mysql_store_result(pmysql);
244 }
245
246 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
247 rc = (int)getvalue(0, mysql_fetch_row(pres));
248 else
249 rc = -1;
250 mysql_free_result(pres);
251
252 return (long)rc;
253}
254
255
256/**
257 * Simple select for a long value.
258 * @returns long value
259 * @param pszTable From part.
260 * @param pszGetColumn Name of column to retreive.
261 * @param pszMatch1 Match column/expression
262 * @param pszMatchValue1 Match value.
263 * @remark Dirty! Don't use this!
264 */
265unsigned short _System dbGet(const char *pszTable, const char *pszGetColumn,
266 const char *pszMatch1, const char *pszMatchValue1)
267{
268 int rc;
269 char szQuery[256];
270 MYSQL_RES *pres;
271
272 /* try find match */
273 sprintf(&szQuery[0], "SELECT %s FROM %s WHERE %s = '%s'\n",
274 pszGetColumn, pszTable, pszMatch1, pszMatchValue1);
275 rc = mysql_query(pmysql, &szQuery[0]);
276 pres = mysql_store_result(pmysql);
277
278 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
279 rc = (int)getvalue(0, mysql_fetch_row(pres));
280 else
281 rc = -1;
282 mysql_free_result(pres);
283
284 return (unsigned short)rc;
285}
286
287
288/**
289 * Updates or inserts a function name into the database.
290 * The update flags is always updated.
291 * @returns Success indicator. TRUE / FALSE.
292 * @param lDll Dll refcode.
293 * @param pszFunction Function name.
294 * @param pszIntFunction Internal function name. (required!)
295 * @param ulOrdinal Ordinal value.
296 * @param fIgnoreOrdinal Do not update ordinal value.
297 * @param fchType Function type flag. One of the FUNCTION_* defines.
298 */
299BOOL _System dbInsertUpdateFunction(signed long lDll,
300 const char *pszFunction, const char *pszIntFunction,
301 unsigned long ulOrdinal, BOOL fIgnoreOrdinal, char fchType)
302{
303 int rc;
304 long lFunction = -1;
305 char szQuery[512];
306 char * pszQuery = &szQuery[0];
307 MYSQL_RES *pres;
308
309 /* when no internal name fail! */
310 if (pszIntFunction == NULL || *pszIntFunction == '\0')
311 return FALSE;
312
313 /* try find function */
314 sprintf(pszQuery, "SELECT refcode, intname FROM function WHERE dll = %d AND name = '%s'", lDll, pszFunction);
315 rc = mysql_query(pmysql, pszQuery);
316 pres = mysql_store_result(pmysql);
317 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) != 0)
318 { /*
319 * Found the function. So now we'll update it.
320 */
321 MYSQL_ROW parow;
322 if (mysql_num_rows(pres) > 1)
323 {
324 fprintf(stderr, "internal database integrity error(%s): More function by the same name for the same dll. "
325 "lDll = %d, pszFunction = %s\n", __FUNCTION__, lDll, pszFunction);
326 return FALSE;
327 }
328
329 parow = mysql_fetch_row(pres);
330 lFunction = getvalue(0, parow);
331 mysql_free_result(pres);
332
333 strcpy(pszQuery, "UPDATE function SET updated = updated + 1");
334 pszQuery += strlen(pszQuery);
335 if (strcmp(parow[1], pszIntFunction) != 0)
336 pszQuery += sprintf(pszQuery, ", intname = '%s'", pszIntFunction);
337
338 if (!fIgnoreOrdinal)
339 pszQuery += sprintf(pszQuery, ", ordinal = %ld", ulOrdinal);
340
341 sprintf(pszQuery, ", type = '%c' WHERE refcode = %ld", fchType, lFunction);
342 rc = mysql_query(pmysql, &szQuery[0]);
343 }
344 else
345 { /*
346 * The function was not found. (or maybe an error occured?)
347 * Insert it.
348 */
349 sprintf(&szQuery[0], "INSERT INTO function(dll, name, intname, ordinal, updated, type) VALUES(%d, '%s', '%s', %ld, 1, '%c')",
350 lDll, pszFunction, pszIntFunction, ulOrdinal, fchType);
351 rc = mysql_query(pmysql, &szQuery[0]);
352 }
353
354 return rc >= 0;
355}
356
357
358
359/**
360 * Inserts or updates (existing) file information.
361 * @returns Success indicator (TRUE / FALSE).
362 * @param lDll Dll reference code.
363 * @param pszFilename Filename.
364 * @param pszDescription Pointer to file description.
365 * @param pszLastDateTime Date and time for last change (ISO).
366 * @param lLastAuthor Author number. (-1 if not found.)
367 * @param pszRevision Pointer to revision string.
368 * @sketch
369 * @remark
370 */
371BOOL _System dbInsertUpdateFile(signed long lDll,
372 const char *pszFilename,
373 const char *pszDescription,
374 const char *pszLastDateTime,
375 signed long lLastAuthor,
376 const char *pszRevision)
377{
378 int rc;
379 long lFile = -1;
380 char szQuery[0x10000];
381 MYSQL_RES *pres;
382
383 /* parameter assertions */
384 assert(lDll != 0);
385 assert(pszFilename != NULL);
386 assert(*pszFilename != '\0');
387
388 /* try find file */
389 sprintf(&szQuery[0], "SELECT refcode, name FROM file WHERE dll = %d AND name = '%s'", lDll, pszFilename);
390 rc = mysql_query(pmysql, &szQuery[0]);
391 pres = mysql_store_result(pmysql);
392 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) != 0)
393 { /* update file (file is found) */
394 MYSQL_ROW parow;
395 if (mysql_num_rows(pres) > 1)
396 {
397 fprintf(stderr, "internal database integrity error(%s): More files by the same name in the same dll. "
398 "lDll = %d, pszFilename = %s\n", __FUNCTION__, lDll, pszFilename);
399 return FALSE;
400 }
401
402 parow = mysql_fetch_row(pres);
403 assert(parow);
404 lFile = getvalue(0, parow);
405 mysql_free_result(pres);
406
407 if (strcmp(parow[1], pszFilename) != 0) /* case might have changed... */
408 {
409 sprintf(&szQuery[0], "UPDATE file SET name = '%s' WHERE refcode = %ld",
410 pszFilename, lFile);
411 rc = mysql_query(pmysql, &szQuery[0]);
412 }
413
414 if (rc >= 0)
415 {
416 if (pszDescription != NULL && pszDescription != '\0')
417 {
418 szQuery[0] = '\0';
419 sqlstrcat(&szQuery[0], "UPDATE file SET description = ", pszDescription, NULL);
420 sprintf(&szQuery[strlen(szQuery)], " WHERE refcode = %ld", lFile);
421 }
422 else
423 sprintf(&szQuery[0], "UPDATE file SET description = NULL WHERE refcode = %ld",
424 lFile);
425 rc = mysql_query(pmysql, &szQuery[0]);
426 }
427
428 if (rc >= 0 && pszLastDateTime != NULL && *pszLastDateTime != '\0')
429 {
430 sprintf(&szQuery[0], "UPDATE file SET lastdatetime = '%s' WHERE refcode = %ld",
431 pszLastDateTime, lFile);
432 rc = mysql_query(pmysql, &szQuery[0]);
433 }
434
435 if (rc >= 0)
436 {
437 sprintf(&szQuery[0], "UPDATE file SET lastauthor = %ld WHERE refcode = %ld",
438 lLastAuthor, lFile);
439 rc = mysql_query(pmysql, &szQuery[0]);
440 }
441
442 if (rc >= 0 && pszRevision != NULL && *pszRevision != '\0')
443 {
444 sprintf(&szQuery[0], "UPDATE file SET revision = '%s' WHERE refcode = %ld",
445 pszRevision, lFile);
446 rc = mysql_query(pmysql, &szQuery[0]);
447 }
448
449 }
450 else
451 { /* insert */
452 sprintf(&szQuery[0], "INSERT INTO file(dll, name, lastauthor, description, lastdatetime, revision) VALUES(%d, '%s', %ld, ",
453 lDll, pszFilename, lLastAuthor);
454 if (pszDescription != NULL && *pszDescription != '\0')
455 sqlstrcat(&szQuery[0], NULL, pszDescription);
456 else
457 strcat(&szQuery[0], "NULL");
458
459 if (pszLastDateTime != NULL && *pszLastDateTime != '\0')
460 sqlstrcat(&szQuery[0], ", ", pszLastDateTime);
461 else
462 strcat(&szQuery[0], ", '1975-03-13 14:00:00'"); /* dummy */
463
464 if (pszRevision != NULL && *pszRevision != '\0')
465 sqlstrcat(&szQuery[0], ", ", pszRevision, ")");
466 else
467 strcat(&szQuery[0], ", '')");
468
469 rc = mysql_query(pmysql, &szQuery[0]);
470 }
471
472 return rc >= 0;
473}
474
475
476/**
477 * Get a long value.
478 * @returns Number value of pRow[iField]. -1 on error.
479 * @param iField Index into pRow.
480 * @param pRow Pointer to array (of string pointers).
481 */
482static long getvalue(int iField, MYSQL_ROW papszRow)
483{
484 if (papszRow[iField] != NULL)
485 return atol((char*)papszRow[iField]);
486
487 return -1;
488}
489
490
491#if 0
492/*
493 * Stubs used while optimizing sqls.
494 */
495int mysql_query1(MYSQL *mysql, const char *q)
496{ return mysql_query(mysql, q); }
497int mysql_query2(MYSQL *mysql, const char *q)
498{ return mysql_query(mysql, q); }
499int mysql_query3(MYSQL *mysql, const char *q)
500{ return mysql_query(mysql, q); }
501int mysql_query4(MYSQL *mysql, const char *q)
502{ return mysql_query(mysql, q); }
503int mysql_query5(MYSQL *mysql, const char *q)
504{ return mysql_query(mysql, q); }
505int mysql_query6(MYSQL *mysql, const char *q)
506{ return mysql_query(mysql, q); }
507
508#else
509
510#define mysql_query1 mysql_query
511#define mysql_query2 mysql_query
512#define mysql_query3 mysql_query
513#define mysql_query4 mysql_query
514#define mysql_query5 mysql_query
515#define mysql_query6 mysql_query
516
517#endif
518
519
520
521/**
522 * Find occurences of a function, given by internal name.
523 * @returns success indicator, TRUE / FALSE.
524 * @param pszFunctionName Pointer to a function name string. (input)
525 * @param pFnFindBuf Pointer to a find buffer. (output)
526 * @param lDll Dll refcode (optional). If given the search is limited to
527 * the given dll and aliasing functions is updated (slow!).
528 * @sketch 1) Get functions for this dll(if given).
529 * 2) Get functions which aliases the functions found in (1).
530 * 3) Get new aliases by intname
531 * 4) Get new aliases by name
532 * 5) Update all functions from (1) to have aliasfn -2 (DONTMIND)
533 * 6) Update all functions from (3) and (4) to alias the first function from 1.
534 */
535BOOL _System dbFindFunction(const char *pszFunctionName, PFNFINDBUF pFnFindBuf, signed long lDll)
536{
537 MYSQL_RES *pres;
538 MYSQL_ROW row;
539 int rc;
540 char szQuery[1024];
541
542 /*
543 * 1) Get functions for this dll(if given).
544 */
545 if (lDll < 0)
546 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function WHERE intname = '%s'",
547 pszFunctionName);
548 else
549 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function "
550 "WHERE intname = '%s' AND dll = %ld",
551 pszFunctionName, lDll);
552
553 rc = mysql_query1(pmysql, &szQuery[0]);
554 if (rc >= 0)
555 {
556 pres = mysql_store_result(pmysql);
557 if (pres != NULL)
558 {
559 char szFnName[NBR_FUNCTIONS][80];
560
561 pFnFindBuf->cFns = 0;
562 while ((row = mysql_fetch_row(pres)) != NULL)
563 {
564 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]);
565 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
566 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
567 pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]);
568 strcpy(szFnName[pFnFindBuf->cFns], row[4]);
569
570 /* next */
571 pFnFindBuf->cFns++;
572 }
573 mysql_free_result(pres);
574
575 /* alias check and fix */
576 if (lDll >= 0 && pFnFindBuf->cFns != 0)
577 {
578 int cFnsThisDll, cFnsAliasesAndThisDll, i, f;
579
580 /*
581 * 2) Get functions which aliases the functions found in (1).
582 */
583 cFnsThisDll = (int)pFnFindBuf->cFns;
584 strcpy(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function WHERE aliasfn IN (");
585 for (i = 0; i < cFnsThisDll; i++)
586 {
587 if (i > 0) strcat(&szQuery[0], " OR ");
588 sprintf(&szQuery[strlen(szQuery)], "(%ld)", pFnFindBuf->alRefCode[i]);
589 }
590 strcat(&szQuery[0], ")");
591
592 rc = mysql_query2(pmysql, &szQuery[0]);
593 if (rc >= 0)
594 {
595 pres = mysql_store_result(pmysql);
596 if (pres != NULL)
597 {
598 while ((row = mysql_fetch_row(pres)) != NULL)
599 {
600 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]);
601 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
602 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
603 pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]);
604 strcpy(szFnName[pFnFindBuf->cFns], row[4]);
605
606 /* next */
607 pFnFindBuf->cFns++;
608 }
609 mysql_free_result(pres);
610
611 /*
612 * 3) Get new aliases by intname
613 */
614 cFnsAliasesAndThisDll = (int)pFnFindBuf->cFns;
615 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file FROM function "
616 "WHERE aliasfn = (-1) AND dll <> %ld AND (intname = '%s'",
617 lDll, pszFunctionName);
618 for (i = 0; i < cFnsAliasesAndThisDll; i++)
619 sprintf(&szQuery[strlen(&szQuery[0])], " OR intname = '%s'", szFnName[i]);
620 strcat(&szQuery[0], ")");
621
622 rc = mysql_query3(pmysql, &szQuery[0]);
623 if (rc >= 0)
624 {
625 pres = mysql_store_result(pmysql);
626 if (pres != NULL)
627 {
628 while ((row = mysql_fetch_row(pres)) != NULL)
629 {
630 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]);
631 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
632 if (row[2] != NULL)
633 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
634 else
635 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL;
636 pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]);
637
638 /* next */
639 pFnFindBuf->cFns++;
640 }
641 mysql_free_result(pres);
642
643
644 /*
645 * 4) Get new aliases by name
646 */
647 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file FROM function "
648 "WHERE aliasfn = (-1) AND dll <> %ld AND (name = '%s'",
649 lDll, pszFunctionName);
650 for (i = 0; i < cFnsAliasesAndThisDll; i++)
651 sprintf(&szQuery[strlen(&szQuery[0])], " OR name = '%s'", szFnName[i]);
652 strcat(&szQuery[0], ")");
653
654 rc = mysql_query4(pmysql, &szQuery[0]);
655 if (rc >= 0)
656 {
657 pres = mysql_store_result(pmysql);
658 if (pres != NULL)
659 {
660 while ((row = mysql_fetch_row(pres)) != NULL)
661 {
662 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]);
663 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
664 if (row[2] != NULL)
665 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
666 else
667 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL;
668 pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]);
669
670 /* next */
671 pFnFindBuf->cFns++;
672 }
673 mysql_free_result(pres);
674
675 /*
676 * 5) Update all functions from (1) to have aliasfn -2 (DONTMIND)
677 */
678 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (-2) "
679 "WHERE refcode IN (",
680 lDll, pszFunctionName);
681 for (f = 0, i = 0; i < cFnsThisDll; i++)
682 if (pFnFindBuf->alAliasFn[i] != ALIAS_DONTMIND)
683 sprintf(&szQuery[strlen(&szQuery[0])],
684 f++ != 0 ? ", %ld" : "%ld", pFnFindBuf->alRefCode[i]);
685 strcat(&szQuery[0], ") AND aliasfn <> (-2)");
686 if (f > 0)
687 rc = mysql_query5(pmysql, &szQuery[0]);
688 else
689 rc = 0;
690 if (rc >= 0 && cFnsAliasesAndThisDll < pFnFindBuf->cFns)
691 {
692 /*
693 * 6) Update all functions from (3) and (4) to alias the first function from 1.
694 */
695 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld), file = (%ld) "
696 "WHERE aliasfn = (-1) AND refcode IN (",
697 pFnFindBuf->alRefCode[0], pFnFindBuf->alFileRefCode[0]);
698 for (i = cFnsAliasesAndThisDll; i < pFnFindBuf->cFns; i++)
699 {
700 sprintf(&szQuery[strlen(&szQuery[0])],
701 i > cFnsAliasesAndThisDll ? ", %ld" : "%ld", pFnFindBuf->alRefCode[i]);
702 }
703 strcat(&szQuery[0], ")");
704 rc = mysql_query6(pmysql, &szQuery[0]);
705 } /* query 5 */
706 }
707 } /* query 4 */
708 }
709 } /* query 3 */
710 }
711 } /* query 2 */
712 }
713 } /* query 1 */
714 else
715 rc = -1;
716 }
717
718 return rc >= 0;
719}
720
721
722/**
723 * Finds the refcode for a file (if it exists).
724 * @returns File 'refcode'.
725 * -1 on error or not found.
726 * @param lDll Refcode of the dll which this file belongs to.
727 * @param pszFilename The filename to search for.
728 */
729signed long _System dbFindFile(signed long lDll, const char *pszFilename)
730{
731 char szQuery[256];
732 MYSQL_RES * pres;
733 signed long lRefCode = -1;
734
735 assert(lDll >= 0);
736 assert(pszFilename != NULL);
737 assert(*pszFilename != '\0');
738
739 sprintf(&szQuery[0], "SELECT refcode FROM file WHERE dll = %ld AND name = '%s'",
740 lDll, pszFilename);
741 if (mysql_query(pmysql, &szQuery[0]) >= 0)
742 {
743 pres = mysql_store_result(pmysql);
744 if (pres != NULL)
745 {
746 MYSQL_ROW parow = mysql_fetch_row(pres);
747 if (parow != NULL)
748 lRefCode = getvalue(0, parow);
749 mysql_free_result(pres);
750 }
751 }
752
753 return lRefCode;
754}
755
756
757/**
758 * Finds the refcode for an author, if the author exists.
759 * @returns Author 'refcode'.
760 * @param pszAuthor String which holds the identifier of an author.
761 * This doesn't have to be the name. Initials, alias and email
762 * is also searched.
763 * @param pszEmail Email address. Might be NULL!
764 */
765signed long _System dbFindAuthor(const char *pszAuthor, const char *pszEmail)
766{
767 signed long refcode = -1;
768 MYSQL_RES *pres;
769 char szQuery[512];
770
771 /*
772 * parameter validations
773 */
774 if (pszAuthor == NULL || strlen(pszAuthor) > 64)
775 return -1;
776 if (pszEmail != NULL && strlen(pszEmail) > 64)
777 {
778 fprintf(stderr, "email too long!");
779 return -1;
780 }
781
782 /*
783 * Query
784 */
785 sprintf(&szQuery[0],
786 "SELECT refcode FROM author "
787 "WHERE name = '%s' OR "
788 " initials = '%s' OR "
789 " alias = '%s' OR "
790 " email = '%s'",
791 pszAuthor, pszAuthor, pszAuthor, pszAuthor);
792
793 if (pszEmail != NULL)
794 sprintf(&szQuery[strlen(&szQuery[0])], " OR email = '%s'", pszEmail);
795
796 if (mysql_query(pmysql, &szQuery[0]) >= 0)
797 {
798 pres = mysql_store_result(pmysql);
799 if (pres != NULL)
800 {
801 MYSQL_ROW parow;
802
803 /* integrety check */
804 if (mysql_num_rows(pres) > 1)
805 fprintf(stderr, "Integrety: author '%s' is not unique!\n", pszAuthor);
806 parow = mysql_fetch_row(pres);
807 if (parow != NULL)
808 refcode = getvalue(0, parow);
809
810 mysql_free_result(pres);
811 }
812 }
813
814 return refcode;
815}
816
817
818/**
819 * Gets the state of a function.
820 * @returns state code. On error -1.
821 * @param lRefCode Function refcode.
822 */
823signed long _System dbGetFunctionState(signed long lRefCode)
824{
825 signed long lState = -1;
826 MYSQL_RES *pres;
827 char szQuery[128];
828
829 sprintf(&szQuery[0], "SELECT state FROM function WHERE refcode = %ld", lRefCode);
830 if (mysql_query(pmysql, &szQuery[0]) >= 0)
831 {
832 pres = mysql_store_result(pmysql);
833 if (pres != NULL)
834 {
835 MYSQL_ROW parow = mysql_fetch_row(pres);
836 if (parow != NULL)
837 lState = getvalue(0, parow);
838 mysql_free_result(pres);
839 }
840 }
841
842 return lState;
843}
844
845#if 1
846/*
847 * Stubs used while optimizing sqls.
848 */
849int mysql_queryu1(MYSQL *mysql, const char *q)
850{ return mysql_query(mysql, q); }
851int mysql_queryu2(MYSQL *mysql, const char *q)
852{ return mysql_query(mysql, q); }
853int mysql_queryu3(MYSQL *mysql, const char *q)
854{ return mysql_query(mysql, q); }
855int mysql_queryu4(MYSQL *mysql, const char *q)
856{ return mysql_query(mysql, q); }
857int mysql_queryu5(MYSQL *mysql, const char *q)
858{ return mysql_query(mysql, q); }
859int mysql_queryu6(MYSQL *mysql, const char *q)
860{ return mysql_query(mysql, q); }
861int mysql_queryu7(MYSQL *mysql, const char *q)
862{ return mysql_query(mysql, q); }
863int mysql_queryu8(MYSQL *mysql, const char *q)
864{ return mysql_query(mysql, q); }
865#else
866#define mysql_queryu1 mysql_query
867#define mysql_queryu2 mysql_query
868#define mysql_queryu3 mysql_query
869#define mysql_queryu4 mysql_query
870#define mysql_queryu5 mysql_query
871#define mysql_queryu6 mysql_query
872#define mysql_queryu7 mysql_query
873#define mysql_queryu8 mysql_query
874#endif
875
876/**
877 * Updates function information.
878 * @returns number of errors.
879 * @param pFnDesc Function description struct.
880 * @param lDll Dll which we are working at.
881 * @param pszError Buffer for error messages
882 * @result on error(s) pszError will hold information about the error(s).
883 */
884unsigned long _System dbUpdateFunction(PFNDESC pFnDesc, signed long lDll, char *pszError)
885{
886 MYSQL_RES * pres;
887 MYSQL_ROW row;
888 char * pszQuery2 = (char*)malloc(65500);
889 char * pszQuery = pszQuery2;
890 long lCurrentState;
891 int i,k,rc;
892 unsigned long ulRc = 0;
893
894 /* check if malloc have failed allocating memory for us. */
895 if (pszQuery2 == NULL)
896 {
897 strcpy(pszError, "internal dbUpdateFunction error - malloc failed!\n");
898 return 1;
899 }
900
901
902 /*
903 * Loop thru all functions in the array of refocodes.
904 */
905 for (k = 0; k < pFnDesc->cRefCodes; k++)
906 {
907 /*
908 * Get current status
909 */
910 lCurrentState = dbGetFunctionState(pFnDesc->alRefCode[k]);
911 if (lCurrentState == -1 && dbGetLastErrorDesc() != NULL && strlen(dbGetLastErrorDesc()) != 0)
912 {
913 strcpy(pszError, dbGetLastErrorDesc());
914 /*
915 * Set updated flag
916 */
917 sprintf(pszQuery, "UPDATE function SET updated = updated + 1 WHERE refcode = %ld",
918 pFnDesc->alRefCode[k]);
919 rc = mysql_queryu1(pmysql, pszQuery2);
920 free(pszQuery2);
921 return 1;
922 }
923
924
925 /*
926 * Update function table first
927 */
928 strcpy(pszQuery, "UPDATE function SET updated = updated + 1");
929 pszQuery += strlen(pszQuery);
930
931 /* Status */
932 if (lCurrentState != pFnDesc->lStatus
933 && pFnDesc->lStatus != 0
934 && (lCurrentState == 0 || pFnDesc->lStatus != 99)
935 )
936 pszQuery += sprintf(pszQuery, ", state = %ld", pFnDesc->lStatus);
937
938 /* File */
939 if (pFnDesc->lFile >= 0)
940 pszQuery += sprintf(pszQuery, ", file = %ld", pFnDesc->lFile);
941 else
942 pszQuery += sprintf(pszQuery, ", file = -1");
943
944 /* Line */
945 if (pFnDesc->lLine >= 0)
946 pszQuery += sprintf(pszQuery, ", line = %ld", pFnDesc->lLine);
947 else
948 pszQuery += sprintf(pszQuery, ", line = -1");
949
950 /* return type */
951 if (pFnDesc->pszReturnType != NULL)
952 pszQuery = sqlstrcat(pszQuery, ", return = ", pFnDesc->pszReturnType);
953 else
954 pszQuery += sprintf(pszQuery, ", return = NULL");
955
956 /* Description */
957 if (pFnDesc->pszDescription != NULL)
958 pszQuery = sqlstrcat(pszQuery, ", description = ", pFnDesc->pszDescription);
959 else
960 pszQuery += sprintf(pszQuery, ", description = NULL");
961
962 /* Remark */
963 if (pFnDesc->pszRemark != NULL)
964 pszQuery = sqlstrcat(pszQuery, ", remark = ", pFnDesc->pszRemark);
965 else
966 pszQuery += sprintf(pszQuery, ", remark = NULL");
967
968 /* Description */
969 if (pFnDesc->pszReturnDesc != NULL)
970 pszQuery = sqlstrcat(pszQuery, ", returndesc = ", pFnDesc->pszReturnDesc);
971 else
972 pszQuery += sprintf(pszQuery, ", returndesc = NULL");
973
974 /* Sketch */
975 if (pFnDesc->pszSketch != NULL)
976 pszQuery = sqlstrcat(pszQuery, ", sketch = ", pFnDesc->pszSketch);
977 else
978 pszQuery += sprintf(pszQuery, ", sketch = NULL");
979
980 /* Equiv */
981 if (pFnDesc->pszEquiv != NULL)
982 pszQuery = sqlstrcat(pszQuery, ", equiv = ", pFnDesc->pszEquiv);
983 else
984 pszQuery += sprintf(pszQuery, ", equiv = NULL");
985
986 /* Time */
987 if (pFnDesc->pszTime != NULL)
988 pszQuery = sqlstrcat(pszQuery, ", time = ", pFnDesc->pszTime);
989 else
990 pszQuery += sprintf(pszQuery, ", time = NULL");
991
992 /* Execute update query? */
993 sprintf(pszQuery + strlen(pszQuery), " WHERE refcode = %ld", pFnDesc->alRefCode[k]);
994 rc = mysql_queryu2(pmysql, pszQuery2);
995 if (rc < 0)
996 {
997 sprintf(pszError, "Updating functiontable failed with error: %s - (sql=%s) ",
998 dbGetLastErrorDesc(), pszQuery2);
999 pszError += strlen(pszError) - 1;
1000 ulRc++;
1001 }
1002
1003
1004 /*
1005 * Parameters
1006 */
1007 pszQuery = pszQuery2;
1008 sprintf(pszQuery, "SELECT count(*) FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]);
1009 rc = mysql_queryu3(pmysql, pszQuery);
1010 if (rc >= 0)
1011 {
1012 pres = mysql_store_result(pmysql);
1013 if (pres != NULL)
1014 row = mysql_fetch_row(pres);
1015 if (pres != NULL && row != NULL && mysql_num_rows(pres) == 1)
1016 {
1017 #if 0 /* keep getting duplicate keys when parameter order/names are changed. */
1018 if (atol(row[0]) == pFnDesc->cParams)
1019 { /* update parameters */
1020 for (i = 0; i < pFnDesc->cParams; i++)
1021 {
1022 sprintf(pszQuery, "UPDATE parameter SET type = '%s', name = '%s'",
1023 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
1024 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : "");
1025 if (pFnDesc->apszParamDesc[i] != NULL)
1026 sqlstrcat(pszQuery, ", description = ", pFnDesc->apszParamDesc[i]);
1027 sprintf(pszQuery + strlen(pszQuery), " WHERE function = (%ld) AND sequencenbr = (%ld)",
1028 pFnDesc->alRefCode[k], i);
1029 rc = mysql_queryu4(pmysql, pszQuery);
1030 if (rc < 0)
1031 {
1032 if (*pszError == ' ')
1033 strcpy(pszError++, "\n\t");
1034 sprintf(pszError, "Updating parameter %i failed with error: %s - (sql=%s) ",
1035 i, dbGetLastErrorDesc(), pszQuery);
1036 pszError += strlen(pszError) - 1;
1037 ulRc++;
1038 }
1039 }
1040 }
1041 else
1042 #endif
1043 {
1044 if (atol(row[0]) != 0)
1045 { /* delete old parameters */
1046 sprintf(pszQuery, "DELETE FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]);
1047 rc = mysql_queryu5(pmysql, pszQuery);
1048 if (rc < 0)
1049 {
1050 if (*pszError == ' ')
1051 strcpy(pszError++, "\n\t");
1052 sprintf(pszError, "Deleting old parameters failed with error: %s - (sql=%s) ",
1053 dbGetLastErrorDesc(), pszQuery);
1054 pszError += strlen(pszError) - 1;
1055 ulRc++;
1056 }
1057 }
1058
1059 /* insert parameters */
1060 for (i = 0; i < pFnDesc->cParams; i++)
1061 {
1062 sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name, description) "
1063 "VALUES (%ld, %d, '%s', '%s'",
1064 pFnDesc->alRefCode[k], i,
1065 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
1066 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : ""
1067 );
1068 if (pFnDesc->apszParamDesc[i] != NULL)
1069 sqlstrcat(pszQuery, ", ", pFnDesc->apszParamDesc[i], ")");
1070 else
1071 strcat(pszQuery, ", NULL)");
1072
1073 rc = mysql_queryu6(pmysql, pszQuery2);
1074 if (rc < 0)
1075 {
1076 if (*pszError == ' ')
1077 strcpy(pszError++, "\n\t");
1078 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
1079 i, dbGetLastErrorDesc(), pszQuery);
1080 pszError += strlen(pszError) - 1;
1081 ulRc++;
1082 }
1083 }
1084 }
1085 }
1086 else
1087 {
1088 if (*pszError == ' ')
1089 strcpy(pszError++, "\n\t");
1090 sprintf(pszError, "failed to store result or to fetch a row , error: %s - (sql=%s) ",
1091 dbGetLastErrorDesc(), pszQuery);
1092 pszError += strlen(pszError) - 1;
1093 ulRc++;
1094 }
1095 }
1096 else
1097 {
1098 if (*pszError == ' ')
1099 strcpy(pszError++, "\n\t");
1100 sprintf(pszError, "Failed querying number of parameters, error: %s - (sql=%s) ",
1101 dbGetLastErrorDesc(), pszQuery);
1102 pszError += strlen(pszError) - 1;
1103 ulRc++;
1104 }
1105
1106
1107 /*
1108 * Authors
1109 */
1110 sprintf(pszQuery, "DELETE FROM fnauthor WHERE function = %ld", pFnDesc->alRefCode[k]);
1111 rc = mysql_queryu7(pmysql, pszQuery);
1112 if (rc < 0)
1113 {
1114 if (*pszError == ' ')
1115 strcpy(pszError++, "\n\t");
1116 sprintf(pszError, "Deleting old authors failed with error: %s - (sql=%s) ",
1117 dbGetLastErrorDesc(), pszQuery);
1118 pszError += strlen(pszError) - 1;
1119 ulRc++;
1120 }
1121
1122 for (i = 0; i < pFnDesc->cAuthors; i++)
1123 {
1124 if (pFnDesc->alAuthorRefCode[i] == -1)
1125 continue;
1126 sprintf(pszQuery, "INSERT INTO fnauthor(author, function) "
1127 "VALUES (%ld, %ld)",
1128 pFnDesc->alAuthorRefCode[i], pFnDesc->alRefCode[k]);
1129 rc = mysql_queryu8(pmysql, pszQuery);
1130 if (rc < 0)
1131 {
1132 if (*pszError == ' ')
1133 strcpy(pszError++, "\n\t");
1134 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
1135 i, dbGetLastErrorDesc(), pszQuery);
1136 pszError += strlen(pszError) - 1;
1137 ulRc++;
1138 }
1139 }
1140 } /* for */
1141
1142 lDll = lDll;
1143 free(pszQuery2);
1144 return ulRc;
1145}
1146
1147
1148/**
1149 * Removes all the existing design notes in the specified file.
1150 * @returns Success indicator.
1151 * @param lFile File refcode of the file to remove all design notes for.
1152 * @sketch
1153 * @status
1154 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1155 * @remark
1156 */
1157BOOL _System dbRemoveDesignNotes(signed long lFile)
1158{
1159 char szQuery[80];
1160
1161 assert(lFile >= 0);
1162 sprintf(&szQuery[0], "DELETE FROM designnote WHERE file = %ld", lFile);
1163 return mysql_query(pmysql, &szQuery[0]) >= 0;
1164}
1165
1166
1167/**
1168 * Adds a design note.
1169 * @returns Success indicator.
1170 * @param lDll Dll refcode.
1171 * @param lFile File refcode.
1172 * @param pszTitle Design note title.
1173 * @param pszText Design note text.
1174 * @param lSeqNbr Sequence number (in dll). If 0 the use next available number.
1175 * @param lSeqNbrFile Sequence number in file.
1176 * @param lLine Line number (1 - based!).
1177 */
1178BOOL _System dbAddDesignNote(signed long lDll,
1179 signed long lFile,
1180 const char *pszTitle,
1181 const char *pszText,
1182 signed long lSeqNbr,
1183 signed long lSeqNbrFile,
1184 signed long lLine)
1185{
1186 char szQuery[0x10200];
1187 MYSQL_RES * pres;
1188
1189
1190 assert(lDll >= 0 && lFile >= 0);
1191 assert(lSeqNbrFile >= 0);
1192
1193 /*
1194 * If no lSqlNbr the make one.
1195 */
1196 if (lSeqNbr == 0)
1197 {
1198 sprintf(&szQuery[0], "SELECT MAX(seqnbr) + 1 FROM designnote WHERE dll = %ld", lDll);
1199 if (mysql_query(pmysql, &szQuery[0]) >= 0)
1200 {
1201 pres = mysql_store_result(pmysql);
1202 if (pres != NULL)
1203 {
1204 MYSQL_ROW parow = mysql_fetch_row(pres);
1205 if (parow != NULL)
1206 lSeqNbr = getvalue(0, parow);
1207 else
1208 lSeqNbr = 1;
1209 mysql_free_result(pres);
1210 }
1211 else
1212 return FALSE;
1213 }
1214 else
1215 return FALSE;
1216 }
1217
1218 /*
1219 * Create update query.
1220 */
1221 sprintf(&szQuery[0], "INSERT INTO designnote(dll, file, seqnbrfile, seqnbr, line, title, note) "
1222 "VALUES (%ld, %ld, %ld, %ld, %ld, ",
1223 lDll, lFile, lSeqNbrFile, lSeqNbr, lLine);
1224 if (pszTitle != NULL && *pszTitle != '\0')
1225 sqlstrcat(&szQuery[0], NULL, pszTitle);
1226 else
1227 strcat(&szQuery[0], "NULL");
1228 sqlstrcat(&szQuery[0], ", ", pszText == NULL ? "" : pszText, ")");
1229
1230 return mysql_query(pmysql, &szQuery[0]) >= 0;
1231}
1232
1233
1234
1235/**
1236 * Updates the history tables.
1237 * @returns Number of signals/errors.
1238 * @param pszError Pointer to buffer which will hold the error messages.
1239 * @remark This should be called whenever updates have been completed.
1240 */
1241unsigned long _System dbCreateHistory(char *pszError)
1242{
1243 unsigned long ulRc = 0;
1244 MYSQL_RES *pres;
1245 MYSQL_ROW row;
1246 char szQuery[256];
1247 char *pszQuery = &szQuery[0];
1248 int rc;
1249 char szCurDt[20] = {0}; /*yyyy-mm-dd\0*/
1250
1251 mysql_refresh(pmysql, REFRESH_TABLES);
1252
1253 /* get currentdate - just in case the date changes between the delete and the update is completed. */
1254 strcpy(pszQuery, "SELECT CURDATE()");
1255 rc = mysql_query(pmysql, pszQuery);
1256 pres = mysql_use_result(pmysql);
1257 if (rc >= 0 && pres != NULL)
1258 {
1259 row = mysql_fetch_row(pres);
1260 if (row != NULL && mysql_num_rows(pres) == 1)
1261 {
1262 strcpy(&szCurDt[0], row[0]);
1263 while (mysql_fetch_row(pres) != NULL)
1264 pres=pres;
1265
1266 /* delete - all rows on this date in the history tables */
1267 sprintf(pszQuery, "DELETE FROM historydll WHERE date = '%s'", &szCurDt[0]);
1268 rc = mysql_query(pmysql, pszQuery);
1269 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1270
1271 sprintf(pszQuery, "DELETE FROM historyapigroup WHERE date = '%s'", &szCurDt[0]);
1272 rc = mysql_query(pmysql, pszQuery);
1273 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1274
1275 sprintf(pszQuery, "DELETE FROM historydlltotal WHERE date = '%s'", &szCurDt[0]);
1276 rc = mysql_query(pmysql, pszQuery);
1277 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1278
1279 sprintf(pszQuery, "DELETE FROM historyapigrouptotal WHERE date = '%s'", &szCurDt[0]);
1280 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1281
1282 /* insert new stats */
1283 sprintf(pszQuery, "INSERT INTO historydll(dll, state, date, count) "
1284 "SELECT dll, state, '%s', count(*) FROM function GROUP BY dll, state",
1285 &szCurDt[0]);
1286 rc = mysql_query(pmysql, pszQuery);
1287 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1288
1289 sprintf(pszQuery, "INSERT INTO historyapigroup(apigroup, state, date, count) "
1290 "SELECT apigroup, state, '%s', count(*) FROM function WHERE apigroup IS NOT NULL "
1291 "GROUP BY apigroup, state",
1292 &szCurDt[0]);
1293 rc = mysql_query(pmysql, pszQuery);
1294 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1295
1296 /* inserting new totals */
1297 sprintf(pszQuery, "INSERT INTO historydlltotal(dll, date, totalcount) "
1298 "SELECT dll, '%s', count(*) FROM function GROUP BY dll",
1299 &szCurDt[0]);
1300 rc = mysql_query(pmysql, pszQuery);
1301 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1302
1303 sprintf(pszQuery, "INSERT INTO historyapigrouptotal(apigroup, date, totalcount) "
1304 "SELECT apigroup, '%s', count(*) FROM function WHERE apigroup IS NOT NULL "
1305 "GROUP BY apigroup",
1306 &szCurDt[0]);
1307 rc = mysql_query(pmysql, pszQuery);
1308 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1309 }
1310 else
1311 {
1312 sprintf(pszError, "error getting current date (row == NULL): %s - (sql=%s) ",
1313 dbGetLastErrorDesc(), pszQuery);
1314 ulRc++;
1315 }
1316 }
1317 else
1318 {
1319 sprintf(pszError, "error getting current date: %s - (sql=%s) ",
1320 dbGetLastErrorDesc(), pszQuery);
1321 ulRc++;
1322 }
1323
1324 mysql_refresh(pmysql, REFRESH_TABLES);
1325
1326 return ulRc;
1327}
1328
1329
1330/**
1331 * Check that database integrety is ok. Verfies foreign keys.
1332 * @returns numbers of errors.
1333 * @param pszError Very large buffer which will hold error messges (if any).
1334 * @sketch
1335 * @remark current versions of mysql don't support 'SELECT ... WHERE id NOT IN(SELECT id FROM table)'
1336 */
1337unsigned long _System dbCheckIntegrity(char *pszError)
1338{
1339 char szQuery[384];
1340 char *pszQuery = &szQuery[0];
1341 MYSQL_RES *pres1;
1342 MYSQL_RES *pres2;
1343 MYSQL_ROW row1;
1344 int rc;
1345 unsigned long ulRc = 0;
1346
1347 mysql_refresh(pmysql, REFRESH_TABLES);
1348
1349 /* foreign keys in function table */
1350 strcpy(pszQuery, "SELECT refcode, dll, state, apigroup, file FROM function");
1351 rc = mysql_query(pmysql, pszQuery);
1352 if (rc >= 0)
1353 {
1354 pres1 = mysql_store_result(pmysql);
1355 if (pres1 != NULL)
1356 {
1357 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1358 {
1359 /* check dll */
1360 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1361 rc = mysql_query(pmysql, pszQuery);
1362 CheckFKError("function/dll", "Foreign key 'dll' not found in the dll table");
1363
1364 /* check state */
1365 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
1366 rc = mysql_query(pmysql, pszQuery);
1367 CheckFKError("function/state", "Foreign key 'state' not found in the state table");
1368
1369 /* check apigroup */
1370 if (row1[3] != NULL)
1371 {
1372 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[3]);
1373 rc = mysql_query(pmysql, pszQuery);
1374 CheckFKError("function/state", "Foreign key 'state' not found in the state table");
1375 }
1376
1377 /* check file */
1378 if (atoi(row1[4]) >= 0)
1379 {
1380 sprintf(pszQuery, "SELECT refcode FROM file WHERE refcode = %s", row1[4]);
1381 rc = mysql_query(pmysql, pszQuery);
1382 CheckFKError("function/file", "Foreign key 'file' not found in the file table");
1383 }
1384 }
1385 mysql_free_result(pres1);
1386 }
1387 }
1388 else
1389 ulRc += logDbError(pszError, pszQuery);
1390
1391 /* foreign keys in file */
1392 strcpy(pszQuery, "SELECT refcode, dll FROM file");
1393 rc = mysql_query(pmysql, pszQuery);
1394 if (rc >= 0)
1395 {
1396 pres1 = mysql_store_result(pmysql);
1397 if (pres1 != NULL)
1398 {
1399 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1400 {
1401 /* check dll */
1402 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1403 rc = mysql_query(pmysql, pszQuery);
1404 CheckFKError("apigroup/dll", "Foreign key 'dll' not found in the dll table");
1405 }
1406 mysql_free_result(pres1);
1407 }
1408 }
1409 else
1410 ulRc += logDbError(pszError, pszQuery);
1411
1412 /* foreign keys in apigroup */
1413 strcpy(pszQuery, "SELECT refcode, dll FROM apigroup");
1414 rc = mysql_query(pmysql, pszQuery);
1415 if (rc >= 0)
1416 {
1417 pres1 = mysql_store_result(pmysql);
1418 if (pres1 != NULL)
1419 {
1420 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1421 {
1422 /* check dll */
1423 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1424 rc = mysql_query(pmysql, pszQuery);
1425 CheckFKError("file/dll", "Foreign key 'dll' not found in the dll table");
1426 }
1427 mysql_free_result(pres1);
1428 }
1429 }
1430 else
1431 ulRc += logDbError(pszError, pszQuery);
1432
1433 /* foreign keys in fnauthor */
1434 strcpy(pszQuery, "SELECT function, author FROM fnauthor");
1435 rc = mysql_query(pmysql, pszQuery);
1436 if (rc >= 0)
1437 {
1438 pres1 = mysql_store_result(pmysql);
1439 if (pres1 != NULL)
1440 {
1441 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1442 {
1443 /* check function */
1444 sprintf(pszQuery, "SELECT refcode FROM function WHERE refcode = %s", row1[1]);
1445 rc = mysql_query(pmysql, pszQuery);
1446 CheckFKError("fnauthor/function", "Foreign key 'function' not found in the function table");
1447
1448 /* check author */
1449 sprintf(pszQuery, "SELECT refcode FROM author WHERE refcode = %s", row1[1]);
1450 rc = mysql_query(pmysql, pszQuery);
1451 CheckFKError("fnauthor/author", "Foreign key 'author' not found in the author table");
1452 }
1453 mysql_free_result(pres1);
1454 }
1455 }
1456 else
1457 ulRc += logDbError(pszError, pszQuery);
1458
1459 /* foreign keys in historydll table */
1460 strcpy(pszQuery, "SELECT date, dll, state FROM historydll");
1461 rc = mysql_query(pmysql, pszQuery);
1462 if (rc >= 0)
1463 {
1464 pres1 = mysql_store_result(pmysql);
1465 if (pres1 != NULL)
1466 {
1467 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1468 {
1469 /* check dll */
1470 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1471 rc = mysql_query(pmysql, pszQuery);
1472 CheckFKError("historydll/dll", "Foreign key 'dll' not found in the dll table");
1473
1474 /* check state */
1475 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
1476 rc = mysql_query(pmysql, pszQuery);
1477 CheckFKError("historydll/state", "Foreign key 'state' not found in the state table");
1478 }
1479 mysql_free_result(pres1);
1480 }
1481 }
1482 else
1483 ulRc += logDbError(pszError, pszQuery);
1484
1485 /* foreign keys in historyapigroup table */
1486 strcpy(pszQuery, "SELECT date, apigroup, state FROM historyapigroup");
1487 rc = mysql_query(pmysql, pszQuery);
1488 if (rc >= 0)
1489 {
1490 pres1 = mysql_store_result(pmysql);
1491 if (pres1 != NULL)
1492 {
1493 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1494 {
1495 /* check dll */
1496 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[1]);
1497 rc = mysql_query(pmysql, pszQuery);
1498 CheckFKError("historyapigroup/apigroup", "Foreign key 'apigroup' not found in the apigroup table");
1499
1500 /* check state */
1501 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
1502 rc = mysql_query(pmysql, pszQuery);
1503 CheckFKError("historyapigroup/state", "Foreign key 'state' not found in the state table");
1504 }
1505 mysql_free_result(pres1);
1506 }
1507 }
1508 else
1509 ulRc += logDbError(pszError, pszQuery);
1510
1511 /* foreign keys in historydlltotal table */
1512 strcpy(pszQuery, "SELECT date, dll FROM historydlltotal");
1513 rc = mysql_query(pmysql, pszQuery);
1514 if (rc >= 0)
1515 {
1516 pres1 = mysql_store_result(pmysql);
1517 if (pres1 != NULL)
1518 {
1519 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1520 {
1521 /* check dll */
1522 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1523 rc = mysql_query(pmysql, pszQuery);
1524 CheckFKError("historydlltotal/dll", "Foreign key 'dll' not found in the dll table");
1525 }
1526 mysql_free_result(pres1);
1527 }
1528 }
1529 else
1530 ulRc += logDbError(pszError, pszQuery);
1531
1532 /* foreign keys in historyapigroup table */
1533 strcpy(pszQuery, "SELECT date, apigroup FROM historyapigrouptotal");
1534 rc = mysql_query(pmysql, pszQuery);
1535 if (rc >= 0)
1536 {
1537 pres1 = mysql_store_result(pmysql);
1538 if (pres1 != NULL)
1539 {
1540 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1541 {
1542 /* check dll */
1543 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[1]);
1544 rc = mysql_query(pmysql, pszQuery);
1545 CheckFKError("historyapigrouptotal/apigroup", "Foreign key 'apigroup' not found in the apigroup table");
1546 }
1547 mysql_free_result(pres1);
1548 }
1549 }
1550 else
1551 ulRc += logDbError(pszError, pszQuery);
1552
1553 /* foreign keys in parameter table */
1554 strcpy(pszQuery, "SELECT sequencenbr, function FROM parameter");
1555 rc = mysql_query(pmysql, pszQuery);
1556 if (rc >= 0)
1557 {
1558 pres1 = mysql_store_result(pmysql);
1559 if (pres1 != NULL)
1560 {
1561 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1562 {
1563 /* check function */
1564 sprintf(pszQuery, "SELECT refcode FROM function WHERE refcode = %s", row1[1]);
1565 rc = mysql_query(pmysql, pszQuery);
1566 CheckFKError("parameter/function", "Foreign key 'function' not found in the function table");
1567 }
1568 mysql_free_result(pres1);
1569 }
1570 }
1571 else
1572 ulRc += logDbError(pszError, pszQuery);
1573
1574 /* Author table is special, since you should be able to interchangably reference an
1575 * author by any of the following tables:
1576 * name
1577 * initials
1578 * alias
1579 * email
1580 */
1581 strcpy(pszQuery, "SELECT name, initials, alias, email FROM author");
1582 rc = mysql_query(pmysql, pszQuery);
1583 if (rc >= 0)
1584 {
1585 pres1 = mysql_store_result(pmysql);
1586 if (pres1 != NULL)
1587 {
1588 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1589 {
1590 /* check name */
1591 sprintf(pszQuery, "SELECT name FROM author WHERE "
1592 "initials = '%s' OR alias = '%s' OR email = '%s'",
1593 row1[0], row1[0], row1[0]);
1594 ulRc += CheckAuthorError(pszError, "name", row1[0], pszQuery);
1595
1596 /* check initials */
1597 sprintf(pszQuery, "SELECT name FROM author WHERE "
1598 "alias = '%s' OR email = '%s'",
1599 row1[1], row1[1]);
1600 ulRc += CheckAuthorError(pszError, "initials", row1[1], pszQuery);
1601
1602 /* alias */
1603 if (row1[2] != NULL)
1604 {
1605 sprintf(pszQuery, "SELECT name FROM author WHERE "
1606 "email = '%s'",
1607 row1[2]);
1608 ulRc += CheckAuthorError(pszError, "alias", row1[2], pszQuery);
1609 }
1610 }
1611 mysql_free_result(pres1);
1612 }
1613 }
1614 else
1615 ulRc += logDbError(pszError, pszQuery);
1616
1617 return ulRc;
1618}
1619
1620
1621/**
1622 * Checks for duplicate key and sql error for a given author key in the author table... (arg!)
1623 * @returns Number of errors.
1624 * @param pszError Reference to error buffer pointer.
1625 * @param pszFieldName Key field name; used for logging.
1626 * @param pszFieldValue Key value; used for logging
1627 * @param pszQuery Query which is to be exectued to test for duplicate key.
1628 * @remark Uses pszError[1] == '\xFE' to detect when to insert '\n\t'.
1629 */
1630static unsigned long CheckAuthorError(char * &pszError, const char *pszFieldName, const char *pszFieldValue, const char *pszQuery)
1631{
1632 MYSQL_ROW row;
1633 MYSQL_RES *pres;
1634 unsigned long ulRc = 0;
1635 int rc;
1636
1637 rc = mysql_query(pmysql, pszQuery);
1638 pres = mysql_store_result(pmysql);
1639 if (rc < 0 || (pres != NULL && mysql_num_rows(pres) != 0))
1640 { /* some kind of error has occurred */
1641 if (pszError[1] == '\xFE')
1642 {
1643 strcat(pszError, "\n\t");
1644 pszError += 2;
1645 }
1646
1647 if (rc < 0) /* sql error or 'duplicate key' */
1648 {
1649 sprintf(pszError, "author/%s: select failed - %s (sql=%s)",
1650 pszFieldName, dbGetLastErrorDesc(), pszQuery);
1651 }
1652 else
1653 { /* 'duplicate key' - print duplicates */
1654 sprintf(pszError, "author/%s: 'duplicate key', %s='%s': ",
1655 pszFieldName, pszFieldValue, pszFieldName);
1656
1657 while ((row = mysql_fetch_row(pres)) != NULL)
1658 {
1659 pszError += strlen(pszError);
1660 sprintf(pszError, "'%s' ", row[0]);
1661 }
1662 }
1663
1664 pszError += strlen(pszError);
1665 pszError[1] = '\xFE';
1666 ulRc = 1;
1667 }
1668 if (pres != NULL)
1669 mysql_free_result(pres);
1670
1671 return ulRc;
1672}
1673
1674
1675/**
1676 * Writes db error (rc<0) to the log buffer.
1677 * @returns Number of signals.
1678 * @param pszError Reference to the error buffer pointer.
1679 * @param pszQuery Pointer to query which was executed.
1680 * @remark Uses pszError[1] == '\xFE' to detect when to insert '\n\t'.
1681 */
1682static unsigned long logDbError(char * &pszError, const char *pszQuery)
1683{
1684 if (pszError[1] == '\xFE')
1685 {
1686 strcat(pszError, "\n\t");
1687 pszError += 2;
1688 }
1689 sprintf(pszError, "select failed: %s - (sql=%s)", dbGetLastErrorDesc(), pszQuery);
1690
1691 pszError += strlen(pszError);
1692 pszError[1] = '\xFE';
1693
1694 return 1;
1695}
1696
1697
1698/**
1699 * Executes a give query and returns a result identifier/pointer.
1700 * @returns Query result identifier/pointer. NULL on error.
1701 * @param pszQuery Pointer to query.
1702 * @remark Used by and designed for kHtmlPC.
1703 */
1704void * _System dbExecuteQuery(const char *pszQuery)
1705{
1706 assert(pmysql != NULL);
1707 if (mysql_query(pmysql, pszQuery) >= 0)
1708 return mysql_store_result(pmysql);
1709
1710 return NULL;
1711}
1712
1713
1714/**
1715 * Asks for the number of rows in the result.
1716 * @returns Number of rows in the result. -1 on error.
1717 * @param pres Query result identifier/pointer.
1718 * @remark Used by and designed for kHtmlPC.
1719 */
1720signed long _System dbQueryResultRows(void *pres)
1721{
1722 if (pres == NULL)
1723 return -1;
1724 return mysql_num_rows((MYSQL_RES*)pres);
1725}
1726
1727
1728/**
1729 * Frees the storage allocated by the given result.
1730 * @returns Success indicator, TRUE/FALSE.
1731 * @param pres Query result identifier/pointer.
1732 * @remark Used by and designed for kHtmlPC.
1733 */
1734BOOL _System dbFreeResult(void *pres)
1735{
1736 if (pres != NULL)
1737 mysql_free_result((MYSQL_RES*)pres);
1738 else
1739 return FALSE;
1740 return TRUE;
1741}
1742
1743
1744/**
1745 * Fetch data from a result. Returns the data by calling the given callback function.
1746 * @returns Success indicator, TRUE/FALSE.
1747 * @param pres Query result identifier/pointer.
1748 * @param dbFetchCallBack Callback-function.
1749 * @param pvUser User parameter which is passed onto dbFetchCallBack.
1750 * @remark Used by and designed for kHtmlPC.
1751 */
1752BOOL _System dbFetch(void *pres, DBCALLBACKFETCH dbFetchCallBack, void *pvUser)
1753{
1754 BOOL fRc = FALSE;
1755 MYSQL_ROW row = mysql_fetch_row((MYSQL_RES*)pres);
1756
1757 if (row)
1758 {
1759 MYSQL_FIELD *pField;
1760 int i = 0;
1761 mysql_field_seek((MYSQL_RES*)pres, 0);
1762
1763 while ((pField = mysql_fetch_field((MYSQL_RES*)pres)) != NULL)
1764 if (dbFetchCallBack(row[i++], pField->name, pvUser) != 0)
1765 return FALSE;
1766
1767 fRc = TRUE;
1768 }
1769
1770 return fRc;
1771}
1772
1773
1774/**
1775 * Converts an ISO date to days after Christ, year 0.
1776 * @returns days. -1 on error;
1777 * @param pszDate ISO Date.
1778 */
1779signed long _System dbDateToDaysAfterChrist(const char *pszDate)
1780{
1781 signed long lRet = -1;
1782 char szQuery[128];
1783
1784 sprintf(&szQuery[0], "SELECT to_days('%s')", pszDate);
1785 if (mysql_query(pmysql, &szQuery[0]) >= 0)
1786 {
1787 MYSQL_ROW row;
1788 MYSQL_RES *pres = mysql_use_result(pmysql);
1789 row = mysql_fetch_row(pres);
1790 if (row != NULL)
1791 {
1792 lRet = atol(row[0]);
1793 do { row = mysql_fetch_row(pres); } while (row != NULL);
1794 }
1795 }
1796
1797 return lRet;
1798}
1799
1800
1801/**
1802 * Converts days after Christ (year 0) to ISO date.
1803 * @returns Success indicator. TRUE/FALSE;
1804 * @param lDays Days after Christ (year 0).
1805 * @param pszDate ISO Date. Result.
1806 */
1807BOOL _System dbDaysAfterChristToDate(signed long lDays, char *pszDate)
1808{
1809 BOOL fRet = FALSE;
1810 char szQuery[128];
1811
1812 if (lDays < 0)
1813 return FALSE;
1814
1815 sprintf(&szQuery[0], "SELECT from_days(%ld)", lDays);
1816 if (mysql_query(pmysql, &szQuery[0]) >= 0)
1817 {
1818 MYSQL_ROW row;
1819 MYSQL_RES *pres = mysql_use_result(pmysql);
1820 row = mysql_fetch_row(pres);
1821 if (row != NULL)
1822 {
1823 fRet = strlen(row[0]) == (4+1+2+1+2) && row[0][4] == '-' && row[0][7] == '-'
1824 && strcmp(row[0], "0000-00-00") != 0;
1825 if (fRet)
1826 strcpy(pszDate, row[0]);
1827 do { row = mysql_fetch_row(pres); } while (row != NULL);
1828 }
1829 }
1830
1831 return fRet;
1832}
1833
1834
1835/**
1836 * Display all functions for, the given dll, that is not updated.
1837 * @returns TRUE / FALSE.
1838 * @param lDll Dll reference number.
1839 * @param dbFetchCall Callback function which will be called once for each
1840 * field for all the functions not updated.
1841 * pvUser is NULL, pszValue field value, pszFieldName the field name.
1842 */
1843BOOL _System dbGetNotUpdatedFunction(signed long lDll, DBCALLBACKFETCH dbFetchCallBack)
1844{
1845 BOOL fRet = FALSE;
1846 void *pres;
1847 char szQuery[256];
1848
1849 /* not updated names */
1850 sprintf(&szQuery[0], "SELECT f1.name, f1.intname, f1.updated, f1.aliasfn, d.name, f2.name, f2.intname AS last "
1851 "FROM function f1 LEFT OUTER JOIN function f2 ON f1.aliasfn = f2.refcode "
1852 " LEFT JOIN dll d ON f2.dll = d.refcode "
1853 "WHERE f1.dll = %ld AND f1.updated = 0",
1854 lDll);
1855 pres = dbExecuteQuery(szQuery);
1856 if (pres != NULL)
1857 {
1858 BOOL f;
1859 do
1860 {
1861 f = dbFetch(pres, dbFetchCallBack, NULL);
1862 } while (f);
1863 dbFreeResult(pres);
1864 fRet = TRUE;
1865 }
1866
1867 /* warn about updated > 1 too */
1868 sprintf(&szQuery[0], "SELECT f1.name, f1.intname, f1.updated, f1.aliasfn, d.name, f2.name, f2.intname AS last "
1869 "FROM function f1 LEFT OUTER JOIN function f2 ON f1.aliasfn = f2.refcode "
1870 " LEFT JOIN dll d ON f2.dll = d.refcode "
1871 "WHERE f1.dll = %ld AND f1.updated > 1",
1872 lDll);
1873 pres = dbExecuteQuery(szQuery);
1874 if (pres != NULL)
1875 {
1876 BOOL f;
1877 do
1878 {
1879 f = dbFetch(pres, dbFetchCallBack, NULL);
1880 } while (f);
1881 dbFreeResult(pres);
1882 fRet = TRUE;
1883 }
1884
1885 strcpy(&szQuery[0], "UPDATE function SET updated = 0");
1886 mysql_query(pmysql, &szQuery[0]);
1887
1888 return fRet;
1889}
1890
1891
1892/**
1893 * Counts the function for the given DLL which has been updated.
1894 * @returns -1 on error, number of updated function on success.
1895 * @param lDll Dll reference number.
1896 */
1897signed long _System dbGetNumberOfUpdatedFunction(signed long lDll)
1898{
1899 int rc;
1900 char szQuery[128];
1901 MYSQL_RES * pres;
1902
1903 sprintf(&szQuery[0], "SELECT count(*) FROM function WHERE dll = (%ld) AND updated > 0\n", lDll);
1904 rc = mysql_query(pmysql, &szQuery[0]);
1905 pres = mysql_store_result(pmysql);
1906 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
1907 rc = (int)getvalue(0, mysql_fetch_row(pres));
1908 else
1909 rc = -1;
1910 mysql_free_result(pres);
1911 return (signed long)rc;
1912}
1913
1914
1915
1916/**
1917 * Clear the update flags for all file in a dll/module.
1918 * @returns Success indicator. (TRUE / FALSE)
1919 * @param lDll Dll refcode.
1920 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1921 * @remark Intended for use by APIImport.
1922 */
1923BOOL _System dbClearUpdateFlagFile(signed long lDll)
1924{
1925 int rc;
1926 char szQuery[128];
1927
1928 sprintf(&szQuery[0],
1929 "UPDATE file SET updated = 0 WHERE dll = (%ld)",
1930 lDll);
1931 rc = mysql_query(pmysql, &szQuery[0]);
1932 return rc == 0;
1933}
1934
1935
1936/**
1937 * Clear update flag
1938 * @returns Success indicator.
1939 * @param lDll Dll refcode.
1940 * @param fAll All dll. If false only APIs and Internal APIs are cleared
1941 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1942 * @remark Intended for use by APIImport.
1943 */
1944BOOL _System dbClearUpdateFlagFunction(signed long lDll, BOOL fAll)
1945{
1946 int rc;
1947 char szQuery[128];
1948
1949 sprintf(&szQuery[0],
1950 "UPDATE function SET updated = 0 WHERE dll = (%ld)",
1951 lDll);
1952 if (!fAll)
1953 strcat(&szQuery[0], " AND type IN ('A', 'I')");
1954 rc = mysql_query(pmysql, &szQuery[0]);
1955 return rc == 0;
1956}
1957
1958
1959
1960/**
1961 * Deletes all the files in a dll/module which was not found/updated.
1962 * @returns Success indicator.
1963 * @param lDll Dll refcode.
1964 * @sketch Select all files which is to be deleted.
1965 * Set all references to each file in function to -1.
1966 * Delete all files which is to be deleted.
1967 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1968 * @remark Use with GRATE CARE!
1969 */
1970BOOL _System dbDeleteNotUpdatedFiles(signed long lDll)
1971{
1972 MYSQL_RES * pres;
1973 int rc;
1974 BOOL fRc = TRUE;
1975 char szQuery[128];
1976
1977 sprintf(&szQuery[0],
1978 "SELECT refcode FROM file WHERE dll = (%ld) AND updated = 0",
1979 lDll);
1980 rc = mysql_query(pmysql, &szQuery[0]);
1981 pres = mysql_store_result(pmysql);
1982 if (pres != NULL && mysql_num_rows(pres))
1983 {
1984 MYSQL_ROW row;
1985 while ((row = mysql_fetch_row(pres)) != NULL)
1986 {
1987 sprintf(&szQuery[0],
1988 "UPDATE function SET file = -1 WHERE file = %s",
1989 row[0]);
1990 rc = mysql_query(pmysql, &szQuery[0]);
1991 if (rc) fRc = FALSE;
1992 }
1993 }
1994
1995 sprintf(&szQuery[0],
1996 "DELETE FROM file WHERE dll = %ld AND updated = 0",
1997 lDll);
1998 rc = mysql_query(pmysql, &szQuery[0]);
1999 if (rc) fRc = FALSE;
2000
2001 return fRc;
2002}
2003
2004
2005/**
2006 * Deletes all the functions which haven't been updated.
2007 * All rows in other tables which references the functions are
2008 * also delete.
2009 *
2010 * @returns Success indicator. (TRUE / FALSE)
2011 * @param lDll The refcode of the dll owning the functions.
2012 * @param fAll All function. If FALSE then only APIs and Internal APIs.
2013 * @sketch Select all functions which wan't updated (ie. updated = 0 and dll = lDll).
2014 * If anyone Then
2015 * Delete the referenced to the functions in:
2016 * parameters
2017 * fnauthor
2018 * Delete all function which wasn't updated.
2019 * EndIf
2020 * @remark Use with GREATE CARE!
2021 */
2022BOOL _System dbDeleteNotUpdatedFunctions(signed long lDll, BOOL fAll)
2023{
2024 MYSQL_RES * pres;
2025 int rc;
2026 BOOL fRc = TRUE;
2027 char szQuery[128];
2028
2029 sprintf(&szQuery[0],
2030 "SELECT refcode FROM function WHERE dll = %ld AND updated = 0",
2031 lDll);
2032 if (!fAll)
2033 strcat(&szQuery[0], " AND type IN ('A', 'I')");
2034 rc = mysql_query(pmysql, &szQuery[0]);
2035 pres = mysql_store_result(pmysql);
2036
2037 if (pres != NULL && mysql_num_rows(pres))
2038 {
2039 MYSQL_ROW row;
2040 while ((row = mysql_fetch_row(pres)) != NULL)
2041 {
2042 /* delete parameters */
2043 sprintf(&szQuery[0], "DELETE FROM parameter WHERE function = %s", row[0]);
2044 rc = mysql_query(pmysql, &szQuery[0]);
2045 if (rc) fRc = FALSE;
2046
2047 /* author relations */
2048 sprintf(&szQuery[0], "DELETE FROM fnauthor WHERE function = %s", row[0]);
2049 rc = mysql_query(pmysql, &szQuery[0]);
2050 if (rc) fRc = FALSE;
2051 }
2052
2053 /*
2054 * Delete the functions only if above completed without errors.
2055 *
2056 * Deleting the functions before all the references has successfully be
2057 * deleted causes database corruption!
2058 */
2059 if (fRc)
2060 {
2061 sprintf(&szQuery[0],
2062 "DELETE FROM function WHERE dll = %ld AND updated = 0",
2063 lDll);
2064 if (!fAll)
2065 strcat(&szQuery[0], " AND type IN ('A', 'I')");
2066 rc = mysql_query(pmysql, &szQuery[0]);
2067 if (rc) fRc = FALSE;
2068 }
2069 }
2070
2071 return fRc;
2072}
2073
2074
2075
2076/**
2077 * Appends a set of strings to a query. The main string (pszStr) is enclosed in "'"s.
2078 * @returns Pointer to end of the string.
2079 * @param pszQuery Outputbuffer
2080 * @param pszBefore Text before string, might be NULL.
2081 * @param pszStr String (NOT NULL)
2082 * @param pszAfter Text after, might be NULL.
2083 * @status completely implemented
2084 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
2085 */
2086static char *sqlstrcat(char *pszQuery, const char *pszBefore, const char *pszStr, const char *pszAfter)
2087{
2088 char * pszLineStart = pszQuery;
2089 register char ch;
2090
2091 pszQuery += strlen(pszQuery);
2092
2093 /*
2094 * String before
2095 */
2096 if (pszBefore != NULL)
2097 {
2098 strcpy(pszQuery, pszBefore);
2099 pszQuery += strlen(pszQuery);
2100 }
2101
2102 /*
2103 * THE String
2104 */
2105 *pszQuery++ = '\'';
2106 while ((ch = *pszStr++) != '\0')
2107 {
2108 switch (ch)
2109 {
2110 case '\'':
2111 *pszQuery++ = '\\';
2112 *pszQuery++ = '\'';
2113 break;
2114
2115 case '"':
2116 *pszQuery++ = '\\';
2117 *pszQuery++ = '"';
2118 break;
2119
2120 case '\\':
2121 *pszQuery++ = '\\';
2122 *pszQuery++ = '\\';
2123 break;
2124
2125 case '%':
2126 *pszQuery++ = '\\';
2127 *pszQuery++ = '%';
2128 break;
2129
2130 case '_':
2131 *pszQuery++ = '\\';
2132 *pszQuery++ = '_';
2133 break;
2134
2135 case '\n':
2136 *pszQuery++ = '\\';
2137 *pszQuery++ = 'r';
2138 *pszQuery++ = '\\';
2139 *pszQuery++ = 'n';
2140 break;
2141
2142 case '\t':
2143 *pszQuery++ = '\\';
2144 *pszQuery++ = 't';
2145 break;
2146
2147 case '\r':
2148 break;
2149
2150 default:
2151 *pszQuery++ = ch;
2152 }
2153
2154 /* Add new lines every 80 chars MySql don't like long lines. */
2155 if (pszLineStart - pszQuery > 80)
2156 {
2157 *pszQuery = '\n';
2158 pszLineStart = pszQuery;
2159 }
2160 }
2161 *pszQuery++ = '\'';
2162
2163 /*
2164 * String after
2165 */
2166 if (pszAfter != NULL)
2167 {
2168 strcpy(pszQuery, pszAfter);
2169 pszQuery += strlen(pszQuery);
2170 }
2171 else
2172 *pszQuery = '\0';
2173
2174
2175 return pszQuery;
2176}
2177
2178
2179#ifndef DLL
2180/**
2181 * Signal handler.
2182 * Ensures that the database connection is closed at termination.
2183 * @param sig Signal number.
2184 */
2185void dbHandler(int sig)
2186{
2187 if (pmysql != NULL)
2188 {
2189 fprintf(stderr, "\n\t!disconnecting from database!\n");
2190 dbDisconnect();
2191 }
2192
2193 flushall();
2194 switch (sig)
2195 {
2196 case SIGBREAK:
2197 printf("\nSIGBREAK\n");
2198 exit(-1);
2199 break;
2200 case SIGINT:
2201 printf("\nSIGINT\n");
2202 exit(-1);
2203 break;
2204 case SIGTERM:
2205 printf("\nSIGTERM\n");
2206 exit(-1);
2207 break;
2208 case SIGSEGV:
2209 raise(sig);
2210 break;
2211 case SIGILL:
2212 printf("\nSIGILL\n");
2213 exit(-1);
2214 break;
2215 }
2216}
2217
2218
2219#else
2220/*******/
2221/* DLL */
2222/*******/
2223/* prototypes used in the _DLL_InitTerm function */
2224extern "C"
2225{
2226 int _CRT_init(void);
2227 void _CRT_term(void);
2228 void __ctordtorInit( void );
2229 void __ctordtorTerm( void );
2230 unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long ulFlag);
2231}
2232
2233
2234/**
2235 * Dll InitTerm function.
2236 * @returns 0 on success.
2237 * 1 on error.
2238 * @param hModule
2239 * @param ulFlags
2240 * @remark We'll ensure that the database connection is terminated as we terminate.
2241 */
2242unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long ulFlag)
2243{
2244 /*-------------------------------------------------------------------------*/
2245 /* If ulFlag is zero then the DLL is being loaded so initialization should */
2246 /* be performed. If ulFlag is 1 then the DLL is being freed so */
2247 /* termination should be performed. */
2248 /*-------------------------------------------------------------------------*/
2249
2250 switch (ulFlag)
2251 {
2252 case 0:
2253 if (_CRT_init() == -1)
2254 return 0;
2255 __ctordtorInit();
2256 break;
2257
2258 case 1:
2259 /* ensure that db connection is terminated */
2260 if (pmysql != NULL)
2261 {
2262 fprintf(stderr, "\n\t!disconnecting from database!\n");
2263 dbDisconnect();
2264 }
2265 __ctordtorTerm();
2266 break;
2267
2268 default:
2269 return 0;
2270 }
2271 hModule = hModule;
2272 return 1;
2273}
2274
2275/*****************************************************************/
2276/* -why is this terminate function referenced but not defined??? */
2277/* and where is it referenced??? */
2278/* -Probably an export missing from the libraries. */
2279/*****************************************************************/
2280void terminate(void)
2281{
2282 DosPutMessage(0, sizeof("terminate")-1, "terminate");
2283 exit(-1);
2284}
2285
2286/****************************************/
2287/* EMX run-time trouble */
2288/* _environ is missing when using -Zomf */
2289/****************************************/
2290char **_environ = environ;
2291
2292#endif
Note: See TracBrowser for help on using the repository browser.