Changeset 3134
- Timestamp:
- Feb 4, 2005, 1:44:40 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/archive/psdb/psDB.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/psdb/psDB.c
r3126 r3134 8 8 * @author Joshua Hoblitt 9 9 * 10 * @version $Revision: 1.1 $ $Name: not supported by cvs2svn $ @date $Date: 2005-02-04 04:06:56 $ 10 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2005-02-04 23:44:40 $ 11 12 * 12 13 * Copyright 2005 Joshua Hoblitt, University of Hawaii … … 17 18 #include <math.h> 18 19 #include <mysql/mysql.h> 20 #include <mysql/mysql_com.h> // enum_field_types 19 21 20 22 #include "psDB.h" 21 23 #include "pslib.h" 24 25 typedef struct 26 { 27 psU32 type; 28 bool isUnsigned; 29 } mysqlType; 22 30 23 31 static char *p_psDBIntToString(psU64 n); … … 29 37 static psHash *p_psDBGetSQLToPSTable(void); 30 38 static void p_psDBSQLToPSTableCleanup(void); 39 static void p_psDBPSToMySQLTableCleanup(void); 31 40 static size_t p_psStringAppend(char **dest, const char *source); 32 41 static bool p_psDBRunQuery(psDB *dbh, const char *query); … … 39 48 static char *p_psDBGenerateSelectColumnSQL(const char *tableName, const char *col, const psU64 limit); 40 49 static char *p_psDBGenerateSelectRowsSQL(const char *tableName, psMetadata *where); 50 static psPtr p_psDBAllocMySQLType(enum enum_field_types type, bool isUnsigned); 41 51 42 52 psDB *psDBInit(const char *host, const char *user, const char *passwd, const char *dbname) … … 70 80 p_psDBPSToSQLTableCleanup(); 71 81 p_psDBSQLToPSTableCleanup(); 82 p_psDBPSToMySQLTableCleanup(); 72 83 } 73 84 … … 428 439 { 429 440 char *query; 430 MYSQL_STMT *sth; 441 MYSQL_STMT *stmt; 442 MYSQL_BIND *bind; 443 psU32 paramCount; 431 444 432 445 query = p_psDBGenerateInsertRowSQL(tableName, row); … … 434 447 fprintf(stderr, "complete query is: %s\n", query); 435 448 436 sth = mysql_stmt_init(dbh->mysql); 437 449 stmt = mysql_stmt_init(dbh->mysql); 450 if (!stmt) { 451 // call abort? 452 fprintf(stderr, " mysql_stmt_init(), out of memory\n"); 453 454 return false; 455 } 456 457 if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) { 458 fprintf(stderr, "Failed to prepare query. Error: %s\n", mysql_stmt_error(stmt)); 459 460 mysql_stmt_close(stmt); 461 psFree(query); 462 463 return false; 464 } 465 466 paramCount = (psU32)mysql_stmt_param_count(stmt); 467 468 bind = psAlloc(sizeof(MYSQL_BIND) * paramCount); 469 470 memset(bind, 0, sizeof(MYSQL_BIND) * paramCount); 471 472 if (mysql_stmt_bind_param(stmt, bind)) { 473 fprintf(stderr, "Failed to bind params. Error: %s\n", mysql_stmt_error(stmt)); 474 475 psFree(bind); 476 mysql_stmt_close(stmt); 477 psFree(query); 478 479 return false; 480 } 481 482 bind[0].buffer_type; 483 bind[0].is_null; 484 bind[0].is_unsigned; 485 486 mysql_commit(dbh->mysql); 487 488 mysql_stmt_close(stmt); 438 489 psFree(query); 439 490 … … 478 529 } 479 530 531 psFree(cursor); 532 480 533 p_psStringAppend(&query, ")"); 481 482 psFree(cursor);483 534 484 535 return query; … … 722 773 break; 723 774 case PS_TYPE_S32: 724 PTYPETOSTRING(psS32, "% li");775 PTYPETOSTRING(psS32, "%i"); 725 776 break; 726 777 case PS_TYPE_S64: 727 PTYPETOSTRING(psS64, "%l li");778 PTYPETOSTRING(psS64, "%li"); 728 779 break; 729 780 case PS_TYPE_U8: … … 734 785 break; 735 786 case PS_TYPE_U32: 736 PTYPETOSTRING(psU32, "% lu");787 PTYPETOSTRING(psU32, "%u"); 737 788 break; 738 789 case PS_TYPE_U64: 739 PTYPETOSTRING(psU64, "%l lu");790 PTYPETOSTRING(psU64, "%lu"); 740 791 break; 741 792 case PS_TYPE_F32: … … 752 803 break; 753 804 case PS_TYPE_BOOL: 805 PTYPETOSTRING(bool, "%u"); 754 806 break; 755 807 case PS_TYPE_PTR: 808 // unsafe to convert unless it's a string 756 809 break; 757 810 } … … 937 990 } 938 991 992 static psHash *p_psDBGetPSToMySQLTable(void) 993 { 994 static psHash *lookupTable = NULL; 995 996 if (!lookupTable) { 997 lookupTable = psHashAlloc(16); 998 999 p_psDBAddToLookupTable(lookupTable, PS_TYPE_S8, p_psDBAllocMySQLType(MYSQL_TYPE_TINY, false)); 1000 p_psDBAddToLookupTable(lookupTable, PS_TYPE_S16, p_psDBAllocMySQLType(MYSQL_TYPE_SHORT, false)); 1001 p_psDBAddToLookupTable(lookupTable, PS_TYPE_S32, p_psDBAllocMySQLType(MYSQL_TYPE_LONG, false)); 1002 p_psDBAddToLookupTable(lookupTable, PS_TYPE_S64, p_psDBAllocMySQLType(MYSQL_TYPE_LONGLONG, false)); 1003 p_psDBAddToLookupTable(lookupTable, PS_TYPE_U8, p_psDBAllocMySQLType(MYSQL_TYPE_TINY, true)); 1004 p_psDBAddToLookupTable(lookupTable, PS_TYPE_U16, p_psDBAllocMySQLType(MYSQL_TYPE_SHORT, true)); 1005 p_psDBAddToLookupTable(lookupTable, PS_TYPE_U32, p_psDBAllocMySQLType(MYSQL_TYPE_LONG, true)); 1006 p_psDBAddToLookupTable(lookupTable, PS_TYPE_U64, p_psDBAllocMySQLType(MYSQL_TYPE_LONGLONG, true)); 1007 p_psDBAddToLookupTable(lookupTable, PS_TYPE_F32, p_psDBAllocMySQLType(MYSQL_TYPE_FLOAT, false)); 1008 p_psDBAddToLookupTable(lookupTable, PS_TYPE_F64, p_psDBAllocMySQLType(MYSQL_TYPE_DOUBLE, false)); 1009 p_psDBAddToLookupTable(lookupTable, PS_TYPE_C32, NULL); // unsupported 1010 p_psDBAddToLookupTable(lookupTable, PS_TYPE_C64, NULL); // unsupported 1011 p_psDBAddToLookupTable(lookupTable, PS_TYPE_BOOL, p_psDBAllocMySQLType(MYSQL_TYPE_TINY, true)); 1012 p_psDBAddToLookupTable(lookupTable, PS_TYPE_PTR, p_psDBAllocMySQLType(MYSQL_TYPE_VAR_STRING, false)); 1013 } 1014 1015 // simulate true ref counting 1016 psMemIncrRefCounter(lookupTable); 1017 1018 return lookupTable; 1019 } 1020 1021 static psPtr p_psDBAllocMySQLType(enum enum_field_types type, bool isUnsigned) 1022 { 1023 mysqlType *mType; 1024 1025 mType = psAlloc(sizeof(mysqlType)); 1026 mType->type = type; 1027 mType->isUnsigned = isUnsigned; 1028 1029 return (psPtr)mType; 1030 } 1031 1032 static void p_psDBPSToMySQLTableCleanup(void) 1033 { 1034 psHash *lookupTable; 1035 1036 lookupTable = p_psDBGetPSToMySQLTable(); 1037 1038 psMemDecrRefCounter(lookupTable); 1039 psFree(lookupTable); 1040 } 1041 939 1042 static void p_psDBAddToLookupTable(psHash *lookupTable, const psU32 type, const char *string) 940 1043 { … … 961 1064 : 2; 962 1065 string = psAlloc(length); 963 sprintf(string, "%l li", n);1066 sprintf(string, "%li", n); 964 1067 965 1068 return string;
Note:
See TracChangeset
for help on using the changeset viewer.
