Changeset 2773 for trunk/tools/database/db.cpp
- Timestamp:
- Feb 14, 2000, 2:49:14 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/database/db.cpp
r2770 r2773 1 /* $Id: db.cpp,v 1. 8 2000-02-12 23:54:29bird Exp $ *1 /* $Id: db.cpp,v 1.9 2000-02-14 13:49:13 bird Exp $ * 2 2 * 3 3 * DB - contains all database routines. … … 364 364 365 365 366 #if 1366 #if 0 367 367 /* 368 368 * Stubs used while optimizing sqls. … … 378 378 int mysql_query5(MYSQL *mysql, const char *q) 379 379 { return mysql_query(mysql, q); } 380 MYSQL_RES * mysql_store_result1(MYSQL *mysql) 381 { return mysql_store_result(mysql); } 382 MYSQL_RES * mysql_store_result5(MYSQL *mysql) 383 { return mysql_store_result(mysql); } 380 int mysql_query6(MYSQL *mysql, const char *q) 381 { return mysql_query(mysql, q); } 384 382 385 383 #else … … 390 388 #define mysql_query4 mysql_query 391 389 #define mysql_query5 mysql_query 392 #define mysql_store_result1 mysql_store_result 393 #define mysql_store_result5 mysql_store_result 390 #define mysql_query6 mysql_query 394 391 395 392 #endif … … 403 400 * @param pFnFindBuf 404 401 * @param lDll 402 * @sketch 1) Get functions for this dll(if given). 403 * 2) Get functions which aliases the functions found in (1). 404 * 3) Get new aliases by intname 405 * 4) Get new aliases by name 406 * 5) Update all functions from (1) to have aliasfn -2 (DONTMIND) 407 * 6) Update all functions from (3) and (4) to alias the first function from 1. 405 408 */ 406 409 BOOL _System dbFindFunction(const char *pszFunctionName, PFNFINDBUF pFnFindBuf, signed long lDll) … … 411 414 char szQuery[256]; 412 415 416 /* 417 * 1) Get functions for this dll(if given). 418 */ 413 419 if (lDll < 0) 414 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function WHERE intname = '%s'",420 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function WHERE intname = '%s'", 415 421 pszFunctionName); 416 422 else 417 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function "423 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function " 418 424 "WHERE intname = '%s' AND dll = %ld", 419 425 pszFunctionName, lDll); … … 422 428 if (rc >= 0) 423 429 { 424 pres = mysql_store_result 1(pmysql);430 pres = mysql_store_result(pmysql); 425 431 if (pres != NULL) 426 432 { 433 char szFnName[NBR_FUNCTIONS][80]; 434 427 435 pFnFindBuf->cFns = 0; 428 436 while ((row = mysql_fetch_row(pres)) != NULL) … … 431 439 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]); 432 440 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]); 441 strcpy(szFnName[pFnFindBuf->cFns], row[3]); 433 442 434 443 /* next */ … … 440 449 if (lDll >= 0 && pFnFindBuf->cFns != 0) 441 450 { 442 #if 0 443 int i; 444 /* Make the selected function to DONT TOUCH */ 445 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (-2) " 446 "WHERE (", 447 lDll, pszFunctionName); 448 for (i = 0; i < pFnFindBuf->cFns; i++) 451 int cFnsThisDll, cFnsAliasesAndThisDll, i; 452 453 /* 454 * 2) Get functions which aliases the functions found in (1). 455 */ 456 cFnsThisDll = (int)pFnFindBuf->cFns; 457 strcpy(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function WHERE aliasfn IN ("); 458 for (i = 0; i < cFnsThisDll; i++) 449 459 { 450 if (i != 0) strcat(&szQuery[0], " OR");451 sprintf(&szQuery[strlen(szQuery)], " refcode = %ld", pFnFindBuf->alRefCode[i]);460 if (i > 0) strcat(&szQuery[0], " OR "); 461 sprintf(&szQuery[strlen(szQuery)], "(%ld)", pFnFindBuf->alRefCode[i]); 452 462 } 453 strcat(&szQuery[0], ") AND aliasfn <> (-2)"); 454 455 rc = mysql_query2(pmysql, &szQuery[0]); 456 if (rc >= 0) 457 { 458 /* Update all with equal internal... which is not in this Dll */ 459 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) " 460 "WHERE aliasfn = (-1) AND dll <> %ld AND intname = '%s'", 461 pFnFindBuf->alRefCode[0], lDll, pszFunctionName, pszFunctionName); 462 rc = mysql_query3(pmysql, &szQuery[0]); 463 if (rc >= 0) 464 { 465 /* Update all with equal external name... which is not in this Dll */ 466 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) " 467 "WHERE aliasfn = (-1) AND dll <> %ld AND name = '%s'", 468 pFnFindBuf->alRefCode[0], lDll, pszFunctionName, pszFunctionName); 469 470 rc = mysql_query4(pmysql, &szQuery[0]); 471 if (rc >= 0) 472 { 473 /* get the functions aliases to the functions we have allready found. */ 474 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function WHERE dll = %ld AND aliasfn IN (", lDll); 475 for (i = 0; i < pFnFindBuf->cFns; i++) 476 { 477 if (i != 0) strcat(&szQuery[0], ", "); 478 sprintf(&szQuery[strlen(szQuery)], "%ld", pFnFindBuf->alRefCode[i]); 479 } 480 strcat(&szQuery[strlen(szQuery)], ")"); 481 482 DosSleep(0); 483 rc = mysql_query5(pmysql, &szQuery[0]); 484 if (rc >= 0) 485 { 486 pres = mysql_store_result5(pmysql); 487 if (pres != NULL) 488 { 489 while ((row = mysql_fetch_row(pres)) != NULL) 490 { 491 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]); 492 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]); 493 if (row[2] != NULL) 494 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]); 495 else 496 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL; 497 498 /* next */ 499 pFnFindBuf->cFns++; 500 } 501 mysql_free_result(pres); 502 } 503 } 504 } 505 } 506 } 507 #else 508 int i; 509 int cFnsThisDll = (int)pFnFindBuf->cFns; 510 511 /* get the functions aliases to the functions we have allready found. */ 512 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function " 513 "WHERE aliasfn = (-1) AND dll <> %ld AND intname = '%s'", 514 lDll, pszFunctionName); 463 strcat(&szQuery[0], ")"); 464 515 465 rc = mysql_query2(pmysql, &szQuery[0]); 516 466 if (rc >= 0) … … 523 473 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]); 524 474 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]); 525 if (row[2] != NULL) 526 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]); 527 else 528 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL; 475 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]); 476 strcpy(szFnName[pFnFindBuf->cFns], row[3]); 529 477 530 478 /* next */ … … 533 481 mysql_free_result(pres); 534 482 535 536 /* get the functions aliases to the functions we have allready found. */ 483 /* 484 * 3) Get new aliases by intname 485 */ 486 cFnsAliasesAndThisDll = (int)pFnFindBuf->cFns; 537 487 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function " 538 "WHERE aliasfn = (-1) AND dll <> %ld AND name = '%s'",488 "WHERE aliasfn = (-1) AND dll <> %ld AND (intname = '%s'", 539 489 lDll, pszFunctionName); 490 for (i = 0; i < cFnsAliasesAndThisDll; i++) 491 sprintf(&szQuery[strlen(&szQuery[0])], " OR intname = '%s'", szFnName[i]); 492 strcat(&szQuery[0], ")"); 493 540 494 rc = mysql_query3(pmysql, &szQuery[0]); 541 495 if (rc >= 0) … … 558 512 mysql_free_result(pres); 559 513 560 /* do updates! */ 561 /* Make the selected function to DONT TOUCH */ 562 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (-2) " 563 "WHERE (", 514 515 /* 516 * 4) Get new aliases by name 517 */ 518 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function " 519 "WHERE aliasfn = (-1) AND dll <> %ld AND (name = '%s'", 564 520 lDll, pszFunctionName); 565 for (i = 0; i < cFnsThisDll; i++) 566 { 567 if (i != 0) strcat(&szQuery[0], " OR"); 568 sprintf(&szQuery[strlen(szQuery)], " refcode = %ld", pFnFindBuf->alRefCode[i]); 569 } 570 strcat(&szQuery[0], ") AND aliasfn <> (-2)"); 521 for (i = 0; i < cFnsAliasesAndThisDll; i++) 522 sprintf(&szQuery[strlen(&szQuery[0])], " OR name = '%s'", szFnName[i]); 523 strcat(&szQuery[0], ")"); 571 524 572 525 rc = mysql_query4(pmysql, &szQuery[0]); 573 526 if (rc >= 0) 574 527 { 575 /* Update all with equal internal... which is not in this Dll */ 576 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) " 577 "WHERE aliasfn = (-1) AND dll <> %ld AND intname = '%s'", 578 pFnFindBuf->alRefCode[0], lDll, pszFunctionName, pszFunctionName); 579 rc = mysql_query5(pmysql, &szQuery[0]); 580 if (rc >= 0) 528 pres = mysql_store_result(pmysql); 529 if (pres != NULL) 581 530 { 582 /* Update all with equal external name... which is not in this Dll */ 583 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) " 584 "WHERE aliasfn = (-1) AND dll <> %ld AND name = '%s'", 585 pFnFindBuf->alRefCode[0], lDll, pszFunctionName, pszFunctionName); 586 587 rc = mysql_query4(pmysql, &szQuery[0]); 531 while ((row = mysql_fetch_row(pres)) != NULL) 532 { 533 pFnFindBuf->alRefCode[pFnFindBuf->cFns] = atol(row[0]); 534 pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]); 535 if (row[2] != NULL) 536 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]); 537 else 538 pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL; 539 540 /* next */ 541 pFnFindBuf->cFns++; 542 } 543 mysql_free_result(pres); 544 545 /* 546 * 5) Update all functions from (1) to have aliasfn -2 (DONTMIND) 547 */ 548 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (-2) " 549 "WHERE (", 550 lDll, pszFunctionName); 551 for (i = 0; i < cFnsThisDll; i++) 552 { 553 if (i != 0) strcat(&szQuery[0], " OR"); 554 sprintf(&szQuery[strlen(szQuery)], " refcode = %ld", pFnFindBuf->alRefCode[i]); 555 } 556 strcat(&szQuery[0], ") AND aliasfn <> (-2)"); 557 558 rc = mysql_query5(pmysql, &szQuery[0]); 559 if (rc >= 0 && cFnsAliasesAndThisDll < pFnFindBuf->cFns) 560 { 561 /* 562 * 6) Update all functions from (3) and (4) to alias the first function from 1. 563 */ 564 sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) " 565 "WHERE aliasfn = (-1) AND refcode IN (", 566 pFnFindBuf->alRefCode[0]); 567 for (i = cFnsAliasesAndThisDll; i < pFnFindBuf->cFns; i++) 568 sprintf(&szQuery[strlen(&szQuery[0])], 569 i > 0 ? ", %ld" : "%ld", pFnFindBuf->alRefCode[i]); 570 strcat(&szQuery[0], ")"); 571 rc = mysql_query6(pmysql, &szQuery[0]); 572 } /* query 5 */ 588 573 } 589 } 574 } /* query 4 */ 590 575 } 591 } 576 } /* query 3 */ 592 577 } 593 } 594 #endif 578 } /* query 2 */ 595 579 } 596 } 580 } /* query 1 */ 597 581 else 598 582 rc = -1; … … 672 656 } 673 657 658 #if 1 659 /* 660 * Stubs used while optimizing sqls. 661 */ 662 int mysql_queryu1(MYSQL *mysql, const char *q) 663 { return mysql_query(mysql, q); } 664 int mysql_queryu2(MYSQL *mysql, const char *q) 665 { return mysql_query(mysql, q); } 666 int mysql_queryu3(MYSQL *mysql, const char *q) 667 { return mysql_query(mysql, q); } 668 int mysql_queryu4(MYSQL *mysql, const char *q) 669 { return mysql_query(mysql, q); } 670 int mysql_queryu5(MYSQL *mysql, const char *q) 671 { return mysql_query(mysql, q); } 672 int mysql_queryu6(MYSQL *mysql, const char *q) 673 { return mysql_query(mysql, q); } 674 int mysql_queryu7(MYSQL *mysql, const char *q) 675 { return mysql_query(mysql, q); } 676 int mysql_queryu8(MYSQL *mysql, const char *q) 677 { return mysql_query(mysql, q); } 678 #else 679 #define mysql_queryu1 mysql_query 680 #define mysql_queryu2 mysql_query 681 #define mysql_queryu3 mysql_query 682 #define mysql_queryu4 mysql_query 683 #define mysql_queryu5 mysql_query 684 #define mysql_queryu6 mysql_query 685 #define mysql_queryu7 mysql_query 686 #define mysql_queryu8 mysql_query 687 #endif 674 688 675 689 /** … … 683 697 unsigned long _System dbUpdateFunction(PFNDESC pFnDesc, signed long lDll, char *pszError) 684 698 { 685 char szQuery[256]; 686 char *pszQuery = &szQuery[0]; 687 long lCurrentState; 688 int i,k,rc; 689 unsigned long ulRc = 0; 699 MYSQL_RES * pres; 700 MYSQL_ROW row; 701 char szQuery[512]; 702 char * pszQuery = &szQuery[0]; 703 long lCurrentState; 704 int i,k,rc; 705 unsigned long ulRc = 0; 690 706 691 707 for (k = 0; k < pFnDesc->cRefCodes; k++) … … 693 709 BOOL f = FALSE; 694 710 695 /* set updated flag */ 711 /* 712 * Set updated flag 713 */ 696 714 sprintf(pszQuery, "UPDATE function SET updated = updated + 1 WHERE refcode = %ld", 697 715 pFnDesc->alRefCode[k]); 698 rc = mysql_query(pmysql, &szQuery[0]); 699 700 /* get current status */ 716 rc = mysql_queryu1(pmysql, &szQuery[0]); 717 718 719 /* 720 * Get current status 721 */ 701 722 lCurrentState = dbGetFunctionState(pFnDesc->alRefCode[k]); 702 723 if (lCurrentState == -1) … … 706 727 } 707 728 708 /* update function table first */ 729 730 /* 731 * Update function table first 732 */ 709 733 strcpy(pszQuery, "UPDATE function SET "); 710 734 pszQuery += strlen(pszQuery); … … 727 751 { 728 752 sprintf(pszQuery + strlen(pszQuery), "WHERE refcode = %ld", pFnDesc->alRefCode[k]); 729 rc = mysql_query (pmysql, &szQuery[0]);753 rc = mysql_queryu2(pmysql, &szQuery[0]); 730 754 if (rc < 0) 731 755 { … … 737 761 } 738 762 739 /* parameters */ 763 764 /* 765 * Parameters 766 */ 740 767 pszQuery = &szQuery[0]; 741 sprintf(pszQuery, "DELETE FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]); 742 rc = mysql_query(pmysql, &szQuery[0]); 743 if (rc < 0) 768 sprintf(pszQuery, "SELECT count(*) FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]); 769 rc = mysql_queryu3(pmysql, pszQuery); 770 if (rc >= 0) 771 { 772 pres = mysql_store_result(pmysql); 773 if (pres != NULL) 774 row = mysql_fetch_row(pres); 775 if (pres != NULL && row != NULL && mysql_num_rows(pres) == 1) 776 { 777 if (atol(row[0]) == pFnDesc->cParams) 778 { /* update parameters */ 779 for (i = 0; i < pFnDesc->cParams; i++) 780 { 781 sprintf(pszQuery, "UPDATE parameter SET type = '%s', name = '%s' " 782 "WHERE function = (%ld) AND sequencenbr == (%ld)", 783 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "", 784 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : "", 785 pFnDesc->alRefCode[k], i 786 ); 787 rc = mysql_queryu4(pmysql, pszQuery); 788 if (rc < 0) 789 { 790 if (*pszError == ' ') 791 strcpy(pszError++, "\n\t"); 792 sprintf(pszError, "Updateing parameter %i failed with error: %s - (sql=%s) ", 793 i, dbGetLastErrorDesc(), pszQuery); 794 pszError += strlen(pszError) - 1; 795 ulRc++; 796 } 797 } 798 } 799 else 800 { 801 if (atol(row[0]) != 0) 802 { /* delete old parameters */ 803 sprintf(pszQuery, "DELETE FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]); 804 rc = mysql_queryu5(pmysql, pszQuery); 805 if (rc < 0) 806 { 807 if (*pszError == ' ') 808 strcpy(pszError++, "\n\t"); 809 sprintf(pszError, "Deleting old parameters failed with error: %s - (sql=%s) ", 810 dbGetLastErrorDesc(), pszQuery); 811 pszError += strlen(pszError) - 1; 812 ulRc++; 813 } 814 } 815 816 /* insert parameters */ 817 for (i = 0; i < pFnDesc->cParams; i++) 818 { 819 sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name) " 820 "VALUES (%ld, %d, '%s', '%s')", 821 pFnDesc->alRefCode[k], i, 822 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "", 823 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : "" 824 ); 825 rc = mysql_queryu6(pmysql, pszQuery); 826 if (rc < 0) 827 { 828 if (*pszError == ' ') 829 strcpy(pszError++, "\n\t"); 830 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ", 831 i, dbGetLastErrorDesc(), pszQuery); 832 pszError += strlen(pszError) - 1; 833 ulRc++; 834 } 835 } 836 } 837 } 838 else 839 { 840 if (*pszError == ' ') 841 strcpy(pszError++, "\n\t"); 842 sprintf(pszError, "failed to store result or to fetch a row , error: %s - (sql=%s) ", 843 dbGetLastErrorDesc(), pszQuery); 844 pszError += strlen(pszError) - 1; 845 ulRc++; 846 } 847 } 848 else 744 849 { 745 850 if (*pszError == ' ') 746 851 strcpy(pszError++, "\n\t"); 747 sprintf(pszError, " Deleting old parameters failed witherror: %s - (sql=%s) ",748 dbGetLastErrorDesc(), &szQuery[0]);852 sprintf(pszError, "Failed querying number of parameters, error: %s - (sql=%s) ", 853 dbGetLastErrorDesc(), pszQuery); 749 854 pszError += strlen(pszError) - 1; 750 855 ulRc++; 751 856 } 752 857 753 for (i = 0; i < pFnDesc->cParams; i++) 754 { 755 sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name) " 756 "VALUES (%ld, %d, '%s', '%s')", 757 pFnDesc->alRefCode[k], i, 758 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "", 759 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : "" 760 ); 761 rc = mysql_query(pmysql, pszQuery); 858 859 /* 860 * Authors 861 */ 862 sprintf(pszQuery, "DELETE FROM fnauthor WHERE function = %ld", pFnDesc->alRefCode[k]); 863 rc = mysql_queryu7(pmysql, pszQuery); 864 if (rc < 0) 865 { 866 if (*pszError == ' ') 867 strcpy(pszError++, "\n\t"); 868 sprintf(pszError, "Deleting old authors failed with error: %s - (sql=%s) ", 869 dbGetLastErrorDesc(), pszQuery); 870 pszError += strlen(pszError) - 1; 871 ulRc++; 872 } 873 874 for (i = 0; i < pFnDesc->cAuthors; i++) 875 { 876 if (pFnDesc->alAuthorRefCode[i] == -1) 877 continue; 878 sprintf(pszQuery, "INSERT INTO fnauthor(author, function) " 879 "VALUES (%ld, %ld)", 880 pFnDesc->alAuthorRefCode[i], pFnDesc->alRefCode[k]); 881 rc = mysql_queryu8(pmysql, pszQuery); 762 882 if (rc < 0) 763 883 { … … 765 885 strcpy(pszError++, "\n\t"); 766 886 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ", 767 i, dbGetLastErrorDesc(), &szQuery[0]); 768 pszError += strlen(pszError) - 1; 769 ulRc++; 770 } 771 } 772 773 /* authors */ 774 pszQuery = &szQuery[0]; 775 sprintf(pszQuery, "DELETE FROM fnauthor WHERE function = %ld", pFnDesc->alRefCode[k]); 776 rc = mysql_query(pmysql, &szQuery[0]); 777 if (rc < 0) 778 { 779 if (*pszError == ' ') 780 strcpy(pszError++, "\n\t"); 781 sprintf(pszError, "Deleting old authors failed with error: %s - (sql=%s) ", 782 dbGetLastErrorDesc(), &szQuery[0]); 783 pszError += strlen(pszError) - 1; 784 ulRc++; 785 } 786 787 for (i = 0; i < pFnDesc->cAuthors; i++) 788 { 789 if (pFnDesc->alAuthorRefCode[i] == -1) 790 continue; 791 sprintf(pszQuery, "INSERT INTO fnauthor(author, function) " 792 "VALUES (%ld, %ld)", 793 pFnDesc->alAuthorRefCode[i], pFnDesc->alRefCode[k]); 794 rc = mysql_query(pmysql, pszQuery); 795 if (rc < 0) 796 { 797 if (*pszError == ' ') 798 strcpy(pszError++, "\n\t"); 799 sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ", 800 i, dbGetLastErrorDesc(), &szQuery[0]); 887 i, dbGetLastErrorDesc(), pszQuery); 801 888 pszError += strlen(pszError) - 1; 802 889 ulRc++;
Note:
See TracChangeset
for help on using the changeset viewer.