test_schema.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. ** 2006 June 10
  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. ** Code for testing the virtual table interfaces. This code
  13. ** is not included in the SQLite library. It is used for automated
  14. ** testing of the SQLite library.
  15. */
  16. /* The code in this file defines a sqlite3 virtual-table module that
  17. ** provides a read-only view of the current database schema. There is one
  18. ** row in the schema table for each column in the database schema.
  19. */
  20. #define SCHEMA \
  21. "CREATE TABLE x(" \
  22. "database," /* Name of database (i.e. main, temp etc.) */ \
  23. "tablename," /* Name of table */ \
  24. "cid," /* Column number (from left-to-right, 0 upward) */ \
  25. "name," /* Column name */ \
  26. "type," /* Specified type (i.e. VARCHAR(32)) */ \
  27. "not_null," /* Boolean. True if NOT NULL was specified */ \
  28. "dflt_value," /* Default value for this column */ \
  29. "pk" /* True if this column is part of the primary key */ \
  30. ")"
  31. /* If SQLITE_TEST is defined this code is preprocessed for use as part
  32. ** of the sqlite test binary "testfixture". Otherwise it is preprocessed
  33. ** to be compiled into an sqlite dynamic extension.
  34. */
  35. #ifdef SQLITE_TEST
  36. #include "sqliteInt.h"
  37. #include "tcl.h"
  38. #else
  39. #include "sqlite3ext.h"
  40. SQLITE_EXTENSION_INIT1
  41. #endif
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <assert.h>
  45. typedef struct schema_vtab schema_vtab;
  46. typedef struct schema_cursor schema_cursor;
  47. /* A schema table object */
  48. struct schema_vtab {
  49. sqlite3_vtab base;
  50. sqlite3 *db;
  51. };
  52. /* A schema table cursor object */
  53. struct schema_cursor {
  54. sqlite3_vtab_cursor base;
  55. sqlite3_stmt *pDbList;
  56. sqlite3_stmt *pTableList;
  57. sqlite3_stmt *pColumnList;
  58. int rowid;
  59. };
  60. /*
  61. ** None of this works unless we have virtual tables.
  62. */
  63. #ifndef SQLITE_OMIT_VIRTUALTABLE
  64. /*
  65. ** Table destructor for the schema module.
  66. */
  67. static int schemaDestroy(sqlite3_vtab *pVtab){
  68. sqlite3_free(pVtab);
  69. return 0;
  70. }
  71. /*
  72. ** Table constructor for the schema module.
  73. */
  74. static int schemaCreate(
  75. sqlite3 *db,
  76. void *pAux,
  77. int argc, const char *const*argv,
  78. sqlite3_vtab **ppVtab,
  79. char **pzErr
  80. ){
  81. int rc = SQLITE_NOMEM;
  82. schema_vtab *pVtab = sqlite3_malloc(sizeof(schema_vtab));
  83. if( pVtab ){
  84. memset(pVtab, 0, sizeof(schema_vtab));
  85. pVtab->db = db;
  86. #ifndef SQLITE_OMIT_VIRTUALTABLE
  87. rc = sqlite3_declare_vtab(db, SCHEMA);
  88. #endif
  89. }
  90. *ppVtab = (sqlite3_vtab *)pVtab;
  91. return rc;
  92. }
  93. /*
  94. ** Open a new cursor on the schema table.
  95. */
  96. static int schemaOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
  97. int rc = SQLITE_NOMEM;
  98. schema_cursor *pCur;
  99. pCur = sqlite3_malloc(sizeof(schema_cursor));
  100. if( pCur ){
  101. memset(pCur, 0, sizeof(schema_cursor));
  102. *ppCursor = (sqlite3_vtab_cursor *)pCur;
  103. rc = SQLITE_OK;
  104. }
  105. return rc;
  106. }
  107. /*
  108. ** Close a schema table cursor.
  109. */
  110. static int schemaClose(sqlite3_vtab_cursor *cur){
  111. schema_cursor *pCur = (schema_cursor *)cur;
  112. sqlite3_finalize(pCur->pDbList);
  113. sqlite3_finalize(pCur->pTableList);
  114. sqlite3_finalize(pCur->pColumnList);
  115. sqlite3_free(pCur);
  116. return SQLITE_OK;
  117. }
  118. /*
  119. ** Retrieve a column of data.
  120. */
  121. static int schemaColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
  122. schema_cursor *pCur = (schema_cursor *)cur;
  123. switch( i ){
  124. case 0:
  125. sqlite3_result_value(ctx, sqlite3_column_value(pCur->pDbList, 1));
  126. break;
  127. case 1:
  128. sqlite3_result_value(ctx, sqlite3_column_value(pCur->pTableList, 0));
  129. break;
  130. default:
  131. sqlite3_result_value(ctx, sqlite3_column_value(pCur->pColumnList, i-2));
  132. break;
  133. }
  134. return SQLITE_OK;
  135. }
  136. /*
  137. ** Retrieve the current rowid.
  138. */
  139. static int schemaRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  140. schema_cursor *pCur = (schema_cursor *)cur;
  141. *pRowid = pCur->rowid;
  142. return SQLITE_OK;
  143. }
  144. static int finalize(sqlite3_stmt **ppStmt){
  145. int rc = sqlite3_finalize(*ppStmt);
  146. *ppStmt = 0;
  147. return rc;
  148. }
  149. static int schemaEof(sqlite3_vtab_cursor *cur){
  150. schema_cursor *pCur = (schema_cursor *)cur;
  151. return (pCur->pDbList ? 0 : 1);
  152. }
  153. /*
  154. ** Advance the cursor to the next row.
  155. */
  156. static int schemaNext(sqlite3_vtab_cursor *cur){
  157. int rc = SQLITE_OK;
  158. schema_cursor *pCur = (schema_cursor *)cur;
  159. schema_vtab *pVtab = (schema_vtab *)(cur->pVtab);
  160. char *zSql = 0;
  161. while( !pCur->pColumnList || SQLITE_ROW!=sqlite3_step(pCur->pColumnList) ){
  162. if( SQLITE_OK!=(rc = finalize(&pCur->pColumnList)) ) goto next_exit;
  163. while( !pCur->pTableList || SQLITE_ROW!=sqlite3_step(pCur->pTableList) ){
  164. if( SQLITE_OK!=(rc = finalize(&pCur->pTableList)) ) goto next_exit;
  165. assert(pCur->pDbList);
  166. while( SQLITE_ROW!=sqlite3_step(pCur->pDbList) ){
  167. rc = finalize(&pCur->pDbList);
  168. goto next_exit;
  169. }
  170. /* Set zSql to the SQL to pull the list of tables from the
  171. ** sqlite_master (or sqlite_temp_master) table of the database
  172. ** identfied by the row pointed to by the SQL statement pCur->pDbList
  173. ** (iterating through a "PRAGMA database_list;" statement).
  174. */
  175. if( sqlite3_column_int(pCur->pDbList, 0)==1 ){
  176. zSql = sqlite3_mprintf(
  177. "SELECT name FROM sqlite_temp_master WHERE type='table'"
  178. );
  179. }else{
  180. sqlite3_stmt *pDbList = pCur->pDbList;
  181. zSql = sqlite3_mprintf(
  182. "SELECT name FROM %Q.sqlite_master WHERE type='table'",
  183. sqlite3_column_text(pDbList, 1)
  184. );
  185. }
  186. if( !zSql ){
  187. rc = SQLITE_NOMEM;
  188. goto next_exit;
  189. }
  190. rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pTableList, 0);
  191. sqlite3_free(zSql);
  192. if( rc!=SQLITE_OK ) goto next_exit;
  193. }
  194. /* Set zSql to the SQL to the table_info pragma for the table currently
  195. ** identified by the rows pointed to by statements pCur->pDbList and
  196. ** pCur->pTableList.
  197. */
  198. zSql = sqlite3_mprintf("PRAGMA %Q.table_info(%Q)",
  199. sqlite3_column_text(pCur->pDbList, 1),
  200. sqlite3_column_text(pCur->pTableList, 0)
  201. );
  202. if( !zSql ){
  203. rc = SQLITE_NOMEM;
  204. goto next_exit;
  205. }
  206. rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pColumnList, 0);
  207. sqlite3_free(zSql);
  208. if( rc!=SQLITE_OK ) goto next_exit;
  209. }
  210. pCur->rowid++;
  211. next_exit:
  212. /* TODO: Handle rc */
  213. return rc;
  214. }
  215. /*
  216. ** Reset a schema table cursor.
  217. */
  218. static int schemaFilter(
  219. sqlite3_vtab_cursor *pVtabCursor,
  220. int idxNum, const char *idxStr,
  221. int argc, sqlite3_value **argv
  222. ){
  223. int rc;
  224. schema_vtab *pVtab = (schema_vtab *)(pVtabCursor->pVtab);
  225. schema_cursor *pCur = (schema_cursor *)pVtabCursor;
  226. pCur->rowid = 0;
  227. finalize(&pCur->pTableList);
  228. finalize(&pCur->pColumnList);
  229. finalize(&pCur->pDbList);
  230. rc = sqlite3_prepare(pVtab->db,"PRAGMA database_list", -1, &pCur->pDbList, 0);
  231. return (rc==SQLITE_OK ? schemaNext(pVtabCursor) : rc);
  232. }
  233. /*
  234. ** Analyse the WHERE condition.
  235. */
  236. static int schemaBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
  237. return SQLITE_OK;
  238. }
  239. /*
  240. ** A virtual table module that merely echos method calls into TCL
  241. ** variables.
  242. */
  243. static sqlite3_module schemaModule = {
  244. 0, /* iVersion */
  245. schemaCreate,
  246. schemaCreate,
  247. schemaBestIndex,
  248. schemaDestroy,
  249. schemaDestroy,
  250. schemaOpen, /* xOpen - open a cursor */
  251. schemaClose, /* xClose - close a cursor */
  252. schemaFilter, /* xFilter - configure scan constraints */
  253. schemaNext, /* xNext - advance a cursor */
  254. schemaEof, /* xEof */
  255. schemaColumn, /* xColumn - read data */
  256. schemaRowid, /* xRowid - read data */
  257. 0, /* xUpdate */
  258. 0, /* xBegin */
  259. 0, /* xSync */
  260. 0, /* xCommit */
  261. 0, /* xRollback */
  262. 0, /* xFindMethod */
  263. 0, /* xRename */
  264. };
  265. #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
  266. #ifdef SQLITE_TEST
  267. /*
  268. ** Decode a pointer to an sqlite3 object.
  269. */
  270. extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
  271. /*
  272. ** Register the schema virtual table module.
  273. */
  274. static int register_schema_module(
  275. ClientData clientData, /* Not used */
  276. Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
  277. int objc, /* Number of arguments */
  278. Tcl_Obj *CONST objv[] /* Command arguments */
  279. ){
  280. sqlite3 *db;
  281. if( objc!=2 ){
  282. Tcl_WrongNumArgs(interp, 1, objv, "DB");
  283. return TCL_ERROR;
  284. }
  285. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  286. #ifndef SQLITE_OMIT_VIRTUALTABLE
  287. sqlite3_create_module(db, "schema", &schemaModule, 0);
  288. #endif
  289. return TCL_OK;
  290. }
  291. /*
  292. ** Register commands with the TCL interpreter.
  293. */
  294. int Sqlitetestschema_Init(Tcl_Interp *interp){
  295. static struct {
  296. char *zName;
  297. Tcl_ObjCmdProc *xProc;
  298. void *clientData;
  299. } aObjCmd[] = {
  300. { "register_schema_module", register_schema_module, 0 },
  301. };
  302. int i;
  303. for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
  304. Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
  305. aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
  306. }
  307. return TCL_OK;
  308. }
  309. #else
  310. /*
  311. ** Extension load function.
  312. */
  313. #ifdef _WIN32
  314. __declspec(dllexport)
  315. #endif
  316. int sqlite3_schema_init(
  317. sqlite3 *db,
  318. char **pzErrMsg,
  319. const sqlite3_api_routines *pApi
  320. ){
  321. SQLITE_EXTENSION_INIT2(pApi);
  322. #ifndef SQLITE_OMIT_VIRTUALTABLE
  323. sqlite3_create_module(db, "schema", &schemaModule, 0);
  324. #endif
  325. return 0;
  326. }
  327. #endif