legacy.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include "sqliteInt.h"
  18. /*
  19. ** Execute SQL code. Return one of the SQLITE_ success/failure
  20. ** codes. Also write an error message into memory obtained from
  21. ** malloc() and make *pzErrMsg point to that message.
  22. **
  23. ** If the SQL is a query, then for each row in the query result
  24. ** the xCallback() function is called. pArg becomes the first
  25. ** argument to xCallback(). If xCallback=NULL then no callback
  26. ** is invoked, even for queries.
  27. */
  28. int sqlite3_exec(
  29. sqlite3 *db, /* The database on which the SQL executes */
  30. const char *zSql, /* The SQL to be executed */
  31. sqlite3_callback xCallback, /* Invoke this callback routine */
  32. void *pArg, /* First argument to xCallback() */
  33. char **pzErrMsg /* Write error messages here */
  34. ){
  35. int rc = SQLITE_OK; /* Return code */
  36. const char *zLeftover; /* Tail of unprocessed SQL */
  37. sqlite3_stmt *pStmt = 0; /* The current SQL statement */
  38. char **azCols = 0; /* Names of result columns */
  39. int callbackIsInit; /* True if callback data is initialized */
  40. if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
  41. if( zSql==0 ) zSql = "";
  42. sqlite3_mutex_enter(db->mutex);
  43. sqlite3Error(db, SQLITE_OK, 0);
  44. while( rc==SQLITE_OK && zSql[0] ){
  45. int nCol;
  46. char **azVals = 0;
  47. pStmt = 0;
  48. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
  49. assert( rc==SQLITE_OK || pStmt==0 );
  50. if( rc!=SQLITE_OK ){
  51. continue;
  52. }
  53. if( !pStmt ){
  54. /* this happens for a comment or white-space */
  55. zSql = zLeftover;
  56. continue;
  57. }
  58. callbackIsInit = 0;
  59. nCol = sqlite3_column_count(pStmt);
  60. while( 1 ){
  61. int i;
  62. rc = sqlite3_step(pStmt);
  63. /* Invoke the callback function if required */
  64. if( xCallback && (SQLITE_ROW==rc ||
  65. (SQLITE_DONE==rc && !callbackIsInit
  66. && db->flags&SQLITE_NullCallback)) ){
  67. if( !callbackIsInit ){
  68. azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
  69. if( azCols==0 ){
  70. goto exec_out;
  71. }
  72. for(i=0; i<nCol; i++){
  73. azCols[i] = (char *)sqlite3_column_name(pStmt, i);
  74. /* sqlite3VdbeSetColName() installs column names as UTF8
  75. ** strings so there is no way for sqlite3_column_name() to fail. */
  76. assert( azCols[i]!=0 );
  77. }
  78. callbackIsInit = 1;
  79. }
  80. if( rc==SQLITE_ROW ){
  81. azVals = &azCols[nCol];
  82. for(i=0; i<nCol; i++){
  83. azVals[i] = (char *)sqlite3_column_text(pStmt, i);
  84. if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
  85. db->mallocFailed = 1;
  86. goto exec_out;
  87. }
  88. }
  89. }
  90. if( xCallback(pArg, nCol, azVals, azCols) ){
  91. rc = SQLITE_ABORT;
  92. sqlite3VdbeFinalize((Vdbe *)pStmt);
  93. pStmt = 0;
  94. sqlite3Error(db, SQLITE_ABORT, 0);
  95. goto exec_out;
  96. }
  97. }
  98. if( rc!=SQLITE_ROW ){
  99. rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
  100. pStmt = 0;
  101. zSql = zLeftover;
  102. while( sqlite3Isspace(zSql[0]) ) zSql++;
  103. break;
  104. }
  105. }
  106. sqlite3DbFree(db, azCols);
  107. azCols = 0;
  108. }
  109. exec_out:
  110. if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
  111. sqlite3DbFree(db, azCols);
  112. rc = sqlite3ApiExit(db, rc);
  113. if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
  114. int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
  115. *pzErrMsg = sqlite3Malloc(nErrMsg);
  116. if( *pzErrMsg ){
  117. memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
  118. }else{
  119. rc = SQLITE_NOMEM;
  120. sqlite3Error(db, SQLITE_NOMEM, 0);
  121. }
  122. }else if( pzErrMsg ){
  123. *pzErrMsg = 0;
  124. }
  125. assert( (rc&db->errMask)==rc );
  126. sqlite3_mutex_leave(db->mutex);
  127. return rc;
  128. }