statement.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include <sqlite3_ruby.h>
  2. #define REQUIRE_OPEN_STMT(_ctxt) \
  3. if(!_ctxt->st) \
  4. rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed statement");
  5. VALUE cSqlite3Statement;
  6. static void deallocate(void * ctx)
  7. {
  8. sqlite3StmtRubyPtr c = (sqlite3StmtRubyPtr)ctx;
  9. xfree(c);
  10. }
  11. static VALUE allocate(VALUE klass)
  12. {
  13. sqlite3StmtRubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3StmtRuby));
  14. ctx->st = NULL;
  15. ctx->done_p = 0;
  16. return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
  17. }
  18. /* call-seq: SQLite3::Statement.new(db, sql)
  19. *
  20. * Create a new statement attached to the given Database instance, and which
  21. * encapsulates the given SQL text. If the text contains more than one
  22. * statement (i.e., separated by semicolons), then the #remainder property
  23. * will be set to the trailing text.
  24. */
  25. static VALUE initialize(VALUE self, VALUE db, VALUE sql)
  26. {
  27. sqlite3RubyPtr db_ctx;
  28. sqlite3StmtRubyPtr ctx;
  29. const char *tail = NULL;
  30. int status;
  31. StringValue(sql);
  32. Data_Get_Struct(db, sqlite3Ruby, db_ctx);
  33. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  34. if(!db_ctx->db)
  35. rb_raise(rb_eArgError, "prepare called on a closed database");
  36. #ifdef HAVE_RUBY_ENCODING_H
  37. if(!UTF8_P(sql)) {
  38. sql = rb_str_export_to_enc(sql, rb_utf8_encoding());
  39. }
  40. #endif
  41. #ifdef HAVE_SQLITE3_PREPARE_V2
  42. status = sqlite3_prepare_v2(
  43. #else
  44. status = sqlite3_prepare(
  45. #endif
  46. db_ctx->db,
  47. (const char *)StringValuePtr(sql),
  48. (int)RSTRING_LEN(sql),
  49. &ctx->st,
  50. &tail
  51. );
  52. CHECK(db_ctx->db, status);
  53. rb_iv_set(self, "@connection", db);
  54. rb_iv_set(self, "@remainder", rb_str_new2(tail));
  55. rb_iv_set(self, "@columns", Qnil);
  56. rb_iv_set(self, "@types", Qnil);
  57. return self;
  58. }
  59. /* call-seq: stmt.close
  60. *
  61. * Closes the statement by finalizing the underlying statement
  62. * handle. The statement must not be used after being closed.
  63. */
  64. static VALUE sqlite3_rb_close(VALUE self)
  65. {
  66. sqlite3StmtRubyPtr ctx;
  67. sqlite3 * db;
  68. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  69. REQUIRE_OPEN_STMT(ctx);
  70. db = sqlite3_db_handle(ctx->st);
  71. CHECK(db, sqlite3_finalize(ctx->st));
  72. ctx->st = NULL;
  73. return self;
  74. }
  75. /* call-seq: stmt.closed?
  76. *
  77. * Returns true if the statement has been closed.
  78. */
  79. static VALUE closed_p(VALUE self)
  80. {
  81. sqlite3StmtRubyPtr ctx;
  82. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  83. if(!ctx->st) return Qtrue;
  84. return Qfalse;
  85. }
  86. static VALUE step(VALUE self)
  87. {
  88. sqlite3StmtRubyPtr ctx;
  89. sqlite3_stmt *stmt;
  90. int value, length;
  91. VALUE list;
  92. #ifdef HAVE_RUBY_ENCODING_H
  93. rb_encoding * internal_encoding;
  94. int enc_index;
  95. #endif
  96. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  97. REQUIRE_OPEN_STMT(ctx);
  98. if(ctx->done_p) return Qnil;
  99. #ifdef HAVE_RUBY_ENCODING_H
  100. {
  101. VALUE db = rb_iv_get(self, "@connection");
  102. VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
  103. enc_index = NIL_P(encoding) ? rb_utf8_encindex() : rb_to_encoding_index(encoding);
  104. internal_encoding = rb_default_internal_encoding();
  105. }
  106. #endif
  107. stmt = ctx->st;
  108. value = sqlite3_step(stmt);
  109. length = sqlite3_column_count(stmt);
  110. list = rb_ary_new2((long)length);
  111. switch(value) {
  112. case SQLITE_ROW:
  113. {
  114. int i;
  115. for(i = 0; i < length; i++) {
  116. switch(sqlite3_column_type(stmt, i)) {
  117. case SQLITE_INTEGER:
  118. rb_ary_push(list, LL2NUM(sqlite3_column_int64(stmt, i)));
  119. break;
  120. case SQLITE_FLOAT:
  121. rb_ary_push(list, rb_float_new(sqlite3_column_double(stmt, i)));
  122. break;
  123. case SQLITE_TEXT:
  124. {
  125. VALUE str = rb_tainted_str_new(
  126. (const char *)sqlite3_column_text(stmt, i),
  127. (long)sqlite3_column_bytes(stmt, i)
  128. );
  129. #ifdef HAVE_RUBY_ENCODING_H
  130. rb_enc_associate_index(str, enc_index);
  131. if(internal_encoding)
  132. str = rb_str_export_to_enc(str, internal_encoding);
  133. #endif
  134. rb_ary_push(list, str);
  135. }
  136. break;
  137. case SQLITE_BLOB:
  138. {
  139. VALUE str = rb_tainted_str_new(
  140. (const char *)sqlite3_column_blob(stmt, i),
  141. (long)sqlite3_column_bytes(stmt, i)
  142. );
  143. rb_ary_push(list, str);
  144. }
  145. break;
  146. case SQLITE_NULL:
  147. rb_ary_push(list, Qnil);
  148. break;
  149. default:
  150. rb_raise(rb_eRuntimeError, "bad type");
  151. }
  152. }
  153. }
  154. break;
  155. case SQLITE_DONE:
  156. ctx->done_p = 1;
  157. return Qnil;
  158. break;
  159. default:
  160. CHECK(sqlite3_db_handle(ctx->st), value);
  161. }
  162. return list;
  163. }
  164. /* call-seq: stmt.bind_param(key, value)
  165. *
  166. * Binds value to the named (or positional) placeholder. If +param+ is a
  167. * Fixnum, it is treated as an index for a positional placeholder.
  168. * Otherwise it is used as the name of the placeholder to bind to.
  169. *
  170. * See also #bind_params.
  171. */
  172. static VALUE bind_param(VALUE self, VALUE key, VALUE value)
  173. {
  174. sqlite3StmtRubyPtr ctx;
  175. int status;
  176. int index;
  177. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  178. REQUIRE_OPEN_STMT(ctx);
  179. switch(TYPE(key)) {
  180. case T_SYMBOL:
  181. key = rb_funcall(key, rb_intern("to_s"), 0);
  182. case T_STRING:
  183. if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key);
  184. index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key));
  185. break;
  186. default:
  187. index = (int)NUM2INT(key);
  188. }
  189. if(index == 0)
  190. rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter");
  191. switch(TYPE(value)) {
  192. case T_STRING:
  193. if(CLASS_OF(value) == cSqlite3Blob
  194. #ifdef HAVE_RUBY_ENCODING_H
  195. || rb_enc_get_index(value) == rb_ascii8bit_encindex()
  196. #endif
  197. ) {
  198. status = sqlite3_bind_blob(
  199. ctx->st,
  200. index,
  201. (const char *)StringValuePtr(value),
  202. (int)RSTRING_LEN(value),
  203. SQLITE_TRANSIENT
  204. );
  205. } else {
  206. #ifdef HAVE_RUBY_ENCODING_H
  207. if(!UTF8_P(value)) {
  208. VALUE db = rb_iv_get(self, "@connection");
  209. VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
  210. rb_encoding * enc = rb_to_encoding(encoding);
  211. value = rb_str_export_to_enc(value, enc);
  212. }
  213. #endif
  214. status = sqlite3_bind_text(
  215. ctx->st,
  216. index,
  217. (const char *)StringValuePtr(value),
  218. (int)RSTRING_LEN(value),
  219. SQLITE_TRANSIENT
  220. );
  221. }
  222. break;
  223. case T_BIGNUM:
  224. if (RBIGNUM_LEN(value) * SIZEOF_BDIGITS <= 8) {
  225. status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)NUM2LL(value));
  226. break;
  227. }
  228. case T_FLOAT:
  229. status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value));
  230. break;
  231. case T_FIXNUM:
  232. status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value));
  233. break;
  234. case T_NIL:
  235. status = sqlite3_bind_null(ctx->st, index);
  236. break;
  237. default:
  238. rb_raise(rb_eRuntimeError, "can't prepare %s",
  239. rb_class2name(CLASS_OF(value)));
  240. break;
  241. }
  242. CHECK(sqlite3_db_handle(ctx->st), status);
  243. return self;
  244. }
  245. /* call-seq: stmt.reset!
  246. *
  247. * Resets the statement. This is typically done internally, though it might
  248. * occassionally be necessary to manually reset the statement.
  249. */
  250. static VALUE reset_bang(VALUE self)
  251. {
  252. sqlite3StmtRubyPtr ctx;
  253. int status;
  254. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  255. REQUIRE_OPEN_STMT(ctx);
  256. status = sqlite3_reset(ctx->st);
  257. ctx->done_p = 0;
  258. return self;
  259. }
  260. /* call-seq: stmt.clear_bindings!
  261. *
  262. * Resets the statement. This is typically done internally, though it might
  263. * occassionally be necessary to manually reset the statement.
  264. */
  265. static VALUE clear_bindings(VALUE self)
  266. {
  267. sqlite3StmtRubyPtr ctx;
  268. int status;
  269. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  270. REQUIRE_OPEN_STMT(ctx);
  271. status = sqlite3_clear_bindings(ctx->st);
  272. ctx->done_p = 0;
  273. return self;
  274. }
  275. /* call-seq: stmt.done?
  276. *
  277. * returns true if all rows have been returned.
  278. */
  279. static VALUE done_p(VALUE self)
  280. {
  281. sqlite3StmtRubyPtr ctx;
  282. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  283. if(ctx->done_p) return Qtrue;
  284. return Qfalse;
  285. }
  286. /* call-seq: stmt.column_count
  287. *
  288. * Returns the number of columns to be returned for this statement
  289. */
  290. static VALUE column_count(VALUE self)
  291. {
  292. sqlite3StmtRubyPtr ctx;
  293. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  294. REQUIRE_OPEN_STMT(ctx);
  295. return INT2NUM((long)sqlite3_column_count(ctx->st));
  296. }
  297. /* call-seq: stmt.column_name(index)
  298. *
  299. * Get the column name at +index+. 0 based.
  300. */
  301. static VALUE column_name(VALUE self, VALUE index)
  302. {
  303. sqlite3StmtRubyPtr ctx;
  304. const char * name;
  305. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  306. REQUIRE_OPEN_STMT(ctx);
  307. name = sqlite3_column_name(ctx->st, (int)NUM2INT(index));
  308. if(name) return SQLITE3_UTF8_STR_NEW2(name);
  309. return Qnil;
  310. }
  311. /* call-seq: stmt.column_decltype(index)
  312. *
  313. * Get the column type at +index+. 0 based.
  314. */
  315. static VALUE column_decltype(VALUE self, VALUE index)
  316. {
  317. sqlite3StmtRubyPtr ctx;
  318. const char * name;
  319. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  320. REQUIRE_OPEN_STMT(ctx);
  321. name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));
  322. if(name) return rb_str_new2(name);
  323. return Qnil;
  324. }
  325. /* call-seq: stmt.bind_parameter_count
  326. *
  327. * Return the number of bind parameters
  328. */
  329. static VALUE bind_parameter_count(VALUE self)
  330. {
  331. sqlite3StmtRubyPtr ctx;
  332. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  333. REQUIRE_OPEN_STMT(ctx);
  334. return INT2NUM((long)sqlite3_bind_parameter_count(ctx->st));
  335. }
  336. #ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
  337. /* call-seq: stmt.database_name(column_index)
  338. *
  339. * Return the database name for the column at +column_index+
  340. */
  341. static VALUE database_name(VALUE self, VALUE index)
  342. {
  343. sqlite3StmtRubyPtr ctx;
  344. Data_Get_Struct(self, sqlite3StmtRuby, ctx);
  345. REQUIRE_OPEN_STMT(ctx);
  346. return SQLITE3_UTF8_STR_NEW2(
  347. sqlite3_column_database_name(ctx->st, NUM2INT(index)));
  348. }
  349. #endif
  350. void init_sqlite3_statement()
  351. {
  352. cSqlite3Statement = rb_define_class_under(mSqlite3, "Statement", rb_cObject);
  353. rb_define_alloc_func(cSqlite3Statement, allocate);
  354. rb_define_method(cSqlite3Statement, "initialize", initialize, 2);
  355. rb_define_method(cSqlite3Statement, "close", sqlite3_rb_close, 0);
  356. rb_define_method(cSqlite3Statement, "closed?", closed_p, 0);
  357. rb_define_method(cSqlite3Statement, "bind_param", bind_param, 2);
  358. rb_define_method(cSqlite3Statement, "reset!", reset_bang, 0);
  359. rb_define_method(cSqlite3Statement, "clear_bindings!", clear_bindings, 0);
  360. rb_define_method(cSqlite3Statement, "step", step, 0);
  361. rb_define_method(cSqlite3Statement, "done?", done_p, 0);
  362. rb_define_method(cSqlite3Statement, "column_count", column_count, 0);
  363. rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
  364. rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
  365. rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
  366. #ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
  367. rb_define_method(cSqlite3Statement, "database_name", database_name, 1);
  368. #endif
  369. }