You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
8.0 KiB

21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2006 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. case SQLITE_MISUSE:
  59. case SQLITE_BUSY:
  60. default:
  61. pdo_sqlite_error_stmt(stmt);
  62. return 0;
  63. }
  64. }
  65. static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
  66. enum pdo_param_event event_type TSRMLS_DC)
  67. {
  68. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  69. switch (event_type) {
  70. case PDO_PARAM_EVT_EXEC_PRE:
  71. if (stmt->executed && !S->done) {
  72. sqlite3_reset(S->stmt);
  73. S->done = 1;
  74. }
  75. if (param->is_param) {
  76. if (param->paramno == -1) {
  77. param->paramno = sqlite3_bind_parameter_index(S->stmt, param->name) - 1;
  78. }
  79. switch (PDO_PARAM_TYPE(param->param_type)) {
  80. case PDO_PARAM_STMT:
  81. return 0;
  82. case PDO_PARAM_NULL:
  83. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  84. return 1;
  85. }
  86. pdo_sqlite_error_stmt(stmt);
  87. return 0;
  88. case PDO_PARAM_LOB:
  89. if (Z_TYPE_P(param->parameter) == IS_RESOURCE) {
  90. php_stream *stm;
  91. php_stream_from_zval_no_verify(stm, &param->parameter);
  92. if (stm) {
  93. SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
  94. Z_TYPE_P(param->parameter) = IS_STRING;
  95. Z_STRLEN_P(param->parameter) = php_stream_copy_to_mem(stm,
  96. &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0);
  97. } else {
  98. pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource" TSRMLS_CC);
  99. return 0;
  100. }
  101. }
  102. /* fall through */
  103. case PDO_PARAM_STR:
  104. default:
  105. if (Z_TYPE_P(param->parameter) == IS_NULL) {
  106. if (sqlite3_bind_null(S->stmt, param->paramno + 1) == SQLITE_OK) {
  107. return 1;
  108. }
  109. } else {
  110. convert_to_string(param->parameter);
  111. if(SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
  112. Z_STRVAL_P(param->parameter),
  113. Z_STRLEN_P(param->parameter),
  114. SQLITE_STATIC)) {
  115. return 1;
  116. }
  117. }
  118. pdo_sqlite_error_stmt(stmt);
  119. return 0;
  120. }
  121. }
  122. break;
  123. default:
  124. ;
  125. }
  126. return 1;
  127. }
  128. static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
  129. enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
  130. {
  131. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  132. int i;
  133. if (!S->stmt) {
  134. return 0;
  135. }
  136. if (S->pre_fetched) {
  137. S->pre_fetched = 0;
  138. return 1;
  139. }
  140. if (S->done) {
  141. return 0;
  142. }
  143. i = sqlite3_step(S->stmt);
  144. switch (i) {
  145. case SQLITE_ROW:
  146. return 1;
  147. case SQLITE_DONE:
  148. S->done = 1;
  149. sqlite3_reset(S->stmt);
  150. return 0;
  151. default:
  152. pdo_sqlite_error_stmt(stmt);
  153. return 0;
  154. }
  155. }
  156. static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
  157. {
  158. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  159. if(colno >= sqlite3_column_count(S->stmt)) {
  160. /* error invalid column */
  161. pdo_sqlite_error_stmt(stmt);
  162. return 0;
  163. }
  164. stmt->columns[colno].name = estrdup(sqlite3_column_name(S->stmt, colno));
  165. stmt->columns[colno].namelen = strlen(stmt->columns[colno].name);
  166. stmt->columns[colno].maxlen = 0xffffffff;
  167. stmt->columns[colno].precision = 0;
  168. switch (sqlite3_column_type(S->stmt, colno)) {
  169. case SQLITE_INTEGER:
  170. case SQLITE_FLOAT:
  171. case SQLITE3_TEXT:
  172. case SQLITE_BLOB:
  173. case SQLITE_NULL:
  174. default:
  175. stmt->columns[colno].param_type = PDO_PARAM_STR;
  176. break;
  177. }
  178. return 1;
  179. }
  180. static int pdo_sqlite_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
  181. {
  182. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  183. if (!S->stmt) {
  184. return 0;
  185. }
  186. if(colno >= sqlite3_data_count(S->stmt)) {
  187. /* error invalid column */
  188. pdo_sqlite_error_stmt(stmt);
  189. return 0;
  190. }
  191. switch (sqlite3_column_type(S->stmt, colno)) {
  192. case SQLITE_NULL:
  193. *ptr = NULL;
  194. *len = 0;
  195. return 1;
  196. case SQLITE_BLOB:
  197. *ptr = (char*)sqlite3_column_blob(S->stmt, colno);
  198. *len = sqlite3_column_bytes(S->stmt, colno);
  199. return 1;
  200. case SQLITE3_TEXT:
  201. *ptr = (char*)sqlite3_column_text(S->stmt, colno);
  202. *len = sqlite3_column_bytes(S->stmt, colno);
  203. if (*len) {
  204. /* sqlite3.h says "the NUL terminator is included in the byte count
  205. * for TEXT values" */
  206. *len--;
  207. }
  208. return 1;
  209. default:
  210. *ptr = (char*)sqlite3_column_text(S->stmt, colno);
  211. *len = sqlite3_column_bytes(S->stmt, colno);
  212. return 1;
  213. }
  214. }
  215. static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
  216. {
  217. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  218. char *str;
  219. zval *flags;
  220. if (!S->stmt) {
  221. return FAILURE;
  222. }
  223. if(colno >= sqlite3_data_count(S->stmt)) {
  224. /* error invalid column */
  225. pdo_sqlite_error_stmt(stmt);
  226. return FAILURE;
  227. }
  228. array_init(return_value);
  229. MAKE_STD_ZVAL(flags);
  230. array_init(flags);
  231. switch (sqlite3_column_type(S->stmt, colno)) {
  232. case SQLITE_NULL:
  233. add_assoc_string(return_value, "native_type", "null", 1);
  234. break;
  235. case SQLITE_FLOAT:
  236. add_assoc_string(return_value, "native_type", "double", 1);
  237. break;
  238. case SQLITE_BLOB:
  239. add_next_index_string(flags, "blob", 1);
  240. case SQLITE_TEXT:
  241. add_assoc_string(return_value, "native_type", "string", 1);
  242. break;
  243. case SQLITE_INTEGER:
  244. add_assoc_string(return_value, "native_type", "integer", 1);
  245. break;
  246. }
  247. str = (char*)sqlite3_column_decltype(S->stmt, colno);
  248. if (str) {
  249. add_assoc_string(return_value, "sqlite:decl_type", str, 1);
  250. }
  251. add_assoc_zval(return_value, "flags", flags);
  252. return SUCCESS;
  253. }
  254. static int pdo_sqlite_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
  255. {
  256. pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
  257. sqlite3_reset(S->stmt);
  258. return 1;
  259. }
  260. struct pdo_stmt_methods sqlite_stmt_methods = {
  261. pdo_sqlite_stmt_dtor,
  262. pdo_sqlite_stmt_execute,
  263. pdo_sqlite_stmt_fetch,
  264. pdo_sqlite_stmt_describe,
  265. pdo_sqlite_stmt_get_col,
  266. pdo_sqlite_stmt_param_hook,
  267. NULL, /* set_attr */
  268. NULL, /* get_attr */
  269. pdo_sqlite_stmt_col_meta,
  270. NULL, /* next_rowset */
  271. pdo_sqlite_stmt_cursor_closer
  272. };
  273. /*
  274. * Local variables:
  275. * tab-width: 4
  276. * c-basic-offset: 4
  277. * End:
  278. * vim600: noet sw=4 ts=4 fdm=marker
  279. * vim<600: noet sw=4 ts=4
  280. */