sqlite_statement.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_ini.h"
  23. #include "ext/standard/info.h"
  24. #include "pdo/php_pdo.h"
  25. #include "pdo/php_pdo_driver.h"
  26. #include "php_pdo_sqlite.h"
  27. #include "php_pdo_sqlite_int.h"
  28. static int pdo_sqlite_stmt_dtor(pdo_stmt_t *stmt)
  29. {
  30. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  31. if (S->stmt) {
  32. sqlite3_finalize(S->stmt);
  33. S->stmt = NULL;
  34. }
  35. efree(S);
  36. return 1;
  37. }
  38. /**
  39. * Change the column count on the statement.
  40. *
  41. * Since PHP 7.2 sqlite3_prepare_v2 is used which auto recompile prepared statement on schema change.
  42. * Instead of raise an error on schema change, the result set will change, and the statement's columns must be updated.
  43. *
  44. * See bug #78192
  45. */
  46. static void pdo_sqlite_stmt_set_column_count(pdo_stmt_t *stmt, int new_count)
  47. {
  48. /* Columns not yet "described" */
  49. if (!stmt->columns) {
  50. stmt->column_count = new_count;
  51. return;
  52. }
  53. /*
  54. * The column count has not changed : no need to reload columns description
  55. * Note: Do not handle attribute name change, without column count change
  56. */
  57. if (new_count == stmt->column_count) {
  58. return;
  59. }
  60. /* Free previous columns to force reload description */
  61. int i;
  62. for (i = 0; i < stmt->column_count; i++) {
  63. if (stmt->columns[i].name) {
  64. zend_string_release(stmt->columns[i].name);
  65. stmt->columns[i].name = NULL;
  66. }
  67. }
  68. efree(stmt->columns);
  69. stmt->columns = NULL;
  70. stmt->column_count = new_count;
  71. }
  72. static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt)
  73. {
  74. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  75. if (stmt->executed && !S->done) {
  76. sqlite3_reset(S->stmt);
  77. }
  78. S->done = 0;
  79. switch (sqlite3_step(S->stmt)) {
  80. case SQLITE_ROW:
  81. S->pre_fetched = 1;
  82. pdo_sqlite_stmt_set_column_count(stmt, sqlite3_data_count(S->stmt));
  83. return 1;
  84. case SQLITE_DONE:
  85. pdo_sqlite_stmt_set_column_count(stmt, sqlite3_column_count(S->stmt));
  86. stmt->row_count = sqlite3_changes(S->H->db);
  87. sqlite3_reset(S->stmt);
  88. S->done = 1;
  89. return 1;
  90. case SQLITE_ERROR:
  91. sqlite3_reset(S->stmt);
  92. case SQLITE_MISUSE:
  93. case SQLITE_BUSY:
  94. default:
  95. pdo_sqlite_error_stmt(stmt);
  96. return 0;
  97. }
  98. }
  99. static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
  100. enum pdo_param_event event_type)
  101. {
  102. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  103. zval *parameter;
  104. switch (event_type) {
  105. case PDO_PARAM_EVT_EXEC_PRE:
  106. if (stmt->executed && !S->done) {
  107. sqlite3_reset(S->stmt);
  108. S->done = 1;
  109. }
  110. if (param->is_param) {
  111. if (param->paramno == -1) {
  112. param->paramno = sqlite3_bind_parameter_index(S->stmt, ZSTR_VAL(param->name)) - 1;
  113. }
  114. switch (PDO_PARAM_TYPE(param->param_type)) {
  115. case PDO_PARAM_STMT:
  116. return 0;
  117. case PDO_PARAM_NULL:
  118. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  119. return 1;
  120. }
  121. pdo_sqlite_error_stmt(stmt);
  122. return 0;
  123. case PDO_PARAM_INT:
  124. case PDO_PARAM_BOOL:
  125. if (Z_ISREF(param->parameter)) {
  126. parameter = Z_REFVAL(param->parameter);
  127. } else {
  128. parameter = &param->parameter;
  129. }
  130. if (Z_TYPE_P(parameter) == IS_NULL) {
  131. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  132. return 1;
  133. }
  134. } else {
  135. convert_to_long(parameter);
  136. #if ZEND_LONG_MAX > 2147483647
  137. if (SQLITE_OK == sqlite3_bind_int64(S->stmt, param->paramno + 1, Z_LVAL_P(parameter))) {
  138. return 1;
  139. }
  140. #else
  141. if (SQLITE_OK == sqlite3_bind_int(S->stmt, param->paramno + 1, Z_LVAL_P(parameter))) {
  142. return 1;
  143. }
  144. #endif
  145. }
  146. pdo_sqlite_error_stmt(stmt);
  147. return 0;
  148. case PDO_PARAM_LOB:
  149. if (Z_ISREF(param->parameter)) {
  150. parameter = Z_REFVAL(param->parameter);
  151. } else {
  152. parameter = &param->parameter;
  153. }
  154. if (Z_TYPE_P(parameter) == IS_RESOURCE) {
  155. php_stream *stm = NULL;
  156. php_stream_from_zval_no_verify(stm, parameter);
  157. if (stm) {
  158. zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
  159. zval_ptr_dtor(parameter);
  160. ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
  161. } else {
  162. pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
  163. return 0;
  164. }
  165. } else if (Z_TYPE_P(parameter) == IS_NULL) {
  166. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  167. return 1;
  168. }
  169. pdo_sqlite_error_stmt(stmt);
  170. return 0;
  171. } else {
  172. convert_to_string(parameter);
  173. }
  174. if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1,
  175. Z_STRVAL_P(parameter),
  176. Z_STRLEN_P(parameter),
  177. SQLITE_STATIC)) {
  178. return 1;
  179. }
  180. return 0;
  181. case PDO_PARAM_STR:
  182. default:
  183. if (Z_ISREF(param->parameter)) {
  184. parameter = Z_REFVAL(param->parameter);
  185. } else {
  186. parameter = &param->parameter;
  187. }
  188. if (Z_TYPE_P(parameter) == IS_NULL) {
  189. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  190. return 1;
  191. }
  192. } else {
  193. convert_to_string(parameter);
  194. if (SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
  195. Z_STRVAL_P(parameter),
  196. Z_STRLEN_P(parameter),
  197. SQLITE_STATIC)) {
  198. return 1;
  199. }
  200. }
  201. pdo_sqlite_error_stmt(stmt);
  202. return 0;
  203. }
  204. }
  205. break;
  206. default:
  207. ;
  208. }
  209. return 1;
  210. }
  211. static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
  212. enum pdo_fetch_orientation ori, zend_long offset)
  213. {
  214. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  215. int i;
  216. if (!S->stmt) {
  217. return 0;
  218. }
  219. if (S->pre_fetched) {
  220. S->pre_fetched = 0;
  221. return 1;
  222. }
  223. if (S->done) {
  224. return 0;
  225. }
  226. i = sqlite3_step(S->stmt);
  227. switch (i) {
  228. case SQLITE_ROW:
  229. return 1;
  230. case SQLITE_DONE:
  231. S->done = 1;
  232. sqlite3_reset(S->stmt);
  233. return 0;
  234. case SQLITE_ERROR:
  235. sqlite3_reset(S->stmt);
  236. default:
  237. pdo_sqlite_error_stmt(stmt);
  238. return 0;
  239. }
  240. }
  241. static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno)
  242. {
  243. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  244. const char *str;
  245. if(colno >= sqlite3_column_count(S->stmt)) {
  246. /* error invalid column */
  247. pdo_sqlite_error_stmt(stmt);
  248. return 0;
  249. }
  250. str = sqlite3_column_name(S->stmt, colno);
  251. stmt->columns[colno].name = zend_string_init(str, strlen(str), 0);
  252. stmt->columns[colno].maxlen = SIZE_MAX;
  253. stmt->columns[colno].precision = 0;
  254. switch (sqlite3_column_type(S->stmt, colno)) {
  255. case SQLITE_INTEGER:
  256. case SQLITE_FLOAT:
  257. case SQLITE3_TEXT:
  258. case SQLITE_BLOB:
  259. case SQLITE_NULL:
  260. default:
  261. stmt->columns[colno].param_type = PDO_PARAM_STR;
  262. break;
  263. }
  264. return 1;
  265. }
  266. static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees)
  267. {
  268. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  269. if (!S->stmt) {
  270. return 0;
  271. }
  272. if(colno >= sqlite3_data_count(S->stmt)) {
  273. /* error invalid column */
  274. pdo_sqlite_error_stmt(stmt);
  275. return 0;
  276. }
  277. switch (sqlite3_column_type(S->stmt, colno)) {
  278. case SQLITE_NULL:
  279. *ptr = NULL;
  280. *len = 0;
  281. return 1;
  282. case SQLITE_BLOB:
  283. *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
  284. *len = sqlite3_column_bytes(S->stmt, colno);
  285. return 1;
  286. default:
  287. *ptr = (char*)sqlite3_column_text(S->stmt, colno);
  288. *len = sqlite3_column_bytes(S->stmt, colno);
  289. return 1;
  290. }
  291. }
  292. static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
  293. {
  294. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  295. const char *str;
  296. zval flags;
  297. if (!S->stmt) {
  298. return FAILURE;
  299. }
  300. if(colno >= sqlite3_column_count(S->stmt)) {
  301. /* error invalid column */
  302. pdo_sqlite_error_stmt(stmt);
  303. return FAILURE;
  304. }
  305. array_init(return_value);
  306. array_init(&flags);
  307. switch (sqlite3_column_type(S->stmt, colno)) {
  308. case SQLITE_NULL:
  309. add_assoc_string(return_value, "native_type", "null");
  310. break;
  311. case SQLITE_FLOAT:
  312. add_assoc_string(return_value, "native_type", "double");
  313. break;
  314. case SQLITE_BLOB:
  315. add_next_index_string(&flags, "blob");
  316. case SQLITE_TEXT:
  317. add_assoc_string(return_value, "native_type", "string");
  318. break;
  319. case SQLITE_INTEGER:
  320. add_assoc_string(return_value, "native_type", "integer");
  321. break;
  322. }
  323. str = sqlite3_column_decltype(S->stmt, colno);
  324. if (str) {
  325. add_assoc_string(return_value, "sqlite:decl_type", (char *)str);
  326. }
  327. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  328. str = sqlite3_column_table_name(S->stmt, colno);
  329. if (str) {
  330. add_assoc_string(return_value, "table", (char *)str);
  331. }
  332. #endif
  333. add_assoc_zval(return_value, "flags", &flags);
  334. return SUCCESS;
  335. }
  336. static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt)
  337. {
  338. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  339. sqlite3_reset(S->stmt);
  340. return 1;
  341. }
  342. const struct pdo_stmt_methods sqlite_stmt_methods = {
  343. pdo_sqlite_stmt_dtor,
  344. pdo_sqlite_stmt_execute,
  345. pdo_sqlite_stmt_fetch,
  346. pdo_sqlite_stmt_describe,
  347. pdo_sqlite_stmt_get_col,
  348. pdo_sqlite_stmt_param_hook,
  349. NULL, /* set_attr */
  350. NULL, /* get_attr */
  351. pdo_sqlite_stmt_col_meta,
  352. NULL, /* next_rowset */
  353. pdo_sqlite_stmt_cursor_closer
  354. };
  355. /*
  356. * Local variables:
  357. * tab-width: 4
  358. * c-basic-offset: 4
  359. * End:
  360. * vim600: noet sw=4 ts=4 fdm=marker
  361. * vim<600: noet sw=4 ts=4
  362. */