pdo_dbh.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Wez Furlong <wez@php.net> |
  14. | Marcus Boerger <helly@php.net> |
  15. | Sterling Hughes <sterling@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* The PDO Database Handle Class */
  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 "php_pdo.h"
  26. #include "php_pdo_driver.h"
  27. #include "php_pdo_int.h"
  28. #include "zend_exceptions.h"
  29. #include "zend_object_handlers.h"
  30. #include "zend_hash.h"
  31. #include "pdo_dbh_arginfo.h"
  32. static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value);
  33. void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error)
  34. {
  35. zval error_info,pdo_exception;
  36. char *pdo_exception_message;
  37. object_init_ex(&pdo_exception, php_pdo_get_exception());
  38. array_init(&error_info);
  39. add_next_index_string(&error_info, *pdo_error);
  40. add_next_index_long(&error_info, driver_errcode);
  41. add_next_index_string(&error_info, driver_errmsg);
  42. spprintf(&pdo_exception_message, 0,"SQLSTATE[%s] [%d] %s",*pdo_error, driver_errcode, driver_errmsg);
  43. zend_update_property(php_pdo_get_exception(), Z_OBJ(pdo_exception), "errorInfo", sizeof("errorInfo")-1, &error_info);
  44. zend_update_property_long(php_pdo_get_exception(), Z_OBJ(pdo_exception), "code", sizeof("code")-1, driver_errcode);
  45. zend_update_property_string(
  46. php_pdo_get_exception(),
  47. Z_OBJ(pdo_exception),
  48. "message",
  49. sizeof("message")-1,
  50. pdo_exception_message
  51. );
  52. efree(pdo_exception_message);
  53. zval_ptr_dtor(&error_info);
  54. zend_throw_exception_object(&pdo_exception);
  55. }
  56. void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *sqlstate, const char *supp) /* {{{ */
  57. {
  58. pdo_error_type *pdo_err = &dbh->error_code;
  59. char *message = NULL;
  60. const char *msg;
  61. if (dbh && dbh->error_mode == PDO_ERRMODE_SILENT) {
  62. #if 0
  63. /* BUG: if user is running in silent mode and hits an error at the driver level
  64. * when they use the PDO methods to call up the error information, they may
  65. * get bogus information */
  66. return;
  67. #endif
  68. }
  69. if (stmt) {
  70. pdo_err = &stmt->error_code;
  71. }
  72. strncpy(*pdo_err, sqlstate, 6);
  73. /* hash sqlstate to error messages */
  74. msg = pdo_sqlstate_state_to_description(*pdo_err);
  75. if (!msg) {
  76. msg = "<<Unknown error>>";
  77. }
  78. if (supp) {
  79. spprintf(&message, 0, "SQLSTATE[%s]: %s: %s", *pdo_err, msg, supp);
  80. } else {
  81. spprintf(&message, 0, "SQLSTATE[%s]: %s", *pdo_err, msg);
  82. }
  83. if (dbh && dbh->error_mode != PDO_ERRMODE_EXCEPTION) {
  84. php_error_docref(NULL, E_WARNING, "%s", message);
  85. } else {
  86. zval ex, info;
  87. zend_class_entry *pdo_ex = php_pdo_get_exception();
  88. object_init_ex(&ex, pdo_ex);
  89. zend_update_property_string(zend_ce_exception, Z_OBJ(ex), "message", sizeof("message")-1, message);
  90. zend_update_property_string(zend_ce_exception, Z_OBJ(ex), "code", sizeof("code")-1, *pdo_err);
  91. array_init(&info);
  92. add_next_index_string(&info, *pdo_err);
  93. add_next_index_long(&info, 0);
  94. zend_update_property(pdo_ex, Z_OBJ(ex), "errorInfo", sizeof("errorInfo")-1, &info);
  95. zval_ptr_dtor(&info);
  96. zend_throw_exception_object(&ex);
  97. }
  98. if (message) {
  99. efree(message);
  100. }
  101. }
  102. /* }}} */
  103. PDO_API void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt) /* {{{ */
  104. {
  105. pdo_error_type *pdo_err = &dbh->error_code;
  106. const char *msg = "<<Unknown>>";
  107. char *supp = NULL;
  108. zend_long native_code = 0;
  109. zend_string *message = NULL;
  110. zval info;
  111. if (dbh == NULL || dbh->error_mode == PDO_ERRMODE_SILENT) {
  112. return;
  113. }
  114. if (stmt) {
  115. pdo_err = &stmt->error_code;
  116. }
  117. /* hash sqlstate to error messages */
  118. msg = pdo_sqlstate_state_to_description(*pdo_err);
  119. if (!msg) {
  120. msg = "<<Unknown error>>";
  121. }
  122. ZVAL_UNDEF(&info);
  123. if (dbh->methods->fetch_err) {
  124. zval *item;
  125. array_init(&info);
  126. add_next_index_string(&info, *pdo_err);
  127. dbh->methods->fetch_err(dbh, stmt, &info);
  128. if ((item = zend_hash_index_find(Z_ARRVAL(info), 1)) != NULL
  129. && Z_TYPE_P(item) == IS_LONG) {
  130. native_code = Z_LVAL_P(item);
  131. }
  132. if ((item = zend_hash_index_find(Z_ARRVAL(info), 2)) != NULL) {
  133. supp = estrndup(Z_STRVAL_P(item), Z_STRLEN_P(item));
  134. }
  135. }
  136. if (native_code && supp) {
  137. message = strpprintf(0, "SQLSTATE[%s]: %s: " ZEND_LONG_FMT " %s", *pdo_err, msg, native_code, supp);
  138. } else if (supp) {
  139. message = strpprintf(0, "SQLSTATE[%s]: %s: %s", *pdo_err, msg, supp);
  140. } else {
  141. message = strpprintf(0, "SQLSTATE[%s]: %s", *pdo_err, msg);
  142. }
  143. if (dbh->error_mode == PDO_ERRMODE_WARNING) {
  144. php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(message));
  145. } else if (EG(exception) == NULL) {
  146. zval ex;
  147. zend_class_entry *pdo_ex = php_pdo_get_exception();
  148. object_init_ex(&ex, pdo_ex);
  149. zend_update_property_str(zend_ce_exception, Z_OBJ(ex), "message", sizeof("message") - 1, message);
  150. zend_update_property_string(zend_ce_exception, Z_OBJ(ex), "code", sizeof("code") - 1, *pdo_err);
  151. if (!Z_ISUNDEF(info)) {
  152. zend_update_property(pdo_ex, Z_OBJ(ex), "errorInfo", sizeof("errorInfo") - 1, &info);
  153. }
  154. zend_throw_exception_object(&ex);
  155. }
  156. if (!Z_ISUNDEF(info)) {
  157. zval_ptr_dtor(&info);
  158. }
  159. if (message) {
  160. zend_string_release_ex(message, 0);
  161. }
  162. if (supp) {
  163. efree(supp);
  164. }
  165. }
  166. /* }}} */
  167. static char *dsn_from_uri(char *uri, char *buf, size_t buflen) /* {{{ */
  168. {
  169. php_stream *stream;
  170. char *dsn = NULL;
  171. stream = php_stream_open_wrapper(uri, "rb", REPORT_ERRORS, NULL);
  172. if (stream) {
  173. dsn = php_stream_get_line(stream, buf, buflen, NULL);
  174. php_stream_close(stream);
  175. }
  176. return dsn;
  177. }
  178. /* }}} */
  179. /* {{{ */
  180. PHP_METHOD(PDO, __construct)
  181. {
  182. zval *object = ZEND_THIS;
  183. pdo_dbh_t *dbh = NULL;
  184. bool is_persistent = 0;
  185. char *data_source;
  186. size_t data_source_len;
  187. char *colon;
  188. char *username=NULL, *password=NULL;
  189. size_t usernamelen, passwordlen;
  190. pdo_driver_t *driver = NULL;
  191. zval *options = NULL;
  192. char alt_dsn[512];
  193. int call_factory = 1;
  194. zend_error_handling zeh;
  195. ZEND_PARSE_PARAMETERS_START(1, 4)
  196. Z_PARAM_STRING(data_source, data_source_len)
  197. Z_PARAM_OPTIONAL
  198. Z_PARAM_STRING_OR_NULL(username, usernamelen)
  199. Z_PARAM_STRING_OR_NULL(password, passwordlen)
  200. Z_PARAM_ARRAY_OR_NULL(options)
  201. ZEND_PARSE_PARAMETERS_END();
  202. /* parse the data source name */
  203. colon = strchr(data_source, ':');
  204. if (!colon) {
  205. /* let's see if this string has a matching dsn in the php.ini */
  206. char *ini_dsn = NULL;
  207. snprintf(alt_dsn, sizeof(alt_dsn), "pdo.dsn.%s", data_source);
  208. if (FAILURE == cfg_get_string(alt_dsn, &ini_dsn)) {
  209. zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source name");
  210. RETURN_THROWS();
  211. }
  212. data_source = ini_dsn;
  213. colon = strchr(data_source, ':');
  214. if (!colon) {
  215. zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source name (via INI: %s)", alt_dsn);
  216. RETURN_THROWS();
  217. }
  218. }
  219. if (!strncmp(data_source, "uri:", sizeof("uri:")-1)) {
  220. /* the specified URI holds connection details */
  221. data_source = dsn_from_uri(data_source + sizeof("uri:")-1, alt_dsn, sizeof(alt_dsn));
  222. if (!data_source) {
  223. zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source URI");
  224. RETURN_THROWS();
  225. }
  226. colon = strchr(data_source, ':');
  227. if (!colon) {
  228. zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source name (via URI)");
  229. RETURN_THROWS();
  230. }
  231. }
  232. driver = pdo_find_driver(data_source, colon - data_source);
  233. if (!driver) {
  234. /* NB: don't want to include the data_source in the error message as
  235. * it might contain a password */
  236. zend_throw_exception_ex(php_pdo_get_exception(), 0, "could not find driver");
  237. RETURN_THROWS();
  238. }
  239. dbh = Z_PDO_DBH_P(object);
  240. /* is this supposed to be a persistent connection ? */
  241. if (options) {
  242. int plen = 0;
  243. char *hashkey = NULL;
  244. zend_resource *le;
  245. pdo_dbh_t *pdbh = NULL;
  246. zval *v;
  247. if ((v = zend_hash_index_find_deref(Z_ARRVAL_P(options), PDO_ATTR_PERSISTENT)) != NULL) {
  248. if (Z_TYPE_P(v) == IS_STRING &&
  249. !is_numeric_string(Z_STRVAL_P(v), Z_STRLEN_P(v), NULL, NULL, 0) && Z_STRLEN_P(v) > 0) {
  250. /* user specified key */
  251. plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s:%s", data_source,
  252. username ? username : "",
  253. password ? password : "",
  254. Z_STRVAL_P(v));
  255. is_persistent = 1;
  256. } else {
  257. is_persistent = zval_get_long(v) ? 1 : 0;
  258. plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s", data_source,
  259. username ? username : "",
  260. password ? password : "");
  261. }
  262. }
  263. if (is_persistent) {
  264. /* let's see if we have one cached.... */
  265. if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashkey, plen)) != NULL) {
  266. if (le->type == php_pdo_list_entry()) {
  267. pdbh = (pdo_dbh_t*)le->ptr;
  268. /* is the connection still alive ? */
  269. if (pdbh->methods->check_liveness && FAILURE == (pdbh->methods->check_liveness)(pdbh)) {
  270. /* nope... need to kill it */
  271. pdbh->refcount--;
  272. zend_list_close(le);
  273. pdbh = NULL;
  274. }
  275. }
  276. }
  277. if (pdbh) {
  278. call_factory = 0;
  279. } else {
  280. /* need a brand new pdbh */
  281. pdbh = pecalloc(1, sizeof(*pdbh), 1);
  282. pdbh->refcount = 1;
  283. pdbh->is_persistent = 1;
  284. pdbh->persistent_id = pemalloc(plen + 1, 1);
  285. memcpy((char *)pdbh->persistent_id, hashkey, plen+1);
  286. pdbh->persistent_id_len = plen;
  287. pdbh->def_stmt_ce = dbh->def_stmt_ce;
  288. }
  289. }
  290. if (pdbh) {
  291. efree(dbh);
  292. /* switch over to the persistent one */
  293. Z_PDO_OBJECT_P(object)->inner = pdbh;
  294. pdbh->refcount++;
  295. dbh = pdbh;
  296. }
  297. if (hashkey) {
  298. efree(hashkey);
  299. }
  300. }
  301. if (call_factory) {
  302. dbh->data_source_len = strlen(colon + 1);
  303. dbh->data_source = (const char*)pestrdup(colon + 1, is_persistent);
  304. dbh->username = username ? pestrdup(username, is_persistent) : NULL;
  305. dbh->password = password ? pestrdup(password, is_persistent) : NULL;
  306. dbh->default_fetch_type = PDO_FETCH_BOTH;
  307. }
  308. dbh->auto_commit = pdo_attr_lval(options, PDO_ATTR_AUTOCOMMIT, 1);
  309. dbh->error_mode = pdo_attr_lval(options, PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
  310. if (!dbh->data_source || (username && !dbh->username) || (password && !dbh->password)) {
  311. php_error_docref(NULL, E_ERROR, "Out of memory");
  312. }
  313. /* pdo_dbh_attribute_set() can emit a Warning if the ERR_MODE is set to warning
  314. * As we are in a constructor we override the behaviour by replacing the error handler */
  315. zend_replace_error_handling(EH_THROW, pdo_exception_ce, &zeh);
  316. if (!call_factory) {
  317. /* we got a persistent guy from our cache */
  318. goto options;
  319. }
  320. if (driver->db_handle_factory(dbh, options)) {
  321. /* all set */
  322. if (is_persistent) {
  323. /* register in the persistent list etc. */
  324. /* we should also need to replace the object store entry,
  325. since it was created with emalloc */
  326. if ((zend_register_persistent_resource(
  327. (char*)dbh->persistent_id, dbh->persistent_id_len, dbh, php_pdo_list_entry())) == NULL) {
  328. php_error_docref(NULL, E_ERROR, "Failed to register persistent entry");
  329. }
  330. }
  331. dbh->driver = driver;
  332. options:
  333. if (options) {
  334. zval *attr_value;
  335. zend_ulong long_key;
  336. zend_string *str_key = NULL;
  337. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(options), long_key, str_key, attr_value) {
  338. if (str_key) {
  339. continue;
  340. }
  341. ZVAL_DEREF(attr_value);
  342. /* TODO: Should the constructor fail when the attribute cannot be set? */
  343. pdo_dbh_attribute_set(dbh, long_key, attr_value);
  344. } ZEND_HASH_FOREACH_END();
  345. }
  346. zend_restore_error_handling(&zeh);
  347. return;
  348. }
  349. /* the connection failed; things will tidy up in free_storage */
  350. if (is_persistent) {
  351. dbh->refcount--;
  352. }
  353. /* XXX raise exception */
  354. zend_restore_error_handling(&zeh);
  355. if (!EG(exception)) {
  356. zend_throw_exception(pdo_exception_ce, "Constructor failed", 0);
  357. }
  358. }
  359. /* }}} */
  360. static zval *pdo_stmt_instantiate(pdo_dbh_t *dbh, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
  361. {
  362. if (!Z_ISUNDEF_P(ctor_args)) {
  363. /* This implies an error within PDO if this does not hold */
  364. ZEND_ASSERT(Z_TYPE_P(ctor_args) == IS_ARRAY);
  365. if (!dbstmt_ce->constructor) {
  366. zend_throw_error(NULL, "User-supplied statement does not accept constructor arguments");
  367. return NULL;
  368. }
  369. }
  370. if (UNEXPECTED(object_init_ex(object, dbstmt_ce) != SUCCESS)) {
  371. if (EXPECTED(!EG(exception))) {
  372. zend_throw_error(NULL, "Cannot instantiate user-supplied statement class");
  373. }
  374. return NULL;
  375. }
  376. return object;
  377. } /* }}} */
  378. static void pdo_stmt_construct(zend_execute_data *execute_data, pdo_stmt_t *stmt, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
  379. {
  380. zval query_string;
  381. zend_string *key;
  382. ZVAL_STR(&query_string, stmt->query_string);
  383. key = zend_string_init("queryString", sizeof("queryString") - 1, 0);
  384. zend_std_write_property(Z_OBJ_P(object), key, &query_string, NULL);
  385. zend_string_release_ex(key, 0);
  386. if (dbstmt_ce->constructor) {
  387. zend_fcall_info fci;
  388. zend_fcall_info_cache fcc;
  389. zval retval;
  390. fci.size = sizeof(zend_fcall_info);
  391. ZVAL_UNDEF(&fci.function_name);
  392. fci.object = Z_OBJ_P(object);
  393. fci.retval = &retval;
  394. fci.param_count = 0;
  395. fci.params = NULL;
  396. fci.named_params = NULL;
  397. zend_fcall_info_args(&fci, ctor_args);
  398. fcc.function_handler = dbstmt_ce->constructor;
  399. fcc.called_scope = Z_OBJCE_P(object);
  400. fcc.object = Z_OBJ_P(object);
  401. if (zend_call_function(&fci, &fcc) != FAILURE) {
  402. zval_ptr_dtor(&retval);
  403. }
  404. zend_fcall_info_args_clear(&fci, 1);
  405. }
  406. }
  407. /* }}} */
  408. /* {{{ Prepares a statement for execution and returns a statement object */
  409. PHP_METHOD(PDO, prepare)
  410. {
  411. pdo_stmt_t *stmt;
  412. zend_string *statement;
  413. zval *options = NULL, *value, *item, ctor_args;
  414. zend_class_entry *dbstmt_ce, *pce;
  415. pdo_dbh_object_t *dbh_obj = Z_PDO_OBJECT_P(ZEND_THIS);
  416. pdo_dbh_t *dbh = dbh_obj->inner;
  417. ZEND_PARSE_PARAMETERS_START(1, 2)
  418. Z_PARAM_STR(statement)
  419. Z_PARAM_OPTIONAL
  420. Z_PARAM_ARRAY(options)
  421. ZEND_PARSE_PARAMETERS_END();
  422. PDO_CONSTRUCT_CHECK;
  423. if (ZSTR_LEN(statement) == 0) {
  424. zend_argument_value_error(1, "cannot be empty");
  425. RETURN_THROWS();
  426. }
  427. PDO_DBH_CLEAR_ERR();
  428. if (options && (value = zend_hash_index_find(Z_ARRVAL_P(options), PDO_ATTR_STATEMENT_CLASS)) != NULL) {
  429. if (Z_TYPE_P(value) != IS_ARRAY) {
  430. zend_type_error("PDO::ATTR_STATEMENT_CLASS value must be of type array, %s given",
  431. zend_zval_type_name(value));
  432. RETURN_THROWS();
  433. }
  434. if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 0)) == NULL) {
  435. zend_value_error("PDO::ATTR_STATEMENT_CLASS value must be an array with the format "
  436. "array(classname, constructor_args)");
  437. RETURN_THROWS();
  438. }
  439. if (Z_TYPE_P(item) != IS_STRING || (pce = zend_lookup_class(Z_STR_P(item))) == NULL) {
  440. zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be a valid class");
  441. RETURN_THROWS();
  442. }
  443. dbstmt_ce = pce;
  444. if (!instanceof_function(dbstmt_ce, pdo_dbstmt_ce)) {
  445. zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be derived from PDOStatement");
  446. RETURN_THROWS();
  447. }
  448. if (dbstmt_ce->constructor && !(dbstmt_ce->constructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED))) {
  449. zend_type_error("User-supplied statement class cannot have a public constructor");
  450. RETURN_THROWS();
  451. }
  452. if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) {
  453. if (Z_TYPE_P(item) != IS_ARRAY) {
  454. zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given",
  455. zend_zval_type_name(value));
  456. RETURN_THROWS();
  457. }
  458. ZVAL_COPY_VALUE(&ctor_args, item);
  459. } else {
  460. ZVAL_UNDEF(&ctor_args);
  461. }
  462. } else {
  463. dbstmt_ce = dbh->def_stmt_ce;
  464. ZVAL_COPY_VALUE(&ctor_args, &dbh->def_stmt_ctor_args);
  465. }
  466. if (!pdo_stmt_instantiate(dbh, return_value, dbstmt_ce, &ctor_args)) {
  467. RETURN_THROWS();
  468. }
  469. stmt = Z_PDO_STMT_P(return_value);
  470. /* unconditionally keep this for later reference */
  471. stmt->query_string = zend_string_copy(statement);
  472. stmt->default_fetch_type = dbh->default_fetch_type;
  473. stmt->dbh = dbh;
  474. /* give it a reference to me */
  475. ZVAL_OBJ_COPY(&stmt->database_object_handle, &dbh_obj->std);
  476. /* we haven't created a lazy object yet */
  477. ZVAL_UNDEF(&stmt->lazy_object_ref);
  478. if (dbh->methods->preparer(dbh, statement, stmt, options)) {
  479. pdo_stmt_construct(execute_data, stmt, return_value, dbstmt_ce, &ctor_args);
  480. return;
  481. }
  482. PDO_HANDLE_DBH_ERR();
  483. /* kill the object handle for the stmt here */
  484. zval_ptr_dtor(return_value);
  485. RETURN_FALSE;
  486. }
  487. /* }}} */
  488. static bool pdo_is_in_transaction(pdo_dbh_t *dbh) {
  489. if (dbh->methods->in_transaction) {
  490. return dbh->methods->in_transaction(dbh);
  491. }
  492. return dbh->in_txn;
  493. }
  494. /* {{{ Initiates a transaction */
  495. PHP_METHOD(PDO, beginTransaction)
  496. {
  497. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  498. ZEND_PARSE_PARAMETERS_NONE();
  499. PDO_CONSTRUCT_CHECK;
  500. if (pdo_is_in_transaction(dbh)) {
  501. zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is already an active transaction");
  502. RETURN_THROWS();
  503. }
  504. if (!dbh->methods->begin) {
  505. /* Throw an exception when the driver does not support transactions */
  506. zend_throw_exception_ex(php_pdo_get_exception(), 0, "This driver doesn't support transactions");
  507. RETURN_THROWS();
  508. }
  509. if (dbh->methods->begin(dbh)) {
  510. dbh->in_txn = true;
  511. RETURN_TRUE;
  512. }
  513. PDO_HANDLE_DBH_ERR();
  514. RETURN_FALSE;
  515. }
  516. /* }}} */
  517. /* {{{ Commit a transaction */
  518. PHP_METHOD(PDO, commit)
  519. {
  520. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  521. ZEND_PARSE_PARAMETERS_NONE();
  522. PDO_CONSTRUCT_CHECK;
  523. if (!pdo_is_in_transaction(dbh)) {
  524. zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is no active transaction");
  525. RETURN_THROWS();
  526. }
  527. if (dbh->methods->commit(dbh)) {
  528. dbh->in_txn = false;
  529. RETURN_TRUE;
  530. }
  531. PDO_HANDLE_DBH_ERR();
  532. RETURN_FALSE;
  533. }
  534. /* }}} */
  535. /* {{{ roll back a transaction */
  536. PHP_METHOD(PDO, rollBack)
  537. {
  538. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  539. ZEND_PARSE_PARAMETERS_NONE();
  540. PDO_CONSTRUCT_CHECK;
  541. if (!pdo_is_in_transaction(dbh)) {
  542. zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is no active transaction");
  543. RETURN_THROWS();
  544. }
  545. if (dbh->methods->rollback(dbh)) {
  546. dbh->in_txn = false;
  547. RETURN_TRUE;
  548. }
  549. PDO_HANDLE_DBH_ERR();
  550. RETURN_FALSE;
  551. }
  552. /* }}} */
  553. /* {{{ determine if inside a transaction */
  554. PHP_METHOD(PDO, inTransaction)
  555. {
  556. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  557. ZEND_PARSE_PARAMETERS_NONE();
  558. PDO_CONSTRUCT_CHECK;
  559. RETURN_BOOL(pdo_is_in_transaction(dbh));
  560. }
  561. /* }}} */
  562. PDO_API bool pdo_get_long_param(zend_long *lval, zval *value)
  563. {
  564. switch (Z_TYPE_P(value)) {
  565. case IS_LONG:
  566. case IS_TRUE:
  567. case IS_FALSE:
  568. *lval = zval_get_long(value);
  569. return true;
  570. case IS_STRING:
  571. if (IS_LONG == is_numeric_str_function(Z_STR_P(value), lval, NULL)) {
  572. return true;
  573. }
  574. ZEND_FALLTHROUGH;
  575. default:
  576. zend_type_error("Attribute value must be of type int for selected attribute, %s given", zend_zval_type_name(value));
  577. return false;
  578. }
  579. }
  580. PDO_API bool pdo_get_bool_param(bool *bval, zval *value)
  581. {
  582. switch (Z_TYPE_P(value)) {
  583. case IS_TRUE:
  584. *bval = true;
  585. return true;
  586. case IS_FALSE:
  587. *bval = false;
  588. return true;
  589. case IS_LONG:
  590. *bval = zval_is_true(value);
  591. return true;
  592. case IS_STRING: /* TODO Should string be allowed? */
  593. default:
  594. zend_type_error("Attribute value must be of type bool for selected attribute, %s given", zend_zval_type_name(value));
  595. return false;
  596. }
  597. }
  598. /* Return false on failure, true otherwise */
  599. static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /* {{{ */
  600. {
  601. zend_long lval;
  602. bool bval;
  603. switch (attr) {
  604. case PDO_ATTR_ERRMODE:
  605. if (!pdo_get_long_param(&lval, value)) {
  606. return false;
  607. }
  608. switch (lval) {
  609. case PDO_ERRMODE_SILENT:
  610. case PDO_ERRMODE_WARNING:
  611. case PDO_ERRMODE_EXCEPTION:
  612. dbh->error_mode = lval;
  613. return true;
  614. default:
  615. zend_value_error("Error mode must be one of the PDO::ERRMODE_* constants");
  616. return false;
  617. }
  618. return false;
  619. case PDO_ATTR_CASE:
  620. if (!pdo_get_long_param(&lval, value)) {
  621. return false;
  622. }
  623. switch (lval) {
  624. case PDO_CASE_NATURAL:
  625. case PDO_CASE_UPPER:
  626. case PDO_CASE_LOWER:
  627. dbh->desired_case = lval;
  628. return true;
  629. default:
  630. zend_value_error("Case folding mode must be one of the PDO::CASE_* constants");
  631. return false;
  632. }
  633. return false;
  634. case PDO_ATTR_ORACLE_NULLS:
  635. if (!pdo_get_long_param(&lval, value)) {
  636. return false;
  637. }
  638. /* TODO Check for valid value (NULL_NATURAL, NULL_EMPTY_STRING, NULL_TO_STRING)? */
  639. dbh->oracle_nulls = lval;
  640. return true;
  641. case PDO_ATTR_DEFAULT_FETCH_MODE:
  642. if (Z_TYPE_P(value) == IS_ARRAY) {
  643. zval *tmp;
  644. if ((tmp = zend_hash_index_find(Z_ARRVAL_P(value), 0)) != NULL && Z_TYPE_P(tmp) == IS_LONG) {
  645. if (Z_LVAL_P(tmp) == PDO_FETCH_INTO || Z_LVAL_P(tmp) == PDO_FETCH_CLASS) {
  646. zend_value_error("PDO::FETCH_INTO and PDO::FETCH_CLASS cannot be set as the default fetch mode");
  647. return false;
  648. }
  649. }
  650. lval = zval_get_long(value);
  651. } else {
  652. if (!pdo_get_long_param(&lval, value)) {
  653. return false;
  654. }
  655. }
  656. if (lval == PDO_FETCH_USE_DEFAULT) {
  657. zend_value_error("Fetch mode must be a bitmask of PDO::FETCH_* constants");
  658. return false;
  659. }
  660. dbh->default_fetch_type = lval;
  661. return true;
  662. case PDO_ATTR_STRINGIFY_FETCHES:
  663. if (!pdo_get_bool_param(&bval, value)) {
  664. return false;
  665. }
  666. dbh->stringify = bval;
  667. return true;
  668. case PDO_ATTR_STATEMENT_CLASS: {
  669. /* array(string classname, array(mixed ctor_args)) */
  670. zend_class_entry *pce;
  671. zval *item;
  672. if (dbh->is_persistent) {
  673. /* TODO: ValueError/ PDOException? */
  674. pdo_raise_impl_error(dbh, NULL, "HY000",
  675. "PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances"
  676. );
  677. PDO_HANDLE_DBH_ERR();
  678. return false;
  679. }
  680. if (Z_TYPE_P(value) != IS_ARRAY) {
  681. zend_type_error("PDO::ATTR_STATEMENT_CLASS value must be of type array, %s given",
  682. zend_zval_type_name(value));
  683. return false;
  684. }
  685. if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 0)) == NULL) {
  686. zend_value_error("PDO::ATTR_STATEMENT_CLASS value must be an array with the format "
  687. "array(classname, constructor_args)");
  688. return false;
  689. }
  690. if (Z_TYPE_P(item) != IS_STRING || (pce = zend_lookup_class(Z_STR_P(item))) == NULL) {
  691. zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be a valid class");
  692. return false;
  693. }
  694. if (!instanceof_function(pce, pdo_dbstmt_ce)) {
  695. zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be derived from PDOStatement");
  696. return false;
  697. }
  698. if (pce->constructor && !(pce->constructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED))) {
  699. zend_type_error("User-supplied statement class cannot have a public constructor");
  700. return false;
  701. }
  702. dbh->def_stmt_ce = pce;
  703. if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
  704. zval_ptr_dtor(&dbh->def_stmt_ctor_args);
  705. ZVAL_UNDEF(&dbh->def_stmt_ctor_args);
  706. }
  707. if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) {
  708. if (Z_TYPE_P(item) != IS_ARRAY) {
  709. zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given",
  710. zend_zval_type_name(value));
  711. return false;
  712. }
  713. ZVAL_COPY(&dbh->def_stmt_ctor_args, item);
  714. }
  715. return true;
  716. }
  717. /* Don't throw a ValueError as the attribute might be a driver specific one */
  718. default:;
  719. }
  720. if (!dbh->methods->set_attribute) {
  721. goto fail;
  722. }
  723. PDO_DBH_CLEAR_ERR();
  724. if (dbh->methods->set_attribute(dbh, attr, value)) {
  725. return true;
  726. }
  727. fail:
  728. if (!dbh->methods->set_attribute) {
  729. pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support setting attributes");
  730. } else {
  731. PDO_HANDLE_DBH_ERR();
  732. }
  733. return false;
  734. }
  735. /* }}} */
  736. /* {{{ Set an attribute */
  737. PHP_METHOD(PDO, setAttribute)
  738. {
  739. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  740. zend_long attr;
  741. zval *value;
  742. ZEND_PARSE_PARAMETERS_START(2, 2)
  743. Z_PARAM_LONG(attr)
  744. Z_PARAM_ZVAL(value)
  745. ZEND_PARSE_PARAMETERS_END();
  746. PDO_DBH_CLEAR_ERR();
  747. PDO_CONSTRUCT_CHECK;
  748. RETURN_BOOL(pdo_dbh_attribute_set(dbh, attr, value));
  749. }
  750. /* }}} */
  751. /* {{{ Get an attribute */
  752. PHP_METHOD(PDO, getAttribute)
  753. {
  754. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  755. zend_long attr;
  756. ZEND_PARSE_PARAMETERS_START(1, 1)
  757. Z_PARAM_LONG(attr)
  758. ZEND_PARSE_PARAMETERS_END();
  759. PDO_DBH_CLEAR_ERR();
  760. PDO_CONSTRUCT_CHECK;
  761. /* handle generic PDO-level attributes */
  762. switch (attr) {
  763. case PDO_ATTR_PERSISTENT:
  764. RETURN_BOOL(dbh->is_persistent);
  765. case PDO_ATTR_CASE:
  766. RETURN_LONG(dbh->desired_case);
  767. case PDO_ATTR_ORACLE_NULLS:
  768. RETURN_LONG(dbh->oracle_nulls);
  769. case PDO_ATTR_ERRMODE:
  770. RETURN_LONG(dbh->error_mode);
  771. case PDO_ATTR_DRIVER_NAME:
  772. RETURN_STRINGL((char*)dbh->driver->driver_name, dbh->driver->driver_name_len);
  773. case PDO_ATTR_STATEMENT_CLASS:
  774. array_init(return_value);
  775. add_next_index_str(return_value, zend_string_copy(dbh->def_stmt_ce->name));
  776. if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
  777. Z_TRY_ADDREF(dbh->def_stmt_ctor_args);
  778. add_next_index_zval(return_value, &dbh->def_stmt_ctor_args);
  779. }
  780. return;
  781. case PDO_ATTR_DEFAULT_FETCH_MODE:
  782. RETURN_LONG(dbh->default_fetch_type);
  783. default:
  784. break;
  785. }
  786. if (!dbh->methods->get_attribute) {
  787. pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support getting attributes");
  788. RETURN_FALSE;
  789. }
  790. switch (dbh->methods->get_attribute(dbh, attr, return_value)) {
  791. case -1:
  792. PDO_HANDLE_DBH_ERR();
  793. RETURN_FALSE;
  794. case 0:
  795. pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support that attribute");
  796. RETURN_FALSE;
  797. default:
  798. /* No error state, just return as the return_value has been assigned
  799. * by the get_attribute handler */
  800. return;
  801. }
  802. }
  803. /* }}} */
  804. /* {{{ Execute a statement that does not return a row set, returning the number of affected rows */
  805. PHP_METHOD(PDO, exec)
  806. {
  807. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  808. zend_string *statement;
  809. zend_long ret;
  810. ZEND_PARSE_PARAMETERS_START(1, 1)
  811. Z_PARAM_STR(statement)
  812. ZEND_PARSE_PARAMETERS_END();
  813. if (ZSTR_LEN(statement) == 0) {
  814. zend_argument_value_error(1, "cannot be empty");
  815. RETURN_THROWS();
  816. }
  817. PDO_DBH_CLEAR_ERR();
  818. PDO_CONSTRUCT_CHECK;
  819. ret = dbh->methods->doer(dbh, statement);
  820. if (ret == -1) {
  821. PDO_HANDLE_DBH_ERR();
  822. RETURN_FALSE;
  823. } else {
  824. RETURN_LONG(ret);
  825. }
  826. }
  827. /* }}} */
  828. /* {{{ Returns the id of the last row that we affected on this connection. Some databases require a sequence or table name to be passed in. Not always meaningful. */
  829. PHP_METHOD(PDO, lastInsertId)
  830. {
  831. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  832. zend_string *name = NULL;
  833. zend_string *last_id = NULL;
  834. ZEND_PARSE_PARAMETERS_START(0, 1)
  835. Z_PARAM_OPTIONAL
  836. Z_PARAM_STR_OR_NULL(name)
  837. ZEND_PARSE_PARAMETERS_END();
  838. PDO_CONSTRUCT_CHECK;
  839. PDO_DBH_CLEAR_ERR();
  840. if (!dbh->methods->last_id) {
  841. pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support lastInsertId()");
  842. RETURN_FALSE;
  843. }
  844. last_id = dbh->methods->last_id(dbh, name);
  845. if (!last_id) {
  846. PDO_HANDLE_DBH_ERR();
  847. RETURN_FALSE;
  848. }
  849. RETURN_STR(last_id);
  850. }
  851. /* }}} */
  852. /* {{{ Fetch the error code associated with the last operation on the database handle */
  853. PHP_METHOD(PDO, errorCode)
  854. {
  855. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  856. ZEND_PARSE_PARAMETERS_NONE();
  857. PDO_CONSTRUCT_CHECK;
  858. if (dbh->query_stmt) {
  859. RETURN_STRING(dbh->query_stmt->error_code);
  860. }
  861. if (dbh->error_code[0] == '\0') {
  862. RETURN_NULL();
  863. }
  864. /**
  865. * Making sure that we fallback to the default implementation
  866. * if the dbh->error_code is not null.
  867. */
  868. RETURN_STRING(dbh->error_code);
  869. }
  870. /* }}} */
  871. /* {{{ Fetch extended error information associated with the last operation on the database handle */
  872. PHP_METHOD(PDO, errorInfo)
  873. {
  874. int error_count;
  875. int error_count_diff = 0;
  876. int error_expected_count = 3;
  877. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  878. ZEND_PARSE_PARAMETERS_NONE();
  879. PDO_CONSTRUCT_CHECK;
  880. array_init(return_value);
  881. if (dbh->query_stmt) {
  882. add_next_index_string(return_value, dbh->query_stmt->error_code);
  883. if(!strncmp(dbh->query_stmt->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE))) goto fill_array;
  884. } else {
  885. add_next_index_string(return_value, dbh->error_code);
  886. if(!strncmp(dbh->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE))) goto fill_array;
  887. }
  888. if (dbh->methods->fetch_err) {
  889. dbh->methods->fetch_err(dbh, dbh->query_stmt, return_value);
  890. }
  891. fill_array:
  892. /**
  893. * In order to be consistent, we have to make sure we add the good amount
  894. * of nulls depending on the current number of elements. We make a simple
  895. * difference and add the needed elements
  896. */
  897. error_count = zend_hash_num_elements(Z_ARRVAL_P(return_value));
  898. if (error_expected_count > error_count) {
  899. int current_index;
  900. error_count_diff = error_expected_count - error_count;
  901. for (current_index = 0; current_index < error_count_diff; current_index++) {
  902. add_next_index_null(return_value);
  903. }
  904. }
  905. }
  906. /* }}} */
  907. /* {{{ Prepare and execute $sql; returns the statement object for iteration */
  908. PHP_METHOD(PDO, query)
  909. {
  910. pdo_stmt_t *stmt;
  911. zend_string *statement;
  912. zend_long fetch_mode;
  913. bool fetch_mode_is_null = 1;
  914. zval *args = NULL;
  915. uint32_t num_args = 0;
  916. pdo_dbh_object_t *dbh_obj = Z_PDO_OBJECT_P(ZEND_THIS);
  917. pdo_dbh_t *dbh = dbh_obj->inner;
  918. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S|l!*", &statement,
  919. &fetch_mode, &fetch_mode_is_null, &args, &num_args)) {
  920. RETURN_THROWS();
  921. }
  922. PDO_CONSTRUCT_CHECK;
  923. if (ZSTR_LEN(statement) == 0) {
  924. zend_argument_value_error(1, "cannot be empty");
  925. RETURN_THROWS();
  926. }
  927. PDO_DBH_CLEAR_ERR();
  928. if (!pdo_stmt_instantiate(dbh, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args)) {
  929. RETURN_THROWS();
  930. }
  931. stmt = Z_PDO_STMT_P(return_value);
  932. /* unconditionally keep this for later reference */
  933. stmt->query_string = zend_string_copy(statement);
  934. stmt->active_query_string = zend_string_copy(stmt->query_string);
  935. stmt->default_fetch_type = dbh->default_fetch_type;
  936. stmt->dbh = dbh;
  937. /* give it a reference to me */
  938. ZVAL_OBJ_COPY(&stmt->database_object_handle, &dbh_obj->std);
  939. /* we haven't created a lazy object yet */
  940. ZVAL_UNDEF(&stmt->lazy_object_ref);
  941. if (dbh->methods->preparer(dbh, statement, stmt, NULL)) {
  942. PDO_STMT_CLEAR_ERR();
  943. if (fetch_mode_is_null || pdo_stmt_setup_fetch_mode(stmt, fetch_mode, 2, args, num_args)) {
  944. /* now execute the statement */
  945. PDO_STMT_CLEAR_ERR();
  946. if (stmt->methods->executer(stmt)) {
  947. bool ret = true;
  948. if (!stmt->executed) {
  949. if (stmt->dbh->alloc_own_columns) {
  950. ret = pdo_stmt_describe_columns(stmt);
  951. }
  952. stmt->executed = 1;
  953. }
  954. if (ret) {
  955. pdo_stmt_construct(execute_data, stmt, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args);
  956. return;
  957. }
  958. }
  959. }
  960. /* something broke */
  961. dbh->query_stmt = stmt;
  962. ZVAL_OBJ(&dbh->query_stmt_zval, Z_OBJ_P(return_value));
  963. Z_DELREF(stmt->database_object_handle);
  964. ZVAL_UNDEF(&stmt->database_object_handle);
  965. PDO_HANDLE_STMT_ERR();
  966. } else {
  967. PDO_HANDLE_DBH_ERR();
  968. zval_ptr_dtor(return_value);
  969. }
  970. RETURN_FALSE;
  971. }
  972. /* }}} */
  973. /* {{{ quotes string for use in a query.
  974. * The optional paramtype acts as a hint for drivers that have alternate quoting styles.
  975. * The default value is PDO_PARAM_STR */
  976. PHP_METHOD(PDO, quote)
  977. {
  978. pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
  979. zend_string *str;
  980. zend_long paramtype = PDO_PARAM_STR;
  981. ZEND_PARSE_PARAMETERS_START(1, 2)
  982. Z_PARAM_STR(str)
  983. Z_PARAM_OPTIONAL
  984. Z_PARAM_LONG(paramtype)
  985. ZEND_PARSE_PARAMETERS_END();
  986. PDO_CONSTRUCT_CHECK;
  987. PDO_DBH_CLEAR_ERR();
  988. if (!dbh->methods->quoter) {
  989. pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support quoting");
  990. RETURN_FALSE;
  991. }
  992. RETURN_STR(dbh->methods->quoter(dbh, str, paramtype));
  993. }
  994. /* }}} */
  995. /* {{{ Return array of available PDO drivers */
  996. PHP_METHOD(PDO, getAvailableDrivers)
  997. {
  998. pdo_driver_t *pdriver;
  999. ZEND_PARSE_PARAMETERS_NONE();
  1000. array_init(return_value);
  1001. ZEND_HASH_FOREACH_PTR(&pdo_driver_hash, pdriver) {
  1002. add_next_index_stringl(return_value, (char*)pdriver->driver_name, pdriver->driver_name_len);
  1003. } ZEND_HASH_FOREACH_END();
  1004. }
  1005. /* }}} */
  1006. static void cls_method_dtor(zval *el) /* {{{ */ {
  1007. zend_function *func = (zend_function*)Z_PTR_P(el);
  1008. if (func->common.function_name) {
  1009. zend_string_release_ex(func->common.function_name, 0);
  1010. }
  1011. efree(func);
  1012. }
  1013. /* }}} */
  1014. static void cls_method_pdtor(zval *el) /* {{{ */ {
  1015. zend_function *func = (zend_function*)Z_PTR_P(el);
  1016. if (func->common.function_name) {
  1017. zend_string_release_ex(func->common.function_name, 1);
  1018. }
  1019. pefree(func, 1);
  1020. }
  1021. /* }}} */
  1022. /* {{{ overloaded object handlers for PDO class */
  1023. bool pdo_hash_methods(pdo_dbh_object_t *dbh_obj, int kind)
  1024. {
  1025. const zend_function_entry *funcs;
  1026. zend_internal_function func;
  1027. size_t namelen;
  1028. char *lc_name;
  1029. pdo_dbh_t *dbh = dbh_obj->inner;
  1030. if (!dbh || !dbh->methods || !dbh->methods->get_driver_methods) {
  1031. return false;
  1032. }
  1033. funcs = dbh->methods->get_driver_methods(dbh, kind);
  1034. if (!funcs) {
  1035. return false;
  1036. }
  1037. dbh->cls_methods[kind] = pemalloc(sizeof(HashTable), dbh->is_persistent);
  1038. zend_hash_init(dbh->cls_methods[kind], 8, NULL,
  1039. dbh->is_persistent? cls_method_pdtor : cls_method_dtor, dbh->is_persistent);
  1040. memset(&func, 0, sizeof(func));
  1041. while (funcs->fname) {
  1042. func.type = ZEND_INTERNAL_FUNCTION;
  1043. func.handler = funcs->handler;
  1044. func.function_name = zend_string_init(funcs->fname, strlen(funcs->fname), dbh->is_persistent);
  1045. func.scope = dbh_obj->std.ce;
  1046. func.prototype = NULL;
  1047. if (funcs->flags) {
  1048. func.fn_flags = funcs->flags | ZEND_ACC_NEVER_CACHE;
  1049. } else {
  1050. func.fn_flags = ZEND_ACC_PUBLIC | ZEND_ACC_NEVER_CACHE;
  1051. }
  1052. if (funcs->arg_info) {
  1053. zend_internal_function_info *info = (zend_internal_function_info*)funcs->arg_info;
  1054. func.arg_info = (zend_internal_arg_info*)funcs->arg_info + 1;
  1055. func.num_args = funcs->num_args;
  1056. if (info->required_num_args == (uint32_t)-1) {
  1057. func.required_num_args = funcs->num_args;
  1058. } else {
  1059. func.required_num_args = info->required_num_args;
  1060. }
  1061. if (ZEND_ARG_SEND_MODE(info)) {
  1062. func.fn_flags |= ZEND_ACC_RETURN_REFERENCE;
  1063. }
  1064. if (ZEND_ARG_IS_VARIADIC(&funcs->arg_info[funcs->num_args])) {
  1065. func.fn_flags |= ZEND_ACC_VARIADIC;
  1066. /* Don't count the variadic argument */
  1067. func.num_args--;
  1068. }
  1069. } else {
  1070. func.arg_info = NULL;
  1071. func.num_args = 0;
  1072. func.required_num_args = 0;
  1073. }
  1074. zend_set_function_arg_flags((zend_function*)&func);
  1075. namelen = strlen(funcs->fname);
  1076. lc_name = emalloc(namelen+1);
  1077. zend_str_tolower_copy(lc_name, funcs->fname, namelen);
  1078. zend_hash_str_add_mem(dbh->cls_methods[kind], lc_name, namelen, &func, sizeof(func));
  1079. efree(lc_name);
  1080. funcs++;
  1081. }
  1082. return true;
  1083. }
  1084. static zend_function *dbh_method_get(zend_object **object, zend_string *method_name, const zval *key)
  1085. {
  1086. zend_function *fbc = NULL;
  1087. pdo_dbh_object_t *dbh_obj = php_pdo_dbh_fetch_object(*object);
  1088. zend_string *lc_method_name;
  1089. if ((fbc = zend_std_get_method(object, method_name, key)) == NULL) {
  1090. /* not a pre-defined method, nor a user-defined method; check
  1091. * the driver specific methods */
  1092. if (!dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
  1093. if (!pdo_hash_methods(dbh_obj,
  1094. PDO_DBH_DRIVER_METHOD_KIND_DBH)
  1095. || !dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
  1096. goto out;
  1097. }
  1098. }
  1099. lc_method_name = zend_string_tolower(method_name);
  1100. fbc = zend_hash_find_ptr(dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH], lc_method_name);
  1101. zend_string_release_ex(lc_method_name, 0);
  1102. }
  1103. out:
  1104. return fbc;
  1105. }
  1106. static HashTable *dbh_get_gc(zend_object *object, zval **gc_data, int *gc_count)
  1107. {
  1108. pdo_dbh_t *dbh = php_pdo_dbh_fetch_inner(object);
  1109. zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
  1110. zend_get_gc_buffer_add_zval(gc_buffer, &dbh->def_stmt_ctor_args);
  1111. if (dbh->methods && dbh->methods->get_gc) {
  1112. dbh->methods->get_gc(dbh, gc_buffer);
  1113. }
  1114. zend_get_gc_buffer_use(gc_buffer, gc_data, gc_count);
  1115. return zend_std_get_properties(object);
  1116. }
  1117. static zend_object_handlers pdo_dbh_object_handlers;
  1118. static void pdo_dbh_free_storage(zend_object *std);
  1119. void pdo_dbh_init(void)
  1120. {
  1121. pdo_dbh_ce = register_class_PDO();
  1122. pdo_dbh_ce->create_object = pdo_dbh_new;
  1123. memcpy(&pdo_dbh_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  1124. pdo_dbh_object_handlers.offset = XtOffsetOf(pdo_dbh_object_t, std);
  1125. pdo_dbh_object_handlers.free_obj = pdo_dbh_free_storage;
  1126. pdo_dbh_object_handlers.clone_obj = NULL;
  1127. pdo_dbh_object_handlers.get_method = dbh_method_get;
  1128. pdo_dbh_object_handlers.compare = zend_objects_not_comparable;
  1129. pdo_dbh_object_handlers.get_gc = dbh_get_gc;
  1130. REGISTER_PDO_CLASS_CONST_LONG("PARAM_BOOL", (zend_long)PDO_PARAM_BOOL);
  1131. REGISTER_PDO_CLASS_CONST_LONG("PARAM_NULL", (zend_long)PDO_PARAM_NULL);
  1132. REGISTER_PDO_CLASS_CONST_LONG("PARAM_INT", (zend_long)PDO_PARAM_INT);
  1133. REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR", (zend_long)PDO_PARAM_STR);
  1134. REGISTER_PDO_CLASS_CONST_LONG("PARAM_LOB", (zend_long)PDO_PARAM_LOB);
  1135. REGISTER_PDO_CLASS_CONST_LONG("PARAM_STMT", (zend_long)PDO_PARAM_STMT);
  1136. REGISTER_PDO_CLASS_CONST_LONG("PARAM_INPUT_OUTPUT", (zend_long)PDO_PARAM_INPUT_OUTPUT);
  1137. REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR_NATL", (zend_long)PDO_PARAM_STR_NATL);
  1138. REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR_CHAR", (zend_long)PDO_PARAM_STR_CHAR);
  1139. REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_ALLOC", (zend_long)PDO_PARAM_EVT_ALLOC);
  1140. REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FREE", (zend_long)PDO_PARAM_EVT_FREE);
  1141. REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_EXEC_PRE", (zend_long)PDO_PARAM_EVT_EXEC_PRE);
  1142. REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_EXEC_POST", (zend_long)PDO_PARAM_EVT_EXEC_POST);
  1143. REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FETCH_PRE", (zend_long)PDO_PARAM_EVT_FETCH_PRE);
  1144. REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FETCH_POST", (zend_long)PDO_PARAM_EVT_FETCH_POST);
  1145. REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_NORMALIZE", (zend_long)PDO_PARAM_EVT_NORMALIZE);
  1146. REGISTER_PDO_CLASS_CONST_LONG("FETCH_DEFAULT", (zend_long)PDO_FETCH_USE_DEFAULT);
  1147. REGISTER_PDO_CLASS_CONST_LONG("FETCH_LAZY", (zend_long)PDO_FETCH_LAZY);
  1148. REGISTER_PDO_CLASS_CONST_LONG("FETCH_ASSOC", (zend_long)PDO_FETCH_ASSOC);
  1149. REGISTER_PDO_CLASS_CONST_LONG("FETCH_NUM", (zend_long)PDO_FETCH_NUM);
  1150. REGISTER_PDO_CLASS_CONST_LONG("FETCH_BOTH", (zend_long)PDO_FETCH_BOTH);
  1151. REGISTER_PDO_CLASS_CONST_LONG("FETCH_OBJ", (zend_long)PDO_FETCH_OBJ);
  1152. REGISTER_PDO_CLASS_CONST_LONG("FETCH_BOUND", (zend_long)PDO_FETCH_BOUND);
  1153. REGISTER_PDO_CLASS_CONST_LONG("FETCH_COLUMN", (zend_long)PDO_FETCH_COLUMN);
  1154. REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASS", (zend_long)PDO_FETCH_CLASS);
  1155. REGISTER_PDO_CLASS_CONST_LONG("FETCH_INTO", (zend_long)PDO_FETCH_INTO);
  1156. REGISTER_PDO_CLASS_CONST_LONG("FETCH_FUNC", (zend_long)PDO_FETCH_FUNC);
  1157. REGISTER_PDO_CLASS_CONST_LONG("FETCH_GROUP", (zend_long)PDO_FETCH_GROUP);
  1158. REGISTER_PDO_CLASS_CONST_LONG("FETCH_UNIQUE", (zend_long)PDO_FETCH_UNIQUE);
  1159. REGISTER_PDO_CLASS_CONST_LONG("FETCH_KEY_PAIR", (zend_long)PDO_FETCH_KEY_PAIR);
  1160. REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASSTYPE", (zend_long)PDO_FETCH_CLASSTYPE);
  1161. REGISTER_PDO_CLASS_CONST_LONG("FETCH_SERIALIZE",(zend_long)PDO_FETCH_SERIALIZE);
  1162. REGISTER_PDO_CLASS_CONST_LONG("FETCH_PROPS_LATE", (zend_long)PDO_FETCH_PROPS_LATE);
  1163. REGISTER_PDO_CLASS_CONST_LONG("FETCH_NAMED", (zend_long)PDO_FETCH_NAMED);
  1164. REGISTER_PDO_CLASS_CONST_LONG("ATTR_AUTOCOMMIT", (zend_long)PDO_ATTR_AUTOCOMMIT);
  1165. REGISTER_PDO_CLASS_CONST_LONG("ATTR_PREFETCH", (zend_long)PDO_ATTR_PREFETCH);
  1166. REGISTER_PDO_CLASS_CONST_LONG("ATTR_TIMEOUT", (zend_long)PDO_ATTR_TIMEOUT);
  1167. REGISTER_PDO_CLASS_CONST_LONG("ATTR_ERRMODE", (zend_long)PDO_ATTR_ERRMODE);
  1168. REGISTER_PDO_CLASS_CONST_LONG("ATTR_SERVER_VERSION", (zend_long)PDO_ATTR_SERVER_VERSION);
  1169. REGISTER_PDO_CLASS_CONST_LONG("ATTR_CLIENT_VERSION", (zend_long)PDO_ATTR_CLIENT_VERSION);
  1170. REGISTER_PDO_CLASS_CONST_LONG("ATTR_SERVER_INFO", (zend_long)PDO_ATTR_SERVER_INFO);
  1171. REGISTER_PDO_CLASS_CONST_LONG("ATTR_CONNECTION_STATUS", (zend_long)PDO_ATTR_CONNECTION_STATUS);
  1172. REGISTER_PDO_CLASS_CONST_LONG("ATTR_CASE", (zend_long)PDO_ATTR_CASE);
  1173. REGISTER_PDO_CLASS_CONST_LONG("ATTR_CURSOR_NAME", (zend_long)PDO_ATTR_CURSOR_NAME);
  1174. REGISTER_PDO_CLASS_CONST_LONG("ATTR_CURSOR", (zend_long)PDO_ATTR_CURSOR);
  1175. REGISTER_PDO_CLASS_CONST_LONG("ATTR_ORACLE_NULLS", (zend_long)PDO_ATTR_ORACLE_NULLS);
  1176. REGISTER_PDO_CLASS_CONST_LONG("ATTR_PERSISTENT", (zend_long)PDO_ATTR_PERSISTENT);
  1177. REGISTER_PDO_CLASS_CONST_LONG("ATTR_STATEMENT_CLASS", (zend_long)PDO_ATTR_STATEMENT_CLASS);
  1178. REGISTER_PDO_CLASS_CONST_LONG("ATTR_FETCH_TABLE_NAMES", (zend_long)PDO_ATTR_FETCH_TABLE_NAMES);
  1179. REGISTER_PDO_CLASS_CONST_LONG("ATTR_FETCH_CATALOG_NAMES", (zend_long)PDO_ATTR_FETCH_CATALOG_NAMES);
  1180. REGISTER_PDO_CLASS_CONST_LONG("ATTR_DRIVER_NAME", (zend_long)PDO_ATTR_DRIVER_NAME);
  1181. REGISTER_PDO_CLASS_CONST_LONG("ATTR_STRINGIFY_FETCHES", (zend_long)PDO_ATTR_STRINGIFY_FETCHES);
  1182. REGISTER_PDO_CLASS_CONST_LONG("ATTR_MAX_COLUMN_LEN", (zend_long)PDO_ATTR_MAX_COLUMN_LEN);
  1183. REGISTER_PDO_CLASS_CONST_LONG("ATTR_EMULATE_PREPARES", (zend_long)PDO_ATTR_EMULATE_PREPARES);
  1184. REGISTER_PDO_CLASS_CONST_LONG("ATTR_DEFAULT_FETCH_MODE", (zend_long)PDO_ATTR_DEFAULT_FETCH_MODE);
  1185. REGISTER_PDO_CLASS_CONST_LONG("ATTR_DEFAULT_STR_PARAM", (zend_long)PDO_ATTR_DEFAULT_STR_PARAM);
  1186. REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_SILENT", (zend_long)PDO_ERRMODE_SILENT);
  1187. REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_WARNING", (zend_long)PDO_ERRMODE_WARNING);
  1188. REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_EXCEPTION", (zend_long)PDO_ERRMODE_EXCEPTION);
  1189. REGISTER_PDO_CLASS_CONST_LONG("CASE_NATURAL", (zend_long)PDO_CASE_NATURAL);
  1190. REGISTER_PDO_CLASS_CONST_LONG("CASE_LOWER", (zend_long)PDO_CASE_LOWER);
  1191. REGISTER_PDO_CLASS_CONST_LONG("CASE_UPPER", (zend_long)PDO_CASE_UPPER);
  1192. REGISTER_PDO_CLASS_CONST_LONG("NULL_NATURAL", (zend_long)PDO_NULL_NATURAL);
  1193. REGISTER_PDO_CLASS_CONST_LONG("NULL_EMPTY_STRING", (zend_long)PDO_NULL_EMPTY_STRING);
  1194. REGISTER_PDO_CLASS_CONST_LONG("NULL_TO_STRING", (zend_long)PDO_NULL_TO_STRING);
  1195. REGISTER_PDO_CLASS_CONST_STRING("ERR_NONE", PDO_ERR_NONE);
  1196. REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_NEXT", (zend_long)PDO_FETCH_ORI_NEXT);
  1197. REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_PRIOR", (zend_long)PDO_FETCH_ORI_PRIOR);
  1198. REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_FIRST", (zend_long)PDO_FETCH_ORI_FIRST);
  1199. REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_LAST", (zend_long)PDO_FETCH_ORI_LAST);
  1200. REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_ABS", (zend_long)PDO_FETCH_ORI_ABS);
  1201. REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_REL", (zend_long)PDO_FETCH_ORI_REL);
  1202. REGISTER_PDO_CLASS_CONST_LONG("CURSOR_FWDONLY", (zend_long)PDO_CURSOR_FWDONLY);
  1203. REGISTER_PDO_CLASS_CONST_LONG("CURSOR_SCROLL", (zend_long)PDO_CURSOR_SCROLL);
  1204. }
  1205. static void dbh_free(pdo_dbh_t *dbh, bool free_persistent)
  1206. {
  1207. int i;
  1208. if (dbh->query_stmt) {
  1209. zval_ptr_dtor(&dbh->query_stmt_zval);
  1210. dbh->query_stmt = NULL;
  1211. }
  1212. if (dbh->is_persistent) {
  1213. #if ZEND_DEBUG
  1214. ZEND_ASSERT(!free_persistent || (dbh->refcount == 1));
  1215. #endif
  1216. if (!free_persistent && (--dbh->refcount)) {
  1217. return;
  1218. }
  1219. }
  1220. if (dbh->methods) {
  1221. dbh->methods->closer(dbh);
  1222. }
  1223. if (dbh->data_source) {
  1224. pefree((char *)dbh->data_source, dbh->is_persistent);
  1225. }
  1226. if (dbh->username) {
  1227. pefree(dbh->username, dbh->is_persistent);
  1228. }
  1229. if (dbh->password) {
  1230. pefree(dbh->password, dbh->is_persistent);
  1231. }
  1232. if (dbh->persistent_id) {
  1233. pefree((char *)dbh->persistent_id, dbh->is_persistent);
  1234. }
  1235. if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
  1236. zval_ptr_dtor(&dbh->def_stmt_ctor_args);
  1237. }
  1238. for (i = 0; i < PDO_DBH_DRIVER_METHOD_KIND__MAX; i++) {
  1239. if (dbh->cls_methods[i]) {
  1240. zend_hash_destroy(dbh->cls_methods[i]);
  1241. pefree(dbh->cls_methods[i], dbh->is_persistent);
  1242. }
  1243. }
  1244. pefree(dbh, dbh->is_persistent);
  1245. }
  1246. static void pdo_dbh_free_storage(zend_object *std)
  1247. {
  1248. pdo_dbh_t *dbh = php_pdo_dbh_fetch_inner(std);
  1249. if (dbh->driver_data && dbh->methods && dbh->methods->rollback && pdo_is_in_transaction(dbh)) {
  1250. dbh->methods->rollback(dbh);
  1251. dbh->in_txn = false;
  1252. }
  1253. if (dbh->is_persistent && dbh->methods && dbh->methods->persistent_shutdown) {
  1254. dbh->methods->persistent_shutdown(dbh);
  1255. }
  1256. zend_object_std_dtor(std);
  1257. dbh_free(dbh, 0);
  1258. }
  1259. zend_object *pdo_dbh_new(zend_class_entry *ce)
  1260. {
  1261. pdo_dbh_object_t *dbh;
  1262. dbh = zend_object_alloc(sizeof(pdo_dbh_object_t), ce);
  1263. zend_object_std_init(&dbh->std, ce);
  1264. object_properties_init(&dbh->std, ce);
  1265. rebuild_object_properties(&dbh->std);
  1266. dbh->inner = ecalloc(1, sizeof(pdo_dbh_t));
  1267. dbh->inner->def_stmt_ce = pdo_dbstmt_ce;
  1268. dbh->std.handlers = &pdo_dbh_object_handlers;
  1269. return &dbh->std;
  1270. }
  1271. /* }}} */
  1272. ZEND_RSRC_DTOR_FUNC(php_pdo_pdbh_dtor) /* {{{ */
  1273. {
  1274. if (res->ptr) {
  1275. pdo_dbh_t *dbh = (pdo_dbh_t*)res->ptr;
  1276. dbh_free(dbh, 1);
  1277. res->ptr = NULL;
  1278. }
  1279. }
  1280. /* }}} */