amalgalite 1.6.0-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +49 -0
- data/HISTORY.md +346 -0
- data/LICENSE +31 -0
- data/Manifest.txt +104 -0
- data/README.md +65 -0
- data/Rakefile +26 -0
- data/TODO.md +57 -0
- data/bin/amalgalite-pack +147 -0
- data/examples/a.rb +9 -0
- data/examples/blob.rb +88 -0
- data/examples/bootstrap.rb +36 -0
- data/examples/define_aggregate.rb +75 -0
- data/examples/define_function.rb +104 -0
- data/examples/fts5.rb +152 -0
- data/examples/gem-db.rb +94 -0
- data/examples/require_me.rb +11 -0
- data/examples/requires.rb +42 -0
- data/examples/schema-info.rb +34 -0
- data/ext/amalgalite/c/amalgalite.c +355 -0
- data/ext/amalgalite/c/amalgalite.h +151 -0
- data/ext/amalgalite/c/amalgalite_blob.c +240 -0
- data/ext/amalgalite/c/amalgalite_constants.c +1226 -0
- data/ext/amalgalite/c/amalgalite_database.c +1178 -0
- data/ext/amalgalite/c/amalgalite_requires_bootstrap.c +282 -0
- data/ext/amalgalite/c/amalgalite_statement.c +649 -0
- data/ext/amalgalite/c/extconf.rb +62 -0
- data/ext/amalgalite/c/gen_constants.rb +330 -0
- data/ext/amalgalite/c/notes.txt +134 -0
- data/ext/amalgalite/c/sqlite3.c +205352 -0
- data/ext/amalgalite/c/sqlite3.h +10727 -0
- data/ext/amalgalite/c/sqlite3_options.h +4 -0
- data/ext/amalgalite/c/sqlite3ext.h +578 -0
- data/lib/amalgalite.rb +51 -0
- data/lib/amalgalite/2.0/amalgalite.so +0 -0
- data/lib/amalgalite/2.1/amalgalite.so +0 -0
- data/lib/amalgalite/2.2/amalgalite.so +0 -0
- data/lib/amalgalite/2.3/amalgalite.so +0 -0
- data/lib/amalgalite/2.4/amalgalite.so +0 -0
- data/lib/amalgalite/aggregate.rb +67 -0
- data/lib/amalgalite/blob.rb +186 -0
- data/lib/amalgalite/boolean.rb +42 -0
- data/lib/amalgalite/busy_timeout.rb +47 -0
- data/lib/amalgalite/column.rb +99 -0
- data/lib/amalgalite/core_ext/kernel/require.rb +21 -0
- data/lib/amalgalite/csv_table_importer.rb +74 -0
- data/lib/amalgalite/database.rb +984 -0
- data/lib/amalgalite/function.rb +61 -0
- data/lib/amalgalite/index.rb +43 -0
- data/lib/amalgalite/memory_database.rb +15 -0
- data/lib/amalgalite/packer.rb +231 -0
- data/lib/amalgalite/paths.rb +80 -0
- data/lib/amalgalite/profile_tap.rb +131 -0
- data/lib/amalgalite/progress_handler.rb +21 -0
- data/lib/amalgalite/requires.rb +151 -0
- data/lib/amalgalite/schema.rb +225 -0
- data/lib/amalgalite/sqlite3.rb +6 -0
- data/lib/amalgalite/sqlite3/constants.rb +95 -0
- data/lib/amalgalite/sqlite3/database/function.rb +48 -0
- data/lib/amalgalite/sqlite3/database/status.rb +68 -0
- data/lib/amalgalite/sqlite3/status.rb +60 -0
- data/lib/amalgalite/sqlite3/version.rb +55 -0
- data/lib/amalgalite/statement.rb +418 -0
- data/lib/amalgalite/table.rb +91 -0
- data/lib/amalgalite/taps.rb +2 -0
- data/lib/amalgalite/taps/console.rb +27 -0
- data/lib/amalgalite/taps/io.rb +71 -0
- data/lib/amalgalite/trace_tap.rb +35 -0
- data/lib/amalgalite/type_map.rb +63 -0
- data/lib/amalgalite/type_maps/default_map.rb +166 -0
- data/lib/amalgalite/type_maps/storage_map.rb +38 -0
- data/lib/amalgalite/type_maps/text_map.rb +21 -0
- data/lib/amalgalite/version.rb +8 -0
- data/lib/amalgalite/view.rb +26 -0
- data/spec/aggregate_spec.rb +154 -0
- data/spec/amalgalite_spec.rb +4 -0
- data/spec/blob_spec.rb +78 -0
- data/spec/boolean_spec.rb +24 -0
- data/spec/busy_handler.rb +157 -0
- data/spec/data/iso-3166-country.txt +242 -0
- data/spec/data/iso-3166-schema.sql +22 -0
- data/spec/data/iso-3166-subcountry.txt +3995 -0
- data/spec/data/make-iso-db.sh +12 -0
- data/spec/database_spec.rb +508 -0
- data/spec/default_map_spec.rb +92 -0
- data/spec/function_spec.rb +78 -0
- data/spec/integeration_spec.rb +97 -0
- data/spec/iso_3166_database.rb +58 -0
- data/spec/packer_spec.rb +60 -0
- data/spec/paths_spec.rb +28 -0
- data/spec/progress_handler_spec.rb +91 -0
- data/spec/requires_spec.rb +54 -0
- data/spec/rtree_spec.rb +66 -0
- data/spec/schema_spec.rb +131 -0
- data/spec/spec_helper.rb +48 -0
- data/spec/sqlite3/constants_spec.rb +108 -0
- data/spec/sqlite3/database_status_spec.rb +36 -0
- data/spec/sqlite3/status_spec.rb +22 -0
- data/spec/sqlite3/version_spec.rb +28 -0
- data/spec/sqlite3_spec.rb +53 -0
- data/spec/statement_spec.rb +168 -0
- data/spec/storage_map_spec.rb +38 -0
- data/spec/tap_spec.rb +57 -0
- data/spec/text_map_spec.rb +20 -0
- data/spec/type_map_spec.rb +14 -0
- data/spec/version_spec.rb +8 -0
- data/tasks/custom.rake +102 -0
- data/tasks/default.rake +240 -0
- data/tasks/extension.rake +38 -0
- data/tasks/this.rb +208 -0
- metadata +318 -0
@@ -0,0 +1,282 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
* All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
*
|
5
|
+
* vim: shiftwidth=4
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include "amalgalite.h"
|
9
|
+
#include <stdio.h>
|
10
|
+
extern VALUE mA;
|
11
|
+
VALUE cAR;
|
12
|
+
VALUE cARB;
|
13
|
+
VALUE eARB_Error;
|
14
|
+
|
15
|
+
/*
|
16
|
+
* cleanup the datatbase and statment values if they are currently open and then
|
17
|
+
* raise the message. It is assumed that the *msg pointer passed in is NOT
|
18
|
+
* allocated via malloc() or the like. It should be a local static buffer
|
19
|
+
* that we do not have to worry about freeing.
|
20
|
+
*/
|
21
|
+
void am_bootstrap_cleanup_and_raise( const char* msg, sqlite3* db, sqlite3_stmt* stmt )
|
22
|
+
{
|
23
|
+
|
24
|
+
if ( NULL != stmt ) { sqlite3_finalize( stmt ); stmt = NULL; }
|
25
|
+
if ( NULL != db ) { sqlite3_close( db ); }
|
26
|
+
|
27
|
+
rb_raise(eARB_Error, "%s", msg );
|
28
|
+
}
|
29
|
+
|
30
|
+
|
31
|
+
void am_bootstrap_from_db( sqlite3* db, VALUE args )
|
32
|
+
{
|
33
|
+
sqlite3_stmt* stmt = NULL;
|
34
|
+
int rc;
|
35
|
+
char raise_msg[BUFSIZ];
|
36
|
+
int last_row_good;
|
37
|
+
|
38
|
+
VALUE am_tbl_c = rb_const_get( cARB, rb_intern("DEFAULT_BOOTSTRAP_TABLE") );
|
39
|
+
VALUE am_pk_c = rb_const_get( cARB, rb_intern("DEFAULT_ROWID_COLUMN") );
|
40
|
+
VALUE am_fname_c = rb_const_get( cARB, rb_intern("DEFAULT_FILENAME_COLUMN") );
|
41
|
+
VALUE am_content_c = rb_const_get( cARB, rb_intern("DEFAULT_CONTENTS_COLUMN") );
|
42
|
+
|
43
|
+
char* tbl_name = NULL;
|
44
|
+
char* pk_col = NULL;
|
45
|
+
char* fname_col = NULL;
|
46
|
+
char* content_col = NULL;
|
47
|
+
|
48
|
+
|
49
|
+
char sql[BUFSIZ];
|
50
|
+
const char* sql_tail = NULL;
|
51
|
+
int sql_bytes = 0;
|
52
|
+
|
53
|
+
const unsigned char* result_text = NULL;
|
54
|
+
int result_length = 0;
|
55
|
+
|
56
|
+
VALUE require_name = Qnil; /* ruby string of the file name for use in eval */
|
57
|
+
VALUE eval_this_code = Qnil; /* ruby string of the code to eval from the db */
|
58
|
+
VALUE toplevel_binding = rb_const_get( rb_cObject, rb_intern("TOPLEVEL_BINDING") ) ;
|
59
|
+
VALUE tmp = Qnil;
|
60
|
+
|
61
|
+
ID eval_id = rb_intern("eval");
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
tbl_name = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "table_name" ) ) ) ) ? StringValuePtr( am_tbl_c ) : StringValuePtr( tmp );
|
66
|
+
pk_col = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "rowid_column" ) ) ) ) ? StringValuePtr( am_pk_c ) : StringValuePtr( tmp );
|
67
|
+
fname_col = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "filename_column" ) ) ) ) ? StringValuePtr( am_fname_c ) : StringValuePtr( tmp );
|
68
|
+
content_col = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "contents_column" ) ) ) ) ? StringValuePtr( am_content_c ) : StringValuePtr( tmp );
|
69
|
+
|
70
|
+
|
71
|
+
/* prepare the db query */
|
72
|
+
memset( sql, 0, BUFSIZ );
|
73
|
+
sql_bytes = snprintf( sql, BUFSIZ, "SELECT %s, %s FROM %s ORDER BY %s", fname_col, content_col, tbl_name, pk_col );
|
74
|
+
rc = sqlite3_prepare_v2( db, sql, sql_bytes, &stmt, &sql_tail ) ;
|
75
|
+
if ( SQLITE_OK != rc) {
|
76
|
+
memset( raise_msg, 0, BUFSIZ );
|
77
|
+
snprintf( raise_msg, BUFSIZ,
|
78
|
+
"Failure to prepare bootload select statement table = '%s', rowid col = '%s', filename col ='%s', contents col = '%s' : [SQLITE_ERROR %d] %s\n",
|
79
|
+
tbl_name, pk_col, fname_col, content_col, rc, sqlite3_errmsg( db ));
|
80
|
+
am_bootstrap_cleanup_and_raise( raise_msg, db, stmt );
|
81
|
+
}
|
82
|
+
|
83
|
+
/* loop over the resulting rows, eval'ing and loading $LOADED_FEATURES */
|
84
|
+
last_row_good = -1;
|
85
|
+
while ( SQLITE_ROW == ( rc = sqlite3_step( stmt ) ) ) {
|
86
|
+
/* file name */
|
87
|
+
result_text = sqlite3_column_text( stmt, 0 );
|
88
|
+
result_length = sqlite3_column_bytes( stmt, 0 );
|
89
|
+
require_name = rb_str_new( (const char*)result_text, result_length );
|
90
|
+
|
91
|
+
/* ruby code */
|
92
|
+
result_text = sqlite3_column_text( stmt, 1 );
|
93
|
+
result_length = sqlite3_column_bytes( stmt, 1 );
|
94
|
+
eval_this_code = rb_str_new( (const char*)result_text, result_length );
|
95
|
+
|
96
|
+
/* Kernel.eval( code, TOPLEVEL_BINDING, filename, 1 ) */
|
97
|
+
rb_funcall(rb_mKernel, eval_id, 4, eval_this_code, toplevel_binding, require_name, INT2FIX(1) );
|
98
|
+
|
99
|
+
/* TODO: for ruby 1.9 -- put in ? sqlite3://path/to/database?tablename=tbl_name#require_name */
|
100
|
+
/* update $LOADED_FEATURES */
|
101
|
+
rb_ary_push( rb_gv_get( "$LOADED_FEATURES" ), require_name );
|
102
|
+
}
|
103
|
+
|
104
|
+
/* if there was some sqlite error in the processing of the rows */
|
105
|
+
if ( SQLITE_DONE != rc ) {
|
106
|
+
memset( raise_msg, 0, BUFSIZ );
|
107
|
+
snprintf( raise_msg, BUFSIZ, "Failure in bootloading, last successfully loaded rowid was %d : [SQLITE_ERROR %d] %s\n",
|
108
|
+
last_row_good, rc, sqlite3_errmsg( db ) );
|
109
|
+
am_bootstrap_cleanup_and_raise( raise_msg, db, stmt );
|
110
|
+
}
|
111
|
+
|
112
|
+
/* finalize the statement */
|
113
|
+
rc = sqlite3_finalize( stmt );
|
114
|
+
if ( SQLITE_OK != rc ) {
|
115
|
+
memset( raise_msg, 0, BUFSIZ );
|
116
|
+
snprintf( raise_msg, BUFSIZ, "Failure to finalize bootload statement : [SQLITE_ERROR %d] %s\n", rc, sqlite3_errmsg( db ) );
|
117
|
+
am_bootstrap_cleanup_and_raise( raise_msg, db, stmt );
|
118
|
+
}
|
119
|
+
|
120
|
+
}
|
121
|
+
|
122
|
+
|
123
|
+
/**
|
124
|
+
* call-seq:
|
125
|
+
* Amalgalite::Requires::Bootstrap.lift( 'dbfile' => "lib.db", 'table_name' => "bootload", 'rowid_column' => "id", 'filename_column' => "filename", 'content_column' => "contents" )
|
126
|
+
*
|
127
|
+
* *WARNING* *WARNING* *WARNING* *WARNING* *WARNING* *WARNING* *WARNING*
|
128
|
+
*
|
129
|
+
* This is a boostrap mechanism to eval all the code in a particular column in a
|
130
|
+
* specially formatted table in an sqlite database. It should only be used for
|
131
|
+
* a specific purpose, mainly loading the Amalgalite ruby code directly from an
|
132
|
+
* sqlite table.
|
133
|
+
*
|
134
|
+
* Amalgalite::Requires adds in the ability to _require_ code that is in an
|
135
|
+
* sqlite database. Since Amalgalite::Requires is itself ruby code, if
|
136
|
+
* Amalgalite::Requires was in an sqlite database, it could not _require_
|
137
|
+
* itself. Therefore this method is made available. It is a pure C extension
|
138
|
+
* method that directly calls the sqlite3 C functions directly and uses the ruby
|
139
|
+
* C api to eval the data in the table.
|
140
|
+
*
|
141
|
+
* This method attaches to an sqlite3 database (filename) and then does:
|
142
|
+
*
|
143
|
+
* SELECT filename_column_name, content_column_name
|
144
|
+
* FROM table_name
|
145
|
+
* ORDER BY rowid_column_name
|
146
|
+
*
|
147
|
+
* For each row returned it does an _eval_ on the code in the
|
148
|
+
* *content_column_name* and then updates _$LOADED_FEATURES_ directly with the value from
|
149
|
+
* *filename_column_name*.
|
150
|
+
*
|
151
|
+
* The database to be opened by _lift_ *must* be an sqlite3 UTF-8 database.
|
152
|
+
*
|
153
|
+
*/
|
154
|
+
VALUE am_bootstrap_lift( VALUE self, VALUE args )
|
155
|
+
{
|
156
|
+
sqlite3 *db = NULL;
|
157
|
+
int rc;
|
158
|
+
char raise_msg[BUFSIZ];
|
159
|
+
VALUE tmp = Qnil;
|
160
|
+
|
161
|
+
VALUE am_db_c = rb_const_get( cARB, rb_intern("DEFAULT_DB") );
|
162
|
+
|
163
|
+
char *dbfile = NULL;
|
164
|
+
|
165
|
+
|
166
|
+
if ( Qnil == args ) {
|
167
|
+
args = rb_hash_new();
|
168
|
+
} else {
|
169
|
+
args = rb_ary_shift( args );
|
170
|
+
}
|
171
|
+
|
172
|
+
Check_Type( args, T_HASH );
|
173
|
+
|
174
|
+
/* get the arguments */
|
175
|
+
dbfile = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "dbfile" ) ) ) ) ? StringValuePtr( am_db_c ) : StringValuePtr( tmp );
|
176
|
+
|
177
|
+
/* open the database */
|
178
|
+
rc = sqlite3_open_v2( dbfile , &db, SQLITE_OPEN_READONLY, NULL);
|
179
|
+
if ( SQLITE_OK != rc ) {
|
180
|
+
memset( raise_msg, 0, BUFSIZ );
|
181
|
+
snprintf(raise_msg, BUFSIZ, "Failure to open database %s for bootload: [SQLITE_ERROR %d] : %s", dbfile, rc, sqlite3_errmsg( db ) );
|
182
|
+
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
|
183
|
+
}
|
184
|
+
|
185
|
+
am_bootstrap_from_db( db, args );
|
186
|
+
|
187
|
+
/* close the database */
|
188
|
+
rc = sqlite3_close( db );
|
189
|
+
if ( SQLITE_OK != rc ) {
|
190
|
+
memset( raise_msg, 0, BUFSIZ );
|
191
|
+
snprintf( raise_msg, BUFSIZ, "Failure to close database : [SQLITE_ERROR %d] : %s\n", rc, sqlite3_errmsg( db )),
|
192
|
+
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
|
193
|
+
}
|
194
|
+
|
195
|
+
return Qnil;
|
196
|
+
}
|
197
|
+
|
198
|
+
/**
|
199
|
+
* call-seq:
|
200
|
+
* Amalgalite::Requires::Bootstrap.lift_str( sql, 'table_name' => "bootload", 'rowid_column' => "id", 'filename_column' => "filename", 'content_column' => "contents" )
|
201
|
+
*
|
202
|
+
* Bootstrap Amalgalite from a string containing an SQL dump. See Amalgalite::Requires::Bootstrap.lift.
|
203
|
+
*
|
204
|
+
* For example:
|
205
|
+
*
|
206
|
+
* Amalgalite::Requires::Bootstrap.lifts(File.read("lib.sql"))
|
207
|
+
*/
|
208
|
+
VALUE am_bootstrap_lift_str( VALUE self, VALUE args )
|
209
|
+
{
|
210
|
+
sqlite3 *db = NULL;
|
211
|
+
int rc;
|
212
|
+
char raise_msg[BUFSIZ];
|
213
|
+
VALUE sql = Qnil;
|
214
|
+
VALUE args_hsh = Qnil;
|
215
|
+
|
216
|
+
sql = rb_ary_shift(args);
|
217
|
+
StringValue(sql);
|
218
|
+
if ( Qnil == sql ) { rb_raise(eARB_Error, "SQL required." ); }
|
219
|
+
|
220
|
+
args_hsh = rb_ary_shift(args);
|
221
|
+
if ( Qnil == args_hsh ) { args_hsh = rb_hash_new(); }
|
222
|
+
|
223
|
+
|
224
|
+
rc = sqlite3_open_v2( ":memory:", &db, SQLITE_OPEN_READWRITE, NULL );
|
225
|
+
if ( SQLITE_OK != rc ) {
|
226
|
+
memset( raise_msg, 0, BUFSIZ );
|
227
|
+
snprintf(raise_msg, BUFSIZ, "Failure to open database :memory: for bootload: [SQLITE_ERROR %d] : %s", rc, sqlite3_errmsg( db ) );
|
228
|
+
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
|
229
|
+
}
|
230
|
+
|
231
|
+
/* Load the bootstrap SQL into the database */
|
232
|
+
rc = sqlite3_exec( db, StringValuePtr( sql ), NULL, NULL, NULL );
|
233
|
+
|
234
|
+
if ( SQLITE_OK != rc ) {
|
235
|
+
memset( raise_msg, 0, BUFSIZ );
|
236
|
+
snprintf(raise_msg, BUFSIZ, "Failure to import bootload sql: [SQLITE_ERROR %d] : %s", rc, sqlite3_errmsg( db ) );
|
237
|
+
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
|
238
|
+
}
|
239
|
+
|
240
|
+
am_bootstrap_from_db( db, args_hsh );
|
241
|
+
|
242
|
+
rc = sqlite3_close( db );
|
243
|
+
|
244
|
+
if ( SQLITE_OK != rc ) {
|
245
|
+
memset( raise_msg, 0, BUFSIZ );
|
246
|
+
snprintf( raise_msg, BUFSIZ, "Failure to close database : [SQLITE_ERROR %d] : %s\n", rc, sqlite3_errmsg( db )),
|
247
|
+
am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
|
248
|
+
}
|
249
|
+
|
250
|
+
return Qnil;
|
251
|
+
|
252
|
+
}
|
253
|
+
|
254
|
+
|
255
|
+
/**
|
256
|
+
* Bootstrapping module to help _require_ when Amalgalite::Requires is not
|
257
|
+
* availble in files.
|
258
|
+
*/
|
259
|
+
void Init_amalgalite_requires_bootstrap()
|
260
|
+
{
|
261
|
+
|
262
|
+
mA = rb_define_module("Amalgalite");
|
263
|
+
cAR = rb_define_class_under(mA, "Requires", rb_cObject);
|
264
|
+
cARB = rb_define_class_under(cAR, "Bootstrap", rb_cObject);
|
265
|
+
|
266
|
+
eARB_Error = rb_define_class_under(cARB, "Error", rb_eStandardError);
|
267
|
+
|
268
|
+
rb_define_module_function(cARB, "lift", am_bootstrap_lift, -2);
|
269
|
+
rb_define_module_function(cARB, "lifts", am_bootstrap_lift, -2);
|
270
|
+
|
271
|
+
/* constants for default db, table, column, rowid, contents */
|
272
|
+
rb_define_const(cARB, "DEFAULT_DB", rb_str_new2( "lib.db" ));
|
273
|
+
rb_define_const(cARB, "DEFAULT_TABLE", rb_str_new2( "rubylibs" ));
|
274
|
+
rb_define_const(cARB, "DEFAULT_BOOTSTRAP_TABLE", rb_str_new2( "bootstrap" ));
|
275
|
+
rb_define_const(cARB, "DEFAULT_ROWID_COLUMN", rb_str_new2( "id" ));
|
276
|
+
rb_define_const(cARB, "DEFAULT_FILENAME_COLUMN", rb_str_new2( "filename" ));
|
277
|
+
rb_define_const(cARB, "DEFAULT_CONTENTS_COLUMN", rb_str_new2( "contents" ));
|
278
|
+
rb_define_const(cARB, "DEFAULT_COMPRESSED_COLUMN", rb_str_new2( "compressed" ));
|
279
|
+
|
280
|
+
return;
|
281
|
+
}
|
282
|
+
|
@@ -0,0 +1,649 @@
|
|
1
|
+
#include "amalgalite.h"
|
2
|
+
/**
|
3
|
+
* Copyright (c) 2008 Jeremy Hinegardner
|
4
|
+
* All rights reserved. See LICENSE and/or COPYING for details.
|
5
|
+
*
|
6
|
+
* vim: shiftwidth=4
|
7
|
+
*/
|
8
|
+
|
9
|
+
VALUE cAS_Statement; /* class Amalgliate::SQLite3::Statement */
|
10
|
+
|
11
|
+
/**
|
12
|
+
* call-seq:
|
13
|
+
* stmt.bind_null( position ) -> int
|
14
|
+
*
|
15
|
+
* bind a null value to the variable at postion.
|
16
|
+
*
|
17
|
+
*/
|
18
|
+
VALUE am_sqlite3_statement_bind_null(VALUE self, VALUE position )
|
19
|
+
{
|
20
|
+
am_sqlite3_stmt *am_stmt;
|
21
|
+
int pos = FIX2INT( position );
|
22
|
+
int rc;
|
23
|
+
|
24
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
25
|
+
rc = sqlite3_bind_null( am_stmt->stmt, pos );
|
26
|
+
if ( SQLITE_OK != rc ) {
|
27
|
+
rb_raise(eAS_Error, "Error binding NULL at position %d in statement: [SQLITE_ERROR %d] : %s\n",
|
28
|
+
pos,
|
29
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
30
|
+
}
|
31
|
+
|
32
|
+
return INT2FIX(rc);
|
33
|
+
}
|
34
|
+
|
35
|
+
/**
|
36
|
+
* call-seq:
|
37
|
+
* stmt.bind_zeroblob( position, length ) -> int
|
38
|
+
*
|
39
|
+
* bind a blob with +length+ filled with zeros to the position. This is a Blob
|
40
|
+
* that will later filled in with incremental IO routines.
|
41
|
+
*/
|
42
|
+
VALUE am_sqlite3_statement_bind_zeroblob( VALUE self, VALUE position, VALUE length)
|
43
|
+
{
|
44
|
+
am_sqlite3_stmt *am_stmt;
|
45
|
+
int pos = FIX2INT( position );
|
46
|
+
int n = (int)FIX2INT( length );
|
47
|
+
int rc;
|
48
|
+
|
49
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
50
|
+
rc = sqlite3_bind_zeroblob( am_stmt->stmt, pos, n );
|
51
|
+
if ( SQLITE_OK != rc ) {
|
52
|
+
rb_raise(eAS_Error, "Error binding zeroblob of length %d at position %d in statement: [SQLITE_ERROR %d] : %s\n",
|
53
|
+
n, pos,
|
54
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
55
|
+
}
|
56
|
+
|
57
|
+
return INT2FIX(rc);
|
58
|
+
}
|
59
|
+
|
60
|
+
|
61
|
+
/**
|
62
|
+
* call-seq:
|
63
|
+
* stmt.bind_blob( position, blob ) -> int
|
64
|
+
*
|
65
|
+
* bind a blob to the variable at position. This is a blob that is fully held
|
66
|
+
* in memory
|
67
|
+
*/
|
68
|
+
VALUE am_sqlite3_statement_bind_blob( VALUE self, VALUE position, VALUE blob )
|
69
|
+
{
|
70
|
+
am_sqlite3_stmt *am_stmt;
|
71
|
+
int pos = FIX2INT( position );
|
72
|
+
VALUE str = StringValue( blob );
|
73
|
+
int rc;
|
74
|
+
|
75
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
76
|
+
rc = sqlite3_bind_blob( am_stmt->stmt, pos, RSTRING_PTR( str ), (int)RSTRING_LEN( str ), SQLITE_TRANSIENT);
|
77
|
+
if ( SQLITE_OK != rc ) {
|
78
|
+
rb_raise(eAS_Error, "Error binding blob at position %d in statement: [SQLITE_ERROR %d] : %s\n",
|
79
|
+
pos,
|
80
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
81
|
+
}
|
82
|
+
|
83
|
+
return INT2FIX(rc);
|
84
|
+
}
|
85
|
+
|
86
|
+
/**
|
87
|
+
* call-seq:
|
88
|
+
* stmt.bind_double( position, value ) -> nil
|
89
|
+
*
|
90
|
+
* bind a double value to the variable at postion.
|
91
|
+
*
|
92
|
+
*/
|
93
|
+
VALUE am_sqlite3_statement_bind_double(VALUE self, VALUE position, VALUE value)
|
94
|
+
{
|
95
|
+
am_sqlite3_stmt *am_stmt;
|
96
|
+
int pos = FIX2INT( position );
|
97
|
+
double v = NUM2DBL( value );
|
98
|
+
int rc;
|
99
|
+
|
100
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
101
|
+
rc = sqlite3_bind_double( am_stmt->stmt, pos, v );
|
102
|
+
if ( SQLITE_OK != rc ) {
|
103
|
+
rb_raise(eAS_Error, "Error binding [%lf] to double at position %d in statement: [SQLITE_ERROR %d] : %s\n",
|
104
|
+
v, pos,
|
105
|
+
rc, (char*)sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
106
|
+
}
|
107
|
+
|
108
|
+
return INT2FIX(rc);
|
109
|
+
}
|
110
|
+
|
111
|
+
/**
|
112
|
+
* call-seq:
|
113
|
+
* stmt.bind_int( position, value ) -> nil
|
114
|
+
*
|
115
|
+
* bind a int value to the variable at postion.
|
116
|
+
*
|
117
|
+
*/
|
118
|
+
VALUE am_sqlite3_statement_bind_int(VALUE self, VALUE position, VALUE value)
|
119
|
+
{
|
120
|
+
am_sqlite3_stmt *am_stmt;
|
121
|
+
int pos = FIX2INT( position );
|
122
|
+
int v = NUM2INT( value );
|
123
|
+
int rc;
|
124
|
+
|
125
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
126
|
+
rc = sqlite3_bind_int( am_stmt->stmt, pos, v );
|
127
|
+
if ( SQLITE_OK != rc ) {
|
128
|
+
rb_raise(eAS_Error, "Error binding [%d] to int at position %d in statement: [SQLITE_ERROR %d] : %s\n",
|
129
|
+
v, pos,
|
130
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
131
|
+
}
|
132
|
+
|
133
|
+
return INT2FIX(rc);
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
* call-seq:
|
138
|
+
* stmt.bind_int64( position, value ) -> nil
|
139
|
+
*
|
140
|
+
* bind a int64 value to the variable at postion.
|
141
|
+
*
|
142
|
+
*/
|
143
|
+
VALUE am_sqlite3_statement_bind_int64(VALUE self, VALUE position, VALUE value)
|
144
|
+
{
|
145
|
+
am_sqlite3_stmt *am_stmt;
|
146
|
+
int pos = FIX2INT( position );
|
147
|
+
sqlite3_int64 v = NUM2SQLINT64( value );
|
148
|
+
int rc;
|
149
|
+
|
150
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
151
|
+
rc = sqlite3_bind_int64( am_stmt->stmt, pos, v );
|
152
|
+
if ( SQLITE_OK != rc ) {
|
153
|
+
rb_raise(eAS_Error, "Error binding [%lld] to int64 at position %d in statement: [SQLITE_ERROR %d] : %s\n",
|
154
|
+
v, pos,
|
155
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
156
|
+
}
|
157
|
+
|
158
|
+
return INT2FIX(rc);
|
159
|
+
}
|
160
|
+
|
161
|
+
/**
|
162
|
+
* call-seq:
|
163
|
+
* stmt.bind_text( position, value ) -> nil
|
164
|
+
*
|
165
|
+
* bind a string value to the variable at postion.
|
166
|
+
*
|
167
|
+
*/
|
168
|
+
VALUE am_sqlite3_statement_bind_text(VALUE self, VALUE position, VALUE value)
|
169
|
+
{
|
170
|
+
am_sqlite3_stmt *am_stmt;
|
171
|
+
int pos = FIX2INT( position );
|
172
|
+
VALUE str = StringValue( value );
|
173
|
+
int rc;
|
174
|
+
|
175
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
176
|
+
rc = sqlite3_bind_text( am_stmt->stmt, pos, RSTRING_PTR(str), (int)RSTRING_LEN(str), SQLITE_TRANSIENT);
|
177
|
+
if ( SQLITE_OK != rc ) {
|
178
|
+
rb_raise(eAS_Error, "Error binding [%s] to text at position %d in statement: [SQLITE_ERROR %d] : %s\n",
|
179
|
+
RSTRING_PTR(str), pos,
|
180
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
181
|
+
}
|
182
|
+
|
183
|
+
return INT2FIX(rc);
|
184
|
+
}
|
185
|
+
/**
|
186
|
+
* call-seq:
|
187
|
+
* stmt.remaining_sql -> String
|
188
|
+
*
|
189
|
+
* returns the remainging SQL leftover from the initialization sql, or nil if
|
190
|
+
* there is no remaining SQL
|
191
|
+
*/
|
192
|
+
VALUE am_sqlite3_statement_remaining_sql(VALUE self)
|
193
|
+
{
|
194
|
+
am_sqlite3_stmt *am_stmt;
|
195
|
+
|
196
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
197
|
+
return am_stmt->remaining_sql;
|
198
|
+
}
|
199
|
+
/**
|
200
|
+
* call-seq:
|
201
|
+
* stmt.parameter_index( name ) -> Integer
|
202
|
+
*
|
203
|
+
* returns the index of the named parameter from the statement. Used to help
|
204
|
+
* with the :VVV, @VVV and $VVV pareamter replacement styles.
|
205
|
+
*/
|
206
|
+
VALUE am_sqlite3_statement_bind_parameter_index(VALUE self, VALUE parameter_name)
|
207
|
+
{
|
208
|
+
am_sqlite3_stmt *am_stmt;
|
209
|
+
int idx;
|
210
|
+
|
211
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
212
|
+
idx = sqlite3_bind_parameter_index( am_stmt->stmt, StringValuePtr( parameter_name ));
|
213
|
+
return INT2FIX( idx );
|
214
|
+
}
|
215
|
+
|
216
|
+
/**
|
217
|
+
* call-seq:
|
218
|
+
* stmt.parameter_count -> Integer
|
219
|
+
*
|
220
|
+
* return the index of the largest parameter in the in the statement. For all
|
221
|
+
* forms except ?NNN this is the number of unique parameters. Using ?NNN can
|
222
|
+
* create gaps in the list of parameters.
|
223
|
+
*
|
224
|
+
*/
|
225
|
+
VALUE am_sqlite3_statement_bind_parameter_count(VALUE self)
|
226
|
+
{
|
227
|
+
am_sqlite3_stmt *am_stmt;
|
228
|
+
|
229
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
230
|
+
return INT2FIX(sqlite3_bind_parameter_count( am_stmt->stmt ) );
|
231
|
+
}
|
232
|
+
|
233
|
+
/**
|
234
|
+
* call-seq:
|
235
|
+
* stmt.reset! -> nil
|
236
|
+
*
|
237
|
+
* reset the SQLite3 statement back to its initial state.
|
238
|
+
*/
|
239
|
+
VALUE am_sqlite3_statement_reset(VALUE self)
|
240
|
+
{
|
241
|
+
am_sqlite3_stmt *am_stmt;
|
242
|
+
int rc;
|
243
|
+
|
244
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
245
|
+
if ( am_stmt->stmt ) {
|
246
|
+
rc = sqlite3_reset( am_stmt->stmt );
|
247
|
+
if ( rc != SQLITE_OK ) {
|
248
|
+
rb_raise(eAS_Error, "Error resetting statement: [SQLITE_ERROR %d] : %s\n",
|
249
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
250
|
+
}
|
251
|
+
return Qnil;
|
252
|
+
} else {
|
253
|
+
rb_raise(eAS_Error, "Attempting to free a non-existent statement");
|
254
|
+
}
|
255
|
+
}
|
256
|
+
|
257
|
+
/**
|
258
|
+
* call-seq:
|
259
|
+
* stmt.clear_bindings! -> nil
|
260
|
+
*
|
261
|
+
* reset the SQLite3 statement back to its initial state.
|
262
|
+
*/
|
263
|
+
VALUE am_sqlite3_statement_clear_bindings(VALUE self)
|
264
|
+
{
|
265
|
+
am_sqlite3_stmt *am_stmt;
|
266
|
+
int rc;
|
267
|
+
|
268
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
269
|
+
rc = sqlite3_clear_bindings( am_stmt->stmt );
|
270
|
+
if ( rc != SQLITE_OK ) {
|
271
|
+
rb_raise(eAS_Error, "Error resetting statement: [SQLITE_ERROR %d] : %s\n",
|
272
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
273
|
+
}
|
274
|
+
return Qnil;
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
/**
|
279
|
+
* call-seq:
|
280
|
+
* stmt.step -> int
|
281
|
+
*
|
282
|
+
* Step through the next piece of the SQLite3 statement
|
283
|
+
*
|
284
|
+
*/
|
285
|
+
VALUE am_sqlite3_statement_step(VALUE self)
|
286
|
+
{
|
287
|
+
am_sqlite3_stmt *am_stmt;
|
288
|
+
|
289
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
290
|
+
return INT2FIX( sqlite3_step( am_stmt->stmt ) );
|
291
|
+
}
|
292
|
+
|
293
|
+
/**
|
294
|
+
* call-seq:
|
295
|
+
* stmt.column_count -> Fixnum
|
296
|
+
*
|
297
|
+
* return the number of columns in the result set.
|
298
|
+
*
|
299
|
+
*/
|
300
|
+
VALUE am_sqlite3_statement_column_count(VALUE self)
|
301
|
+
{
|
302
|
+
am_sqlite3_stmt *am_stmt;
|
303
|
+
|
304
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
305
|
+
return INT2FIX( sqlite3_column_count( am_stmt->stmt ) );
|
306
|
+
}
|
307
|
+
|
308
|
+
/**
|
309
|
+
* call-seq:
|
310
|
+
* stmt.column_name( index ) -> String
|
311
|
+
*
|
312
|
+
* Return the column name at the ith column in the result set. The left-most column
|
313
|
+
* is number 0.
|
314
|
+
*
|
315
|
+
*/
|
316
|
+
VALUE am_sqlite3_statement_column_name(VALUE self, VALUE v_idx)
|
317
|
+
{
|
318
|
+
am_sqlite3_stmt *am_stmt;
|
319
|
+
int idx = FIX2INT( v_idx );
|
320
|
+
|
321
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
322
|
+
|
323
|
+
return rb_str_new2( sqlite3_column_name( am_stmt->stmt, idx ) );
|
324
|
+
}
|
325
|
+
|
326
|
+
|
327
|
+
/**
|
328
|
+
* call-seq:
|
329
|
+
* stmt.column_declared_type( index ) -> String
|
330
|
+
*
|
331
|
+
* Return the declared type of the ith column in the result set. This is the
|
332
|
+
* value that was in the original CREATE TABLE statement.
|
333
|
+
*
|
334
|
+
*/
|
335
|
+
VALUE am_sqlite3_statement_column_decltype(VALUE self, VALUE v_idx)
|
336
|
+
{
|
337
|
+
am_sqlite3_stmt *am_stmt;
|
338
|
+
int idx = FIX2INT( v_idx );
|
339
|
+
const char *decltype;
|
340
|
+
|
341
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
342
|
+
decltype = sqlite3_column_decltype( am_stmt->stmt, idx ) ;
|
343
|
+
if ( NULL == decltype) {
|
344
|
+
return Qnil;
|
345
|
+
} else {
|
346
|
+
return rb_str_new2( decltype );
|
347
|
+
}
|
348
|
+
}
|
349
|
+
|
350
|
+
/**
|
351
|
+
* call-seq:
|
352
|
+
* stmt.column_type( index ) -> SQLite3::DataType constant
|
353
|
+
*
|
354
|
+
* Return the column type at the ith column in the result set. The left-most column
|
355
|
+
* is number 0.
|
356
|
+
*
|
357
|
+
*/
|
358
|
+
VALUE am_sqlite3_statement_column_type(VALUE self, VALUE v_idx)
|
359
|
+
{
|
360
|
+
am_sqlite3_stmt *am_stmt;
|
361
|
+
int idx = FIX2INT( v_idx );
|
362
|
+
|
363
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
364
|
+
return INT2FIX( sqlite3_column_type( am_stmt->stmt, idx ) );
|
365
|
+
}
|
366
|
+
|
367
|
+
/**
|
368
|
+
* call-seq:
|
369
|
+
* stmt.column_text( index ) -> String
|
370
|
+
*
|
371
|
+
* Return the data in ith column of the result as a String.
|
372
|
+
*
|
373
|
+
*/
|
374
|
+
VALUE am_sqlite3_statement_column_text(VALUE self, VALUE v_idx)
|
375
|
+
{
|
376
|
+
am_sqlite3_stmt *am_stmt;
|
377
|
+
int idx = FIX2INT( v_idx );
|
378
|
+
|
379
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
380
|
+
return rb_str_new2( (const char*)sqlite3_column_text( am_stmt->stmt, idx ) );
|
381
|
+
}
|
382
|
+
|
383
|
+
/**
|
384
|
+
* call-seq:
|
385
|
+
* stmt.column_blob( index ) -> String
|
386
|
+
*
|
387
|
+
* Return the data in ith column of the result as a String.
|
388
|
+
*
|
389
|
+
*/
|
390
|
+
VALUE am_sqlite3_statement_column_blob(VALUE self, VALUE v_idx)
|
391
|
+
{
|
392
|
+
am_sqlite3_stmt *am_stmt;
|
393
|
+
int idx = FIX2INT( v_idx );
|
394
|
+
const char *data;
|
395
|
+
long length;
|
396
|
+
|
397
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
398
|
+
data = sqlite3_column_blob( am_stmt->stmt, idx );
|
399
|
+
length = sqlite3_column_bytes( am_stmt->stmt, idx );
|
400
|
+
return rb_str_new( data, length );
|
401
|
+
|
402
|
+
}
|
403
|
+
|
404
|
+
|
405
|
+
/**
|
406
|
+
* call-seq:
|
407
|
+
* stmt.column_double( index ) -> Float
|
408
|
+
*
|
409
|
+
* Return the data in ith column of the result as an Float
|
410
|
+
*
|
411
|
+
*/
|
412
|
+
VALUE am_sqlite3_statement_column_double(VALUE self, VALUE v_idx)
|
413
|
+
{
|
414
|
+
am_sqlite3_stmt *am_stmt;
|
415
|
+
int idx = FIX2INT( v_idx );
|
416
|
+
|
417
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
418
|
+
return rb_float_new( sqlite3_column_double( am_stmt->stmt, idx )) ;
|
419
|
+
}
|
420
|
+
|
421
|
+
|
422
|
+
/**
|
423
|
+
* call-seq:
|
424
|
+
* stmt.column_int( index ) -> Integer
|
425
|
+
*
|
426
|
+
* Return the data in ith column of the result as an Integer
|
427
|
+
*
|
428
|
+
*/
|
429
|
+
VALUE am_sqlite3_statement_column_int(VALUE self, VALUE v_idx)
|
430
|
+
{
|
431
|
+
am_sqlite3_stmt *am_stmt;
|
432
|
+
int idx = FIX2INT( v_idx );
|
433
|
+
|
434
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
435
|
+
return INT2NUM( sqlite3_column_int( am_stmt->stmt, idx )) ;
|
436
|
+
}
|
437
|
+
|
438
|
+
|
439
|
+
/**
|
440
|
+
* call-seq:
|
441
|
+
* stmt.column_int64( index ) -> Integer
|
442
|
+
*
|
443
|
+
* Return the data in ith column of the result as an Integer
|
444
|
+
*
|
445
|
+
*/
|
446
|
+
VALUE am_sqlite3_statement_column_int64(VALUE self, VALUE v_idx)
|
447
|
+
{
|
448
|
+
am_sqlite3_stmt *am_stmt;
|
449
|
+
int idx = FIX2INT( v_idx );
|
450
|
+
|
451
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
452
|
+
return SQLINT64_2NUM( sqlite3_column_int64( am_stmt->stmt, idx )) ;
|
453
|
+
}
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
/**
|
458
|
+
* call-seq:
|
459
|
+
* stmt.column_database_name( index ) -> String
|
460
|
+
*
|
461
|
+
* Return the database name where the data in the ith column of the result set
|
462
|
+
* comes from.
|
463
|
+
*
|
464
|
+
*/
|
465
|
+
VALUE am_sqlite3_statement_column_database_name(VALUE self, VALUE v_idx)
|
466
|
+
{
|
467
|
+
am_sqlite3_stmt *am_stmt;
|
468
|
+
int idx = FIX2INT( v_idx );
|
469
|
+
const char *n ;
|
470
|
+
|
471
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
472
|
+
n = sqlite3_column_database_name( am_stmt->stmt, idx ) ;
|
473
|
+
return ( n == NULL ? Qnil : rb_str_new2( n ) );
|
474
|
+
}
|
475
|
+
|
476
|
+
/**
|
477
|
+
* call-seq:
|
478
|
+
* stmt.column_table_name( index ) -> String
|
479
|
+
*
|
480
|
+
* Return the table name where the data in the ith column of the result set
|
481
|
+
* comes from.
|
482
|
+
*
|
483
|
+
*/
|
484
|
+
VALUE am_sqlite3_statement_column_table_name(VALUE self, VALUE v_idx)
|
485
|
+
{
|
486
|
+
am_sqlite3_stmt *am_stmt;
|
487
|
+
int idx = FIX2INT( v_idx );
|
488
|
+
const char *n ;
|
489
|
+
|
490
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
491
|
+
n = sqlite3_column_table_name( am_stmt->stmt, idx );
|
492
|
+
return ( n == NULL ? Qnil : rb_str_new2( n ) );
|
493
|
+
}
|
494
|
+
|
495
|
+
|
496
|
+
/**
|
497
|
+
* call-seq:
|
498
|
+
* stmt.column_origin_name( index ) -> String
|
499
|
+
*
|
500
|
+
* Return the column name where the data in the ith column of the result set
|
501
|
+
* comes from.
|
502
|
+
*
|
503
|
+
*/
|
504
|
+
VALUE am_sqlite3_statement_column_origin_name(VALUE self, VALUE v_idx)
|
505
|
+
{
|
506
|
+
am_sqlite3_stmt *am_stmt;
|
507
|
+
int idx = FIX2INT( v_idx );
|
508
|
+
const char *n ;
|
509
|
+
|
510
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
511
|
+
n = sqlite3_column_origin_name( am_stmt->stmt, idx );
|
512
|
+
return ( n == NULL ? Qnil : rb_str_new2( n ) );
|
513
|
+
}
|
514
|
+
|
515
|
+
|
516
|
+
/**
|
517
|
+
* call-seq:
|
518
|
+
* stmt.sql -> String
|
519
|
+
*
|
520
|
+
* Return a copy of the original string used to create the prepared statement.
|
521
|
+
*/
|
522
|
+
VALUE am_sqlite3_statement_sql(VALUE self)
|
523
|
+
{
|
524
|
+
|
525
|
+
am_sqlite3_stmt *am_stmt;
|
526
|
+
|
527
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
528
|
+
return rb_str_new2( sqlite3_sql( am_stmt->stmt ) );
|
529
|
+
|
530
|
+
}
|
531
|
+
|
532
|
+
/**
|
533
|
+
* call-seq:
|
534
|
+
* stmt.close -> nil
|
535
|
+
*
|
536
|
+
* Closes the statement. If there is a problem closing the statement then an
|
537
|
+
* error is raised. Closing a statement when there is an existing error is
|
538
|
+
* perfectly fine.
|
539
|
+
*
|
540
|
+
*/
|
541
|
+
VALUE am_sqlite3_statement_close( VALUE self )
|
542
|
+
{
|
543
|
+
|
544
|
+
am_sqlite3_stmt *am_stmt;
|
545
|
+
int rc, existing_errcode;
|
546
|
+
|
547
|
+
Data_Get_Struct(self, am_sqlite3_stmt, am_stmt);
|
548
|
+
|
549
|
+
/* check the current error code to see if one exists, we could be
|
550
|
+
* closing a statement that has an error, and in that case we do not want to
|
551
|
+
* raise an additional error, we want to let the existing error stand
|
552
|
+
*/
|
553
|
+
existing_errcode = sqlite3_errcode( sqlite3_db_handle( am_stmt->stmt ) );
|
554
|
+
rc = sqlite3_finalize( am_stmt->stmt );
|
555
|
+
|
556
|
+
if ( (SQLITE_OK != rc) && (rc != existing_errcode) ) {
|
557
|
+
rb_raise(eAS_Error, "Failure to close statement : [SQLITE_ERROR %d] : %s\n",
|
558
|
+
rc, sqlite3_errmsg( sqlite3_db_handle( am_stmt->stmt) ));
|
559
|
+
}
|
560
|
+
am_stmt->stmt = NULL;
|
561
|
+
|
562
|
+
return Qnil;
|
563
|
+
}
|
564
|
+
|
565
|
+
/***********************************************************************
|
566
|
+
* Ruby life cycle methods
|
567
|
+
***********************************************************************/
|
568
|
+
|
569
|
+
|
570
|
+
/*
|
571
|
+
* garbage collector free method for the am_sqlite3_statement structure
|
572
|
+
*/
|
573
|
+
void am_sqlite3_statement_free(am_sqlite3_stmt* wrapper)
|
574
|
+
{
|
575
|
+
|
576
|
+
if ( Qnil != wrapper->remaining_sql ) {
|
577
|
+
rb_gc_unregister_address( &(wrapper->remaining_sql) );
|
578
|
+
wrapper->remaining_sql = Qnil;
|
579
|
+
}
|
580
|
+
if ( NULL != wrapper->stmt ) {
|
581
|
+
sqlite3_finalize( wrapper->stmt );
|
582
|
+
wrapper->stmt = NULL;
|
583
|
+
}
|
584
|
+
free(wrapper);
|
585
|
+
return;
|
586
|
+
}
|
587
|
+
|
588
|
+
/*
|
589
|
+
* allocate the am_data structure
|
590
|
+
*/
|
591
|
+
VALUE am_sqlite3_statement_alloc(VALUE klass)
|
592
|
+
{
|
593
|
+
am_sqlite3_stmt *wrapper = ALLOC(am_sqlite3_stmt);
|
594
|
+
VALUE obj = (VALUE)NULL;
|
595
|
+
|
596
|
+
wrapper->remaining_sql = Qnil;
|
597
|
+
wrapper->stmt = NULL;
|
598
|
+
|
599
|
+
obj = Data_Wrap_Struct(klass, NULL, am_sqlite3_statement_free, wrapper);
|
600
|
+
return obj;
|
601
|
+
}
|
602
|
+
|
603
|
+
/**
|
604
|
+
* Amagalite Database extension
|
605
|
+
*/
|
606
|
+
|
607
|
+
void Init_amalgalite_statement( )
|
608
|
+
{
|
609
|
+
|
610
|
+
VALUE ma = rb_define_module("Amalgalite");
|
611
|
+
VALUE mas = rb_define_module_under(ma, "SQLite3");
|
612
|
+
|
613
|
+
/*
|
614
|
+
* Encapsulate the SQLite3 Statement handle in a class
|
615
|
+
*/
|
616
|
+
cAS_Statement = rb_define_class_under( mas, "Statement", rb_cObject );
|
617
|
+
rb_define_alloc_func(cAS_Statement, am_sqlite3_statement_alloc);
|
618
|
+
rb_define_method(cAS_Statement, "sql", am_sqlite3_statement_sql, 0);
|
619
|
+
rb_define_method(cAS_Statement, "close", am_sqlite3_statement_close, 0);
|
620
|
+
rb_define_method(cAS_Statement, "step", am_sqlite3_statement_step, 0);
|
621
|
+
|
622
|
+
rb_define_method(cAS_Statement, "column_count", am_sqlite3_statement_column_count, 0);
|
623
|
+
rb_define_method(cAS_Statement, "column_name", am_sqlite3_statement_column_name, 1);
|
624
|
+
rb_define_method(cAS_Statement, "column_declared_type", am_sqlite3_statement_column_decltype, 1);
|
625
|
+
rb_define_method(cAS_Statement, "column_type", am_sqlite3_statement_column_type, 1);
|
626
|
+
rb_define_method(cAS_Statement, "column_text", am_sqlite3_statement_column_text, 1);
|
627
|
+
rb_define_method(cAS_Statement, "column_blob", am_sqlite3_statement_column_blob, 1);
|
628
|
+
rb_define_method(cAS_Statement, "column_int", am_sqlite3_statement_column_int, 1);
|
629
|
+
rb_define_method(cAS_Statement, "column_int64", am_sqlite3_statement_column_int64, 1);
|
630
|
+
rb_define_method(cAS_Statement, "column_double", am_sqlite3_statement_column_double, 1);
|
631
|
+
|
632
|
+
rb_define_method(cAS_Statement, "column_database_name", am_sqlite3_statement_column_database_name, 1);
|
633
|
+
rb_define_method(cAS_Statement, "column_table_name", am_sqlite3_statement_column_table_name, 1);
|
634
|
+
rb_define_method(cAS_Statement, "column_origin_name", am_sqlite3_statement_column_origin_name, 1);
|
635
|
+
rb_define_method(cAS_Statement, "reset!", am_sqlite3_statement_reset, 0);
|
636
|
+
rb_define_method(cAS_Statement, "clear_bindings!", am_sqlite3_statement_clear_bindings, 0);
|
637
|
+
rb_define_method(cAS_Statement, "parameter_count", am_sqlite3_statement_bind_parameter_count, 0);
|
638
|
+
rb_define_method(cAS_Statement, "parameter_index", am_sqlite3_statement_bind_parameter_index, 1);
|
639
|
+
rb_define_method(cAS_Statement, "remaining_sql", am_sqlite3_statement_remaining_sql, 0);
|
640
|
+
rb_define_method(cAS_Statement, "bind_text", am_sqlite3_statement_bind_text, 2);
|
641
|
+
rb_define_method(cAS_Statement, "bind_int", am_sqlite3_statement_bind_int, 2);
|
642
|
+
rb_define_method(cAS_Statement, "bind_int64", am_sqlite3_statement_bind_int64, 2);
|
643
|
+
rb_define_method(cAS_Statement, "bind_double", am_sqlite3_statement_bind_double, 2);
|
644
|
+
rb_define_method(cAS_Statement, "bind_null", am_sqlite3_statement_bind_null, 1);
|
645
|
+
rb_define_method(cAS_Statement, "bind_blob", am_sqlite3_statement_bind_blob, 2);
|
646
|
+
rb_define_method(cAS_Statement, "bind_zeroblob", am_sqlite3_statement_bind_zeroblob, 2);
|
647
|
+
}
|
648
|
+
|
649
|
+
|