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

Last change on this file since 2761 was 2761, checked in by bird, 26 years ago

Preformance and corrections.

File size: 51.7 KB
Line 
1/* $Id: db.cpp,v 1.6 2000-02-11 23:54:25 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);
82#ifndef DLL
83 extern "C" void dbHandler(int sig);
84#endif
85
86
87/**
88 * Gets the descriptions of the last database error.
89 * @returns Readonly string.
90 */
91char * _System dbGetLastErrorDesc(void)
92{
93 return mysql_error(&mysql);
94}
95
96
97/**
98 * Connects to local database.
99 * @returns Success indicator, TRUE / FALSE.
100 * @param pszDatabase Name of database to use.
101 */
102BOOL _System dbConnect(const char *pszHost, const char *pszUser, const char *pszPassword, const char *pszDatabase)
103{
104 BOOL fRet = FALSE;
105 #ifndef DLL
106 static fHandler = FALSE;
107 /* signal handler */
108 if (!fHandler)
109 {
110 if ( SIG_ERR == signal(SIGBREAK, dbHandler)
111 || SIG_ERR == signal(SIGINT, dbHandler)
112 || SIG_ERR == signal(SIGTERM, dbHandler)
113 || SIG_ERR == signal(SIGABRT, dbHandler)
114 || SIG_ERR == signal(SIGSEGV, dbHandler)
115 || SIG_ERR == signal(SIGILL, dbHandler)
116 )
117 fprintf(stderr, "Error installing signalhandler...");
118 else
119 fHandler = TRUE;
120 }
121 #endif
122
123 /* connect to server */
124 memset(&mysql, 0, sizeof(mysql));
125 pmysql = mysql_connect(&mysql, pszHost, pszUser, pszPassword);
126 if (pmysql != NULL)
127 {
128 /* connect to database */
129 fRet = mysql_select_db(pmysql, pszDatabase) >= 0;
130 if (fRet)
131 mysql_refresh(pmysql, REFRESH_TABLES);
132 }
133
134 return fRet;
135}
136
137
138/**
139 * Disconnects from database.
140 * @returns Success indicator. TRUE / FALSE.
141 */
142BOOL _System dbDisconnect(void)
143{
144 if (pmysql != NULL)
145 {
146 mysql_refresh(pmysql, REFRESH_TABLES);
147 mysql_close(pmysql);
148 pmysql = NULL;
149 }
150 return TRUE;
151}
152
153/**
154 * Gets the refid for the give dll name.
155 * @returns Dll refid. -1 on error.
156 * @param pszDllName Dll name.
157 */
158signed short _System dbGetDll(const char *pszDllName)
159{
160 int rc;
161 char szQuery[256];
162 MYSQL_RES * pres;
163
164 sprintf(&szQuery[0], "SELECT refcode FROM dll WHERE name = '%s'\n", pszDllName);
165 rc = mysql_query(pmysql, &szQuery[0]);
166 pres = mysql_store_result(pmysql);
167
168 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
169 rc = (int)getvalue(0, mysql_fetch_row(pres));
170 else
171 rc = -1;
172 mysql_free_result(pres);
173 return (short)rc;
174}
175
176
177/**
178 * Count the function in a given dll.
179 * @returns Number of functions. -1 on error.
180 * @param usDll Dll refcode.
181 */
182signed long _System dbCountFunctionInDll(signed long ulDll)
183{
184 signed long rc;
185 char szQuery[256];
186 MYSQL_RES * pres;
187
188 if (ulDll >= 0)
189 {
190 sprintf(&szQuery[0], "SELECT count(refcode) FROM function WHERE dll = %ld\n", ulDll);
191 rc = mysql_query(pmysql, &szQuery[0]);
192 pres = mysql_store_result(pmysql);
193
194 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
195 rc = (int)getvalue(0, mysql_fetch_row(pres));
196 else
197 rc = -1;
198 mysql_free_result(pres);
199 }
200 else
201 rc = -1;
202 return rc;
203}
204
205
206
207/**
208 * Checks if dll exists. If not exists the dll is inserted.
209 * @returns Dll refcode. -1 on errors.
210 * @param pszDll Dll name.
211 * @remark This search must be case insensitive.
212 * (In the mysql-world everything is case insensitive!)
213 */
214signed short _System dbCheckInsertDll(const char *pszDll)
215{
216 int rc;
217 char szQuery[256];
218 MYSQL_RES * pres;
219
220 /* try find match */
221 sprintf(&szQuery[0], "SELECT refcode, name FROM dll WHERE name = '%s'\n", pszDll);
222 rc = mysql_query(pmysql, &szQuery[0]);
223 pres = mysql_store_result(pmysql);
224
225 /* not found? - insert dll */
226 if (rc < 0 || pres == NULL || mysql_num_rows(pres) == 0)
227 {
228 mysql_free_result(pres);
229
230 sprintf(&szQuery[0], "INSERT INTO dll(name) VALUES('%s')\n", pszDll);
231 rc = mysql_query(pmysql, &szQuery[0]);
232
233 /* select row to get refcode */
234 sprintf(&szQuery[0], "SELECT refcode, name FROM dll WHERE name = '%s'\n", pszDll);
235 rc = mysql_query(pmysql, &szQuery[0]);
236 pres = mysql_store_result(pmysql);
237 }
238
239 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
240 rc = (int)getvalue(0, mysql_fetch_row(pres));
241 else
242 rc = -1;
243 mysql_free_result(pres);
244
245 return (short)rc;
246}
247
248
249/**
250 * Simple select for a long value.
251 * @returns long value
252 * @param pszTable From part.
253 * @param pszGetColumn Name of column to retreive.
254 * @param pszMatch1 Match column/expression
255 * @param pszMatchValue1 Match value.
256 * @remark Dirty! Don't use this!
257 */
258unsigned short _System dbGet(const char *pszTable, const char *pszGetColumn,
259 const char *pszMatch1, const char *pszMatchValue1)
260{
261 int rc;
262 char szQuery[256];
263 MYSQL_RES *pres;
264
265 /* try find match */
266 sprintf(&szQuery[0], "SELECT %s FROM %s WHERE %s = '%s'\n",
267 pszGetColumn, pszTable, pszMatch1, pszMatchValue1);
268 rc = mysql_query(pmysql, &szQuery[0]);
269 pres = mysql_store_result(pmysql);
270
271 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
272 rc = (int)getvalue(0, mysql_fetch_row(pres));
273 else
274 rc = -1;
275 mysql_free_result(pres);
276
277 return (short)rc;
278}
279
280
281/**
282 * Updates or inserts a function name into the database.
283 * @returns Success indicator. TRUE / FALSE.
284 * @param usDll Dll refcode.
285 * @param pszFunction Function name.
286 * @param pszIntFunction Internal function name.
287 * @param ulOrdinal Ordinal value.
288 * @param fIgnoreOrdinal Do not update ordinal value.
289 */
290BOOL _System dbInsertUpdateFunction(unsigned short usDll,
291 const char *pszFunction, const char *pszIntFunction,
292 unsigned long ulOrdinal, BOOL fIgnoreOrdinal)
293{
294 int rc;
295 long lFunction = -1;
296 char szQuery[256];
297 MYSQL_RES *pres;
298
299 /* when no internal name, the exported name is the internal name too! */
300 if (pszIntFunction == NULL || *pszIntFunction == '\0')
301 pszIntFunction = pszFunction;
302
303 /* try find function */
304 sprintf(&szQuery[0], "SELECT refcode, intname FROM function WHERE dll = %d AND name = '%s'", usDll, pszFunction);
305 rc = mysql_query(pmysql, &szQuery[0]);
306 pres = mysql_store_result(pmysql);
307 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) != 0)
308 { /* update function (function is found) */
309 MYSQL_ROW parow;
310 if (mysql_num_rows(pres) > 1)
311 {
312 fprintf(stderr, "internal database integrity error(%s): More function by the same name for the same dll. "
313 "usDll = %d, pszFunction = %s\n", __FUNCTION__, usDll, pszFunction);
314 return FALSE;
315 }
316
317 parow = mysql_fetch_row(pres);
318 if (parow != NULL)
319 lFunction = getvalue(0, parow);
320 mysql_free_result(pres);
321
322 if (strcmp(parow[1], pszIntFunction) != 0)
323 {
324 sprintf(&szQuery[0], "UPDATE function SET intname = '%s' WHERE refcode = %ld",
325 pszIntFunction, lFunction);
326 rc = mysql_query(pmysql, &szQuery[0]);
327 }
328
329 if (rc >= 0 && !fIgnoreOrdinal)
330 {
331 sprintf(&szQuery[0], "UPDATE function SET ordinal = %ld WHERE refcode = %ld",
332 ulOrdinal, lFunction);
333 rc = mysql_query(pmysql, &szQuery[0]);
334 }
335
336 }
337 else
338 { /* insert */
339 sprintf(&szQuery[0], "INSERT INTO function(dll, name, intname, ordinal) VALUES(%d, '%s', '%s', %ld)",
340 usDll, pszFunction, pszIntFunction, ulOrdinal);
341 rc = mysql_query(pmysql, &szQuery[0]);
342 }
343
344 return rc >= 0;
345}
346
347
348/**
349 * Get a long value.
350 * @returns Number value of pRow[iField]. -1 on error.
351 * @param iField Index into pRow.
352 * @param pRow Pointer to array (of string pointers).
353 */
354static long getvalue(int iField, MYSQL_ROW papszRow)
355{
356 if (papszRow[iField] != NULL)
357 return atol((char*)papszRow[iField]);
358
359 return -1;
360}
361
362#if 0
363/**
364 * Find occurences of a function, given by internal name.
365 * @returns success indicator, TRUE / FALSE.
366 * @param pszFunctionName
367 * @param pFnFindBuf
368 * @param lDll
369 */
370BOOL _System dbFindFunction(const char *pszFunctionName, PFNFINDBUF pFnFindBuf, signed long lDll)
371{
372 MYSQL_RES *pres;
373 MYSQL_ROW row;
374 int rc;
375 char szQuery[256];
376
377 if (lDll < 0)
378 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function WHERE intname = '%s'",
379 pszFunctionName);
380 else
381 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function "
382 "WHERE intname = '%s' AND dll = %ld",
383 pszFunctionName, lDll);
384
385 rc = mysql_query(pmysql, &szQuery[0]);
386 if (rc >= 0)
387 {
388 pres = mysql_store_result(pmysql);
389 if (pres != NULL)
390 {
391 pFnFindBuf->cFns = 0;
392 while ((row = mysql_fetch_row(pres)) != NULL)
393 {
394 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]);
395 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
396 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
397
398 /* next */
399 pFnFindBuf->cFns++;
400 }
401 mysql_free_result(pres);
402
403 /* alias check and fix */
404 if (lDll >= 0 && pFnFindBuf->cFns != 0)
405 {
406 int i;
407
408 /* Make the selected function to DONT TOUCH */
409 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (-2) "
410 "WHERE (",
411 lDll, pszFunctionName);
412 for (i = 0; i < pFnFindBuf->cFns; i++)
413 {
414 if (i != 0) strcat(&szQuery[0], " OR");
415 sprintf(&szQuery[strlen(szQuery)], " refcode = %ld", pFnFindBuf->alRefCode[i]);
416 }
417 strcat(&szQuery[0], ") AND aliasfn <> (-2)");
418
419 rc = mysql_query(pmysql, &szQuery[0]);
420 if (rc >= 0)
421 {
422 /* Update all with equal internal... which is not in this Dll */
423 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) "
424 "WHERE aliasfn = (-1) AND dll <> %ld AND intname = '%s'",
425 pFnFindBuf->alRefCode[0], lDll, pszFunctionName, pszFunctionName);
426 rc = mysql_query(pmysql, &szQuery[0]);
427 if (rc >= 0)
428 {
429 /* Update all with equal external name... which is not in this Dll */
430 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) "
431 "WHERE aliasfn = (-1) AND dll <> %ld AND name = '%s'",
432 pFnFindBuf->alRefCode[0], lDll, pszFunctionName, pszFunctionName);
433
434 rc = mysql_query(pmysql, &szQuery[0]);
435 if (rc >= 0)
436 {
437 /* get the functions aliases to the functions we have allready found. */
438 strcpy(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function WHERE (");
439 for (i = 0; i < pFnFindBuf->cFns; i++)
440 {
441 if (i != 0) strcat(&szQuery[0], "OR ");
442 sprintf(&szQuery[strlen(szQuery)], "aliasfn = %ld ", pFnFindBuf->alRefCode[i]);
443 }
444 sprintf(&szQuery[strlen(szQuery)], ") AND dll <> %ld", lDll);
445
446 rc = mysql_query(pmysql, &szQuery[0]);
447 if (rc >= 0)
448 {
449 pres = mysql_store_result(pmysql);
450 if (pres != NULL)
451 {
452 while ((row = mysql_fetch_row(pres)) != NULL)
453 {
454 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]);
455 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
456 if (row[2] != NULL)
457 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
458 else
459 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL;
460
461 /* next */
462 pFnFindBuf->cFns++;
463 }
464 mysql_free_result(pres);
465 }
466 }
467 }
468 }
469 }
470 }
471 }
472 else
473 rc = -1;
474 }
475
476 return rc >= 0;
477}
478#else
479int mysql_query1(MYSQL *mysql, const char *q)
480{
481 return mysql_query(mysql, q);
482}
483
484int mysql_query2(MYSQL *mysql, const char *q)
485{
486 return mysql_query(mysql, q);
487}
488
489int mysql_query3(MYSQL *mysql, const char *q)
490{
491 return mysql_query(mysql, q);
492}
493
494int mysql_query4(MYSQL *mysql, const char *q)
495{
496 return mysql_query(mysql, q);
497}
498
499int mysql_query5(MYSQL *mysql, const char *q)
500{
501 return mysql_query(mysql, q);
502}
503
504MYSQL_RES * mysql_store_result1(MYSQL *mysql)
505{
506 return mysql_store_result(mysql);
507}
508
509MYSQL_RES * mysql_store_result2(MYSQL *mysql)
510{
511 return mysql_store_result(mysql);
512}
513
514
515/**
516 * Find occurences of a function, given by internal name.
517 * @returns success indicator, TRUE / FALSE.
518 * @param pszFunctionName
519 * @param pFnFindBuf
520 * @param lDll
521 */
522BOOL _System dbFindFunction(const char *pszFunctionName, PFNFINDBUF pFnFindBuf, signed long lDll)
523{
524 MYSQL_RES *pres;
525 MYSQL_ROW row;
526 int rc;
527 char szQuery[256];
528
529 if (lDll < 0)
530 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function WHERE intname = '%s'",
531 pszFunctionName);
532 else
533 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function "
534 "WHERE intname = '%s' AND dll = %ld",
535 pszFunctionName, lDll);
536
537 rc = mysql_query1(pmysql, &szQuery[0]);
538 if (rc >= 0)
539 {
540 pres = mysql_store_result1(pmysql);
541 if (pres != NULL)
542 {
543 pFnFindBuf->cFns = 0;
544 while ((row = mysql_fetch_row(pres)) != NULL)
545 {
546 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]);
547 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
548 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
549
550 /* next */
551 pFnFindBuf->cFns++;
552 }
553 mysql_free_result(pres);
554
555 /* alias check and fix */
556 if (lDll >= 0 && pFnFindBuf->cFns != 0)
557 {
558 int i;
559
560 /* Make the selected function to DONT TOUCH */
561 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (-2) "
562 "WHERE (",
563 lDll, pszFunctionName);
564 for (i = 0; i < pFnFindBuf->cFns; i++)
565 {
566 if (i != 0) strcat(&szQuery[0], " OR");
567 sprintf(&szQuery[strlen(szQuery)], " refcode = %ld", pFnFindBuf->alRefCode[i]);
568 }
569 strcat(&szQuery[0], ") AND aliasfn <> (-2)");
570
571 rc = mysql_query2(pmysql, &szQuery[0]);
572 if (rc >= 0)
573 {
574 /* Update all with equal internal... which is not in this Dll */
575 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) "
576 "WHERE aliasfn = (-1) AND dll <> %ld AND intname = '%s'",
577 pFnFindBuf->alRefCode[0], lDll, pszFunctionName, pszFunctionName);
578 rc = mysql_query3(pmysql, &szQuery[0]);
579 if (rc >= 0)
580 {
581 /* Update all with equal external name... which is not in this Dll */
582 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) "
583 "WHERE aliasfn = (-1) AND dll <> %ld AND name = '%s'",
584 pFnFindBuf->alRefCode[0], lDll, pszFunctionName, pszFunctionName);
585
586 rc = mysql_query4(pmysql, &szQuery[0]);
587 if (rc >= 0)
588 {
589 int cFns = pFnFindBuf->cFns;
590 for (i = 0; i < cFns; i++)
591 {
592 /* get the functions aliases to the functions we have allready found. */
593 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function WHERE aliasfn = %ld AND dll <> %ld",
594 pFnFindBuf->alRefCode[i], lDll);
595 rc = mysql_query5(pmysql, &szQuery[0]);
596 if (rc >= 0)
597 {
598 pres = mysql_store_result2(pmysql);
599 if (pres != NULL)
600 {
601 while ((row = mysql_fetch_row(pres)) != NULL)
602 {
603 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]);
604 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
605 if (row[2] != NULL)
606 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
607 else
608 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL;
609
610 /* next */
611 if (pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] != lDll)
612 pFnFindBuf->cFns++;
613 }
614 mysql_free_result(pres);
615 }
616 }
617 }
618 }
619 }
620 }
621 }
622 }
623 else
624 rc = -1;
625 }
626
627 return rc >= 0;
628}
629
630#endif
631
632
633/**
634 * Finds the refcode for an author, if the author exists.
635 * @returns Author 'refcode'.
636 * @param pszAuthor String which holds the identifier of an author.
637 * This doesn't have to be the name. Initials, alias and email
638 * is also searched.
639 */
640signed long _System dbFindAuthor(const char *pszAuthor)
641{
642 signed long refcode = -1;
643 MYSQL_RES *pres;
644 char szQuery[256];
645
646 sprintf(&szQuery[0],
647 "SELECT refcode FROM author "
648 "WHERE name = '%s' OR "
649 " initials = '%s' OR "
650 " alias = '%s' OR "
651 " email = '%s'",
652 pszAuthor, pszAuthor, pszAuthor, pszAuthor);
653 if (mysql_query(pmysql, &szQuery[0]) >= 0)
654 {
655 pres = mysql_store_result(pmysql);
656 if (pres != NULL)
657 {
658 MYSQL_ROW parow;
659
660 /* integrety check */
661 if (mysql_num_rows(pres) > 1)
662 fprintf(stderr, "Integrety: author '%s' is not unique!\n", pszAuthor);
663 parow = mysql_fetch_row(pres);
664 if (parow != NULL)
665 refcode = getvalue(0, parow);
666
667 mysql_free_result(pres);
668 }
669 }
670
671 return refcode;
672}
673
674
675/**
676 * Gets the state of a function.
677 * @returns state code. On error -1.
678 * @param lRefCode Function refcode.
679 */
680signed long _System dbGetFunctionState(signed long lRefCode)
681{
682 signed long lState = -1;
683 MYSQL_RES *pres;
684 char szQuery[128];
685
686 sprintf(&szQuery[0], "SELECT state FROM function WHERE refcode = %ld", lRefCode);
687 if (mysql_query(pmysql, &szQuery[0]) >= 0)
688 {
689 pres = mysql_store_result(pmysql);
690 if (pres != NULL)
691 {
692 MYSQL_ROW parow = mysql_fetch_row(pres);
693 if (parow != NULL)
694 lState = getvalue(0, parow);
695 mysql_free_result(pres);
696 }
697 }
698
699 return lState;
700}
701
702
703/**
704 * Updates function information.
705 * @returns number of errors.
706 * @param pFnDesc Function description struct.
707 * @param lDll Dll which we are working at.
708 * @param pszError Buffer for error messages
709 * @result on error(s) pszError will hold information about the error(s).
710 */
711unsigned long _System dbUpdateFunction(PFNDESC pFnDesc, signed long lDll, char *pszError)
712{
713 char szQuery[256];
714 char *pszQuery = &szQuery[0];
715 long lCurrentState;
716 int i,k,rc;
717 unsigned long ulRc = 0;
718 BOOL f = FALSE;
719
720 for (k = 0; k < pFnDesc->cRefCodes; k++)
721 {
722 /* set updated flag */
723 sprintf(pszQuery, "UPDATE function SET updated = updated + 1 WHERE refcode = %ld",
724 pFnDesc->alRefCode[k]);
725 rc = mysql_query(pmysql, &szQuery[0]);
726
727 /* get current status */
728 lCurrentState = dbGetFunctionState(pFnDesc->alRefCode[k]);
729 if (lCurrentState == -1)
730 {
731 strcpy(pszError, dbGetLastErrorDesc());
732 return 1;
733 }
734
735 /* update function table first */
736 strcpy(pszQuery, "UPDATE function SET ");
737 pszQuery += strlen(pszQuery);
738 if (pFnDesc->lStatus != 99 || lCurrentState == 0)
739 {
740 sprintf(pszQuery, "state = %ld ", pFnDesc->lStatus);
741 f = TRUE;
742 }
743 pszQuery += strlen(pszQuery);
744
745 if (pFnDesc->pszReturnType != NULL)
746 {
747 if (f) strcat(pszQuery, ", ");
748 sprintf(pszQuery + strlen(pszQuery), "return = '%s' ", pFnDesc->pszReturnType);
749 pszQuery += strlen(pszQuery);
750 f = TRUE;
751 }
752
753 if (f)
754 {
755 sprintf(pszQuery + strlen(pszQuery), "WHERE refcode = %ld", pFnDesc->alRefCode[k]);
756 rc = mysql_query(pmysql, &szQuery[0]);
757 if (rc < 0)
758 {
759 sprintf(pszError, "Updating functiontable failed with error: %s - (sql=%s) ",
760 dbGetLastErrorDesc(), &szQuery[0]);
761 pszError += strlen(pszError) - 1;
762 ulRc++;
763 }
764 }
765
766 /* parameters */
767 pszQuery = &szQuery[0];
768 sprintf(pszQuery, "DELETE FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]);
769 rc = mysql_query(pmysql, &szQuery[0]);
770 if (rc < 0)
771 {
772 if (*pszError == ' ')
773 strcpy(pszError++, "\n\t");
774 sprintf(pszError, "Deleting old parameters failed with error: %s - (sql=%s) ",
775 dbGetLastErrorDesc(), &szQuery[0]);
776 pszError += strlen(pszError) - 1;
777 ulRc++;
778 }
779
780 for (i = 0; i < pFnDesc->cParams; i++)
781 {
782 sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name) "
783 "VALUES (%ld, %d, '%s', '%s')",
784 pFnDesc->alRefCode[k], i,
785 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
786 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : ""
787 );
788 rc = mysql_query(pmysql, pszQuery);
789 if (rc < 0)
790 {
791 if (*pszError == ' ')
792 strcpy(pszError++, "\n\t");
793 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
794 i, dbGetLastErrorDesc(), &szQuery[0]);
795 pszError += strlen(pszError) - 1;
796 ulRc++;
797 }
798 }
799
800 /* authors */
801 pszQuery = &szQuery[0];
802 sprintf(pszQuery, "DELETE FROM fnauthor WHERE function = %ld", pFnDesc->alRefCode[k]);
803 rc = mysql_query(pmysql, &szQuery[0]);
804 if (rc < 0)
805 {
806 if (*pszError == ' ')
807 strcpy(pszError++, "\n\t");
808 sprintf(pszError, "Deleting old authors failed with error: %s - (sql=%s) ",
809 dbGetLastErrorDesc(), &szQuery[0]);
810 pszError += strlen(pszError) - 1;
811 ulRc++;
812 }
813
814 for (i = 0; i < pFnDesc->cAuthors; i++)
815 {
816 if (pFnDesc->alAuthorRefCode[i] == -1)
817 continue;
818 sprintf(pszQuery, "INSERT INTO fnauthor(author, function) "
819 "VALUES (%ld, %ld)",
820 pFnDesc->alAuthorRefCode[i], pFnDesc->alRefCode[k]);
821 rc = mysql_query(pmysql, pszQuery);
822 if (rc < 0)
823 {
824 if (*pszError == ' ')
825 strcpy(pszError++, "\n\t");
826 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
827 i, dbGetLastErrorDesc(), &szQuery[0]);
828 pszError += strlen(pszError) - 1;
829 ulRc++;
830 }
831 }
832 } /* for */
833
834 lDll = lDll;
835 return ulRc;
836}
837
838
839/**
840 * Updates the history tables.
841 * @returns Number of signals/errors.
842 * @param pszError Pointer to buffer which will hold the error messages.
843 * @remark This should be called whenever updates have been completed.
844 */
845unsigned long _System dbCreateHistory(char *pszError)
846{
847 unsigned long ulRc = 0;
848 MYSQL_RES *pres;
849 MYSQL_ROW row;
850 char szQuery[256];
851 char *pszQuery = &szQuery[0];
852 int rc;
853 char szCurDt[20] = {0}; /*yyyy-mm-dd\0*/
854
855 mysql_refresh(pmysql, REFRESH_TABLES);
856
857 /* get currentdate - just in case the date changes between the delete and the update is completed. */
858 strcpy(pszQuery, "SELECT CURDATE()");
859 rc = mysql_query(pmysql, pszQuery);
860 pres = mysql_use_result(pmysql);
861 if (rc >= 0 && pres != NULL)
862 {
863 row = mysql_fetch_row(pres);
864 if (row != NULL && mysql_num_rows(pres) == 1)
865 {
866 strcpy(&szCurDt[0], row[0]);
867 while (mysql_fetch_row(pres) != NULL)
868 pres=pres;
869
870 /* delete - all rows on this date in the history tables */
871 sprintf(pszQuery, "DELETE FROM historydll WHERE date = '%s'", &szCurDt[0]);
872 rc = mysql_query(pmysql, pszQuery);
873 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
874
875 sprintf(pszQuery, "DELETE FROM historyapigroup WHERE date = '%s'", &szCurDt[0]);
876 rc = mysql_query(pmysql, pszQuery);
877 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
878
879 sprintf(pszQuery, "DELETE FROM historydlltotal WHERE date = '%s'", &szCurDt[0]);
880 rc = mysql_query(pmysql, pszQuery);
881 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
882
883 sprintf(pszQuery, "DELETE FROM historyapigrouptotal WHERE date = '%s'", &szCurDt[0]);
884 CheckLogContinue((pszError, "error removing old history rows: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
885
886 /* insert new stats */
887 sprintf(pszQuery, "INSERT INTO historydll(dll, state, date, count) "
888 "SELECT dll, state, '%s', count(*) FROM function GROUP BY dll, state",
889 &szCurDt[0]);
890 rc = mysql_query(pmysql, pszQuery);
891 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
892
893 sprintf(pszQuery, "INSERT INTO historyapigroup(apigroup, state, date, count) "
894 "SELECT dll, state, '%s', count(*) FROM function WHERE apigroup IS NOT NULL "
895 "GROUP BY apigroup, state",
896 &szCurDt[0]);
897 rc = mysql_query(pmysql, pszQuery);
898 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
899
900 /* inserting new totals */
901 sprintf(pszQuery, "INSERT INTO historydlltotal(dll, date, totalcount) "
902 "SELECT dll, '%s', count(*) FROM function GROUP BY dll",
903 &szCurDt[0]);
904 rc = mysql_query(pmysql, pszQuery);
905 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
906
907 sprintf(pszQuery, "INSERT INTO historyapigrouptotal(apigroup, date, totalcount) "
908 "SELECT apigroup, '%s', count(*) FROM function WHERE apigroup IS NOT NULL "
909 "GROUP BY apigroup",
910 &szCurDt[0]);
911 rc = mysql_query(pmysql, pszQuery);
912 CheckLogContinue((pszError, "error inserting: %s - (sql=%s) ", dbGetLastErrorDesc(), pszQuery));
913 }
914 else
915 {
916 sprintf(pszError, "error getting current date (row == NULL): %s - (sql=%s) ",
917 dbGetLastErrorDesc(), pszQuery);
918 ulRc++;
919 }
920 }
921 else
922 {
923 sprintf(pszError, "error getting current date: %s - (sql=%s) ",
924 dbGetLastErrorDesc(), pszQuery);
925 ulRc++;
926 }
927
928 mysql_refresh(pmysql, REFRESH_TABLES);
929
930 return ulRc;
931}
932
933
934/**
935 * Check that database integrety is ok. Verfies foreign keys.
936 * @returns numbers of errors.
937 * @param pszError Very large buffer which will hold error messges (if any).
938 * @sketch
939 * @remark current versions of mysql don't support 'SELECT ... WHERE id NOT IN(SELECT id FROM table)'
940 */
941unsigned long _System dbCheckIntegrity(char *pszError)
942{
943 char szQuery[384];
944 char *pszQuery = &szQuery[0];
945 MYSQL_RES *pres1;
946 MYSQL_RES *pres2;
947 MYSQL_ROW row1;
948 int rc;
949 unsigned long ulRc = 0;
950
951 mysql_refresh(pmysql, REFRESH_TABLES);
952
953 /* foreign keys in function table */
954 strcpy(pszQuery, "SELECT refcode, dll, state, apigroup FROM function");
955 rc = mysql_query(pmysql, pszQuery);
956 if (rc >= 0)
957 {
958 pres1 = mysql_store_result(pmysql);
959 if (pres1 != NULL)
960 {
961 while ((row1 = mysql_fetch_row(pres1)) != NULL)
962 {
963 /* check dll */
964 sprintf(pszQuery, "SELECT name FROM dll WHERE refcode = %s", row1[1]);
965 rc = mysql_query(pmysql, pszQuery);
966 CheckFKError("function/dll", "Foreign key 'dll' not found in the dll table");
967
968 /* check state */
969 sprintf(pszQuery, "SELECT name FROM state WHERE refcode = %s", row1[2]);
970 rc = mysql_query(pmysql, pszQuery);
971 CheckFKError("function/state", "Foreign key 'state' not found in the state table");
972
973 /* check apigroup */
974 if (row1[3] != NULL)
975 {
976 sprintf(pszQuery, "SELECT name FROM apigroup WHERE refcode = %s", row1[3]);
977 rc = mysql_query(pmysql, pszQuery);
978 CheckFKError("function/state", "Foreign key 'state' not found in the state table");
979 }
980 }
981 mysql_free_result(pres1);
982 }
983 }
984 else
985 ulRc += logDbError(pszError, pszQuery);
986
987 /* foreign keys in apigroup */
988 strcpy(pszQuery, "SELECT refcode, dll FROM apigroup");
989 rc = mysql_query(pmysql, pszQuery);
990 if (rc >= 0)
991 {
992 pres1 = mysql_store_result(pmysql);
993 if (pres1 != NULL)
994 {
995 while ((row1 = mysql_fetch_row(pres1)) != NULL)
996 {
997 /* check dll */
998 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
999 rc = mysql_query(pmysql, pszQuery);
1000 CheckFKError("apigroup/dll", "Foreign key 'dll' not found in the dll table");
1001 }
1002 mysql_free_result(pres1);
1003 }
1004 }
1005 else
1006 ulRc += logDbError(pszError, pszQuery);
1007
1008 /* foreign keys in fnauthor */
1009 strcpy(pszQuery, "SELECT function, author FROM fnauthor");
1010 rc = mysql_query(pmysql, pszQuery);
1011 if (rc >= 0)
1012 {
1013 pres1 = mysql_store_result(pmysql);
1014 if (pres1 != NULL)
1015 {
1016 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1017 {
1018 /* check function */
1019 sprintf(pszQuery, "SELECT refcode FROM function WHERE refcode = %s", row1[1]);
1020 rc = mysql_query(pmysql, pszQuery);
1021 CheckFKError("fnauthor/function", "Foreign key 'function' not found in the function table");
1022
1023 /* check author */
1024 sprintf(pszQuery, "SELECT refcode FROM author WHERE refcode = %s", row1[1]);
1025 rc = mysql_query(pmysql, pszQuery);
1026 CheckFKError("fnauthor/author", "Foreign key 'author' not found in the author table");
1027 }
1028 mysql_free_result(pres1);
1029 }
1030 }
1031 else
1032 ulRc += logDbError(pszError, pszQuery);
1033
1034 /* foreign keys in historydll table */
1035 strcpy(pszQuery, "SELECT date, dll, state FROM historydll");
1036 rc = mysql_query(pmysql, pszQuery);
1037 if (rc >= 0)
1038 {
1039 pres1 = mysql_store_result(pmysql);
1040 if (pres1 != NULL)
1041 {
1042 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1043 {
1044 /* check dll */
1045 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1046 rc = mysql_query(pmysql, pszQuery);
1047 CheckFKError("historydll/dll", "Foreign key 'dll' not found in the dll table");
1048
1049 /* check state */
1050 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
1051 rc = mysql_query(pmysql, pszQuery);
1052 CheckFKError("historydll/state", "Foreign key 'state' not found in the state table");
1053 }
1054 mysql_free_result(pres1);
1055 }
1056 }
1057 else
1058 ulRc += logDbError(pszError, pszQuery);
1059
1060 /* foreign keys in historyapigroup table */
1061 strcpy(pszQuery, "SELECT date, apigroup, state FROM historyapigroup");
1062 rc = mysql_query(pmysql, pszQuery);
1063 if (rc >= 0)
1064 {
1065 pres1 = mysql_store_result(pmysql);
1066 if (pres1 != NULL)
1067 {
1068 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1069 {
1070 /* check dll */
1071 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[1]);
1072 rc = mysql_query(pmysql, pszQuery);
1073 CheckFKError("historyapigroup/apigroup", "Foreign key 'apigroup' not found in the apigroup table");
1074
1075 /* check state */
1076 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
1077 rc = mysql_query(pmysql, pszQuery);
1078 CheckFKError("historyapigroup/state", "Foreign key 'state' not found in the state table");
1079 }
1080 mysql_free_result(pres1);
1081 }
1082 }
1083 else
1084 ulRc += logDbError(pszError, pszQuery);
1085
1086 /* foreign keys in historydlltotal table */
1087 strcpy(pszQuery, "SELECT date, dll FROM historydlltotal");
1088 rc = mysql_query(pmysql, pszQuery);
1089 if (rc >= 0)
1090 {
1091 pres1 = mysql_store_result(pmysql);
1092 if (pres1 != NULL)
1093 {
1094 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1095 {
1096 /* check dll */
1097 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
1098 rc = mysql_query(pmysql, pszQuery);
1099 CheckFKError("historydlltotal/dll", "Foreign key 'dll' not found in the dll table");
1100 }
1101 mysql_free_result(pres1);
1102 }
1103 }
1104 else
1105 ulRc += logDbError(pszError, pszQuery);
1106
1107 /* foreign keys in historyapigroup table */
1108 strcpy(pszQuery, "SELECT date, apigroup FROM historyapigrouptotal");
1109 rc = mysql_query(pmysql, pszQuery);
1110 if (rc >= 0)
1111 {
1112 pres1 = mysql_store_result(pmysql);
1113 if (pres1 != NULL)
1114 {
1115 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1116 {
1117 /* check dll */
1118 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[1]);
1119 rc = mysql_query(pmysql, pszQuery);
1120 CheckFKError("historyapigrouptotal/apigroup", "Foreign key 'apigroup' not found in the apigroup table");
1121 }
1122 mysql_free_result(pres1);
1123 }
1124 }
1125 else
1126 ulRc += logDbError(pszError, pszQuery);
1127
1128 /* foreign keys in parameter table */
1129 strcpy(pszQuery, "SELECT sequencenbr, function FROM parameter");
1130 rc = mysql_query(pmysql, pszQuery);
1131 if (rc >= 0)
1132 {
1133 pres1 = mysql_store_result(pmysql);
1134 if (pres1 != NULL)
1135 {
1136 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1137 {
1138 /* check function */
1139 sprintf(pszQuery, "SELECT refcode FROM function WHERE refcode = %s", row1[1]);
1140 rc = mysql_query(pmysql, pszQuery);
1141 CheckFKError("parameter/function", "Foreign key 'function' not found in the function table");
1142 }
1143 mysql_free_result(pres1);
1144 }
1145 }
1146 else
1147 ulRc += logDbError(pszError, pszQuery);
1148
1149 /* Author table is special, since you should be able to interchangably reference an
1150 * author by any of the following tables:
1151 * name
1152 * initials
1153 * alias
1154 * email
1155 */
1156 strcpy(pszQuery, "SELECT name, initials, alias, email FROM author");
1157 rc = mysql_query(pmysql, pszQuery);
1158 if (rc >= 0)
1159 {
1160 pres1 = mysql_store_result(pmysql);
1161 if (pres1 != NULL)
1162 {
1163 while ((row1 = mysql_fetch_row(pres1)) != NULL)
1164 {
1165 /* check name */
1166 sprintf(pszQuery, "SELECT name FROM author WHERE "
1167 "initials = '%s' OR alias = '%s' OR email = '%s'",
1168 row1[0], row1[0], row1[0]);
1169 ulRc += CheckAuthorError(pszError, "name", row1[0], pszQuery);
1170
1171 /* check initials */
1172 sprintf(pszQuery, "SELECT name FROM author WHERE "
1173 "alias = '%s' OR email = '%s'",
1174 row1[1], row1[1]);
1175 ulRc += CheckAuthorError(pszError, "initials", row1[1], pszQuery);
1176
1177 /* alias */
1178 if (row1[2] != NULL)
1179 {
1180 sprintf(pszQuery, "SELECT name FROM author WHERE "
1181 "email = '%s'",
1182 row1[2]);
1183 ulRc += CheckAuthorError(pszError, "alias", row1[2], pszQuery);
1184 }
1185 }
1186 mysql_free_result(pres1);
1187 }
1188 }
1189 else
1190 ulRc += logDbError(pszError, pszQuery);
1191
1192 return ulRc;
1193}
1194
1195
1196/**
1197 * Checks for duplicate key and sql error for a given author key in the author table... (arg!)
1198 * @returns Number of errors.
1199 * @param pszError Reference to error buffer pointer.
1200 * @param pszFieldName Key field name; used for logging.
1201 * @param pszFieldValue Key value; used for logging
1202 * @param pszQuery Query which is to be exectued to test for duplicate key.
1203 * @remark Uses pszError[1] == '\xFE' to detect when to insert '\n\t'.
1204 */
1205static unsigned long CheckAuthorError(char * &pszError, const char *pszFieldName, const char *pszFieldValue, const char *pszQuery)
1206{
1207 MYSQL_ROW row;
1208 MYSQL_RES *pres;
1209 unsigned long ulRc = 0;
1210 int rc;
1211
1212 rc = mysql_query(pmysql, pszQuery);
1213 pres = mysql_store_result(pmysql);
1214 if (rc < 0 || (pres != NULL && mysql_num_rows(pres) != 0))
1215 { /* some kind of error has occurred */
1216 if (pszError[1] == '\xFE')
1217 {
1218 strcat(pszError, "\n\t");
1219 pszError += 2;
1220 }
1221
1222 if (rc < 0) /* sql error or 'duplicate key' */
1223 {
1224 sprintf(pszError, "author/%s: select failed - %s (sql=%s)",
1225 pszFieldName, dbGetLastErrorDesc(), pszQuery);
1226 }
1227 else
1228 { /* 'duplicate key' - print duplicates */
1229 sprintf(pszError, "author/%s: 'duplicate key', %s='%s': ",
1230 pszFieldName, pszFieldValue, pszFieldName);
1231
1232 while ((row = mysql_fetch_row(pres)) != NULL)
1233 {
1234 pszError += strlen(pszError);
1235 sprintf(pszError, "'%s' ", row[0]);
1236 }
1237 }
1238
1239 pszError += strlen(pszError);
1240 pszError[1] = '\xFE';
1241 ulRc = 1;
1242 }
1243 if (pres != NULL)
1244 mysql_free_result(pres);
1245
1246 return ulRc;
1247}
1248
1249
1250/**
1251 * Writes db error (rc<0) to the log buffer.
1252 * @returns Number of signals.
1253 * @param pszError Reference to the error buffer pointer.
1254 * @param pszQuery Pointer to query which was executed.
1255 * @remark Uses pszError[1] == '\xFE' to detect when to insert '\n\t'.
1256 */
1257static unsigned long logDbError(char * &pszError, const char *pszQuery)
1258{
1259 if (pszError[1] == '\xFE')
1260 {
1261 strcat(pszError, "\n\t");
1262 pszError += 2;
1263 }
1264 sprintf(pszError, "select failed: %s - (sql=%s)", dbGetLastErrorDesc(), pszQuery);
1265
1266 pszError += strlen(pszError);
1267 pszError[1] = '\xFE';
1268
1269 return 1;
1270}
1271
1272
1273/**
1274 * Executes a give query and returns a result identifier/pointer.
1275 * @returns Query result identifier/pointer. NULL on error.
1276 * @param pszQuery Pointer to query.
1277 * @remark Used by and designed for kHtmlPC.
1278 */
1279void * _System dbExecuteQuery(const char *pszQuery)
1280{
1281 assert(pmysql != NULL);
1282 if (mysql_query(pmysql, pszQuery) >= 0)
1283 return mysql_store_result(pmysql);
1284
1285 return NULL;
1286}
1287
1288
1289/**
1290 * Asks for the number of rows in the result.
1291 * @returns Number of rows in the result. -1 on error.
1292 * @param pres Query result identifier/pointer.
1293 * @remark Used by and designed for kHtmlPC.
1294 */
1295signed long _System dbQueryResultRows(void *pres)
1296{
1297 if (pres == NULL)
1298 return -1;
1299 return mysql_num_rows((MYSQL_RES*)pres);
1300}
1301
1302
1303/**
1304 * Frees the storage allocated by the given result.
1305 * @returns Success indicator, TRUE/FALSE.
1306 * @param pres Query result identifier/pointer.
1307 * @remark Used by and designed for kHtmlPC.
1308 */
1309BOOL _System dbFreeResult(void *pres)
1310{
1311 if (pres != NULL)
1312 mysql_free_result((MYSQL_RES*)pres);
1313 else
1314 return FALSE;
1315 return TRUE;
1316}
1317
1318
1319/**
1320 * Fetch data from a result. Returns the data by calling the given callback function.
1321 * @returns Success indicator, TRUE/FALSE.
1322 * @param pres Query result identifier/pointer.
1323 * @param dbFetchCallBack Callback-function.
1324 * @param pvUser User parameter which is passed onto dbFetchCallBack.
1325 * @remark Used by and designed for kHtmlPC.
1326 */
1327BOOL _System dbFetch(void *pres, DBCALLBACKFETCH dbFetchCallBack, void *pvUser)
1328{
1329 BOOL fRc = FALSE;
1330 MYSQL_ROW row = mysql_fetch_row((MYSQL_RES*)pres);
1331
1332 if (row)
1333 {
1334 MYSQL_FIELD *pField;
1335 int i = 0;
1336 mysql_field_seek((MYSQL_RES*)pres, 0);
1337
1338 while ((pField = mysql_fetch_field((MYSQL_RES*)pres)) != NULL)
1339 if (dbFetchCallBack(row[i++], pField->name, pvUser) != 0)
1340 return FALSE;
1341
1342 fRc = TRUE;
1343 }
1344
1345 return fRc;
1346}
1347
1348
1349/**
1350 * Converts an ISO date to days after Christ, year 0.
1351 * @returns days. -1 on error;
1352 * @param pszDate ISO Date.
1353 */
1354signed long _System dbDateToDaysAfterChrist(const char *pszDate)
1355{
1356 signed long lRet = -1;
1357 char szQuery[128];
1358
1359 sprintf(&szQuery[0], "SELECT to_days('%s')", pszDate);
1360 if (mysql_query(pmysql, &szQuery[0]) >= 0)
1361 {
1362 MYSQL_ROW row;
1363 MYSQL_RES *pres = mysql_use_result(pmysql);
1364 row = mysql_fetch_row(pres);
1365 if (row != NULL)
1366 {
1367 lRet = atol(row[0]);
1368 do { row = mysql_fetch_row(pres); } while (row != NULL);
1369 }
1370 }
1371
1372 return lRet;
1373}
1374
1375
1376/**
1377 * Converts days after Christ (year 0) to ISO date.
1378 * @returns Success indicator. TRUE/FALSE;
1379 * @param lDays Days after Christ (year 0).
1380 * @param pszDate ISO Date. Result.
1381 */
1382BOOL _System dbDaysAfterChristToDate(signed long lDays, char *pszDate)
1383{
1384 BOOL fRet = FALSE;
1385 char szQuery[128];
1386
1387 if (lDays < 0)
1388 return FALSE;
1389
1390 sprintf(&szQuery[0], "SELECT from_days(%ld)", lDays);
1391 if (mysql_query(pmysql, &szQuery[0]) >= 0)
1392 {
1393 MYSQL_ROW row;
1394 MYSQL_RES *pres = mysql_use_result(pmysql);
1395 row = mysql_fetch_row(pres);
1396 if (row != NULL)
1397 {
1398 fRet = strlen(row[0]) == (4+1+2+1+2) && row[0][4] == '-' && row[0][7] == '-'
1399 && strcmp(row[0], "0000-00-00") != 0;
1400 if (fRet)
1401 strcpy(pszDate, row[0]);
1402 do { row = mysql_fetch_row(pres); } while (row != NULL);
1403 }
1404 }
1405
1406 return fRet;
1407}
1408
1409
1410/**
1411 * Display all functions for, the given dll, that is not updated.
1412 * @returns TRUE / FALSE.
1413 * @param lDll Dll reference number.
1414 * @param dbFetchCall Callback function which will be called once for each
1415 * field for all the functions not updated.
1416 * pvUser is NULL, pszValue field value, pszFieldName the field name.
1417 */
1418BOOL _System dbGetNotUpdatedFunction(signed long lDll, DBCALLBACKFETCH dbFetchCallBack)
1419{
1420 BOOL fRet = FALSE;
1421 void *pres;
1422 char szQuery[128];
1423
1424 /* not updated names */
1425 sprintf(&szQuery[0], "SELECT name, intname FROM function WHERE dll = %ld AND updated = 0",
1426 lDll);
1427 pres = dbExecuteQuery(szQuery);
1428 if (pres != NULL)
1429 {
1430 BOOL f;
1431 do
1432 {
1433 f = dbFetch(pres, dbFetchCallBack, NULL);
1434 } while (f);
1435 dbFreeResult(pres);
1436 fRet = TRUE;
1437 }
1438
1439 /* warn about updated > 1 too */
1440 sprintf(&szQuery[0], "SELECT updated, name, intname FROM function WHERE dll = %ld AND updated > 1",
1441 lDll);
1442 pres = dbExecuteQuery(szQuery);
1443 if (pres != NULL)
1444 {
1445 BOOL f;
1446 do
1447 {
1448 f = dbFetch(pres, dbFetchCallBack, NULL);
1449 } while (f);
1450 dbFreeResult(pres);
1451 fRet = TRUE;
1452 }
1453
1454 strcpy(&szQuery[0], "UPDATE function SET updated = 0");
1455 mysql_query(pmysql, &szQuery[0]);
1456
1457 return fRet;
1458}
1459
1460
1461#ifndef DLL
1462/**
1463 * Signal handler.
1464 * Ensures that the database connection is closed at termination.
1465 * @param sig Signal number.
1466 */
1467void dbHandler(int sig)
1468{
1469 if (pmysql != NULL)
1470 {
1471 fprintf(stderr, "\n\t!disconnecting from database!\n");
1472 dbDisconnect();
1473 }
1474
1475 flushall();
1476 switch (sig)
1477 {
1478 case SIGBREAK:
1479 printf("\nSIGBREAK\n");
1480 exit(-1);
1481 break;
1482 case SIGINT:
1483 printf("\nSIGINT\n");
1484 exit(-1);
1485 break;
1486 case SIGTERM:
1487 printf("\nSIGTERM\n");
1488 exit(-1);
1489 break;
1490 case SIGSEGV:
1491 raise(sig);
1492 break;
1493 case SIGILL:
1494 printf("\nSIGILL\n");
1495 exit(-1);
1496 break;
1497 }
1498}
1499
1500
1501#else
1502/*******/
1503/* DLL */
1504/*******/
1505/* prototypes used in the _DLL_InitTerm function */
1506extern "C"
1507{
1508 int _CRT_init(void);
1509 void _CRT_term(void);
1510 void __ctordtorInit( void );
1511 void __ctordtorTerm( void );
1512 unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long ulFlag);
1513}
1514
1515
1516/**
1517 * Dll InitTerm function.
1518 * @returns 0 on success.
1519 * 1 on error.
1520 * @param hModule
1521 * @param ulFlags
1522 * @remark We'll ensure that the database connection is terminated as we terminate.
1523 */
1524unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long ulFlag)
1525{
1526 /*-------------------------------------------------------------------------*/
1527 /* If ulFlag is zero then the DLL is being loaded so initialization should */
1528 /* be performed. If ulFlag is 1 then the DLL is being freed so */
1529 /* termination should be performed. */
1530 /*-------------------------------------------------------------------------*/
1531
1532 switch (ulFlag)
1533 {
1534 case 0:
1535 if (_CRT_init() == -1)
1536 return 0;
1537 __ctordtorInit();
1538 break;
1539
1540 case 1:
1541 /* ensure that db connection is terminated */
1542 if (pmysql != NULL)
1543 {
1544 fprintf(stderr, "\n\t!disconnecting from database!\n");
1545 dbDisconnect();
1546 }
1547 __ctordtorTerm();
1548 break;
1549
1550 default:
1551 return 0;
1552 }
1553 hModule = hModule;
1554 return 1;
1555}
1556
1557/*****************************************************************/
1558/* -why is this terminate function referenced but not defined??? */
1559/* and where is it referenced??? */
1560/* -Probably an export missing from the libraries. */
1561/*****************************************************************/
1562void terminate(void)
1563{
1564 DosPutMessage(0, sizeof("terminate")-1, "terminate");
1565 exit(-1);
1566}
1567
1568/****************************************/
1569/* EMX run-time trouble */
1570/* _environ is missing when using -Zomf */
1571/****************************************/
1572char **_environ = environ;
1573
1574#endif
Note: See TracBrowser for help on using the repository browser.