Changeset 3838 for trunk/tools/database/db.cpp
- Timestamp:
- Jul 18, 2000, 9:16:01 AM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/database/db.cpp
r2818 r3838 1 /* $Id: db.cpp,v 1.1 1 2000-02-18 12:42:06bird Exp $ *1 /* $Id: db.cpp,v 1.12 2000-07-18 07:15:59 bird Exp $ * 2 2 * 3 3 * DB - contains all database routines. … … 351 351 352 352 353 354 /** 355 * Inserts or updates (existing) file information. 356 * @returns Success indicator (TRUE / FALSE). 357 * @param usDll Dll reference code. 358 * @param pszFilename Filename. 359 * @param pszDescription Pointer to file description. 360 * @param pszLastDateTime Date and time for last change (ISO). 361 * @param lLastAuthor Author number. (-1 if not found.) 362 * @param pszRevision Pointer to revision string. 363 * @sketch 364 * @remark 365 */ 366 BOOL _System dbInsertUpdateFile(unsigned short usDll, 367 const char *pszFilename, 368 const char *pszDescription, 369 const char *pszLastDateTime, 370 signed long lLastAuthor, 371 const char *pszRevision) 372 { 373 int rc; 374 long lFile = -1; 375 char szQuery[0x10000]; 376 MYSQL_RES *pres; 377 378 /* parameter assertions */ 379 assert(usDll != 0); 380 assert(pszFilename != NULL); 381 assert(*pszFilename != '\0'); 382 383 /* try find file */ 384 sprintf(&szQuery[0], "SELECT refcode, name FROM file WHERE dll = %d AND name = '%s'", usDll, pszFilename); 385 rc = mysql_query(pmysql, &szQuery[0]); 386 pres = mysql_store_result(pmysql); 387 if (rc >= 0 && pres != NULL && mysql_num_rows(pres) != 0) 388 { /* update file (file is found) */ 389 MYSQL_ROW parow; 390 if (mysql_num_rows(pres) > 1) 391 { 392 fprintf(stderr, "internal database integrity error(%s): More files by the same name in the same dll. " 393 "usDll = %d, pszFilename = %s\n", __FUNCTION__, usDll, pszFilename); 394 return FALSE; 395 } 396 397 parow = mysql_fetch_row(pres); 398 if (parow != NULL) 399 lFile = getvalue(0, parow); 400 mysql_free_result(pres); 401 402 if (strcmp(parow[1], pszFilename) != 0) /* case might have changed... */ 403 { 404 sprintf(&szQuery[0], "UPDATE file SET name = '%s' WHERE refcode = %ld", 405 pszFilename, lFile); 406 rc = mysql_query(pmysql, &szQuery[0]); 407 } 408 409 if (rc >= 0) 410 { 411 if (pszDescription != NULL && pszDescription != '\0') 412 { 413 szQuery[0] = '\0'; 414 sqlstrcat(&szQuery[0], "UPDATE file SET description = ", pszDescription, NULL); 415 sprintf(&szQuery[strlen(szQuery)], " WHERE refcode = %ld", lFile); 416 } 417 else 418 sprintf(&szQuery[0], "UPDATE file SET description = NULL WHERE refcode = %ld", 419 lFile); 420 rc = mysql_query(pmysql, &szQuery[0]); 421 } 422 423 if (rc >= 0 && pszLastDateTime != NULL && *pszLastDateTime != '\0') 424 { 425 sprintf(&szQuery[0], "UPDATE file SET lastdatetime = '%s' WHERE refcode = %ld", 426 pszLastDateTime, lFile); 427 rc = mysql_query(pmysql, &szQuery[0]); 428 } 429 430 if (rc >= 0) 431 { 432 sprintf(&szQuery[0], "UPDATE file SET lastauthor = %ld WHERE refcode = %ld", 433 lLastAuthor, lFile); 434 rc = mysql_query(pmysql, &szQuery[0]); 435 } 436 437 if (rc >= 0 && pszRevision != NULL && *pszRevision != '\0') 438 { 439 sprintf(&szQuery[0], "UPDATE file SET revision = '%s' WHERE refcode = %ld", 440 pszRevision, lFile); 441 rc = mysql_query(pmysql, &szQuery[0]); 442 } 443 444 } 445 else 446 { /* insert */ 447 sprintf(&szQuery[0], "INSERT INTO file(dll, name, description, lastauthor, lastdatetime, revision) VALUES(%d, '%s', %ld, ", 448 usDll, pszFilename, lLastAuthor); 449 if (pszDescription != NULL && *pszDescription != '\0') 450 sqlstrcat(&szQuery[0], NULL, pszDescription); 451 else 452 strcat(&szQuery[0], "NULL"); 453 454 if (pszLastDateTime != NULL && *pszLastDateTime != '\0') 455 sqlstrcat(&szQuery[0], ", ", pszLastDateTime); 456 else 457 strcat(&szQuery[0], ", '1975-03-13 14:00:00'"); /* dummy */ 458 459 if (pszRevision != NULL && *pszRevision != '\0') 460 sqlstrcat(&szQuery[0], ", ", pszRevision, ")"); 461 else 462 strcat(&szQuery[0], ", '')"); 463 464 rc = mysql_query(pmysql, &szQuery[0]); 465 } 466 467 return rc >= 0; 468 } 469 470 353 471 /** 354 472 * Get a long value. … … 399 517 * Find occurences of a function, given by internal name. 400 518 * @returns success indicator, TRUE / FALSE. 401 * @param pszFunctionName 402 * @param pFnFindBuf 403 * @param lDll 519 * @param pszFunctionName Pointer to a function name string. (input) 520 * @param pFnFindBuf Pointer to a find buffer. (output) 521 * @param lDll Dll refcode (optional). If given the search is limited to 522 * the given dll and aliasing functions is updated (slow!). 404 523 * @sketch 1) Get functions for this dll(if given). 405 524 * 2) Get functions which aliases the functions found in (1). … … 420 539 */ 421 540 if (lDll < 0) 422 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function WHERE intname = '%s'",541 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function WHERE intname = '%s'", 423 542 pszFunctionName); 424 543 else 425 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function "544 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function " 426 545 "WHERE intname = '%s' AND dll = %ld", 427 546 pszFunctionName, lDll); … … 441 560 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]); 442 561 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]); 443 strcpy(szFnName[pFnFindBuf->cFns], row[3]); 562 pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]); 563 strcpy(szFnName[pFnFindBuf->cFns], row[4]); 444 564 445 565 /* next */ … … 457 577 */ 458 578 cFnsThisDll = (int)pFnFindBuf->cFns; 459 strcpy(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function WHERE aliasfn IN (");579 strcpy(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function WHERE aliasfn IN ("); 460 580 for (i = 0; i < cFnsThisDll; i++) 461 581 { … … 476 596 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]); 477 597 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]); 478 strcpy(szFnName[pFnFindBuf->cFns], row[3]); 598 pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]); 599 strcpy(szFnName[pFnFindBuf->cFns], row[4]); 479 600 480 601 /* next */ … … 487 608 */ 488 609 cFnsAliasesAndThisDll = (int)pFnFindBuf->cFns; 489 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function "610 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file FROM function " 490 611 "WHERE aliasfn = (-1) AND dll <> %ld AND (intname = '%s'", 491 612 lDll, pszFunctionName); … … 508 629 else 509 630 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL; 631 pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]); 510 632 511 633 /* next */ … … 518 640 * 4) Get new aliases by name 519 641 */ 520 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function "642 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file FROM function " 521 643 "WHERE aliasfn = (-1) AND dll <> %ld AND (name = '%s'", 522 644 lDll, pszFunctionName); … … 539 661 else 540 662 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL; 663 pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]); 541 664 542 665 /* next */ … … 565 688 * 6) Update all functions from (3) and (4) to alias the first function from 1. 566 689 */ 567 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) "690 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld), file = (%ld) " 568 691 "WHERE aliasfn = (-1) AND refcode IN (", 569 pFnFindBuf->alRefCode[0] );692 pFnFindBuf->alRefCode[0], pFnFindBuf->alFileRefCode[0]); 570 693 for (i = cFnsAliasesAndThisDll; i < pFnFindBuf->cFns; i++) 571 694 { … … 589 712 590 713 return rc >= 0; 714 } 715 716 717 /** 718 * Finds the refcode for a file (if it exists). 719 * @returns File 'refcode'. 720 * -1 on error or not found. 721 * @param lDll Refcode of the dll which this file belongs to. 722 * @param pszFilename The filename to search for. 723 */ 724 signed long _System dbFindFile(signed long lDll, const char *pszFilename) 725 { 726 char szQuery[256]; 727 MYSQL_RES * pres; 728 signed long lRefCode = -1; 729 730 assert(lDll >= 0); 731 assert(pszFilename != NULL); 732 assert(*pszFilename != '\0'); 733 734 sprintf(&szQuery[0], "SELECT refcode FROM file WHERE dll = %ld AND name = '%s'", 735 lDll, pszFilename); 736 if (mysql_query(pmysql, &szQuery[0]) >= 0) 737 { 738 pres = mysql_store_result(pmysql); 739 if (pres != NULL) 740 { 741 MYSQL_ROW parow = mysql_fetch_row(pres); 742 if (parow != NULL) 743 lRefCode = getvalue(0, parow); 744 mysql_free_result(pres); 745 } 746 } 747 748 return lRefCode; 591 749 } 592 750 … … 995 1153 996 1154 /** 1155 * Removes all the existing design notes in the specified file. 1156 * @returns Success indicator. 1157 * @param lFile File refcode of the file to remove all design notes for. 1158 * @sketch 1159 * @status 1160 * @author knut st. osmundsen (knut.stange.osmundsen@pmsc.no) 1161 * @remark 1162 */ 1163 BOOL _System dbRemoveDesignNotes(signed long lFile) 1164 { 1165 char szQuery[80]; 1166 1167 assert(lFile >= 0); 1168 sprintf(&szQuery[0], "DELETE FROM designnote WHERE file = %ld", lFile); 1169 return mysql_query(pmysql, &szQuery[0]) >= 0; 1170 } 1171 1172 1173 /** 1174 * Adds a design note. 1175 * @returns Success indicator. 1176 * @param lDll Dll refcode. 1177 * @param lFile File refcode. 1178 * @param pszTitle Design note title. 1179 * @param pszText Design note text. 1180 * @param lSeqNbr Sequence number (in dll). If 0 the use next available number. 1181 * @param lSeqNbrFile Sequence number in file. 1182 */ 1183 BOOL _System dbAddDesignNote(signed long lDll, 1184 signed long lFile, 1185 const char *pszTitle, 1186 const char *pszText, 1187 signed long lSeqNbr, 1188 signed long lSeqNbrFile) 1189 { 1190 char szQuery[0x10200]; 1191 MYSQL_RES * pres; 1192 1193 1194 assert(lDll >= 0 && lFile >= 0); 1195 assert(lSeqNbrFile >= 0); 1196 1197 /* 1198 * If no lSqlNbr the make one. 1199 */ 1200 if (lSeqNbr == 0) 1201 { 1202 sprintf(&szQuery[0], "SELECT MAX(lSeqNbr) + 1 FROM designnote WHERE dll = %ld'", lDll); 1203 if (mysql_query(pmysql, &szQuery[0]) >= 0) 1204 { 1205 pres = mysql_store_result(pmysql); 1206 if (pres != NULL) 1207 { 1208 MYSQL_ROW parow = mysql_fetch_row(pres); 1209 if (parow != NULL) 1210 lSeqNbr = getvalue(0, parow); 1211 else 1212 lSeqNbr = 1; 1213 mysql_free_result(pres); 1214 } 1215 else 1216 return FALSE; 1217 } 1218 else 1219 return FALSE; 1220 } 1221 1222 /* 1223 * Create update query. 1224 */ 1225 sprintf(&szQuery[0], "INSERT INTO designnote(dll, file, seqnbrfile, seqnbr, title, note) " 1226 "VALUES (%ld, %ld, %ld, %ld, ", 1227 lDll, lFile, lSeqNbrFile, lSeqNbr); 1228 if (pszTitle != NULL && *pszTitle != '\0') 1229 sqlstrcat(&szQuery[0], NULL, pszTitle); 1230 else 1231 strcat(&szQuery[0], "NULL"); 1232 sqlstrcat(&szQuery[0], ", ", pszText == NULL ? "" : pszText, ")"); 1233 1234 return mysql_query(pmysql, &szQuery[0]) >= 0; 1235 } 1236 1237 1238 1239 /** 997 1240 * Updates the history tables. 998 1241 * @returns Number of signals/errors. … … 1109 1352 1110 1353 /* foreign keys in function table */ 1111 strcpy(pszQuery, "SELECT refcode, dll, state, apigroup FROM function");1354 strcpy(pszQuery, "SELECT refcode, dll, state, apigroup, file FROM function"); 1112 1355 rc = mysql_query(pmysql, pszQuery); 1113 1356 if (rc >= 0) … … 1119 1362 { 1120 1363 /* check dll */ 1121 sprintf(pszQuery, "SELECT name FROM dll WHERE refcode = %s", row1[1]);1364 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]); 1122 1365 rc = mysql_query(pmysql, pszQuery); 1123 1366 CheckFKError("function/dll", "Foreign key 'dll' not found in the dll table"); 1124 1367 1125 1368 /* check state */ 1126 sprintf(pszQuery, "SELECT name FROM state WHERE refcode = %s", row1[2]);1369 sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]); 1127 1370 rc = mysql_query(pmysql, pszQuery); 1128 1371 CheckFKError("function/state", "Foreign key 'state' not found in the state table"); … … 1131 1374 if (row1[3] != NULL) 1132 1375 { 1133 sprintf(pszQuery, "SELECT name FROM apigroup WHERE refcode = %s", row1[3]);1376 sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[3]); 1134 1377 rc = mysql_query(pmysql, pszQuery); 1135 1378 CheckFKError("function/state", "Foreign key 'state' not found in the state table"); 1136 1379 } 1380 1381 /* check file */ 1382 if (atoi(row1[4]) >= 0) 1383 { 1384 sprintf(pszQuery, "SELECT refcode FROM file WHERE refcode = %s", row1[4]); 1385 rc = mysql_query(pmysql, pszQuery); 1386 CheckFKError("function/file", "Foreign key 'file' not found in the file table"); 1387 } 1137 1388 } 1138 1389 mysql_free_result(pres1); … … 1142 1393 ulRc += logDbError(pszError, pszQuery); 1143 1394 1144 /* foreign keys in apigroup*/1145 strcpy(pszQuery, "SELECT refcode, dll FROM apigroup");1395 /* foreign keys in file */ 1396 strcpy(pszQuery, "SELECT refcode, dll FROM file"); 1146 1397 rc = mysql_query(pmysql, pszQuery); 1147 1398 if (rc >= 0) … … 1156 1407 rc = mysql_query(pmysql, pszQuery); 1157 1408 CheckFKError("apigroup/dll", "Foreign key 'dll' not found in the dll table"); 1409 } 1410 mysql_free_result(pres1); 1411 } 1412 } 1413 else 1414 ulRc += logDbError(pszError, pszQuery); 1415 1416 /* foreign keys in apigroup */ 1417 strcpy(pszQuery, "SELECT refcode, dll FROM apigroup"); 1418 rc = mysql_query(pmysql, pszQuery); 1419 if (rc >= 0) 1420 { 1421 pres1 = mysql_store_result(pmysql); 1422 if (pres1 != NULL) 1423 { 1424 while ((row1 = mysql_fetch_row(pres1)) != NULL) 1425 { 1426 /* check dll */ 1427 sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]); 1428 rc = mysql_query(pmysql, pszQuery); 1429 CheckFKError("file/dll", "Foreign key 'dll' not found in the dll table"); 1158 1430 } 1159 1431 mysql_free_result(pres1); … … 1646 1918 1647 1919 /** 1648 * 1920 * Appends a set of strings to a query. The main string (pszStr) is enclosed in "'"s. 1649 1921 * @returns Pointer to end of the string. 1650 1922 * @param pszQuery Outputbuffer … … 1677 1949 { 1678 1950 if (ch == '\'') 1951 { 1679 1952 *pszQuery++ = '\\'; 1680 *pszQuery++ = ch; 1953 *pszQuery++ = '\''; 1954 } 1955 else if (ch == '\n') 1956 { 1957 #if 0 1958 *pszQuery++ = '\\'; 1959 *pszQuery++ = 'n'; 1960 #else 1961 *pszQuery++ = '<'; 1962 *pszQuery++ = 'B'; 1963 *pszQuery++ = 'R'; 1964 *pszQuery++ = '>'; 1965 #endif 1966 } 1967 else if (ch == '\r') 1968 { 1969 #if 0 1970 *pszQuery++ = '\\'; 1971 *pszQuery++ = 'r'; 1972 #else 1973 *pszQuery++ = '<'; 1974 *pszQuery++ = 'B'; 1975 *pszQuery++ = 'R'; 1976 *pszQuery++ = '>'; 1977 #endif 1978 } 1979 else 1980 *pszQuery++ = ch; 1681 1981 } 1682 1982 *pszQuery++ = '\'';
Note:
See TracChangeset
for help on using the changeset viewer.