Changeset 3244
- Timestamp:
- Feb 16, 2005, 11:43:35 AM (21 years ago)
- File:
-
- 1 edited
-
trunk/archive/psdb/psDB.c (modified) (46 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/psdb/psDB.c
r3243 r3244 8 8 * @author Joshua Hoblitt 9 9 * 10 * @version $Revision: 1.3 0$ $Name: not supported by cvs2svn $11 * @date $Date: 2005-02-16 02:56:45 $10 * @version $Revision: 1.31 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2005-02-16 21:43:35 $ 12 12 * 13 13 * Copyright 2005 Joshua Hoblitt, University of Hawaii … … 36 36 // SQL generation functions 37 37 static char *psDBGenerateCreateTableSQL(const char *tableName, psMetadata *where); 38 static char *psDBGenerateSelectRowSQL(const char *tableName, const char *col, psMetadata *where, constpsU64 limit);38 static char *psDBGenerateSelectRowSQL(const char *tableName, const char *col, psMetadata *where, psU64 limit); 39 39 static char *psDBGenerateInsertRowSQL(const char *tableName, psMetadata *row); 40 40 static char *psDBGenerateUpdateRowSQL(const char *tableName, psMetadata *where, psMetadata *values); … … 61 61 62 62 static psPtr psDBMySQLTypeAlloc(enum enum_field_types type, bool isUnsigned); 63 static void psDBAddToLookupTable(psHash *lookupTable, constpsU32 type, const char *string);64 static void psDBAddVoidToLookupTable(psHash *lookupTable, constpsU32 type, psPtr value);63 static void psDBAddToLookupTable(psHash *lookupTable, psU32 type, const char *string); 64 static void psDBAddVoidToLookupTable(psHash *lookupTable, psU32 type, psPtr value); 65 65 66 66 // pType utility functions … … 85 85 mysql = mysql_init(NULL); 86 86 if (!mysql) { 87 // call abort? 88 fprintf(stderr, " mysql_init(), out of memory\n"); 89 90 return NULL; 87 psAbort(__func__, "mysql_init(), out of memory."); 91 88 } 92 89 93 90 if (!mysql_real_connect(mysql, host, user, passwd, dbname, 0, NULL, 0)) { 94 fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(mysql));91 psError(PS_ERR_UNKNOWN, true, "Failed to connect to database. Error: %s", mysql_error(mysql)); 95 92 96 93 mysql_close(mysql); … … 129 126 // the MySQL C API notes that mysql_create_db() is deprecated 130 127 status = psDBRunQuery(dbh, query); 128 if (!status) { 129 psError(PS_ERR_UNKNOWN, false, "Failed to create new database."); 130 } 131 131 132 132 psFree(query); … … 138 138 { 139 139 if (mysql_select_db(dbh->mysql, dbname) != 0) { 140 fprintf(stderr, "Failed to change database. Error: %s\n", mysql_error(dbh->mysql));140 psError(PS_ERR_UNKNOWN, true, "Failed to change database. Error: %s", mysql_error(dbh->mysql)); 141 141 142 142 return false; … … 155 155 // the MySQL C API notes that mysql_drop_db() is deprecated 156 156 status = psDBRunQuery(dbh, query); 157 if (!status) { 158 psError(PS_ERR_UNKNOWN, false, "Failed to drop database."); 159 } 157 160 158 161 psFree(query); … … 168 171 query = psDBGenerateCreateTableSQL(tableName, md); 169 172 if (!query) { 173 psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed."); 174 170 175 return NULL; 171 176 } 172 177 173 178 status = psDBRunQuery(dbh, query); 179 if (!status) { 180 psError(PS_ERR_UNKNOWN, false, "Failed to create table."); 181 } 174 182 175 183 psFree(query); … … 186 194 187 195 status = psDBRunQuery(dbh, query); 196 if (!status) { 197 psError(PS_ERR_UNKNOWN, false, "Failed to drop table."); 198 } 188 199 189 200 psFree(query); … … 192 203 } 193 204 194 psArray *psDBSelectColumn(psDB *dbh, const char *tableName, const char *col, constpsU64 limit)205 psArray *psDBSelectColumn(psDB *dbh, const char *tableName, const char *col, psU64 limit) 195 206 { 196 207 MYSQL_RES *result; // complete db result set … … 205 216 query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit); 206 217 if (!query) { 218 psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed."); 219 207 220 return NULL; 208 221 } 209 222 210 223 if (!psDBRunQuery(dbh, query)) { 224 psError(PS_ERR_UNKNOWN, false, "Query execution failed."); 225 211 226 psFree(query); 212 227 … … 224 239 // then something bad has happened. 225 240 if (fieldCount != 0) { 226 // error, should have gotten data 227 fprintf(stderr, "Query returned no data. Error: %s\n", mysql_error(dbh->mysql)); 241 psError(PS_ERR_UNEXPECTED_NULL, true, "Query returned no data. Error: %s", mysql_error(dbh->mysql)); 228 242 229 243 return NULL; … … 285 299 stringColumn = psDBSelectColumn(dbh, tableName, col, limit); 286 300 if (!stringColumn) { 301 // could be an error or the result set was just empty 287 302 return NULL; 288 303 } … … 361 376 query = psDBGenerateSelectRowSQL(tableName, NULL, where, limit); 362 377 if (!query) { 378 psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed."); 379 363 380 return NULL; 364 381 } 365 382 366 383 if (!psDBRunQuery(dbh, query)) { 367 // error 384 psError(PS_ERR_UNKNOWN, false, "Query execution failed."); 385 368 386 psFree(query); 369 387 … … 381 399 // it's non-zero then something bad has happened. 382 400 if (fieldCount != 0) { 383 // error, should have gotten data 384 fprintf(stderr, "Query returned no data. Error: %s\n", mysql_error(dbh->mysql)); 401 psError(PS_ERR_UNEXPECTED_NULL, true, "Query returned no data. Error: %s", mysql_error(dbh->mysql)); 385 402 386 403 return NULL; … … 452 469 453 470 if (!psDBInsertRows(dbh, tableName, rowSet)) { 471 psError(PS_ERR_UNKNOWN, false, "Insert failed."); 472 454 473 psFree(rowSet); 455 474 … … 477 496 query = psDBGenerateInsertRowSQL(tableName, row); 478 497 if (!query) { 498 psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed."); 499 479 500 return NULL; 480 501 } … … 482 503 stmt = mysql_stmt_init(dbh->mysql); 483 504 if (!stmt) { 484 // call abort? 485 fprintf(stderr, " mysql_stmt_init(), out of memory\n"); 505 psAbort(__func__, "mysql_stmt_init(), out of memory."); 506 } 507 508 if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) { 509 psError(PS_ERR_UNKNOWN, true, "Failed to prepare query. Error: %s", mysql_stmt_error(stmt)); 510 511 mysql_stmt_close(stmt); 512 psFree(query); 486 513 487 514 return false; 488 }489 490 if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {491 fprintf(stderr, "Failed to prepare query. Error: %s\n", mysql_stmt_error(stmt));492 493 mysql_stmt_close(stmt);494 psFree(query);495 496 return false;497 515 } 498 516 … … 513 531 514 532 if (!psDBPackRow(bind, row, paramCount)) { 515 fprintf(stderr, "Failed to pack params into bind structure.\n");533 psError(PS_ERR_UNKNOWN, false, "Failed to pack params into bind structure."); 516 534 517 535 mysql_rollback(dbh->mysql); … … 522 540 523 541 if (mysql_stmt_bind_param(stmt, bind)) { 524 fprintf(stderr, "Failed to bind params. Error: %s\n", mysql_stmt_error(stmt));542 psError(PS_ERR_UNKNOWN, true, "Failed to bind params. Error: %s", mysql_stmt_error(stmt)); 525 543 526 544 mysql_rollback(dbh->mysql); … … 533 551 534 552 if (mysql_stmt_execute(stmt)) { 535 fprintf(stderr, "Failed to execute prepared statement. Error: %s\n", mysql_stmt_error(stmt)); 553 psError(PS_ERR_UNKNOWN, true, "Failed to execute prepared statement. Error: %s", 554 mysql_stmt_error(stmt)); 536 555 537 556 mysql_rollback(dbh->mysql); … … 570 589 // find column types 571 590 result = mysql_list_fields(dbh->mysql, tableName, NULL); 591 if (!result) { 592 psError(PS_ERR_UNEXPECTED_NULL, true, "Failed to retrieve column types."); 593 } 594 572 595 field = mysql_fetch_fields(result); 573 596 fieldCount = mysql_num_fields(result); … … 610 633 query = psDBGenerateUpdateRowSQL(tableName, where, values); 611 634 if (!query) { 635 psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed."); 636 612 637 return -1; 613 638 } … … 615 640 stmt = mysql_stmt_init(dbh->mysql); 616 641 if (!stmt) { 617 // call abort? 618 fprintf(stderr, " mysql_stmt_init(), out of memory\n"); 642 psAbort(__func__, "mysql_stmt_init(), out of memory."); 643 } 644 645 if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) { 646 psError(PS_ERR_UNKNOWN, true, "Failed to prepare query. Error: %s", mysql_stmt_error(stmt)); 647 648 mysql_stmt_close(stmt); 649 psFree(query); 619 650 620 651 return -1; 621 }622 623 if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) {624 fprintf(stderr, "Failed to prepare query. Error: %s\n", mysql_stmt_error(stmt));625 626 mysql_stmt_close(stmt);627 psFree(query);628 629 return -1;630 652 } 631 653 … … 649 671 650 672 if (mysql_stmt_bind_param(stmt, bind)) { 651 fprintf(stderr, "Failed to bind params. Error: %s\n", mysql_stmt_error(stmt));673 psError(PS_ERR_UNKNOWN, true, "Failed to bind params. Error: %s", mysql_stmt_error(stmt)); 652 674 653 675 mysql_rollback(dbh->mysql); … … 660 682 661 683 if (mysql_stmt_execute(stmt)) { 662 fprintf(stderr, "Failed to execute prepared statement. Error: %s\n", mysql_stmt_error(stmt)); 684 psError(PS_ERR_UNKNOWN, true, "Failed to execute prepared statement. Error: %s", 685 mysql_stmt_error(stmt)); 663 686 664 687 mysql_rollback(dbh->mysql); … … 688 711 query = psDBGenerateDeleteRowSQL(tableName, where); 689 712 if (!query) { 713 psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed."); 714 690 715 return -1; 691 716 } 692 717 693 718 if (!psDBRunQuery(dbh, query)) { 719 psError(PS_ERR_UNKNOWN, false, "Delete failed."); 720 694 721 mysql_rollback(dbh->mysql); 695 722 … … 703 730 if (!where) { 704 731 // we truncated the table so mysql_affected_rows() won't work 705 706 732 return 1; 707 733 } … … 719 745 { 720 746 if (mysql_real_query(dbh->mysql, query, (unsigned long)strlen(query)) !=0) { 721 fprintf(stderr, "Failed to execute query. Error: %s\n", mysql_error(dbh->mysql));747 psError(PS_ERR_UNKNOWN, true, "Failed to execute query. Error: %s\n", mysql_error(dbh->mysql)); 722 748 723 749 return false; … … 764 790 } else { 765 791 // error, not a pointer to a string 792 psError(PS_ERR_BAD_PARAMETER_TYPE , true, 793 "Only types of PS_META_PRIMITIVE and PS_META_STR are supported."); 794 766 795 psFree(cursor); 767 796 … … 796 825 797 826 if (!table) { 827 psError(PS_ERR_BAD_PARAMETER_NULL, true, "table param may not be null."); 828 798 829 return NULL; 799 830 } … … 815 846 psStringAppend(&query, "%s VARCHAR(%s)", item->name, item->data.V); 816 847 } else { 817 // error, invalid type 848 psError(PS_ERR_BAD_PARAMETER_TYPE, true, 849 "Only types of PS_META_PRIMITIVE and PS_META_STR are supported."); 850 818 851 psFree(query); 819 852 psFree(cursor); … … 864 897 whereSQL = psDBGenerateWhereSQL(where); 865 898 if (!whereSQL) { 899 psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed."); 900 866 901 psFree(query); 867 902 … … 932 967 933 968 if ((!values) || (!where)) { 969 psError(PS_ERR_BAD_PARAMETER_NULL, true, "values and where params may not be NULL."); 970 934 971 return NULL; 935 972 } … … 937 974 setSQL = psDBGenerateSetSQL(values); 938 975 if (!setSQL) { 976 psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed."); 977 939 978 return NULL; 940 979 } … … 942 981 whereSQL = psDBGenerateWhereSQL(where); 943 982 if (!whereSQL) { 983 psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed."); 984 944 985 return NULL; 945 986 } … … 966 1007 whereSQL = psDBGenerateWhereSQL(where); 967 1008 if (!whereSQL) { 1009 psError(PS_ERR_UNEXPECTED_NULL, false, "SQL substring generation failed."); 1010 968 1011 return NULL; 969 1012 } … … 981 1024 982 1025 if (!where) { 1026 psError(PS_ERR_BAD_PARAMETER_NULL, true, "where param may not be NULL."); 1027 983 1028 return NULL; 984 1029 } … … 999 1044 } 1000 1045 } else { 1001 // error, invalid where 1046 psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Only a type of PS_META_STR is supported."); 1047 1002 1048 psFree(cursor); 1003 1049 psFree(query); … … 1024 1070 1025 1071 if (!set) { 1072 psError(PS_ERR_BAD_PARAMETER_NULL, true, "set param may not be NULL."); 1073 1026 1074 return NULL; 1027 1075 } … … 1065 1113 // lookup MySQL column type 1066 1114 key = psDBIntToString((psU64)type); 1067 value = psHashLookup(mysqlToSQLTable, key); 1068 sqlType = psStringCopy(value); 1115 sqlType = psHashLookup(mysqlToSQLTable, key); 1069 1116 psFree(key); 1070 1117 psFree(mysqlToSQLTable); 1118 1119 if (!sqlType) { 1120 psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed."); 1121 1122 psFree(sqlToPSTable); 1123 1124 return -1; 1125 } 1071 1126 1072 1127 // MySQL column types can not be directly translated to PS … … 1080 1135 // convert MySQL type to PS type 1081 1136 value = psHashLookup(sqlToPSTable, sqlType); 1137 psFree(sqlToPSTable); 1138 1139 if (!value) { 1140 psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed."); 1141 1142 return -1; 1143 } 1144 1082 1145 pType = (psU32)atol(value); 1083 psFree(sqlType);1084 psFree(sqlToPSTable);1085 1146 1086 1147 return pType; … … 1098 1159 sqlType = psHashLookup(pTypeToSQLTable, key); 1099 1160 psFree(key); 1100 1101 1161 psFree(pTypeToSQLTable); 1162 1163 if (!sqlType) { 1164 psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed."); 1165 1166 return NULL; 1167 } 1102 1168 1103 1169 psMemIncrRefCounter(sqlType); … … 1117 1183 mType = psHashLookup(pTypeToMySQLTable, key); 1118 1184 psFree(key); 1119 1120 1185 psFree(pTypeToMySQLTable); 1186 1187 if (!mType) { 1188 psError(PS_ERR_UNEXPECTED_NULL, true, "type lookup failed."); 1189 1190 return NULL; 1191 } 1121 1192 1122 1193 psMemIncrRefCounter(mType); … … 1306 1377 } 1307 1378 1308 static void psDBAddToLookupTable(psHash *lookupTable, constpsU32 type, const char *string)1379 static void psDBAddToLookupTable(psHash *lookupTable, psU32 type, const char *string) 1309 1380 { 1310 1381 char *key; … … 1320 1391 } 1321 1392 1322 static void psDBAddVoidToLookupTable(psHash *lookupTable, constpsU32 type, psPtr value)1393 static void psDBAddVoidToLookupTable(psHash *lookupTable, psU32 type, psPtr value) 1323 1394 { 1324 1395 char *key;
Note:
See TracChangeset
for help on using the changeset viewer.
