Debian Codegraph @

ext/expert/test_expert.c

raw
1 /*
2 ** 2017 April 07
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 */
13
14 #if defined(SQLITE_TEST)
15
16 #include "sqlite3expert.h"
17 #include <assert.h>
18 #include <string.h>
19 #include "tclsqlite.h"
20
21 #ifndef SQLITE_OMIT_VIRTUALTABLE
22
23 /*
24 ** Extract an sqlite3* db handle from the object passed as the second
25 ** argument. If successful, set *pDb to point to the db handle and return
26 ** TCL_OK. Otherwise, return TCL_ERROR.
27 */
28 static int dbHandleFromObj(Tcl_Interp *interp, Tcl_Obj *pObj, sqlite3 **pDb){
29 Tcl_CmdInfo info;
30 if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(pObj), &info) ){
31 Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(pObj), NULL);
32 return TCL_ERROR;
33 }
34
35 *pDb = *(sqlite3 **)info.objClientData;
36 return TCL_OK;
37 }
38
39
40 /*
41 ** Tclcmd: $expert sql SQL
42 ** $expert analyze
43 ** $expert count
44 ** $expert report STMT EREPORT
45 ** $expert destroy
46 */
47 static int SQLITE_TCLAPI testExpertCmd(
48 void *clientData,
49 Tcl_Interp *interp,
50 int objc,
51 Tcl_Obj *CONST objv[]
52 ){
53 sqlite3expert *pExpert = (sqlite3expert*)clientData;
54 struct Subcmd {
55 const char *zSub;
56 int nArg;
57 const char *zMsg;
58 } aSub[] = {
59 { "sql", 1, "TABLE", }, /* 0 */
60 { "analyze", 0, "", }, /* 1 */
61 { "count", 0, "", }, /* 2 */
62 { "report", 2, "STMT EREPORT", }, /* 3 */
63 { "destroy", 0, "", }, /* 4 */
64 { 0 }
65 };
66 int iSub;
67 int rc = TCL_OK;
68 char *zErr = 0;
69
70 if( objc<2 ){
71 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
72 return TCL_ERROR;
73 }
74 rc = Tcl_GetIndexFromObjStruct(interp,
75 objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iSub
76 );
77 if( rc!=TCL_OK ) return rc;
78 if( objc!=2+aSub[iSub].nArg ){
79 Tcl_WrongNumArgs(interp, 2, objv, aSub[iSub].zMsg);
80 return TCL_ERROR;
81 }
82
83 switch( iSub ){
84 case 0: { /* sql */
85 char *zArg = Tcl_GetString(objv[2]);
86 rc = sqlite3_expert_sql(pExpert, zArg, &zErr);
87 break;
88 }
89
90 case 1: { /* analyze */
91 rc = sqlite3_expert_analyze(pExpert, &zErr);
92 break;
93 }
94
95 case 2: { /* count */
96 int n = sqlite3_expert_count(pExpert);
97 Tcl_SetObjResult(interp, Tcl_NewIntObj(n));
98 break;
99 }
100
101 case 3: { /* report */
102 const char *aEnum[] = {
103 "sql", "indexes", "plan", "candidates", 0
104 };
105 int iEnum;
106 int iStmt;
107 const char *zReport;
108
109 if( Tcl_GetIntFromObj(interp, objv[2], &iStmt)
110 || Tcl_GetIndexFromObj(interp, objv[3], aEnum, "report", 0, &iEnum)
111 ){
112 return TCL_ERROR;
113 }
114
115 assert( EXPERT_REPORT_SQL==1 );
116 assert( EXPERT_REPORT_INDEXES==2 );
117 assert( EXPERT_REPORT_PLAN==3 );
118 assert( EXPERT_REPORT_CANDIDATES==4 );
119 zReport = sqlite3_expert_report(pExpert, iStmt, 1+iEnum);
120 Tcl_SetObjResult(interp, Tcl_NewStringObj(zReport, -1));
121 break;
122 }
123
124 default: /* destroy */
125 assert( iSub==4 );
126 Tcl_DeleteCommand(interp, Tcl_GetString(objv[0]));
127 break;
128 }
129
130 if( rc!=TCL_OK ){
131 if( zErr ){
132 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1));
133 }else{
134 extern const char *sqlite3ErrName(int);
135 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
136 }
137 }
138 sqlite3_free(zErr);
139 return rc;
140 }
141
142 static void SQLITE_TCLAPI testExpertDel(void *clientData){
143 sqlite3expert *pExpert = (sqlite3expert*)clientData;
144 sqlite3_expert_destroy(pExpert);
145 }
146
147 /*
148 ** sqlite3_expert_new DB
149 */
150 static int SQLITE_TCLAPI test_sqlite3_expert_new(
151 void * clientData,
152 Tcl_Interp *interp,
153 int objc,
154 Tcl_Obj *CONST objv[]
155 ){
156 static int iCmd = 0;
157 sqlite3 *db;
158 char *zCmd = 0;
159 char *zErr = 0;
160 sqlite3expert *pExpert;
161 int rc = TCL_OK;
162
163 if( objc!=2 ){
164 Tcl_WrongNumArgs(interp, 1, objv, "DB");
165 return TCL_ERROR;
166 }
167 if( dbHandleFromObj(interp, objv[1], &db) ){
168 return TCL_ERROR;
169 }
170
171 zCmd = sqlite3_mprintf("sqlite3expert%d", ++iCmd);
172 if( zCmd==0 ){
173 Tcl_AppendResult(interp, "out of memory", (char*)0);
174 return TCL_ERROR;
175 }
176
177 pExpert = sqlite3_expert_new(db, &zErr);
178 if( pExpert==0 ){
179 Tcl_AppendResult(interp, zErr, (char*)0);
180 rc = TCL_ERROR;
181 }else{
182 void *p = (void*)pExpert;
183 Tcl_CreateObjCommand(interp, zCmd, testExpertCmd, p, testExpertDel);
184 Tcl_SetObjResult(interp, Tcl_NewStringObj(zCmd, -1));
185 }
186
187 sqlite3_free(zCmd);
188 sqlite3_free(zErr);
189 return rc;
190 }
191
192 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
193
194 int TestExpert_Init(Tcl_Interp *interp){
195 #ifndef SQLITE_OMIT_VIRTUALTABLE
196 struct Cmd {
197 const char *zCmd;
198 Tcl_ObjCmdProc *xProc;
199 } aCmd[] = {
200 { "sqlite3_expert_new", test_sqlite3_expert_new },
201 };
202 int i;
203
204 for(i=0; i<sizeof(aCmd)/sizeof(struct Cmd); i++){
205 struct Cmd *p = &aCmd[i];
206 Tcl_CreateObjCommand(interp, p->zCmd, p->xProc, 0, 0);
207 }
208 #endif
209 return TCL_OK;
210 }
211
212 #endif