11#include <stdio.h>
22#include <sqlite3_ruby.h>
33
4- #undef ENABLE_TRACE
5-
6- #ifdef ENABLE_TRACE
7- static FILE * pf ;
8- # define TRACE (str ) \
9- fprintf(pf, "%s:%d:%s\n", __FILE__, __LINE__, str); \
10- fflush(pf);
11- #else
12- # define TRACE (str ) ;
13- #endif
14-
154VALUE cSqlite3Module ;
165
176/** structure for ruby virtual table: inherits from sqlite3_vtab */
@@ -46,8 +35,6 @@ static int xCreate(sqlite3* db, VALUE *module_name,
4635 VALUE ruby_class_args [0 ];
4736 const char * module_name_cstr = (const char * )StringValuePtr (* module_name );
4837 const char * table_name_cstr = (const char * )argv [2 ];
49- TRACE ("xCreate" );
50-
5138
5239 // lookup for ruby class named like <module_id>::<table_name>
5340 module_id = rb_intern ( module_name_cstr );
@@ -73,7 +60,6 @@ static int xCreate(sqlite3* db, VALUE *module_name,
7360 if ( sqlite3_declare_vtab (db , StringValuePtr (sql_stmt )) )
7461 rb_raise (rb_eArgError , "fail to declare virtual table" );
7562
76- TRACE ("xCreate done" );
7763 return SQLITE_OK ;
7864}
7965
@@ -82,32 +68,27 @@ static int xConnect(sqlite3* db, void *pAux,
8268 ruby_sqlite3_vtab * * ppVTab ,
8369 char * * pzErr )
8470{
85- TRACE ("xConnect" );
8671 return xCreate (db , pAux , argc , argv , ppVTab , pzErr );
8772}
8873
8974static int xBestIndex (ruby_sqlite3_vtab * pVTab , sqlite3_index_info * info )
9075{
91- TRACE ("xBestIndex" );
9276 return SQLITE_OK ;
9377}
9478
9579static int xDestroy (ruby_sqlite3_vtab * pVTab )
9680{
97- TRACE ("xDestroy" );
9881 free (pVTab );
9982 return SQLITE_OK ;
10083}
10184
10285static int xDisconnect (ruby_sqlite3_vtab * pVTab )
10386{
104- TRACE ("xDisconnect" );
10587 return xDestroy (pVTab );
10688}
10789
10890static int xOpen (ruby_sqlite3_vtab * pVTab , ruby_sqlite3_vtab_cursor * * ppCursor )
10991{
110- TRACE ("xOpen" );
11192 rb_funcall ( pVTab -> vtable , rb_intern ("open" ), 0 );
11293 * ppCursor = (ruby_sqlite3_vtab_cursor * )malloc (sizeof (ruby_sqlite3_vtab_cursor ));
11394 (* ppCursor )-> pVTab = pVTab ;
@@ -117,15 +98,13 @@ static int xOpen(ruby_sqlite3_vtab *pVTab, ruby_sqlite3_vtab_cursor **ppCursor)
11798
11899static int xClose (ruby_sqlite3_vtab_cursor * cursor )
119100{
120- TRACE ("xClose" );
121101 rb_funcall ( cursor -> pVTab -> vtable , rb_intern ("close" ), 0 );
122102 free (cursor );
123103 return SQLITE_OK ;
124104}
125105
126106static int xNext (ruby_sqlite3_vtab_cursor * cursor )
127107{
128- TRACE ("xNext" );
129108 cursor -> row = rb_funcall (cursor -> pVTab -> vtable , rb_intern ("next" ), 0 );
130109 ++ (cursor -> rowid );
131110 return SQLITE_OK ;
@@ -134,29 +113,25 @@ static int xNext(ruby_sqlite3_vtab_cursor* cursor)
134113static int xFilter (ruby_sqlite3_vtab_cursor * cursor , int idxNum , const char * idxStr ,
135114 int argc , sqlite3_value * * argv )
136115{
137- TRACE ("xFilter" );
138116 cursor -> rowid = 0 ;
139117 return xNext (cursor );
140118}
141119
142120static int xEof (ruby_sqlite3_vtab_cursor * cursor )
143121{
144- TRACE ("xEof" );
145122 return (cursor -> row == Qnil );
146123}
147124
148125static int xColumn (ruby_sqlite3_vtab_cursor * cursor , sqlite3_context * context , int i )
149126{
150127 VALUE val = rb_ary_entry (cursor -> row , i );
151- TRACE ("xColumn(%d)" );
152128
153129 set_sqlite3_func_result (context , val );
154130 return SQLITE_OK ;
155131}
156132
157133static int xRowid (ruby_sqlite3_vtab_cursor * cursor , sqlite_int64 * pRowid )
158134{
159- TRACE ("xRowid" );
160135 * pRowid = cursor -> rowid ;
161136 return SQLITE_OK ;
162137}
@@ -228,15 +203,11 @@ static VALUE initialize(VALUE self, VALUE db, VALUE name)
228203 & (ctx -> module_name ) //the vtable required the module name
229204 );
230205
231- TRACE ("module initialized" );
232206 return self ;
233207}
234208
235209void init_sqlite3_module ()
236210{
237- #ifdef ENABLE_TRACE
238- pf = fopen ("trace.log" , "w" );
239- #endif
240211 cSqlite3Module = rb_define_class_under (mSqlite3 , "Module" , rb_cObject );
241212 rb_define_alloc_func (cSqlite3Module , allocate );
242213 rb_define_method (cSqlite3Module , "initialize" , initialize , 2 );
0 commit comments