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

Last change on this file since 3916 was 3899, checked in by bird, 25 years ago

Updates the file column in the function table.

File size: 76.6 KB
Line 
1/* $Id: db.cpp,v 1.17 2000-07-29 14:12:47 bird Exp $ *
2 *
3 * DB - contains all database routines.
4 *
5 * Copyright (c) 1999 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, description, lastauthor, 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 /* return type */
945 if (pFnDesc->pszReturnType != NULL)
946 pszQuery = sqlstrcat(pszQuery, ", return = ", pFnDesc->pszReturnType);
947 else
948 pszQuery += sprintf(pszQuery, ", return = NULL");
949
950 /* Description */
951 if (pFnDesc->pszDescription != NULL)
952 pszQuery = sqlstrcat(pszQuery, ", description = ", pFnDesc->pszDescription);
953 else
954 pszQuery += sprintf(pszQuery, ", description = NULL");
955
956 /* Remark */
957 if (pFnDesc->pszRemark != NULL)
958 pszQuery = sqlstrcat(pszQuery, ", remark = ", pFnDesc->pszRemark);
959 else
960 pszQuery += sprintf(pszQuery, ", remark = NULL");
961
962 /* Description */
963 if (pFnDesc->pszReturnDesc != NULL)
964 pszQuery = sqlstrcat(pszQuery, ", returndesc = ", pFnDesc->pszReturnDesc);
965 else
966 pszQuery += sprintf(pszQuery, ", returndesc = NULL");
967
968 /* Sketch */
969 if (pFnDesc->pszSketch != NULL)
970 pszQuery = sqlstrcat(pszQuery, ", sketch = ", pFnDesc->pszSketch);
971 else
972 pszQuery += sprintf(pszQuery, ", sketch = NULL");
973
974 /* Equiv */
975 if (pFnDesc->pszEquiv != NULL)
976 pszQuery = sqlstrcat(pszQuery, ", equiv = ", pFnDesc->pszEquiv);
977 else
978 pszQuery += sprintf(pszQuery, ", equiv = NULL");
979
980 /* Time */
981 if (pFnDesc->pszTime != NULL)
982 pszQuery = sqlstrcat(pszQuery, ", time = ", pFnDesc->pszTime);
983 else
984 pszQuery += sprintf(pszQuery, ", time = NULL");
985
986 /* Execute update query? */
987 sprintf(pszQuery + strlen(pszQuery), " WHERE refcode = %ld", pFnDesc->alRefCode[k]);
988 rc = mysql_queryu2(pmysql, pszQuery2);
989 if (rc < 0)
990 {
991 sprintf(pszError, "Updating functiontable failed with error: %s - (sql=%s) ",
992 dbGetLastErrorDesc(), pszQuery2);
993 pszError += strlen(pszError) - 1;
994 ulRc++;
995 }
996
997
998 /*
999 * Parameters
1000 */
1001 pszQuery = pszQuery2;
1002 sprintf(pszQuery, "SELECT count(*) FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]);
1003 rc = mysql_queryu3(pmysql, pszQuery);
1004 if (rc >= 0)
1005 {
1006 pres = mysql_store_result(pmysql);
1007 if (pres != NULL)
1008 row = mysql_fetch_row(pres);
1009 if (pres != NULL && row != NULL && mysql_num_rows(pres) == 1)
1010 {
1011 if (atol(row[0]) == pFnDesc->cParams)
1012 { /* update parameters */
1013 for (i = 0; i < pFnDesc->cParams; i++)
1014 {
1015 sprintf(pszQuery, "UPDATE parameter SET type = '%s', name = '%s'",
1016 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
1017 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : "");
1018 if (pFnDesc->apszParamDesc[i] != NULL)
1019 sqlstrcat(pszQuery, ", description = ", pFnDesc->apszParamDesc[i]);
1020 sprintf(pszQuery + strlen(pszQuery), " WHERE function = (%ld) AND sequencenbr = (%ld)",
1021 pFnDesc->alRefCode[k], i);
1022 rc = mysql_queryu4(pmysql, pszQuery);
1023 if (rc < 0)
1024 {
1025 if (*pszError == ' ')
1026 strcpy(pszError++, "\n\t");
1027 sprintf(pszError, "Updating parameter %i failed with error: %s - (sql=%s) ",
1028 i, dbGetLastErrorDesc(), pszQuery);
1029 pszError += strlen(pszError) - 1;
1030 ulRc++;
1031 }
1032 }
1033 }
1034 else
1035 {
1036 if (atol(row[0]) != 0)
1037 { /* delete old parameters */
1038 sprintf(pszQuery, "DELETE FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]);
1039 rc = mysql_queryu5(pmysql, pszQuery);
1040 if (rc < 0)
1041 {
1042 if (*pszError == ' ')
1043 strcpy(pszError++, "\n\t");
1044 sprintf(pszError, "Deleting old parameters failed with error: %s - (sql=%s) ",
1045 dbGetLastErrorDesc(), pszQuery);
1046 pszError += strlen(pszError) - 1;
1047 ulRc++;
1048 }
1049 }
1050
1051 /* insert parameters */
1052 for (i = 0; i < pFnDesc->cParams; i++)
1053 {
1054 sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name, description) "
1055 "VALUES (%ld, %d, '%s', '%s'",
1056 pFnDesc->alRefCode[k], i,
1057 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
1058 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : ""
1059 );
1060 if (pFnDesc->apszParamDesc[i] != NULL)
1061 sqlstrcat(pszQuery, ", ", pFnDesc->apszParamDesc[i]);
1062 else
1063 strcat(pszQuery, ", NULL)");
1064
1065 rc = mysql_queryu6(pmysql, pszQuery2);
1066 if (rc < 0)
1067 {
1068 if (*pszError == ' ')
1069 strcpy(pszError++, "\n\t");
1070 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
1071 i, dbGetLastErrorDesc(), pszQuery);
1072 pszError += strlen(pszError) - 1;
1073 ulRc++;
1074 }
1075 }
1076 }
1077 }
1078 else
1079 {
1080 if (*pszError == ' ')
1081 strcpy(pszError++, "\n\t");
1082 sprintf(pszError, "failed to store result or to fetch a row , error: %s - (sql=%s) ",
1083 dbGetLastErrorDesc(), pszQuery);
1084 pszError += strlen(pszError) - 1;
1085 ulRc++;
1086 }
1087 }
1088 else
1089 {
1090 if (*pszError == ' ')
1091 strcpy(pszError++, "\n\t");
1092 sprintf(pszError, "Failed querying number of parameters, error: %s - (sql=%s) ",
1093 dbGetLastErrorDesc(), pszQuery);
1094 pszError += strlen(pszError) - 1;
1095 ulRc++;
1096 }
1097
1098
1099 /*
1100 * Authors
1101 */
1102 sprintf(pszQuery, "DELETE FROM fnauthor WHERE function = %ld", pFnDesc->alRefCode[k]);
1103 rc = mysql_queryu7(pmysql, pszQuery);
1104 if (rc < 0)
1105 {
1106 if (*pszError == ' ')
1107 strcpy(pszError++, "\n\t");
1108 sprintf(pszError, "Deleting old authors failed with error: %s - (sql=%s) ",
1109 dbGetLastErrorDesc(), pszQuery);
1110 pszError += strlen(pszError) - 1;
1111 ulRc++;
1112 }
1113
1114 for (i = 0; i < pFnDesc->cAuthors; i++)
1115 {
1116 if (pFnDesc->alAuthorRefCode[i] == -1)
1117 continue;
1118 sprintf(pszQuery, "INSERT INTO fnauthor(author, function) "
1119 "VALUES (%ld, %ld)",
1120 pFnDesc->alAuthorRefCode[i], pFnDesc->alRefCode[k]);
1121 rc = mysql_queryu8(pmysql, pszQuery);
1122 if (rc < 0)
1123 {
1124 if (*pszError == ' ')
1125 strcpy(pszError++, "\n\t");
1126 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
1127 i, dbGetLastErrorDesc(), pszQuery);
1128 pszError += strlen(pszError) - 1;
1129 ulRc++;
1130 }
1131 }
1132 } /* for */
1133
1134 lDll = lDll;
1135 free(pszQuery2);
1136 return ulRc;
1137}
1138
1139
1140/**
1141 * Removes all the existing design notes in the specified file.
1142 * @returns Success indicator.
1143 * @param lFile File refcode of the file to remove all design notes for.
1144 * @sketch
1145 * @status
1146 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
1147 * @remark
1148 */
1149BOOL _System dbRemoveDesignNotes(signed long lFile)
1150{
1151 char szQuery[80];
1152
1153 assert(lFile >= 0);
1154 sprintf(&szQuery[0], "DELETE FROM designnote WHERE file = %ld", lFile);
1155 return mysql_query(pmysql, &szQuery[0]) >= 0;
1156}
1157
1158
1159/**
1160 * Adds a design note.
1161 * @returns Success indicator.
1162 * @param lDll Dll refcode.
1163 * @param lFile File refcode.
1164 * @param pszTitle Design note title.
1165 * @param pszText Design note text.
1166 * @param lSeqNbr Sequence number (in dll). If 0 the use next available number.
1167 * @param lSeqNbrFile Sequence number in file.
1168 */
1169BOOL _System dbAddDesignNote(signed long lDll,
1170 signed long lFile,
1171 const char *pszTitle,
1172 const char *pszText,
1173 signed long lSeqNbr,
1174 signed long lSeqNbrFile)
1175{
1176 char szQuery[0x10200];
1177 MYSQL_RES * pres;
1178
1179
1180 assert(lDll >= 0 && lFile >= 0);
1181 assert(lSeqNbrFile >= 0);
1182
1183 /*
1184 * If no lSqlNbr the make one.
1185 */
1186 if (lSeqNbr == 0)
1187 {
1188 sprintf(&szQuery[0], "SELECT MAX(lSeqNbr) + 1 FROM designnote WHERE dll = %ld'", lDll);
1189 if (mysql_query(pmysql, &szQuery[0]) >= 0)
1190 {
1191 pres = mysql_store_result(pmysql);
1192 if (pres != NULL)
1193 {
1194 MYSQL_ROW parow = mysql_fetch_row(pres);
1195 if (parow != NULL)
1196 lSeqNbr = getvalue(0, parow);
1197 else
1198 lSeqNbr = 1;
1199 mysql_free_result(pres);
1200 }
1201 else
1202 return FALSE;
1203 }
1204 else
1205 return FALSE;
1206 }
1207
1208 /*
1209 * Create update query.
1210 */
1211 sprintf(&szQuery[0], "INSERT INTO designnote(dll, file, seqnbrfile, seqnbr, title, note) "
1212 "VALUES (%ld, %ld, %ld, %ld, ",
1213 lDll, lFile, lSeqNbrFile, lSeqNbr);
1214 if (pszTitle != NULL && *pszTitle != '\0')
1215 sqlstrcat(&szQuery[0], NULL, pszTitle);
1216 else
1217 strcat(&szQuery[0], "NULL");
1218 sqlstrcat(&szQuery[0], ", ", pszText == NULL ? "" : pszText, ")");
1219
1220 return mysql_query(pmysql, &szQuery[0]) >= 0;
1221}
1222
1223
1224
1225/**
1226 * Updates the history tables.
1227 * @returns Number of signals/errors.
1228 * @param pszError Pointer to buffer which will hold the error messages.
1229 * @remark This should be called whenever updates have been completed.
1230 */
1231unsigned long _System dbCreateHistory(char *pszError)
1232{
1233 unsigned long ulRc = 0;
1234 MYSQL_RES *pres;
1235 MYSQL_ROW row;
1236 char szQuery[256];
1237 char *pszQuery = &szQuery[0];
1238 int rc;
1239 char szCurDt[20] = {0}; /*yyyy-mm-dd\0*/
1240
1241 mysql_refresh(pmysql, REFRESH_TABLES);
1242
1243 /* get currentdate - just in case the date changes between the delete and the update is completed. */
1244 strcpy(pszQuery, "SELECT CURDATE()");
1245 rc = mysql_query(pmysql, pszQuery);
1246 pres = mysql_use_result(pmysql);
1247 if (rc >= 0 && pres != NULL)
1248 {
1249 row = mysql_fetch_row(pres);
1250 if (row != NULL && mysql_num_rows(pres) == 1)
1251 {
1252 strcpy(&szCurDt[0], row[0]);
1253 while (mysql_fetch_row(pres) != NULL)
1254 pres=pres;
1255
1256 /* delete - all rows on this date in the history tables */
1257 sprintf(pszQuery, "DELETE FROM historydll WHERE date = '%s'", &szCurDt[0]);
1258 rc = mysql_query(pmysql, pszQuery);
1259 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1260
1261 sprintf(pszQuery, "DELETE FROM historyapigroup WHERE date = '%s'", &szCurDt[0]);
1262 rc = mysql_query(pmysql, pszQuery);
1263 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1264
1265 sprintf(pszQuery, "DELETE FROM historydlltotal WHERE date = '%s'", &szCurDt[0]);
1266 rc = mysql_query(pmysql, pszQuery);
1267 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1268
1269 sprintf(pszQuery, "DELETE FROM historyapigrouptotal WHERE date = '%s'", &szCurDt[0]);
1270 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1271
1272 /* insert new stats */
1273 sprintf(pszQuery, "INSERT INTO historydll(dll, state, date, count) "
1274 "SELECT dll, state, '%s', count(*) FROM function GROUP BY dll, state",
1275 &szCurDt[0]);
1276 rc = mysql_query(pmysql, pszQuery);
1277 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1278
1279 sprintf(pszQuery, "INSERT INTO historyapigroup(apigroup, state, date, count) "
1280 "SELECT dll, state, '%s', count(*) FROM function WHERE apigroup IS NOT NULL "
1281 "GROUP BY apigroup, state",
1282 &szCurDt[0]);
1283 rc = mysql_query(pmysql, pszQuery);
1284 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1285
1286 /* inserting new totals */
1287 sprintf(pszQuery, "INSERT INTO historydlltotal(dll, date, totalcount) "
1288 "SELECT dll, '%s', count(*) FROM function GROUP BY dll",
1289 &szCurDt[0]);
1290 rc = mysql_query(pmysql, pszQuery);
1291 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1292
1293 sprintf(pszQuery, "INSERT INTO historyapigrouptotal(apigroup, date, totalcount) "
1294 "SELECT apigroup, '%s', count(*) FROM function WHERE apigroup IS NOT NULL "
1295 "GROUP BY apigroup",
1296 &szCurDt[0]);
1297 rc = mysql_query(pmysql, pszQuery);
1298 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
1299 }
1300 else
1301 {
1302 sprintf(pszError, "error getting current date (row == NULL): %s - (sql=%s) ",
1303 dbGetLastErrorDesc(), pszQuery);
1304 ulRc++;
1305 }
1306 }
1307 else
1308 {
1309 sprintf(pszError, "error getting current date: %s - (sql=%s) ",
1310 dbGetLastErrorDesc(), pszQuery);
1311 ulRc++;
1312 }
1313
1314 mysql_refresh(pmysql, REFRESH_TABLES);
1315
1316 return ulRc;
1317}
1318
1319
1320/**
1321 * Check that database integrety is ok. Verfies foreign keys.
1322 * @returns numbers of errors.
1323 * @param pszError Very large buffer which will hold error messges (if any).
1324 * @sketch
1325 * @remark current versions of mysql don't support 'SELECT ... WHERE id NOT IN(SELECT id FROM table)'
1326 */
1327unsigned long _System dbCheckIntegrity(char *pszError)
1328{
1329 char szQuery[384];
1330 char *pszQuery = &szQuery[0];
1331 MYSQL_RES *pres1;
1332 MYSQL_RES *pres2;
1333 MYSQL_ROW row1;
1334 int rc;
1335 unsigned long ulRc = 0;
1336
1337 mysql_refresh(pmysql, REFRESH_TABLES);
1338
1339 /* foreign keys in function table */
1340 strcpy(pszQuery, "SELECT refcode, dll, state, apigroup, file FROM function");
1341 rc = mysql_query(pmysql, pszQuery);
1342 if (rc >= 0)
1343 {
1344 pres1 = mysql_store_result(pmysql);
1345 if (pres1 != NULL)
1346 {
1347 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1348 {
1349 /* check dll */
1350 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1351 rc = mysql_query(pmysql, pszQuery);
1352 CheckFKError("function/dll", "Foreign key 'dll' not found in the dll table");
1353
1354 /* check state */
1355 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
1356 rc = mysql_query(pmysql, pszQuery);
1357 CheckFKError("function/state", "Foreign key 'state' not found in the state table");
1358
1359 /* check apigroup */
1360 if (row1[3] != NULL)
1361 {
1362 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[3]);
1363 rc = mysql_query(pmysql, pszQuery);
1364 CheckFKError("function/state", "Foreign key 'state' not found in the state table");
1365 }
1366
1367 /* check file */
1368 if (atoi(row1[4]) >= 0)
1369 {
1370 sprintf(pszQuery, "SELECT refcode FROM file WHERE refcode = %s", row1[4]);
1371 rc = mysql_query(pmysql, pszQuery);
1372 CheckFKError("function/file", "Foreign key 'file' not found in the file table");
1373 }
1374 }
1375 mysql_free_result(pres1);
1376 }
1377 }
1378 else
1379 ulRc += logDbError(pszError, pszQuery);
1380
1381 /* foreign keys in file */
1382 strcpy(pszQuery, "SELECT refcode, dll FROM file");
1383 rc = mysql_query(pmysql, pszQuery);
1384 if (rc >= 0)
1385 {
1386 pres1 = mysql_store_result(pmysql);
1387 if (pres1 != NULL)
1388 {
1389 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1390 {
1391 /* check dll */
1392 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1393 rc = mysql_query(pmysql, pszQuery);
1394 CheckFKError("apigroup/dll", "Foreign key 'dll' not found in the dll table");
1395 }
1396 mysql_free_result(pres1);
1397 }
1398 }
1399 else
1400 ulRc += logDbError(pszError, pszQuery);
1401
1402 /* foreign keys in apigroup */
1403 strcpy(pszQuery, "SELECT refcode, dll FROM apigroup");
1404 rc = mysql_query(pmysql, pszQuery);
1405 if (rc >= 0)
1406 {
1407 pres1 = mysql_store_result(pmysql);
1408 if (pres1 != NULL)
1409 {
1410 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1411 {
1412 /* check dll */
1413 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1414 rc = mysql_query(pmysql, pszQuery);
1415 CheckFKError("file/dll", "Foreign key 'dll' not found in the dll table");
1416 }
1417 mysql_free_result(pres1);
1418 }
1419 }
1420 else
1421 ulRc += logDbError(pszError, pszQuery);
1422
1423 /* foreign keys in fnauthor */
1424 strcpy(pszQuery, "SELECT function, author FROM fnauthor");
1425 rc = mysql_query(pmysql, pszQuery);
1426 if (rc >= 0)
1427 {
1428 pres1 = mysql_store_result(pmysql);
1429 if (pres1 != NULL)
1430 {
1431 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1432 {
1433 /* check function */
1434 sprintf(pszQuery, "SELECT refcode FROM function WHERE refcode = %s", row1[1]);
1435 rc = mysql_query(pmysql, pszQuery);
1436 CheckFKError("fnauthor/function", "Foreign key 'function' not found in the function table");
1437
1438 /* check author */
1439 sprintf(pszQuery, "SELECT refcode FROM author WHERE refcode = %s", row1[1]);
1440 rc = mysql_query(pmysql, pszQuery);
1441 CheckFKError("fnauthor/author", "Foreign key 'author' not found in the author table");
1442 }
1443 mysql_free_result(pres1);
1444 }
1445 }
1446 else
1447 ulRc += logDbError(pszError, pszQuery);
1448
1449 /* foreign keys in historydll table */
1450 strcpy(pszQuery, "SELECT date, dll, state FROM historydll");
1451 rc = mysql_query(pmysql, pszQuery);
1452 if (rc >= 0)
1453 {
1454 pres1 = mysql_store_result(pmysql);
1455 if (pres1 != NULL)
1456 {
1457 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1458 {
1459 /* check dll */
1460 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1461 rc = mysql_query(pmysql, pszQuery);
1462 CheckFKError("historydll/dll", "Foreign key 'dll' not found in the dll table");
1463
1464 /* check state */
1465 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
1466 rc = mysql_query(pmysql, pszQuery);
1467 CheckFKError("historydll/state", "Foreign key 'state' not found in the state table");
1468 }
1469 mysql_free_result(pres1);
1470 }
1471 }
1472 else
1473 ulRc += logDbError(pszError, pszQuery);
1474
1475 /* foreign keys in historyapigroup table */
1476 strcpy(pszQuery, "SELECT date, apigroup, state FROM historyapigroup");
1477 rc = mysql_query(pmysql, pszQuery);
1478 if (rc >= 0)
1479 {
1480 pres1 = mysql_store_result(pmysql);
1481 if (pres1 != NULL)
1482 {
1483 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1484 {
1485 /* check dll */
1486 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[1]);
1487 rc = mysql_query(pmysql, pszQuery);
1488 CheckFKError("historyapigroup/apigroup", "Foreign key 'apigroup' not found in the apigroup table");
1489
1490 /* check state */
1491 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
1492 rc = mysql_query(pmysql, pszQuery);
1493 CheckFKError("historyapigroup/state", "Foreign key 'state' not found in the state table");
1494 }
1495 mysql_free_result(pres1);
1496 }
1497 }
1498 else
1499 ulRc += logDbError(pszError, pszQuery);
1500
1501 /* foreign keys in historydlltotal table */
1502 strcpy(pszQuery, "SELECT date, dll FROM historydlltotal");
1503 rc = mysql_query(pmysql, pszQuery);
1504 if (rc >= 0)
1505 {
1506 pres1 = mysql_store_result(pmysql);
1507 if (pres1 != NULL)
1508 {
1509 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1510 {
1511 /* check dll */
1512 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1513 rc = mysql_query(pmysql, pszQuery);
1514 CheckFKError("historydlltotal/dll", "Foreign key 'dll' not found in the dll table");
1515 }
1516 mysql_free_result(pres1);
1517 }
1518 }
1519 else
1520 ulRc += logDbError(pszError, pszQuery);
1521
1522 /* foreign keys in historyapigroup table */
1523 strcpy(pszQuery, "SELECT date, apigroup FROM historyapigrouptotal");
1524 rc = mysql_query(pmysql, pszQuery);
1525 if (rc >= 0)
1526 {
1527 pres1 = mysql_store_result(pmysql);
1528 if (pres1 != NULL)
1529 {
1530 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1531 {
1532 /* check dll */
1533 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[1]);
1534 rc = mysql_query(pmysql, pszQuery);
1535 CheckFKError("historyapigrouptotal/apigroup", "Foreign key 'apigroup' not found in the apigroup table");
1536 }
1537 mysql_free_result(pres1);
1538 }
1539 }
1540 else
1541 ulRc += logDbError(pszError, pszQuery);
1542
1543 /* foreign keys in parameter table */
1544 strcpy(pszQuery, "SELECT sequencenbr, function FROM parameter");
1545 rc = mysql_query(pmysql, pszQuery);
1546 if (rc >= 0)
1547 {
1548 pres1 = mysql_store_result(pmysql);
1549 if (pres1 != NULL)
1550 {
1551 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1552 {
1553 /* check function */
1554 sprintf(pszQuery, "SELECT refcode FROM function WHERE refcode = %s", row1[1]);
1555 rc = mysql_query(pmysql, pszQuery);
1556 CheckFKError("parameter/function", "Foreign key 'function' not found in the function table");
1557 }
1558 mysql_free_result(pres1);
1559 }
1560 }
1561 else
1562 ulRc += logDbError(pszError, pszQuery);
1563
1564 /* Author table is special, since you should be able to interchangably reference an
1565 * author by any of the following tables:
1566 * name
1567 * initials
1568 * alias
1569 * email
1570 */
1571 strcpy(pszQuery, "SELECT name, initials, alias, email FROM author");
1572 rc = mysql_query(pmysql, pszQuery);
1573 if (rc >= 0)
1574 {
1575 pres1 = mysql_store_result(pmysql);
1576 if (pres1 != NULL)
1577 {
1578 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1579 {
1580 /* check name */
1581 sprintf(pszQuery, "SELECT name FROM author WHERE "
1582 "initials = '%s' OR alias = '%s' OR email = '%s'",
1583 row1[0], row1[0], row1[0]);
1584 ulRc += CheckAuthorError(pszError, "name", row1[0], pszQuery);
1585
1586 /* check initials */
1587 sprintf(pszQuery, "SELECT name FROM author WHERE "
1588 "alias = '%s' OR email = '%s'",
1589 row1[1], row1[1]);
1590 ulRc += CheckAuthorError(pszError, "initials", row1[1], pszQuery);
1591
1592 /* alias */
1593 if (row1[2] != NULL)
1594 {
1595 sprintf(pszQuery, "SELECT name FROM author WHERE "
1596 "email = '%s'",
1597 row1[2]);
1598 ulRc += CheckAuthorError(pszError, "alias", row1[2], pszQuery);
1599 }
1600 }
1601 mysql_free_result(pres1);
1602 }
1603 }
1604 else
1605 ulRc += logDbError(pszError, pszQuery);
1606
1607 return ulRc;
1608}
1609
1610
1611/**
1612 * Checks for duplicate key and sql error for a given author key in the author table... (arg!)
1613 * @returns Number of errors.
1614 * @param pszError Reference to error buffer pointer.
1615 * @param pszFieldName Key field name; used for logging.
1616 * @param pszFieldValue Key value; used for logging
1617 * @param pszQuery Query which is to be exectued to test for duplicate key.
1618 * @remark Uses pszError[1] == '\xFE' to detect when to insert '\n\t'.
1619 */
1620static unsigned long CheckAuthorError(char * &pszError, const char *pszFieldName, const char *pszFieldValue, const char *pszQuery)
1621{
1622 MYSQL_ROW row;
1623 MYSQL_RES *pres;
1624 unsigned long ulRc = 0;
1625 int rc;
1626
1627 rc = mysql_query(pmysql, pszQuery);
1628 pres = mysql_store_result(pmysql);
1629 if (rc < 0 || (pres != NULL && mysql_num_rows(pres) != 0))
1630 { /* some kind of error has occurred */
1631 if (pszError[1] == '\xFE')
1632 {
1633 strcat(pszError, "\n\t");
1634 pszError += 2;
1635 }
1636
1637 if (rc < 0) /* sql error or 'duplicate key' */
1638 {
1639 sprintf(pszError, "author/%s: select failed - %s (sql=%s)",
1640 pszFieldName, dbGetLastErrorDesc(), pszQuery);
1641 }
1642 else
1643 { /* 'duplicate key' - print duplicates */
1644 sprintf(pszError, "author/%s: 'duplicate key', %s='%s': ",
1645 pszFieldName, pszFieldValue, pszFieldName);
1646
1647 while ((row = mysql_fetch_row(pres)) != NULL)
1648 {
1649 pszError += strlen(pszError);
1650 sprintf(pszError, "'%s' ", row[0]);
1651 }
1652 }
1653
1654 pszError += strlen(pszError);
1655 pszError[1] = '\xFE';
1656 ulRc = 1;
1657 }
1658 if (pres != NULL)
1659 mysql_free_result(pres);
1660
1661 return ulRc;
1662}
1663
1664
1665/**
1666 * Writes db error (rc<0) to the log buffer.
1667 * @returns Number of signals.
1668 * @param pszError Reference to the error buffer pointer.
1669 * @param pszQuery Pointer to query which was executed.
1670 * @remark Uses pszError[1] == '\xFE' to detect when to insert '\n\t'.
1671 */
1672static unsigned long logDbError(char * &pszError, const char *pszQuery)
1673{
1674 if (pszError[1] == '\xFE')
1675 {
1676 strcat(pszError, "\n\t");
1677 pszError += 2;
1678 }
1679 sprintf(pszError, "select failed: %s - (sql=%s)", dbGetLastErrorDesc(), pszQuery);
1680
1681 pszError += strlen(pszError);
1682 pszError[1] = '\xFE';
1683
1684 return 1;
1685}
1686
1687
1688/**
1689 * Executes a give query and returns a result identifier/pointer.
1690 * @returns Query result identifier/pointer. NULL on error.
1691 * @param pszQuery Pointer to query.
1692 * @remark Used by and designed for kHtmlPC.
1693 */
1694void * _System dbExecuteQuery(const char *pszQuery)
1695{
1696 assert(pmysql != NULL);
1697 if (mysql_query(pmysql, pszQuery) >= 0)
1698 return mysql_store_result(pmysql);
1699
1700 return NULL;
1701}
1702
1703
1704/**
1705 * Asks for the number of rows in the result.
1706 * @returns Number of rows in the result. -1 on error.
1707 * @param pres Query result identifier/pointer.
1708 * @remark Used by and designed for kHtmlPC.
1709 */
1710signed long _System dbQueryResultRows(void *pres)
1711{
1712 if (pres == NULL)
1713 return -1;
1714 return mysql_num_rows((MYSQL_RES*)pres);
1715}
1716
1717
1718/**
1719 * Frees the storage allocated by the given result.
1720 * @returns Success indicator, TRUE/FALSE.
1721 * @param pres Query result identifier/pointer.
1722 * @remark Used by and designed for kHtmlPC.
1723 */
1724BOOL _System dbFreeResult(void *pres)
1725{
1726 if (pres != NULL)
1727 mysql_free_result((MYSQL_RES*)pres);
1728 else
1729 return FALSE;
1730 return TRUE;
1731}
1732
1733
1734/**
1735 * Fetch data from a result. Returns the data by calling the given callback function.
1736 * @returns Success indicator, TRUE/FALSE.
1737 * @param pres Query result identifier/pointer.
1738 * @param dbFetchCallBack Callback-function.
1739 * @param pvUser User parameter which is passed onto dbFetchCallBack.
1740 * @remark Used by and designed for kHtmlPC.
1741 */
1742BOOL _System dbFetch(void *pres, DBCALLBACKFETCH dbFetchCallBack, void *pvUser)
1743{
1744 BOOL fRc = FALSE;
1745 MYSQL_ROW row = mysql_fetch_row((MYSQL_RES*)pres);
1746
1747 if (row)
1748 {
1749 MYSQL_FIELD *pField;
1750 int i = 0;
1751 mysql_field_seek((MYSQL_RES*)pres, 0);
1752
1753 while ((pField = mysql_fetch_field((MYSQL_RES*)pres)) != NULL)
1754 if (dbFetchCallBack(row[i++], pField->name, pvUser) != 0)
1755 return FALSE;
1756
1757 fRc = TRUE;
1758 }
1759
1760 return fRc;
1761}
1762
1763
1764/**
1765 * Converts an ISO date to days after Christ, year 0.
1766 * @returns days. -1 on error;
1767 * @param pszDate ISO Date.
1768 */
1769signed long _System dbDateToDaysAfterChrist(const char *pszDate)
1770{
1771 signed long lRet = -1;
1772 char szQuery[128];
1773
1774 sprintf(&szQuery[0], "SELECT to_days('%s')", pszDate);
1775 if (mysql_query(pmysql, &szQuery[0]) >= 0)
1776 {
1777 MYSQL_ROW row;
1778 MYSQL_RES *pres = mysql_use_result(pmysql);
1779 row = mysql_fetch_row(pres);
1780 if (row != NULL)
1781 {
1782 lRet = atol(row[0]);
1783 do { row = mysql_fetch_row(pres); } while (row != NULL);
1784 }
1785 }
1786
1787 return lRet;
1788}
1789
1790
1791/**
1792 * Converts days after Christ (year 0) to ISO date.
1793 * @returns Success indicator. TRUE/FALSE;
1794 * @param lDays Days after Christ (year 0).
1795 * @param pszDate ISO Date. Result.
1796 */
1797BOOL _System dbDaysAfterChristToDate(signed long lDays, char *pszDate)
1798{
1799 BOOL fRet = FALSE;
1800 char szQuery[128];
1801
1802 if (lDays < 0)
1803 return FALSE;
1804
1805 sprintf(&szQuery[0], "SELECT from_days(%ld)", lDays);
1806 if (mysql_query(pmysql, &szQuery[0]) >= 0)
1807 {
1808 MYSQL_ROW row;
1809 MYSQL_RES *pres = mysql_use_result(pmysql);
1810 row = mysql_fetch_row(pres);
1811 if (row != NULL)
1812 {
1813 fRet = strlen(row[0]) == (4+1+2+1+2) && row[0][4] == '-' && row[0][7] == '-'
1814 && strcmp(row[0], "0000-00-00") != 0;
1815 if (fRet)
1816 strcpy(pszDate, row[0]);
1817 do { row = mysql_fetch_row(pres); } while (row != NULL);
1818 }
1819 }
1820
1821 return fRet;
1822}
1823
1824
1825/**
1826 * Display all functions for, the given dll, that is not updated.
1827 * @returns TRUE / FALSE.
1828 * @param lDll Dll reference number.
1829 * @param dbFetchCall Callback function which will be called once for each
1830 * field for all the functions not updated.
1831 * pvUser is NULL, pszValue field value, pszFieldName the field name.
1832 */
1833BOOL _System dbGetNotUpdatedFunction(signed long lDll, DBCALLBACKFETCH dbFetchCallBack)
1834{
1835 BOOL fRet = FALSE;
1836 void *pres;
1837 char szQuery[256];
1838
1839 /* not updated names */
1840 sprintf(&szQuery[0], "SELECT f1.name, f1.intname, f1.updated, f1.aliasfn, d.name, f2.name, f2.intname AS last "
1841 "FROM function f1 LEFT OUTER JOIN function f2 ON f1.aliasfn = f2.refcode "
1842 " LEFT JOIN dll d ON f2.dll = d.refcode "
1843 "WHERE f1.dll = %ld AND f1.updated = 0",
1844 lDll);
1845 pres = dbExecuteQuery(szQuery);
1846 if (pres != NULL)
1847 {
1848 BOOL f;
1849 do
1850 {
1851 f = dbFetch(pres, dbFetchCallBack, NULL);
1852 } while (f);
1853 dbFreeResult(pres);
1854 fRet = TRUE;
1855 }
1856
1857 /* warn about updated > 1 too */
1858 sprintf(&szQuery[0], "SELECT f1.name, f1.intname, f1.updated, f1.aliasfn, d.name, f2.name, f2.intname AS last "
1859 "FROM function f1 LEFT OUTER JOIN function f2 ON f1.aliasfn = f2.refcode "
1860 " LEFT JOIN dll d ON f2.dll = d.refcode "
1861 "WHERE f1.dll = %ld AND f1.updated > 1",
1862 lDll);
1863 pres = dbExecuteQuery(szQuery);
1864 if (pres != NULL)
1865 {
1866 BOOL f;
1867 do
1868 {
1869 f = dbFetch(pres, dbFetchCallBack, NULL);
1870 } while (f);
1871 dbFreeResult(pres);
1872 fRet = TRUE;
1873 }
1874
1875 strcpy(&szQuery[0], "UPDATE function SET updated = 0");
1876 mysql_query(pmysql, &szQuery[0]);
1877
1878 return fRet;
1879}
1880
1881
1882/**
1883 * Counts the function for the given DLL which has been updated.
1884 * @returns -1 on error, number of updated function on success.
1885 * @param lDll Dll reference number.
1886 */
1887signed long _System dbGetNumberOfUpdatedFunction(signed long lDll)
1888{
1889 int rc;
1890 char szQuery[128];
1891 MYSQL_RES * pres;
1892
1893 sprintf(&szQuery[0], "SELECT count(*) FROM function WHERE dll = (%ld) AND updated > 0\n", lDll);
1894 rc = mysql_query(pmysql, &szQuery[0]);
1895 pres = mysql_store_result(pmysql);
1896 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
1897 rc = (int)getvalue(0, mysql_fetch_row(pres));
1898 else
1899 rc = -1;
1900 mysql_free_result(pres);
1901 return (signed long)rc;
1902}
1903
1904
1905
1906/**
1907 * Clear the update flags for all file in a dll/module.
1908 * @returns Success indicator. (TRUE / FALSE)
1909 * @param lDll Dll refcode.
1910 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1911 * @remark Intended for use by APIImport.
1912 */
1913BOOL _System dbClearUpdateFlagFile(signed long lDll)
1914{
1915 int rc;
1916 char szQuery[128];
1917
1918 sprintf(&szQuery[0],
1919 "UPDATE file SET updated = 0 WHERE dll = (%ld)",
1920 lDll);
1921 rc = mysql_query(pmysql, &szQuery[0]);
1922 return rc == 0;
1923}
1924
1925
1926/**
1927 * Clear update flag
1928 * @returns Success indicator.
1929 * @param lDll Dll refcode.
1930 * @param fAll All dll. If false only APIs and Internal APIs are cleared
1931 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1932 * @remark Intended for use by APIImport.
1933 */
1934BOOL _System dbClearUpdateFlagFunction(signed long lDll, BOOL fAll)
1935{
1936 int rc;
1937 char szQuery[128];
1938
1939 sprintf(&szQuery[0],
1940 "UPDATE function SET updated = 0 WHERE dll = (%ld)",
1941 lDll);
1942 if (!fAll)
1943 strcat(&szQuery[0], " AND type IN ('A', 'I')");
1944 rc = mysql_query(pmysql, &szQuery[0]);
1945 return rc == 0;
1946}
1947
1948
1949
1950/**
1951 * Deletes all the files in a dll/module which was not found/updated.
1952 * @returns Success indicator.
1953 * @param lDll Dll refcode.
1954 * @sketch Select all files which is to be deleted.
1955 * Set all references to each file in function to -1.
1956 * Delete all files which is to be deleted.
1957 * @author knut st. osmundsen (knut.stange.osmundsen@mynd.no)
1958 * @remark Use with GRATE CARE!
1959 */
1960BOOL _System dbDeleteNotUpdatedFiles(signed long lDll)
1961{
1962 MYSQL_RES * pres;
1963 int rc;
1964 BOOL fRc = TRUE;
1965 char szQuery[128];
1966
1967 sprintf(&szQuery[0],
1968 "SELECT refcode FROM file WHERE dll = (%ld) AND updated = 0",
1969 lDll);
1970 rc = mysql_query(pmysql, &szQuery[0]);
1971 pres = mysql_store_result(pmysql);
1972 if (pres != NULL && mysql_num_rows(pres))
1973 {
1974 MYSQL_ROW row;
1975 while ((row = mysql_fetch_row(pres)) != NULL)
1976 {
1977 sprintf(&szQuery[0],
1978 "UPDATE function SET file = -1 WHERE file = %s",
1979 row[0]);
1980 rc = mysql_query(pmysql, &szQuery[0]);
1981 if (rc) fRc = FALSE;
1982 }
1983 }
1984
1985 sprintf(&szQuery[0],
1986 "DELETE FROM file WHERE dll = %ld AND updated = 0",
1987 lDll);
1988 rc = mysql_query(pmysql, &szQuery[0]);
1989 if (rc) fRc = FALSE;
1990
1991 return fRc;
1992}
1993
1994
1995/**
1996 * Deletes all the functions which haven't been updated.
1997 * All rows in other tables which references the functions are
1998 * also delete.
1999 *
2000 * @returns Success indicator. (TRUE / FALSE)
2001 * @param lDll The refcode of the dll owning the functions.
2002 * @param fAll All function. If FALSE then only APIs and Internal APIs.
2003 * @sketch Select all functions which wan't updated (ie. updated = 0 and dll = lDll).
2004 * If anyone Then
2005 * Delete the referenced to the functions in:
2006 * parameters
2007 * fnauthor
2008 * Delete all function which wasn't updated.
2009 * EndIf
2010 * @remark Use with GREATE CARE!
2011 */
2012BOOL _System dbDeleteNotUpdatedFunctions(signed long lDll, BOOL fAll)
2013{
2014 MYSQL_RES * pres;
2015 int rc;
2016 BOOL fRc = TRUE;
2017 char szQuery[128];
2018
2019 sprintf(&szQuery[0],
2020 "SELECT refcode FROM function WHERE dll = %ld AND updated = 0",
2021 lDll);
2022 if (!fAll)
2023 strcat(&szQuery[0], " AND type IN ('A', 'I')");
2024 rc = mysql_query(pmysql, &szQuery[0]);
2025 pres = mysql_store_result(pmysql);
2026
2027 if (pres != NULL && mysql_num_rows(pres))
2028 {
2029 MYSQL_ROW row;
2030 while ((row = mysql_fetch_row(pres)) != NULL)
2031 {
2032 /* delete parameters */
2033 sprintf(&szQuery[0], "DELETE FROM parameter WHERE function %s", row[0]);
2034 rc = mysql_query(pmysql, &szQuery[0]);
2035 if (rc) fRc = FALSE;
2036
2037 /* author relations */
2038 sprintf(&szQuery[0], "DELETE FROM fnauthor WHERE function %s", row[0]);
2039 rc = mysql_query(pmysql, &szQuery[0]);
2040 if (rc) fRc = FALSE;
2041 }
2042
2043 /*
2044 * Delete the functions only if above completed without errors.
2045 *
2046 * Deleting the functions before all the references has successfully be
2047 * deleted causes database corruption!
2048 */
2049 if (fRc)
2050 {
2051 sprintf(&szQuery[0],
2052 "DELETE FROM function WHERE dll = %ld AND updated = 0",
2053 lDll);
2054 if (!fAll)
2055 strcat(&szQuery[0], " AND type IN ('A', 'I')");
2056 rc = mysql_query(pmysql, &szQuery[0]);
2057 if (rc) fRc = FALSE;
2058 }
2059 }
2060
2061 return fRc;
2062}
2063
2064
2065
2066/**
2067 * Appends a set of strings to a query. The main string (pszStr) is enclosed in "'"s.
2068 * @returns Pointer to end of the string.
2069 * @param pszQuery Outputbuffer
2070 * @param pszBefore Text before string, might be NULL.
2071 * @param pszStr String (NOT NULL)
2072 * @param pszAfter Text after, might be NULL.
2073 * @status completely implemented
2074 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
2075 */
2076static char *sqlstrcat(char *pszQuery, const char *pszBefore, const char *pszStr, const char *pszAfter)
2077{
2078 char * pszLineStart = pszQuery;
2079 register char ch;
2080
2081 pszQuery += strlen(pszQuery);
2082
2083 /*
2084 * String before
2085 */
2086 if (pszBefore != NULL)
2087 {
2088 strcpy(pszQuery, pszBefore);
2089 pszQuery += strlen(pszQuery);
2090 }
2091
2092 /*
2093 * THE String
2094 */
2095 *pszQuery++ = '\'';
2096 while ((ch = *pszStr++) != '\0')
2097 {
2098 switch (ch)
2099 {
2100 case '\'':
2101 *pszQuery++ = '\\';
2102 *pszQuery++ = '\'';
2103 break;
2104
2105 case '"':
2106 *pszQuery++ = '\\';
2107 *pszQuery++ = '"';
2108 break;
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 '\n':
2126 *pszQuery++ = '\\';
2127 *pszQuery++ = 'r';
2128 *pszQuery++ = '\\';
2129 *pszQuery++ = 'n';
2130 break;
2131
2132 case '\t':
2133 *pszQuery++ = '\\';
2134 *pszQuery++ = 't';
2135 break;
2136
2137 case '\r':
2138 break;
2139
2140 default:
2141 *pszQuery++ = ch;
2142 }
2143
2144 /* Add new lines every 80 chars MySql don't like long lines. */
2145 if (pszLineStart - pszQuery > 80)
2146 {
2147 *pszQuery = '\n';
2148 pszLineStart = pszQuery;
2149 }
2150 }
2151 *pszQuery++ = '\'';
2152
2153 /*
2154 * String after
2155 */
2156 if (pszAfter != NULL)
2157 {
2158 strcpy(pszQuery, pszAfter);
2159 pszQuery += strlen(pszQuery);
2160 }
2161 else
2162 *pszQuery = '\0';
2163
2164
2165 return pszQuery;
2166}
2167
2168
2169#ifndef DLL
2170/**
2171 * Signal handler.
2172 * Ensures that the database connection is closed at termination.
2173 * @param sig Signal number.
2174 */
2175void dbHandler(int sig)
2176{
2177 if (pmysql != NULL)
2178 {
2179 fprintf(stderr, "\n\t!disconnecting from database!\n");
2180 dbDisconnect();
2181 }
2182
2183 flushall();
2184 switch (sig)
2185 {
2186 case SIGBREAK:
2187 printf("\nSIGBREAK\n");
2188 exit(-1);
2189 break;
2190 case SIGINT:
2191 printf("\nSIGINT\n");
2192 exit(-1);
2193 break;
2194 case SIGTERM:
2195 printf("\nSIGTERM\n");
2196 exit(-1);
2197 break;
2198 case SIGSEGV:
2199 raise(sig);
2200 break;
2201 case SIGILL:
2202 printf("\nSIGILL\n");
2203 exit(-1);
2204 break;
2205 }
2206}
2207
2208
2209#else
2210/*******/
2211/* DLL */
2212/*******/
2213/* prototypes used in the _DLL_InitTerm function */
2214extern "C"
2215{
2216 int _CRT_init(void);
2217 void _CRT_term(void);
2218 void __ctordtorInit( void );
2219 void __ctordtorTerm( void );
2220 unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long ulFlag);
2221}
2222
2223
2224/**
2225 * Dll InitTerm function.
2226 * @returns 0 on success.
2227 * 1 on error.
2228 * @param hModule
2229 * @param ulFlags
2230 * @remark We'll ensure that the database connection is terminated as we terminate.
2231 */
2232unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long ulFlag)
2233{
2234 /*-------------------------------------------------------------------------*/
2235 /* If ulFlag is zero then the DLL is being loaded so initialization should */
2236 /* be performed. If ulFlag is 1 then the DLL is being freed so */
2237 /* termination should be performed. */
2238 /*-------------------------------------------------------------------------*/
2239
2240 switch (ulFlag)
2241 {
2242 case 0:
2243 if (_CRT_init() == -1)
2244 return 0;
2245 __ctordtorInit();
2246 break;
2247
2248 case 1:
2249 /* ensure that db connection is terminated */
2250 if (pmysql != NULL)
2251 {
2252 fprintf(stderr, "\n\t!disconnecting from database!\n");
2253 dbDisconnect();
2254 }
2255 __ctordtorTerm();
2256 break;
2257
2258 default:
2259 return 0;
2260 }
2261 hModule = hModule;
2262 return 1;
2263}
2264
2265/*****************************************************************/
2266/* -why is this terminate function referenced but not defined??? */
2267/* and where is it referenced??? */
2268/* -Probably an export missing from the libraries. */
2269/*****************************************************************/
2270void terminate(void)
2271{
2272 DosPutMessage(0, sizeof("terminate")-1, "terminate");
2273 exit(-1);
2274}
2275
2276/****************************************/
2277/* EMX run-time trouble */
2278/* _environ is missing when using -Zomf */
2279/****************************************/
2280char **_environ = environ;
2281
2282#endif
Note: See TracBrowser for help on using the repository browser.