Changeset 3188 for trunk/archive/psdb/psDB.c
- Timestamp:
- Feb 10, 2005, 10:56:41 AM (21 years ago)
- File:
-
- 1 edited
-
trunk/archive/psdb/psDB.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/psdb/psDB.c
r3175 r3188 8 8 * @author Joshua Hoblitt 9 9 * 10 * @version $Revision: 1. 9$ $Name: not supported by cvs2svn $11 * @date $Date: 2005-02-10 00:02:16$10 * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2005-02-10 20:56:41 $ 12 12 * 13 13 * Copyright 2005 Joshua Hoblitt, University of Hawaii … … 61 61 static char *psDBGenerateSelectColumnSQL(const char *tableName, const char *col, const psU64 limit); 62 62 static char *psDBGenerateSelectRowsSQL(const char *tableName, psMetadata *where); 63 static char *psDBGenerateUpdateRowSQL(const char *tableName, psMetadata *where, psMetadata *values); 64 static char *psDBGenerateSetSQL(psMetadata *set); 63 65 64 66 psDB *psDBInit(const char *host, const char *user, const char *passwd, const char *dbname) … … 628 630 // loop over rows 629 631 for (j = 0; j < rowSet->n; j++) { 630 cursor = psListIteratorAlloc(row->list, 0); 632 // extract the psList, from the psMetadata, in the current psArray element 633 cursor = psListIteratorAlloc(((psMetadata *)rowSet->data[j])->list, 0); 631 634 632 635 // reset bind for each row … … 660 663 } else { 661 664 // error, not a pointer to a string 665 mysql_rollback(dbh->mysql); 666 667 psFree(cursor); 668 psFree(bind); 669 mysql_stmt_close(stmt); 670 671 return false; 662 672 } 663 673 } … … 672 682 fprintf(stderr, "Failed to bind params. Error: %s\n", mysql_stmt_error(stmt)); 673 683 684 mysql_rollback(dbh->mysql); 685 674 686 psFree(bind); 675 687 mysql_stmt_close(stmt); … … 681 693 fprintf(stderr, "Failed to execute prepared statement. Error: %s\n", mysql_stmt_error(stmt)); 682 694 695 mysql_rollback(dbh->mysql); 696 683 697 psFree(bind); 684 698 mysql_stmt_close(stmt); … … 702 716 { 703 717 return psDBSelectRows(dbh, tableName, NULL); 718 } 719 720 psS64 psDBUpdateRows(psDB *dbh, const char *tableName, psMetadata *where, psMetadata *values) 721 { 722 char *query; 723 MYSQL_STMT *stmt; // prepared db statement 724 psU32 paramCount; // number of placeholders in query 725 MYSQL_BIND *bind; // field values to insert 726 psU64 rowsAffected; 727 728 query = psDBGenerateUpdateRowSQL(tableName, where, values); 729 if (!query) { 730 return -1; 731 } 732 733 printf("complete query is %s\n", query); 734 735 stmt = mysql_stmt_init(dbh->mysql); 736 if (!stmt) { 737 // call abort? 738 fprintf(stderr, " mysql_stmt_init(), out of memory\n"); 739 740 return -1; 741 } 742 743 if (mysql_stmt_prepare(stmt, query, (unsigned long)strlen(query))) { 744 fprintf(stderr, "Failed to prepare query. Error: %s\n", mysql_stmt_error(stmt)); 745 746 mysql_stmt_close(stmt); 747 psFree(query); 748 749 return -1; 750 } 751 752 psFree(query); 753 754 // how many place holders are in our query 755 paramCount = (psU32)mysql_stmt_param_count(stmt); 756 757 // structure larger enough to hold one field of data per place holder 758 bind = psAlloc(sizeof(MYSQL_BIND) * paramCount); 759 760 if (mysql_stmt_bind_param(stmt, bind)) { 761 fprintf(stderr, "Failed to bind params. Error: %s\n", mysql_stmt_error(stmt)); 762 763 mysql_rollback(dbh->mysql); 764 765 psFree(bind); 766 mysql_stmt_close(stmt); 767 768 return -1; 769 } 770 771 if (mysql_stmt_execute(stmt)) { 772 fprintf(stderr, "Failed to execute prepared statement. Error: %s\n", mysql_stmt_error(stmt)); 773 774 mysql_rollback(dbh->mysql); 775 776 psFree(bind); 777 mysql_stmt_close(stmt); 778 779 return -1; 780 } 781 782 psFree(bind); 783 784 // point of no return 785 mysql_commit(dbh->mysql); 786 787 rowsAffected = (psU64)mysql_stmt_affected_rows(stmt); 788 789 mysql_stmt_close(stmt); 790 791 return rowsAffected; 792 } 793 794 static char *psDBGenerateUpdateRowSQL(const char *tableName, psMetadata *where, psMetadata *values) 795 { 796 char *query; 797 char *string; 798 799 if ((!values) || (!where)) { 800 return NULL; 801 } 802 803 // init query with a psMemBlock 804 query = psStringCopy("UPDATE "); 805 psStringAppend(&query, tableName); 806 807 string = psDBGenerateSetSQL(values); 808 if (!string) { 809 return NULL; 810 } 811 psStringAppend(&query, " "); 812 psStringAppend(&query, string); 813 psFree(string); 814 815 string = psDBGenerateWhereSQL(where); 816 if (!string) { 817 return NULL; 818 } 819 psStringAppend(&query, " "); 820 psStringAppend(&query, string); 821 psFree(string); 822 823 return query; 704 824 } 705 825 … … 785 905 if (!cursor->offEnd) { 786 906 psStringAppend(&query, " AND "); 907 } 908 } 909 910 psFree(cursor); 911 912 return query; 913 } 914 915 static char *psDBGenerateSetSQL(psMetadata *set) 916 { 917 char *query; 918 psListIterator *cursor; 919 psMetadataItem *item; 920 921 // init query with a psMemBlock 922 query = psStringCopy("SET "); 923 924 cursor = psListIteratorAlloc(set->list, 0); 925 926 // find column name 927 while ((item = psListGetNext(cursor))) { 928 // + column name + _ + = + _ + ? 929 psStringAppend(&query, item->name); 930 psStringAppend(&query, " = ?"); 931 932 // + ", " after every column declaration except the last one 933 if (!cursor->offEnd) { 934 psStringAppend(&query, ", "); 787 935 } 788 936 }
Note:
See TracChangeset
for help on using the changeset viewer.
