Changeset 7652
- Timestamp:
- Jun 22, 2006, 5:49:16 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/db/psDB.c (modified) (25 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/db/psDB.c
r7651 r7652 12 12 * @author Joshua Hoblitt 13 13 * 14 * @version $Revision: 1.6 2$ $Name: not supported by cvs2svn $15 * @date $Date: 2006-06-23 0 2:57:04$14 * @version $Revision: 1.63 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2006-06-23 03:49:16 $ 16 16 * 17 17 * Copyright 2005 Joshua Hoblitt, University of Hawaii … … 179 179 void psDBCleanup(psDB *dbh) 180 180 { 181 // Check if argument dbh is NULL 182 if(!dbh) { 183 psError(PS_ERR_BAD_PARAMETER_TYPE, true, 184 PS_ERRORTEXT_psDB_INVALID_PSDB); 185 return; 186 } 181 PS_ASSERT_PTR_NON_NULL(dbh, ); 187 182 188 183 // Attempt to close specified database connection … … 216 211 const char *dbname) 217 212 { 218 char *query = NULL; 219 bool status; 220 213 PS_ASSERT_PTR_NON_NULL(dbh, false); 214 PS_ASSERT_PTR_NON_NULL(dbname, false); 215 216 char *query = NULL; 221 217 psStringAppend(&query, "CREATE DATABASE %s", dbname); 222 218 223 219 // the MySQL C API notes that mysql_create_db() is deprecated 224 status = p_psDBRunQuery(dbh, query);220 bool status = p_psDBRunQuery(dbh, query); 225 221 if (!status) { 226 222 psError(PS_ERR_UNKNOWN, false, "Failed to create new database."); … … 235 231 const char *dbname) 236 232 { 237 // Verify database object is valid 238 if(dbh == NULL) { 239 psError(PS_ERR_BAD_PARAMETER_TYPE, true, 240 PS_ERRORTEXT_psDB_INVALID_PSDB); 241 return false; 242 } 233 PS_ASSERT_PTR_NON_NULL(dbh, false); 234 PS_ASSERT_PTR_NON_NULL(dbname, false); 243 235 244 236 // Attempt to select new database … … 256 248 const char *dbname) 257 249 { 250 PS_ASSERT_PTR_NON_NULL(dbh, false); 251 PS_ASSERT_PTR_NON_NULL(dbname, false); 252 258 253 char *query = NULL; 259 bool status;260 261 254 psStringAppend(&query, "DROP DATABASE %s", dbname); 262 255 263 256 // the MySQL C API notes that mysql_drop_db() is deprecated 264 status = p_psDBRunQuery(dbh, query);257 bool status = p_psDBRunQuery(dbh, query); 265 258 if (!status) { 266 259 psError(PS_ERR_UNKNOWN, false, "Failed to drop database."); … … 276 269 const psMetadata *md) 277 270 { 278 char *query; 279 bool status; 280 281 // Verify the database object is not NULL 282 if(dbh == NULL) { 283 psError(PS_ERR_BAD_PARAMETER_NULL,true,PS_ERRORTEXT_psDB_INVALID_PSDB); 284 return false; 285 } 271 PS_ASSERT_PTR_NON_NULL(dbh, false); 272 PS_ASSERT_PTR_NON_NULL(tableName, false); 273 PS_ASSERT_PTR_NON_NULL(md, false); 286 274 287 275 // Generate SQL query string 288 query = psDBGenerateCreateTableSQL(tableName, md);276 char *query = psDBGenerateCreateTableSQL(tableName, md); 289 277 if (!query) { 290 278 psError(PS_ERR_UNEXPECTED_NULL, false, PS_ERRORTEXT_psDB_QUERY_GEN_FAIL); … … 293 281 294 282 // Run SQL query to create table 295 status = p_psDBRunQuery(dbh, query);283 bool status = p_psDBRunQuery(dbh, query); 296 284 if (!status) { 297 285 psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psDB_TABLE_CREATE_FAIL); 298 286 } 299 287 300 // Free query string301 288 psFree(query); 302 289 … … 307 294 const char *tableName) 308 295 { 309 char *query = NULL;310 bool status;296 PS_ASSERT_PTR_NON_NULL(dbh, false); 297 PS_ASSERT_PTR_NON_NULL(tableName, false); 311 298 312 299 // Create SQL command string to drop table 300 char *query = NULL; 313 301 psStringAppend(&query, "DROP TABLE %s", tableName); 314 302 315 303 // Execute query 316 status = p_psDBRunQuery(dbh, query);304 bool status = p_psDBRunQuery(dbh, query); 317 305 if (!status) { 318 306 psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psDB_TABLE_DROP_FAIL); … … 329 317 unsigned long long limit) 330 318 { 319 PS_ASSERT_PTR_NON_NULL(dbh, NULL); 320 PS_ASSERT_PTR_NON_NULL(tableName, NULL); 321 PS_ASSERT_PTR_NON_NULL(col, NULL); 322 331 323 MYSQL_RES *result; // complete db result set 332 324 MYSQL_ROW row; // single row of db result set 333 char *query; // SQL query334 325 my_ulonglong rowCount; // number of rows in db result set 335 326 unsigned long dataSize; // size of field … … 339 330 340 331 // Generate SQL query string 341 query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit);332 char *query = psDBGenerateSelectRowSQL(tableName, col, NULL, limit); 342 333 if (!query) { 343 334 psError(PS_ERR_UNEXPECTED_NULL, false, PS_ERRORTEXT_psDB_QUERY_GEN_FAIL); … … 434 425 unsigned long long limit) 435 426 { 436 psArray *stringColumn; // source psArray 437 psVector *column; // dest psVector 438 439 stringColumn = psDBSelectColumn(dbh, tableName, col, limit); 427 PS_ASSERT_PTR_NON_NULL(dbh, NULL); 428 PS_ASSERT_PTR_NON_NULL(tableName, NULL); 429 PS_ASSERT_PTR_NON_NULL(col, NULL); 430 431 psArray *stringColumn = psDBSelectColumn(dbh, tableName, col, limit); 440 432 if (!stringColumn) { 441 433 // could be an error or the result set was just empty … … 443 435 } 444 436 445 column = psVectorAlloc(stringColumn->n, type);437 psVector *column = psVectorAlloc(stringColumn->n, type); 446 438 // column->n = column->nalloc; 447 439 … … 521 513 unsigned long long limit) 522 514 { 515 PS_ASSERT_PTR_NON_NULL(dbh, NULL); 516 PS_ASSERT_PTR_NON_NULL(tableName, NULL); 517 523 518 // Create select row query 524 519 char *query = psDBGenerateSelectRowSQL(tableName, NULL, where, limit); … … 544 539 const psMetadata *row) 545 540 { 546 psArray *rowSet; // psArray of row to insert 547 548 // Check for null row 549 if(row == NULL) { 550 psError(PS_ERR_UNKNOWN,true,PS_ERRORTEXT_psDB_INSERT_ROW_FAIL); 551 return false; 552 } 541 PS_ASSERT_PTR_NON_NULL(dbh, false); 542 PS_ASSERT_PTR_NON_NULL(tableName, false); 543 PS_ASSERT_PTR_NON_NULL(row, false); 553 544 554 545 // Create array to store single row 555 rowSet = psArrayAlloc(1);546 psArray *rowSet = psArrayAlloc(1); 556 547 rowSet->n = 0; 557 548 psArrayAdd(rowSet, 0, (psPtr)row); … … 602 593 const char *tableName) 603 594 { 595 PS_ASSERT_PTR_NON_NULL(dbh, false); 596 PS_ASSERT_PTR_NON_NULL(tableName, false); 597 604 598 return psDBSelectRows(dbh, tableName, NULL, 0); 605 599 } … … 608 602 const char *tableName) 609 603 { 604 PS_ASSERT_PTR_NON_NULL(dbh, false); 605 PS_ASSERT_PTR_NON_NULL(tableName, false); 606 610 607 MYSQL_RES *result; 611 608 MYSQL_FIELD *field; 612 609 unsigned int fieldCount; 613 psMetadata *table;614 610 psU32 pType; 615 611 unsigned int i; 616 612 psPtr column; 617 618 // Verify database object is not null619 if(dbh == NULL) {620 psError(PS_ERR_UNEXPECTED_NULL,true,PS_ERRORTEXT_psDB_INVALID_PSDB);621 return NULL;622 }623 624 // Verify table name is not null625 if(tableName == NULL) {626 psError(PS_ERR_UNEXPECTED_NULL,true,PS_ERRORTEXT_psDB_NULL_TABLE);627 return NULL;628 }629 613 630 614 // find column types … … 636 620 field = mysql_fetch_fields(result); 637 621 fieldCount = mysql_num_fields(result); 638 639 table = psMetadataAlloc(); 622 // Clean up mysql memory 623 mysql_free_result(result); 624 625 // this metadata is returned 626 psMetadata *table = psMetadataAlloc(); 640 627 641 628 // fetch each column and load into psMetadata … … 682 669 psArray *rowData = psArrayAlloc(1); 683 670 psArrayAdd(rowData, 0, values); 684 685 671 long rowsAffected = p_psDBRunQueryPrepared(dbh, rowData, query); 686 672 psFree(rowData); … … 699 685 unsigned long long limit) 700 686 { 701 char *query; 702 psS64 rows = 0; 703 704 // Verify database is not NULL 705 if(dbh == NULL) { 706 psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB); 707 return -1; 708 } 687 PS_ASSERT_PTR_NON_NULL(dbh, -1); 688 PS_ASSERT_PTR_NON_NULL(tableName, -1); 709 689 710 690 // Create SQL statement string 711 query = psDBGenerateDeleteRowSQL(tableName, where,limit);691 char *query = psDBGenerateDeleteRowSQL(tableName, where,limit); 712 692 if (!query) { 713 693 psError(PS_ERR_UNEXPECTED_NULL, false, "Query generation failed."); … … 725 705 726 706 // Get the number of affected row for the delete command 727 rows = (psS64)mysql_affected_rows(dbh->mysql);707 psS64 rows = (psS64)mysql_affected_rows(dbh->mysql); 728 708 729 709 return rows; … … 732 712 long psDBLastInsertID(psDB *dbh) 733 713 { 714 PS_ASSERT_PTR_NON_NULL(dbh, -1); 715 734 716 // XXX return type is actually my_ulonglong - should the return be psU64? 735 717 return (long)mysql_insert_id(dbh->mysql); … … 738 720 bool psDBExplicitTrans(psDB *dbh, bool mode) 739 721 { 740 // Verify database is not NULL 741 if(dbh == NULL) { 742 psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB); 743 return false; 744 } 722 PS_ASSERT_PTR_NON_NULL(dbh, -1); 745 723 746 724 // mode needs to be inverted as autocommits are the opposide of explicit … … 753 731 bool psDBTransaction(psDB *dbh) 754 732 { 755 // Verify database is not NULL 756 if(dbh == NULL) { 757 psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB); 758 return false; 759 } 733 PS_ASSERT_PTR_NON_NULL(dbh, -1); 760 734 761 735 bool status = p_psDBRunQuery(dbh, "START TRANSACTION"); … … 769 743 bool psDBCommit(psDB *dbh) 770 744 { 771 // Verify database is not NULL 772 if(dbh == NULL) { 773 psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB); 774 return false; 775 } 745 PS_ASSERT_PTR_NON_NULL(dbh, -1); 776 746 777 747 // is it safe to assume my_bool always safely casts to bool? … … 782 752 bool psDBRollback(psDB *dbh) 783 753 { 784 // Verify database is not NULL 785 if(dbh == NULL) { 786 psError(PS_ERR_UNEXPECTED_NULL, true, PS_ERRORTEXT_psDB_INVALID_PSDB); 787 return false; 788 } 754 PS_ASSERT_PTR_NON_NULL(dbh, -1); 789 755 790 756 // is it safe to assume my_bool always safely casts to bool?
Note:
See TracChangeset
for help on using the changeset viewer.
