legacy.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. ** 2001 September 15
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** Main file for the SQLite library. The routines in this file
  13. ** implement the programmer interface to the library. Routines in
  14. ** other files are for internal use by SQLite and should not be
  15. ** accessed by users of the library.
  16. **
  17. ** $Id: legacy.c,v 1.22 2007/08/29 12:31:26 danielk1977 Exp $
  18. */
  19. #include "sqliteInt.h"
  20. #include <ctype.h>
  21. /*
  22. ** Execute SQL code. Return one of the SQLITE_ success/failure
  23. ** codes. Also write an error message into memory obtained from
  24. ** malloc() and make *pzErrMsg point to that message.
  25. **
  26. ** If the SQL is a query, then for each row in the query result
  27. ** the xCallback() function is called. pArg becomes the first
  28. ** argument to xCallback(). If xCallback=NULL then no callback
  29. ** is invoked, even for queries.
  30. */
  31. int sqlite3_exec(
  32. sqlite3 *db, /* The database on which the SQL executes */
  33. const char *zSql, /* The SQL to be executed */
  34. sqlite3_callback xCallback, /* Invoke this callback routine */
  35. void *pArg, /* First argument to xCallback() */
  36. char **pzErrMsg /* Write error messages here */
  37. ){
  38. int rc = SQLITE_OK;
  39. const char *zLeftover;
  40. sqlite3_stmt *pStmt = 0;
  41. char **azCols = 0;
  42. int nRetry = 0;
  43. int nCallback;
  44. if( zSql==0 ) return SQLITE_OK;
  45. sqlite3_mutex_enter(db->mutex);
  46. while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
  47. int nCol;
  48. char **azVals = 0;
  49. pStmt = 0;
  50. rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
  51. assert( rc==SQLITE_OK || pStmt==0 );
  52. if( rc!=SQLITE_OK ){
  53. continue;
  54. }
  55. if( !pStmt ){
  56. /* this happens for a comment or white-space */
  57. zSql = zLeftover;
  58. continue;
  59. }
  60. nCallback = 0;
  61. nCol = sqlite3_column_count(pStmt);
  62. azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char *) + 1);
  63. if( azCols==0 ){
  64. goto exec_out;
  65. }
  66. while( 1 ){
  67. int i;
  68. rc = sqlite3_step(pStmt);
  69. /* Invoke the callback function if required */
  70. if( xCallback && (SQLITE_ROW==rc ||
  71. (SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback)) ){
  72. if( 0==nCallback ){
  73. for(i=0; i<nCol; i++){
  74. azCols[i] = (char *)sqlite3_column_name(pStmt, i);
  75. }
  76. nCallback++;
  77. }
  78. if( rc==SQLITE_ROW ){
  79. azVals = &azCols[nCol];
  80. for(i=0; i<nCol; i++){
  81. azVals[i] = (char *)sqlite3_column_text(pStmt, i);
  82. }
  83. }
  84. if( xCallback(pArg, nCol, azVals, azCols) ){
  85. rc = SQLITE_ABORT;
  86. goto exec_out;
  87. }
  88. }
  89. if( rc!=SQLITE_ROW ){
  90. rc = sqlite3_finalize(pStmt);
  91. pStmt = 0;
  92. if( rc!=SQLITE_SCHEMA ){
  93. nRetry = 0;
  94. zSql = zLeftover;
  95. while( isspace((unsigned char)zSql[0]) ) zSql++;
  96. }
  97. break;
  98. }
  99. }
  100. sqlite3_free(azCols);
  101. azCols = 0;
  102. }
  103. exec_out:
  104. if( pStmt ) sqlite3_finalize(pStmt);
  105. if( azCols ) sqlite3_free(azCols);
  106. rc = sqlite3ApiExit(db, rc);
  107. if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
  108. int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
  109. *pzErrMsg = sqlite3_malloc(nErrMsg);
  110. if( *pzErrMsg ){
  111. memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
  112. }
  113. }else if( pzErrMsg ){
  114. *pzErrMsg = 0;
  115. }
  116. assert( (rc&db->errMask)==rc );
  117. sqlite3_mutex_leave(db->mutex);
  118. return rc;
  119. }