pgsql_driver.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. | Authors: Edin Kadribasic <edink@emini.dk> |
  16. | Ilia Alshanestsky <ilia@prohost.org> |
  17. | Wez Furlong <wez@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_ini.h"
  25. #include "ext/standard/info.h"
  26. #include "ext/standard/php_string.h"
  27. #include "main/php_network.h"
  28. #include "pdo/php_pdo.h"
  29. #include "pdo/php_pdo_driver.h"
  30. #include "pdo/php_pdo_error.h"
  31. #include "ext/standard/file.h"
  32. #undef PACKAGE_BUGREPORT
  33. #undef PACKAGE_NAME
  34. #undef PACKAGE_STRING
  35. #undef PACKAGE_TARNAME
  36. #undef PACKAGE_VERSION
  37. #include "pg_config.h" /* needed for PG_VERSION */
  38. #include "php_pdo_pgsql.h"
  39. #include "php_pdo_pgsql_int.h"
  40. #include "zend_exceptions.h"
  41. static char * _pdo_pgsql_trim_message(const char *message, int persistent)
  42. {
  43. register int i = strlen(message)-1;
  44. char *tmp;
  45. if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
  46. --i;
  47. }
  48. while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
  49. --i;
  50. }
  51. ++i;
  52. tmp = pemalloc(i + 1, persistent);
  53. memcpy(tmp, message, i);
  54. tmp[i] = '\0';
  55. return tmp;
  56. }
  57. static zend_string* _pdo_pgsql_escape_credentials(char *str)
  58. {
  59. if (str) {
  60. return php_addcslashes_str(str, strlen(str), "\\'", sizeof("\\'"));
  61. }
  62. return NULL;
  63. }
  64. int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *sqlstate, const char *msg, const char *file, int line) /* {{{ */
  65. {
  66. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  67. pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
  68. pdo_pgsql_error_info *einfo = &H->einfo;
  69. char *errmsg = PQerrorMessage(H->server);
  70. einfo->errcode = errcode;
  71. einfo->file = file;
  72. einfo->line = line;
  73. if (einfo->errmsg) {
  74. pefree(einfo->errmsg, dbh->is_persistent);
  75. einfo->errmsg = NULL;
  76. }
  77. if (sqlstate == NULL || strlen(sqlstate) >= sizeof(pdo_error_type)) {
  78. strcpy(*pdo_err, "HY000");
  79. }
  80. else {
  81. strcpy(*pdo_err, sqlstate);
  82. }
  83. if (msg) {
  84. einfo->errmsg = estrdup(msg);
  85. }
  86. else if (errmsg) {
  87. einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent);
  88. }
  89. if (!dbh->methods) {
  90. pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err);
  91. }
  92. return errcode;
  93. }
  94. /* }}} */
  95. static void _pdo_pgsql_notice(pdo_dbh_t *dbh, const char *message) /* {{{ */
  96. {
  97. /* pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
  98. }
  99. /* }}} */
  100. static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
  101. {
  102. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  103. pdo_pgsql_error_info *einfo = &H->einfo;
  104. if (einfo->errcode) {
  105. add_next_index_long(info, einfo->errcode);
  106. add_next_index_string(info, einfo->errmsg);
  107. }
  108. return 1;
  109. }
  110. /* }}} */
  111. /* {{{ pdo_pgsql_create_lob_stream */
  112. static size_t pgsql_lob_write(php_stream *stream, const char *buf, size_t count)
  113. {
  114. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  115. return lo_write(self->conn, self->lfd, (char*)buf, count);
  116. }
  117. static size_t pgsql_lob_read(php_stream *stream, char *buf, size_t count)
  118. {
  119. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  120. return lo_read(self->conn, self->lfd, buf, count);
  121. }
  122. static int pgsql_lob_close(php_stream *stream, int close_handle)
  123. {
  124. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  125. if (close_handle) {
  126. lo_close(self->conn, self->lfd);
  127. }
  128. zval_ptr_dtor(&self->dbh);
  129. efree(self);
  130. return 0;
  131. }
  132. static int pgsql_lob_flush(php_stream *stream)
  133. {
  134. return 0;
  135. }
  136. static int pgsql_lob_seek(php_stream *stream, zend_off_t offset, int whence,
  137. zend_off_t *newoffset)
  138. {
  139. struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
  140. #if HAVE_PG_LO64 && ZEND_ENABLE_ZVAL_LONG64
  141. zend_off_t pos = lo_lseek64(self->conn, self->lfd, offset, whence);
  142. #else
  143. zend_off_t pos = lo_lseek(self->conn, self->lfd, offset, whence);
  144. #endif
  145. *newoffset = pos;
  146. return pos >= 0 ? 0 : -1;
  147. }
  148. const php_stream_ops pdo_pgsql_lob_stream_ops = {
  149. pgsql_lob_write,
  150. pgsql_lob_read,
  151. pgsql_lob_close,
  152. pgsql_lob_flush,
  153. "pdo_pgsql lob stream",
  154. pgsql_lob_seek,
  155. NULL,
  156. NULL,
  157. NULL
  158. };
  159. php_stream *pdo_pgsql_create_lob_stream(zval *dbh, int lfd, Oid oid)
  160. {
  161. php_stream *stm;
  162. struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
  163. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(Z_PDO_DBH_P(dbh))->driver_data;
  164. ZVAL_COPY_VALUE(&self->dbh, dbh);
  165. self->lfd = lfd;
  166. self->oid = oid;
  167. self->conn = H->server;
  168. stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");
  169. if (stm) {
  170. Z_ADDREF_P(dbh);
  171. return stm;
  172. }
  173. efree(self);
  174. return NULL;
  175. }
  176. /* }}} */
  177. static int pgsql_handle_closer(pdo_dbh_t *dbh) /* {{{ */
  178. {
  179. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  180. if (H) {
  181. if (H->server) {
  182. PQfinish(H->server);
  183. H->server = NULL;
  184. }
  185. if (H->einfo.errmsg) {
  186. pefree(H->einfo.errmsg, dbh->is_persistent);
  187. H->einfo.errmsg = NULL;
  188. }
  189. pefree(H, dbh->is_persistent);
  190. dbh->driver_data = NULL;
  191. }
  192. return 0;
  193. }
  194. /* }}} */
  195. static int pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options)
  196. {
  197. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  198. pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
  199. int scrollable;
  200. int ret;
  201. char *nsql = NULL;
  202. size_t nsql_len = 0;
  203. int emulate = 0;
  204. int execute_only = 0;
  205. S->H = H;
  206. stmt->driver_data = S;
  207. stmt->methods = &pgsql_stmt_methods;
  208. scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
  209. PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL;
  210. if (scrollable) {
  211. if (S->cursor_name) {
  212. efree(S->cursor_name);
  213. }
  214. spprintf(&S->cursor_name, 0, "pdo_crsr_%08x", ++H->stmt_counter);
  215. emulate = 1;
  216. } else if (driver_options) {
  217. if (pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES, H->emulate_prepares) == 1) {
  218. emulate = 1;
  219. }
  220. if (pdo_attr_lval(driver_options, PDO_PGSQL_ATTR_DISABLE_PREPARES, H->disable_prepares) == 1) {
  221. execute_only = 1;
  222. }
  223. } else {
  224. emulate = H->disable_native_prepares || H->emulate_prepares;
  225. execute_only = H->disable_prepares;
  226. }
  227. if (!emulate && PQprotocolVersion(H->server) > 2) {
  228. stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
  229. stmt->named_rewrite_template = "$%d";
  230. ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len);
  231. if (ret == 1) {
  232. /* query was re-written */
  233. sql = nsql;
  234. } else if (ret == -1) {
  235. /* couldn't grok it */
  236. strcpy(dbh->error_code, stmt->error_code);
  237. return 0;
  238. }
  239. if (!execute_only) {
  240. /* prepared query: set the query name and defer the
  241. actual prepare until the first execute call */
  242. spprintf(&S->stmt_name, 0, "pdo_stmt_%08x", ++H->stmt_counter);
  243. }
  244. if (nsql) {
  245. S->query = nsql;
  246. } else {
  247. S->query = estrdup(sql);
  248. }
  249. return 1;
  250. }
  251. stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
  252. return 1;
  253. }
  254. static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
  255. {
  256. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  257. PGresult *res;
  258. zend_long ret = 1;
  259. ExecStatusType qs;
  260. if (!(res = PQexec(H->server, sql))) {
  261. /* fatal error */
  262. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  263. return -1;
  264. }
  265. qs = PQresultStatus(res);
  266. if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
  267. pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
  268. PQclear(res);
  269. return -1;
  270. }
  271. H->pgoid = PQoidValue(res);
  272. if (qs == PGRES_COMMAND_OK) {
  273. ZEND_ATOL(ret, PQcmdTuples(res));
  274. } else {
  275. ret = Z_L(0);
  276. }
  277. PQclear(res);
  278. return ret;
  279. }
  280. static int pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
  281. {
  282. unsigned char *escaped;
  283. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  284. size_t tmp_len;
  285. switch (paramtype) {
  286. case PDO_PARAM_LOB:
  287. /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
  288. escaped = PQescapeByteaConn(H->server, (unsigned char *)unquoted, unquotedlen, &tmp_len);
  289. *quotedlen = tmp_len + 1;
  290. *quoted = emalloc(*quotedlen + 1);
  291. memcpy((*quoted)+1, escaped, *quotedlen-2);
  292. (*quoted)[0] = '\'';
  293. (*quoted)[*quotedlen-1] = '\'';
  294. (*quoted)[*quotedlen] = '\0';
  295. PQfreemem(escaped);
  296. break;
  297. default:
  298. *quoted = safe_emalloc(2, unquotedlen, 3);
  299. (*quoted)[0] = '\'';
  300. *quotedlen = PQescapeStringConn(H->server, *quoted + 1, unquoted, unquotedlen, NULL);
  301. (*quoted)[*quotedlen + 1] = '\'';
  302. (*quoted)[*quotedlen + 2] = '\0';
  303. *quotedlen += 2;
  304. }
  305. return 1;
  306. }
  307. static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len)
  308. {
  309. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  310. char *id = NULL;
  311. PGresult *res;
  312. ExecStatusType status;
  313. if (name == NULL) {
  314. res = PQexec(H->server, "SELECT LASTVAL()");
  315. } else {
  316. const char *q[1];
  317. q[0] = name;
  318. res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
  319. }
  320. status = PQresultStatus(res);
  321. if (res && (status == PGRES_TUPLES_OK)) {
  322. id = estrdup((char *)PQgetvalue(res, 0, 0));
  323. *len = PQgetlength(res, 0, 0);
  324. } else {
  325. pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
  326. }
  327. if (res) {
  328. PQclear(res);
  329. }
  330. return id;
  331. }
  332. static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
  333. {
  334. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  335. switch (attr) {
  336. case PDO_ATTR_EMULATE_PREPARES:
  337. ZVAL_BOOL(return_value, H->emulate_prepares);
  338. break;
  339. case PDO_PGSQL_ATTR_DISABLE_PREPARES:
  340. ZVAL_BOOL(return_value, H->disable_prepares);
  341. break;
  342. case PDO_ATTR_CLIENT_VERSION:
  343. ZVAL_STRING(return_value, PG_VERSION);
  344. break;
  345. case PDO_ATTR_SERVER_VERSION:
  346. if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */
  347. ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"));
  348. } else /* emulate above via a query */
  349. {
  350. PGresult *res = PQexec(H->server, "SELECT VERSION()");
  351. if (res && PQresultStatus(res) == PGRES_TUPLES_OK) {
  352. ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0));
  353. }
  354. if (res) {
  355. PQclear(res);
  356. }
  357. }
  358. break;
  359. case PDO_ATTR_CONNECTION_STATUS:
  360. switch (PQstatus(H->server)) {
  361. case CONNECTION_STARTED:
  362. ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1);
  363. break;
  364. case CONNECTION_MADE:
  365. case CONNECTION_OK:
  366. ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1);
  367. break;
  368. case CONNECTION_AWAITING_RESPONSE:
  369. ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1);
  370. break;
  371. case CONNECTION_AUTH_OK:
  372. ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1);
  373. break;
  374. #ifdef CONNECTION_SSL_STARTUP
  375. case CONNECTION_SSL_STARTUP:
  376. ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1);
  377. break;
  378. #endif
  379. case CONNECTION_SETENV:
  380. ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1);
  381. break;
  382. case CONNECTION_BAD:
  383. default:
  384. ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1);
  385. break;
  386. }
  387. break;
  388. case PDO_ATTR_SERVER_INFO: {
  389. int spid = PQbackendPID(H->server);
  390. zend_string *str_info =
  391. strpprintf(0,
  392. "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s",
  393. spid,
  394. (char*)PQparameterStatus(H->server, "client_encoding"),
  395. (char*)PQparameterStatus(H->server, "is_superuser"),
  396. (char*)PQparameterStatus(H->server, "session_authorization"),
  397. (char*)PQparameterStatus(H->server, "DateStyle"));
  398. ZVAL_STR(return_value, str_info);
  399. }
  400. break;
  401. default:
  402. return 0;
  403. }
  404. return 1;
  405. }
  406. /* {{{ */
  407. static int pdo_pgsql_check_liveness(pdo_dbh_t *dbh)
  408. {
  409. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  410. if (!PQconsumeInput(H->server) || PQstatus(H->server) == CONNECTION_BAD) {
  411. PQreset(H->server);
  412. }
  413. return (PQstatus(H->server) == CONNECTION_OK) ? SUCCESS : FAILURE;
  414. }
  415. /* }}} */
  416. static int pgsql_handle_in_transaction(pdo_dbh_t *dbh)
  417. {
  418. pdo_pgsql_db_handle *H;
  419. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  420. return PQtransactionStatus(H->server) > PQTRANS_IDLE;
  421. }
  422. static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
  423. {
  424. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  425. PGresult *res;
  426. int ret = 1;
  427. res = PQexec(H->server, cmd);
  428. if (PQresultStatus(res) != PGRES_COMMAND_OK) {
  429. pdo_pgsql_error(dbh, PQresultStatus(res), pdo_pgsql_sqlstate(res));
  430. ret = 0;
  431. }
  432. PQclear(res);
  433. return ret;
  434. }
  435. static int pgsql_handle_begin(pdo_dbh_t *dbh)
  436. {
  437. return pdo_pgsql_transaction_cmd("BEGIN", dbh);
  438. }
  439. static int pgsql_handle_commit(pdo_dbh_t *dbh)
  440. {
  441. int ret = pdo_pgsql_transaction_cmd("COMMIT", dbh);
  442. /* When deferred constraints are used the commit could
  443. fail, and a ROLLBACK implicitly ran. See bug #67462 */
  444. if (!ret) {
  445. dbh->in_txn = pgsql_handle_in_transaction(dbh);
  446. }
  447. return ret;
  448. }
  449. static int pgsql_handle_rollback(pdo_dbh_t *dbh)
  450. {
  451. return pdo_pgsql_transaction_cmd("ROLLBACK", dbh);
  452. }
  453. /* {{{ proto string PDO::pgsqlCopyFromArray(string $table_name , array $rows [, string $delimiter [, string $null_as ] [, string $fields])
  454. Returns true if the copy worked fine or false if error */
  455. static PHP_METHOD(PDO, pgsqlCopyFromArray)
  456. {
  457. pdo_dbh_t *dbh;
  458. pdo_pgsql_db_handle *H;
  459. zval *pg_rows;
  460. char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
  461. size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
  462. char *query;
  463. PGresult *pgsql_result;
  464. ExecStatusType status;
  465. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa|sss",
  466. &table_name, &table_name_len, &pg_rows,
  467. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
  468. return;
  469. }
  470. if (!zend_hash_num_elements(Z_ARRVAL_P(pg_rows))) {
  471. php_error_docref(NULL, E_WARNING, "Cannot copy from an empty array");
  472. RETURN_FALSE;
  473. }
  474. dbh = Z_PDO_DBH_P(getThis());
  475. PDO_CONSTRUCT_CHECK;
  476. PDO_DBH_CLEAR_ERR();
  477. /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
  478. if (pg_fields) {
  479. spprintf(&query, 0, "COPY %s (%s) FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  480. } else {
  481. spprintf(&query, 0, "COPY %s FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  482. }
  483. /* Obtain db Handle */
  484. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  485. while ((pgsql_result = PQgetResult(H->server))) {
  486. PQclear(pgsql_result);
  487. }
  488. pgsql_result = PQexec(H->server, query);
  489. efree(query);
  490. query = NULL;
  491. if (pgsql_result) {
  492. status = PQresultStatus(pgsql_result);
  493. } else {
  494. status = (ExecStatusType) PQstatus(H->server);
  495. }
  496. if (status == PGRES_COPY_IN && pgsql_result) {
  497. int command_failed = 0;
  498. size_t buffer_len = 0;
  499. zval *tmp;
  500. PQclear(pgsql_result);
  501. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), tmp) {
  502. size_t query_len;
  503. convert_to_string_ex(tmp);
  504. if (buffer_len < Z_STRLEN_P(tmp)) {
  505. buffer_len = Z_STRLEN_P(tmp);
  506. query = erealloc(query, buffer_len + 2); /* room for \n\0 */
  507. }
  508. memcpy(query, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  509. query_len = Z_STRLEN_P(tmp);
  510. if (query[query_len - 1] != '\n') {
  511. query[query_len++] = '\n';
  512. }
  513. query[query_len] = '\0';
  514. if (PQputCopyData(H->server, query, query_len) != 1) {
  515. efree(query);
  516. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  517. PDO_HANDLE_DBH_ERR();
  518. RETURN_FALSE;
  519. }
  520. } ZEND_HASH_FOREACH_END();
  521. if (query) {
  522. efree(query);
  523. }
  524. if (PQputCopyEnd(H->server, NULL) != 1) {
  525. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  526. PDO_HANDLE_DBH_ERR();
  527. RETURN_FALSE;
  528. }
  529. while ((pgsql_result = PQgetResult(H->server))) {
  530. if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
  531. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  532. command_failed = 1;
  533. }
  534. PQclear(pgsql_result);
  535. }
  536. PDO_HANDLE_DBH_ERR();
  537. RETURN_BOOL(!command_failed);
  538. } else {
  539. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  540. PQclear(pgsql_result);
  541. PDO_HANDLE_DBH_ERR();
  542. RETURN_FALSE;
  543. }
  544. }
  545. /* }}} */
  546. /* {{{ proto string PDO::pgsqlCopyFromFile(string $table_name , string $filename [, string $delimiter [, string $null_as ] [, string $fields])
  547. Returns true if the copy worked fine or false if error */
  548. static PHP_METHOD(PDO, pgsqlCopyFromFile)
  549. {
  550. pdo_dbh_t *dbh;
  551. pdo_pgsql_db_handle *H;
  552. char *table_name, *filename, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
  553. size_t table_name_len, filename_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
  554. char *query;
  555. PGresult *pgsql_result;
  556. ExecStatusType status;
  557. php_stream *stream;
  558. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sp|sss",
  559. &table_name, &table_name_len, &filename, &filename_len,
  560. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
  561. return;
  562. }
  563. /* Obtain db Handler */
  564. dbh = Z_PDO_DBH_P(getThis());
  565. PDO_CONSTRUCT_CHECK;
  566. PDO_DBH_CLEAR_ERR();
  567. stream = php_stream_open_wrapper_ex(filename, "rb", 0, NULL, FG(default_context));
  568. if (!stream) {
  569. pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file");
  570. PDO_HANDLE_DBH_ERR();
  571. RETURN_FALSE;
  572. }
  573. /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
  574. if (pg_fields) {
  575. spprintf(&query, 0, "COPY %s (%s) FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  576. } else {
  577. spprintf(&query, 0, "COPY %s FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  578. }
  579. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  580. while ((pgsql_result = PQgetResult(H->server))) {
  581. PQclear(pgsql_result);
  582. }
  583. pgsql_result = PQexec(H->server, query);
  584. efree(query);
  585. if (pgsql_result) {
  586. status = PQresultStatus(pgsql_result);
  587. } else {
  588. status = (ExecStatusType) PQstatus(H->server);
  589. }
  590. if (status == PGRES_COPY_IN && pgsql_result) {
  591. char *buf;
  592. int command_failed = 0;
  593. size_t line_len = 0;
  594. PQclear(pgsql_result);
  595. while ((buf = php_stream_get_line(stream, NULL, 0, &line_len)) != NULL) {
  596. if (PQputCopyData(H->server, buf, line_len) != 1) {
  597. efree(buf);
  598. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  599. php_stream_close(stream);
  600. PDO_HANDLE_DBH_ERR();
  601. RETURN_FALSE;
  602. }
  603. efree(buf);
  604. }
  605. php_stream_close(stream);
  606. if (PQputCopyEnd(H->server, NULL) != 1) {
  607. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  608. PDO_HANDLE_DBH_ERR();
  609. RETURN_FALSE;
  610. }
  611. while ((pgsql_result = PQgetResult(H->server))) {
  612. if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
  613. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  614. command_failed = 1;
  615. }
  616. PQclear(pgsql_result);
  617. }
  618. PDO_HANDLE_DBH_ERR();
  619. RETURN_BOOL(!command_failed);
  620. } else {
  621. php_stream_close(stream);
  622. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  623. PQclear(pgsql_result);
  624. PDO_HANDLE_DBH_ERR();
  625. RETURN_FALSE;
  626. }
  627. }
  628. /* }}} */
  629. /* {{{ proto string PDO::pgsqlCopyToFile(string $table_name , string $filename, [string $delimiter [, string $null_as [, string $fields]]])
  630. Returns true if the copy worked fine or false if error */
  631. static PHP_METHOD(PDO, pgsqlCopyToFile)
  632. {
  633. pdo_dbh_t *dbh;
  634. pdo_pgsql_db_handle *H;
  635. char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL, *filename = NULL;
  636. size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len, filename_len;
  637. char *query;
  638. PGresult *pgsql_result;
  639. ExecStatusType status;
  640. php_stream *stream;
  641. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sp|sss",
  642. &table_name, &table_name_len, &filename, &filename_len,
  643. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
  644. return;
  645. }
  646. dbh = Z_PDO_DBH_P(getThis());
  647. PDO_CONSTRUCT_CHECK;
  648. PDO_DBH_CLEAR_ERR();
  649. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  650. stream = php_stream_open_wrapper_ex(filename, "wb", 0, NULL, FG(default_context));
  651. if (!stream) {
  652. pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing");
  653. PDO_HANDLE_DBH_ERR();
  654. RETURN_FALSE;
  655. }
  656. while ((pgsql_result = PQgetResult(H->server))) {
  657. PQclear(pgsql_result);
  658. }
  659. /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
  660. if (pg_fields) {
  661. spprintf(&query, 0, "COPY %s (%s) TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  662. } else {
  663. spprintf(&query, 0, "COPY %s TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  664. }
  665. pgsql_result = PQexec(H->server, query);
  666. efree(query);
  667. if (pgsql_result) {
  668. status = PQresultStatus(pgsql_result);
  669. } else {
  670. status = (ExecStatusType) PQstatus(H->server);
  671. }
  672. if (status == PGRES_COPY_OUT && pgsql_result) {
  673. PQclear(pgsql_result);
  674. while (1) {
  675. char *csv = NULL;
  676. int ret = PQgetCopyData(H->server, &csv, 0);
  677. if (ret == -1) {
  678. break; /* done */
  679. } else if (ret > 0) {
  680. if (php_stream_write(stream, csv, ret) != (size_t)ret) {
  681. pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to write to file");
  682. PQfreemem(csv);
  683. php_stream_close(stream);
  684. PDO_HANDLE_DBH_ERR();
  685. RETURN_FALSE;
  686. } else {
  687. PQfreemem(csv);
  688. }
  689. } else {
  690. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  691. php_stream_close(stream);
  692. PDO_HANDLE_DBH_ERR();
  693. RETURN_FALSE;
  694. }
  695. }
  696. php_stream_close(stream);
  697. while ((pgsql_result = PQgetResult(H->server))) {
  698. PQclear(pgsql_result);
  699. }
  700. RETURN_TRUE;
  701. } else {
  702. php_stream_close(stream);
  703. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  704. PQclear(pgsql_result);
  705. PDO_HANDLE_DBH_ERR();
  706. RETURN_FALSE;
  707. }
  708. }
  709. /* }}} */
  710. /* {{{ proto string PDO::pgsqlCopyToArray(string $table_name , [string $delimiter [, string $null_as [, string $fields]]])
  711. Returns true if the copy worked fine or false if error */
  712. static PHP_METHOD(PDO, pgsqlCopyToArray)
  713. {
  714. pdo_dbh_t *dbh;
  715. pdo_pgsql_db_handle *H;
  716. char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
  717. size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
  718. char *query;
  719. PGresult *pgsql_result;
  720. ExecStatusType status;
  721. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|sss",
  722. &table_name, &table_name_len,
  723. &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
  724. return;
  725. }
  726. dbh = Z_PDO_DBH_P(getThis());
  727. PDO_CONSTRUCT_CHECK;
  728. PDO_DBH_CLEAR_ERR();
  729. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  730. while ((pgsql_result = PQgetResult(H->server))) {
  731. PQclear(pgsql_result);
  732. }
  733. /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
  734. if (pg_fields) {
  735. spprintf(&query, 0, "COPY %s (%s) TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  736. } else {
  737. spprintf(&query, 0, "COPY %s TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
  738. }
  739. pgsql_result = PQexec(H->server, query);
  740. efree(query);
  741. if (pgsql_result) {
  742. status = PQresultStatus(pgsql_result);
  743. } else {
  744. status = (ExecStatusType) PQstatus(H->server);
  745. }
  746. if (status == PGRES_COPY_OUT && pgsql_result) {
  747. PQclear(pgsql_result);
  748. array_init(return_value);
  749. while (1) {
  750. char *csv = NULL;
  751. int ret = PQgetCopyData(H->server, &csv, 0);
  752. if (ret == -1) {
  753. break; /* copy done */
  754. } else if (ret > 0) {
  755. add_next_index_stringl(return_value, csv, ret);
  756. PQfreemem(csv);
  757. } else {
  758. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  759. PDO_HANDLE_DBH_ERR();
  760. RETURN_FALSE;
  761. }
  762. }
  763. while ((pgsql_result = PQgetResult(H->server))) {
  764. PQclear(pgsql_result);
  765. }
  766. } else {
  767. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
  768. PQclear(pgsql_result);
  769. PDO_HANDLE_DBH_ERR();
  770. RETURN_FALSE;
  771. }
  772. }
  773. /* }}} */
  774. /* {{{ proto string PDO::pgsqlLOBCreate()
  775. Creates a new large object, returning its identifier. Must be called inside a transaction. */
  776. static PHP_METHOD(PDO, pgsqlLOBCreate)
  777. {
  778. pdo_dbh_t *dbh;
  779. pdo_pgsql_db_handle *H;
  780. Oid lfd;
  781. dbh = Z_PDO_DBH_P(getThis());
  782. PDO_CONSTRUCT_CHECK;
  783. PDO_DBH_CLEAR_ERR();
  784. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  785. lfd = lo_creat(H->server, INV_READ|INV_WRITE);
  786. if (lfd != InvalidOid) {
  787. zend_string *buf = strpprintf(0, ZEND_ULONG_FMT, (zend_long) lfd);
  788. RETURN_STR(buf);
  789. }
  790. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  791. PDO_HANDLE_DBH_ERR();
  792. RETURN_FALSE;
  793. }
  794. /* }}} */
  795. /* {{{ proto resource PDO::pgsqlLOBOpen(string oid [, string mode = 'rb'])
  796. Opens an existing large object stream. Must be called inside a transaction. */
  797. static PHP_METHOD(PDO, pgsqlLOBOpen)
  798. {
  799. pdo_dbh_t *dbh;
  800. pdo_pgsql_db_handle *H;
  801. Oid oid;
  802. int lfd;
  803. char *oidstr;
  804. size_t oidstrlen;
  805. char *modestr = "rb";
  806. size_t modestrlen;
  807. int mode = INV_READ;
  808. char *end_ptr;
  809. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s",
  810. &oidstr, &oidstrlen, &modestr, &modestrlen)) {
  811. RETURN_FALSE;
  812. }
  813. oid = (Oid)strtoul(oidstr, &end_ptr, 10);
  814. if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
  815. RETURN_FALSE;
  816. }
  817. if (strpbrk(modestr, "+w")) {
  818. mode = INV_READ|INV_WRITE;
  819. }
  820. dbh = Z_PDO_DBH_P(getThis());
  821. PDO_CONSTRUCT_CHECK;
  822. PDO_DBH_CLEAR_ERR();
  823. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  824. lfd = lo_open(H->server, oid, mode);
  825. if (lfd >= 0) {
  826. php_stream *stream = pdo_pgsql_create_lob_stream(getThis(), lfd, oid);
  827. if (stream) {
  828. php_stream_to_zval(stream, return_value);
  829. return;
  830. }
  831. } else {
  832. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  833. }
  834. PDO_HANDLE_DBH_ERR();
  835. RETURN_FALSE;
  836. }
  837. /* }}} */
  838. /* {{{ proto bool PDO::pgsqlLOBUnlink(string oid)
  839. Deletes the large object identified by oid. Must be called inside a transaction. */
  840. static PHP_METHOD(PDO, pgsqlLOBUnlink)
  841. {
  842. pdo_dbh_t *dbh;
  843. pdo_pgsql_db_handle *H;
  844. Oid oid;
  845. char *oidstr, *end_ptr;
  846. size_t oidlen;
  847. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s",
  848. &oidstr, &oidlen)) {
  849. RETURN_FALSE;
  850. }
  851. oid = (Oid)strtoul(oidstr, &end_ptr, 10);
  852. if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
  853. RETURN_FALSE;
  854. }
  855. dbh = Z_PDO_DBH_P(getThis());
  856. PDO_CONSTRUCT_CHECK;
  857. PDO_DBH_CLEAR_ERR();
  858. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  859. if (1 == lo_unlink(H->server, oid)) {
  860. RETURN_TRUE;
  861. }
  862. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  863. PDO_HANDLE_DBH_ERR();
  864. RETURN_FALSE;
  865. }
  866. /* }}} */
  867. /* {{{ proto mixed PDO::pgsqlGetNotify([ int $result_type = PDO::FETCH_USE_DEFAULT] [, int $ms_timeout = 0 ]])
  868. Get asyncronous notification */
  869. static PHP_METHOD(PDO, pgsqlGetNotify)
  870. {
  871. pdo_dbh_t *dbh;
  872. pdo_pgsql_db_handle *H;
  873. zend_long result_type = PDO_FETCH_USE_DEFAULT;
  874. zend_long ms_timeout = 0;
  875. PGnotify *pgsql_notify;
  876. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|ll",
  877. &result_type, &ms_timeout)) {
  878. RETURN_FALSE;
  879. }
  880. dbh = Z_PDO_DBH_P(getThis());
  881. PDO_CONSTRUCT_CHECK;
  882. if (result_type == PDO_FETCH_USE_DEFAULT) {
  883. result_type = dbh->default_fetch_type;
  884. }
  885. if (result_type != PDO_FETCH_BOTH && result_type != PDO_FETCH_ASSOC && result_type != PDO_FETCH_NUM) {
  886. php_error_docref(NULL, E_WARNING, "Invalid result type");
  887. RETURN_FALSE;
  888. }
  889. if (ms_timeout < 0) {
  890. php_error_docref(NULL, E_WARNING, "Invalid timeout");
  891. RETURN_FALSE;
  892. #if ZEND_ENABLE_ZVAL_LONG64
  893. } else if (ms_timeout > INT_MAX) {
  894. php_error_docref(NULL, E_WARNING, "timeout was shrunk to %d", INT_MAX);
  895. ms_timeout = INT_MAX;
  896. #endif
  897. }
  898. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  899. if (!PQconsumeInput(H->server)) {
  900. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  901. PDO_HANDLE_DBH_ERR();
  902. RETURN_FALSE;
  903. }
  904. pgsql_notify = PQnotifies(H->server);
  905. if (ms_timeout && !pgsql_notify) {
  906. php_pollfd_for_ms(PQsocket(H->server), PHP_POLLREADABLE, (int)ms_timeout);
  907. if (!PQconsumeInput(H->server)) {
  908. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
  909. PDO_HANDLE_DBH_ERR();
  910. RETURN_FALSE;
  911. }
  912. pgsql_notify = PQnotifies(H->server);
  913. }
  914. if (!pgsql_notify) {
  915. RETURN_FALSE;
  916. }
  917. array_init(return_value);
  918. if (result_type == PDO_FETCH_NUM || result_type == PDO_FETCH_BOTH) {
  919. add_index_string(return_value, 0, pgsql_notify->relname);
  920. add_index_long(return_value, 1, pgsql_notify->be_pid);
  921. if (pgsql_notify->extra && pgsql_notify->extra[0]) {
  922. add_index_string(return_value, 2, pgsql_notify->extra);
  923. }
  924. }
  925. if (result_type == PDO_FETCH_ASSOC || result_type == PDO_FETCH_BOTH) {
  926. add_assoc_string(return_value, "message", pgsql_notify->relname);
  927. add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
  928. if (pgsql_notify->extra && pgsql_notify->extra[0]) {
  929. add_assoc_string(return_value, "payload", pgsql_notify->extra);
  930. }
  931. }
  932. PQfreemem(pgsql_notify);
  933. }
  934. /* }}} */
  935. /* {{{ proto int PDO::pgsqlGetPid()
  936. Get backend(server) pid */
  937. static PHP_METHOD(PDO, pgsqlGetPid)
  938. {
  939. pdo_dbh_t *dbh;
  940. pdo_pgsql_db_handle *H;
  941. dbh = Z_PDO_DBH_P(getThis());
  942. PDO_CONSTRUCT_CHECK;
  943. H = (pdo_pgsql_db_handle *)dbh->driver_data;
  944. RETURN_LONG(PQbackendPID(H->server));
  945. }
  946. /* }}} */
  947. static const zend_function_entry dbh_methods[] = {
  948. PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
  949. PHP_ME(PDO, pgsqlLOBOpen, NULL, ZEND_ACC_PUBLIC)
  950. PHP_ME(PDO, pgsqlLOBUnlink, NULL, ZEND_ACC_PUBLIC)
  951. PHP_ME(PDO, pgsqlCopyFromArray, NULL, ZEND_ACC_PUBLIC)
  952. PHP_ME(PDO, pgsqlCopyFromFile, NULL, ZEND_ACC_PUBLIC)
  953. PHP_ME(PDO, pgsqlCopyToArray, NULL, ZEND_ACC_PUBLIC)
  954. PHP_ME(PDO, pgsqlCopyToFile, NULL, ZEND_ACC_PUBLIC)
  955. PHP_ME(PDO, pgsqlGetNotify, NULL, ZEND_ACC_PUBLIC)
  956. PHP_ME(PDO, pgsqlGetPid, NULL, ZEND_ACC_PUBLIC)
  957. PHP_FE_END
  958. };
  959. static const zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind)
  960. {
  961. switch (kind) {
  962. case PDO_DBH_DRIVER_METHOD_KIND_DBH:
  963. return dbh_methods;
  964. default:
  965. return NULL;
  966. }
  967. }
  968. static int pdo_pgsql_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
  969. {
  970. zend_bool bval = zval_get_long(val)? 1 : 0;
  971. pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
  972. switch (attr) {
  973. case PDO_ATTR_EMULATE_PREPARES:
  974. H->emulate_prepares = bval;
  975. return 1;
  976. case PDO_PGSQL_ATTR_DISABLE_PREPARES:
  977. H->disable_prepares = bval;
  978. return 1;
  979. default:
  980. return 0;
  981. }
  982. }
  983. static const struct pdo_dbh_methods pgsql_methods = {
  984. pgsql_handle_closer,
  985. pgsql_handle_preparer,
  986. pgsql_handle_doer,
  987. pgsql_handle_quoter,
  988. pgsql_handle_begin,
  989. pgsql_handle_commit,
  990. pgsql_handle_rollback,
  991. pdo_pgsql_set_attr,
  992. pdo_pgsql_last_insert_id,
  993. pdo_pgsql_fetch_error_func,
  994. pdo_pgsql_get_attribute,
  995. pdo_pgsql_check_liveness, /* check_liveness */
  996. pdo_pgsql_get_driver_methods, /* get_driver_methods */
  997. NULL,
  998. pgsql_handle_in_transaction,
  999. };
  1000. static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
  1001. {
  1002. pdo_pgsql_db_handle *H;
  1003. int ret = 0;
  1004. char *conn_str, *p, *e;
  1005. zend_string *tmp_user, *tmp_pass;
  1006. zend_long connect_timeout = 30;
  1007. H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
  1008. dbh->driver_data = H;
  1009. dbh->skip_param_evt =
  1010. 1 << PDO_PARAM_EVT_EXEC_POST |
  1011. 1 << PDO_PARAM_EVT_FETCH_PRE |
  1012. 1 << PDO_PARAM_EVT_FETCH_POST;
  1013. H->einfo.errcode = 0;
  1014. H->einfo.errmsg = NULL;
  1015. /* PostgreSQL wants params in the connect string to be separated by spaces,
  1016. * if the PDO standard semicolons are used, we convert them to spaces
  1017. */
  1018. e = (char *) dbh->data_source + strlen(dbh->data_source);
  1019. p = (char *) dbh->data_source;
  1020. while ((p = memchr(p, ';', (e - p)))) {
  1021. *p = ' ';
  1022. }
  1023. if (driver_options) {
  1024. connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
  1025. }
  1026. /* escape username and password, if provided */
  1027. tmp_user = _pdo_pgsql_escape_credentials(dbh->username);
  1028. tmp_pass = _pdo_pgsql_escape_credentials(dbh->password);
  1029. /* support both full connection string & connection string + login and/or password */
  1030. if (tmp_user && tmp_pass) {
  1031. spprintf(&conn_str, 0, "%s user='%s' password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), ZSTR_VAL(tmp_pass), connect_timeout);
  1032. } else if (tmp_user) {
  1033. spprintf(&conn_str, 0, "%s user='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), connect_timeout);
  1034. } else if (tmp_pass) {
  1035. spprintf(&conn_str, 0, "%s password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_pass), connect_timeout);
  1036. } else {
  1037. spprintf(&conn_str, 0, "%s connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, connect_timeout);
  1038. }
  1039. H->server = PQconnectdb(conn_str);
  1040. if (tmp_user) {
  1041. zend_string_release_ex(tmp_user, 0);
  1042. }
  1043. if (tmp_pass) {
  1044. zend_string_release_ex(tmp_pass, 0);
  1045. }
  1046. efree(conn_str);
  1047. if (PQstatus(H->server) != CONNECTION_OK) {
  1048. pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
  1049. goto cleanup;
  1050. }
  1051. PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
  1052. H->attached = 1;
  1053. H->pgoid = -1;
  1054. dbh->methods = &pgsql_methods;
  1055. dbh->alloc_own_columns = 1;
  1056. dbh->max_escaped_char_length = 2;
  1057. ret = 1;
  1058. cleanup:
  1059. dbh->methods = &pgsql_methods;
  1060. if (!ret) {
  1061. pgsql_handle_closer(dbh);
  1062. }
  1063. return ret;
  1064. }
  1065. /* }}} */
  1066. const pdo_driver_t pdo_pgsql_driver = {
  1067. PDO_DRIVER_HEADER(pgsql),
  1068. pdo_pgsql_handle_factory
  1069. };
  1070. /*
  1071. * Local variables:
  1072. * tab-width: 4
  1073. * c-basic-offset: 4
  1074. * End:
  1075. * vim600: noet sw=4 ts=4 fdm=marker
  1076. * vim<600: noet sw=4 ts=4
  1077. */