sqlite_statement.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Wez Furlong <wez@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "pdo/php_pdo.h"
  26. #include "pdo/php_pdo_driver.h"
  27. #include "php_pdo_sqlite.h"
  28. #include "php_pdo_sqlite_int.h"
  29. static int pdo_sqlite_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
  30. {
  31. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  32. if (S->stmt) {
  33. sqlite3_finalize(S->stmt);
  34. S->stmt = NULL;
  35. }
  36. efree(S);
  37. return 1;
  38. }
  39. static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
  40. {
  41. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  42. if (stmt->executed && !S->done) {
  43. sqlite3_reset(S->stmt);
  44. }
  45. S->done = 0;
  46. switch (sqlite3_step(S->stmt)) {
  47. case SQLITE_ROW:
  48. S->pre_fetched = 1;
  49. stmt->column_count = sqlite3_data_count(S->stmt);
  50. return 1;
  51. case SQLITE_DONE:
  52. stmt->column_count = sqlite3_column_count(S->stmt);
  53. stmt->row_count = sqlite3_changes(S->H->db);
  54. sqlite3_reset(S->stmt);
  55. S->done = 1;
  56. return 1;
  57. case SQLITE_ERROR:
  58. sqlite3_reset(S->stmt);
  59. case SQLITE_MISUSE:
  60. case SQLITE_BUSY:
  61. default:
  62. pdo_sqlite_error_stmt(stmt);
  63. return 0;
  64. }
  65. }
  66. static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
  67. enum pdo_param_event event_type TSRMLS_DC)
  68. {
  69. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  70. switch (event_type) {
  71. case PDO_PARAM_EVT_EXEC_PRE:
  72. if (stmt->executed && !S->done) {
  73. sqlite3_reset(S->stmt);
  74. S->done = 1;
  75. }
  76. if (param->is_param) {
  77. if (param->paramno == -1) {
  78. param->paramno = sqlite3_bind_parameter_index(S->stmt, param->name) - 1;
  79. }
  80. switch (PDO_PARAM_TYPE(param->param_type)) {
  81. case PDO_PARAM_STMT:
  82. return 0;
  83. case PDO_PARAM_NULL:
  84. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  85. return 1;
  86. }
  87. pdo_sqlite_error_stmt(stmt);
  88. return 0;
  89. case PDO_PARAM_INT:
  90. case PDO_PARAM_BOOL:
  91. if (Z_TYPE_P(param->parameter) == IS_NULL) {
  92. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  93. return 1;
  94. }
  95. } else {
  96. convert_to_long(param->parameter);
  97. #if LONG_MAX > 2147483647
  98. if (SQLITE_OK == sqlite3_bind_int64(S->stmt, param->paramno + 1, Z_LVAL_P(param->parameter))) {
  99. return 1;
  100. }
  101. #else
  102. if (SQLITE_OK == sqlite3_bind_int(S->stmt, param->paramno + 1, Z_LVAL_P(param->parameter))) {
  103. return 1;
  104. }
  105. #endif
  106. }
  107. pdo_sqlite_error_stmt(stmt);
  108. return 0;
  109. case PDO_PARAM_LOB:
  110. if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
  111. php_stream *stm;
  112. php_stream_from_zval_no_verify(stm, &param->parameter);
  113. if (stm) {
  114. SEPARATE_ZVAL(&param->parameter);
  115. Z_TYPE_P(param->parameter) = IS_STRING;
  116. Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
  117. &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
  118. } else {
  119. pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
  120. return 0;
  121. }
  122. } else if (Z_TYPE_P(param->parameter) == IS_NULL) {
  123. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  124. return 1;
  125. }
  126. pdo_sqlite_error_stmt(stmt);
  127. return 0;
  128. } else {
  129. convert_to_string(param->parameter);
  130. }
  131. if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1,
  132. Z_STRVAL_P(param->parameter),
  133. Z_STRLEN_P(param->parameter),
  134. SQLITE_STATIC)) {
  135. return 1;
  136. }
  137. pdo_sqlite_error_stmt(stmt);
  138. return 0;
  139. case PDO_PARAM_STR:
  140. default:
  141. if (Z_TYPE_P(param->parameter) == IS_NULL) {
  142. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  143. return 1;
  144. }
  145. } else {
  146. convert_to_string(param->parameter);
  147. if(SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
  148. Z_STRVAL_P(param->parameter),
  149. Z_STRLEN_P(param->parameter),
  150. SQLITE_STATIC)) {
  151. return 1;
  152. }
  153. }
  154. pdo_sqlite_error_stmt(stmt);
  155. return 0;
  156. }
  157. }
  158. break;
  159. default:
  160. ;
  161. }
  162. return 1;
  163. }
  164. static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
  165. enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
  166. {
  167. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  168. int i;
  169. if (!S->stmt) {
  170. return 0;
  171. }
  172. if (S->pre_fetched) {
  173. S->pre_fetched = 0;
  174. return 1;
  175. }
  176. if (S->done) {
  177. return 0;
  178. }
  179. i = sqlite3_step(S->stmt);
  180. switch (i) {
  181. case SQLITE_ROW:
  182. return 1;
  183. case SQLITE_DONE:
  184. S->done = 1;
  185. sqlite3_reset(S->stmt);
  186. return 0;
  187. case SQLITE_ERROR:
  188. sqlite3_reset(S->stmt);
  189. default:
  190. pdo_sqlite_error_stmt(stmt);
  191. return 0;
  192. }
  193. }
  194. static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
  195. {
  196. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  197. if(colno >= sqlite3_column_count(S->stmt)) {
  198. /* error invalid column */
  199. pdo_sqlite_error_stmt(stmt);
  200. return 0;
  201. }
  202. stmt->columns[colno].name = estrdup(sqlite3_column_name(S->stmt, colno));
  203. stmt->columns[colno].namelen = strlen(stmt->columns[colno].name);
  204. stmt->columns[colno].maxlen = 0xffffffff;
  205. stmt->columns[colno].precision = 0;
  206. switch (sqlite3_column_type(S->stmt, colno)) {
  207. case SQLITE_INTEGER:
  208. case SQLITE_FLOAT:
  209. case SQLITE3_TEXT:
  210. case SQLITE_BLOB:
  211. case SQLITE_NULL:
  212. default:
  213. stmt->columns[colno].param_type = PDO_PARAM_STR;
  214. break;
  215. }
  216. return 1;
  217. }
  218. static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
  219. {
  220. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  221. if (!S->stmt) {
  222. return 0;
  223. }
  224. if(colno >= sqlite3_data_count(S->stmt)) {
  225. /* error invalid column */
  226. pdo_sqlite_error_stmt(stmt);
  227. return 0;
  228. }
  229. switch (sqlite3_column_type(S->stmt, colno)) {
  230. case SQLITE_NULL:
  231. *ptr = NULL;
  232. *len = 0;
  233. return 1;
  234. case SQLITE_BLOB:
  235. *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
  236. *len = sqlite3_column_bytes(S->stmt, colno);
  237. return 1;
  238. default:
  239. *ptr = (char*)sqlite3_column_text(S->stmt, colno);
  240. *len = sqlite3_column_bytes(S->stmt, colno);
  241. return 1;
  242. }
  243. }
  244. static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
  245. {
  246. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  247. const char *str;
  248. zval *flags;
  249. if (!S->stmt) {
  250. return FAILURE;
  251. }
  252. if(colno >= sqlite3_data_count(S->stmt)) {
  253. /* error invalid column */
  254. pdo_sqlite_error_stmt(stmt);
  255. return FAILURE;
  256. }
  257. array_init(return_value);
  258. MAKE_STD_ZVAL(flags);
  259. array_init(flags);
  260. switch (sqlite3_column_type(S->stmt, colno)) {
  261. case SQLITE_NULL:
  262. add_assoc_string(return_value, "native_type", "null", 1);
  263. break;
  264. case SQLITE_FLOAT:
  265. add_assoc_string(return_value, "native_type", "double", 1);
  266. break;
  267. case SQLITE_BLOB:
  268. add_next_index_string(flags, "blob", 1);
  269. case SQLITE_TEXT:
  270. add_assoc_string(return_value, "native_type", "string", 1);
  271. break;
  272. case SQLITE_INTEGER:
  273. add_assoc_string(return_value, "native_type", "integer", 1);
  274. break;
  275. }
  276. str = sqlite3_column_decltype(S->stmt, colno);
  277. if (str) {
  278. add_assoc_string(return_value, "sqlite:decl_type", (char *)str, 1);
  279. }
  280. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  281. str = sqlite3_column_table_name(S->stmt, colno);
  282. if (str) {
  283. add_assoc_string(return_value, "table", (char *)str, 1);
  284. }
  285. #endif
  286. add_assoc_zval(return_value, "flags", flags);
  287. return SUCCESS;
  288. }
  289. static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
  290. {
  291. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  292. sqlite3_reset(S->stmt);
  293. return 1;
  294. }
  295. struct pdo_stmt_methods sqlite_stmt_methods = {
  296. pdo_sqlite_stmt_dtor,
  297. pdo_sqlite_stmt_execute,
  298. pdo_sqlite_stmt_fetch,
  299. pdo_sqlite_stmt_describe,
  300. pdo_sqlite_stmt_get_col,
  301. pdo_sqlite_stmt_param_hook,
  302. NULL, /* set_attr */
  303. NULL, /* get_attr */
  304. pdo_sqlite_stmt_col_meta,
  305. NULL, /* next_rowset */
  306. pdo_sqlite_stmt_cursor_closer
  307. };
  308. /*
  309. * Local variables:
  310. * tab-width: 4
  311. * c-basic-offset: 4
  312. * End:
  313. * vim600: noet sw=4 ts=4 fdm=marker
  314. * vim<600: noet sw=4 ts=4
  315. */