apr_dbd_pgsql.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apu.h"
  17. #if APU_HAVE_PGSQL
  18. #include "apu_config.h"
  19. #include <ctype.h>
  20. #include <stdlib.h>
  21. #ifdef HAVE_LIBPQ_FE_H
  22. #include <libpq-fe.h>
  23. #elif defined(HAVE_POSTGRESQL_LIBPQ_FE_H)
  24. #include <postgresql/libpq-fe.h>
  25. #endif
  26. #include "apr_strings.h"
  27. #include "apr_time.h"
  28. #include "apr_dbd_internal.h"
  29. #define QUERY_MAX_ARGS 40
  30. struct apr_dbd_transaction_t {
  31. int errnum;
  32. apr_dbd_t *handle;
  33. };
  34. struct apr_dbd_t {
  35. PGconn *conn;
  36. apr_dbd_transaction_t *trans;
  37. };
  38. struct apr_dbd_results_t {
  39. int random;
  40. PGconn *handle;
  41. PGresult *res;
  42. size_t ntuples;
  43. size_t sz;
  44. size_t index;
  45. };
  46. struct apr_dbd_row_t {
  47. int n;
  48. apr_dbd_results_t *res;
  49. };
  50. struct apr_dbd_prepared_t {
  51. const char *name;
  52. int prepared;
  53. int nargs;
  54. };
  55. #define dbd_pgsql_is_success(x) (((x) == PGRES_EMPTY_QUERY) \
  56. || ((x) == PGRES_COMMAND_OK) \
  57. || ((x) == PGRES_TUPLES_OK))
  58. static apr_status_t clear_result(void *data)
  59. {
  60. PQclear(data);
  61. return APR_SUCCESS;
  62. }
  63. static int dbd_pgsql_select(apr_pool_t *pool, apr_dbd_t *sql,
  64. apr_dbd_results_t **results,
  65. const char *query, int seek)
  66. {
  67. PGresult *res;
  68. int ret;
  69. if ( sql->trans && sql->trans->errnum ) {
  70. return sql->trans->errnum;
  71. }
  72. if (seek) { /* synchronous query */
  73. res = PQexec(sql->conn, query);
  74. if (res) {
  75. ret = PQresultStatus(res);
  76. if (dbd_pgsql_is_success(ret)) {
  77. ret = 0;
  78. } else {
  79. PQclear(res);
  80. }
  81. } else {
  82. ret = PGRES_FATAL_ERROR;
  83. }
  84. if (ret != 0) {
  85. if (sql->trans) {
  86. sql->trans->errnum = ret;
  87. }
  88. return ret;
  89. }
  90. if (!*results) {
  91. *results = apr_pcalloc(pool, sizeof(apr_dbd_results_t));
  92. }
  93. (*results)->res = res;
  94. (*results)->ntuples = PQntuples(res);
  95. (*results)->sz = PQnfields(res);
  96. (*results)->random = seek;
  97. apr_pool_cleanup_register(pool, res, clear_result,
  98. apr_pool_cleanup_null);
  99. }
  100. else {
  101. if (PQsendQuery(sql->conn, query) == 0) {
  102. if (sql->trans) {
  103. sql->trans->errnum = 1;
  104. }
  105. return 1;
  106. }
  107. if (*results == NULL) {
  108. *results = apr_pcalloc(pool, sizeof(apr_dbd_results_t));
  109. }
  110. (*results)->random = seek;
  111. (*results)->handle = sql->conn;
  112. }
  113. return 0;
  114. }
  115. static int dbd_pgsql_get_row(apr_pool_t *pool, apr_dbd_results_t *res,
  116. apr_dbd_row_t **rowp, int rownum)
  117. {
  118. apr_dbd_row_t *row = *rowp;
  119. int sequential = ((rownum >= 0) && res->random) ? 0 : 1;
  120. if (row == NULL) {
  121. row = apr_palloc(pool, sizeof(apr_dbd_row_t));
  122. *rowp = row;
  123. row->res = res;
  124. row->n = sequential ? 0 : rownum;
  125. }
  126. else {
  127. if ( sequential ) {
  128. ++row->n;
  129. }
  130. else {
  131. row->n = rownum;
  132. }
  133. }
  134. if (res->random) {
  135. if (row->n >= res->ntuples) {
  136. *rowp = NULL;
  137. apr_pool_cleanup_run(pool, res->res, clear_result);
  138. res->res = NULL;
  139. return -1;
  140. }
  141. }
  142. else {
  143. if (row->n >= res->ntuples) {
  144. /* no data; we have to fetch some */
  145. row->n -= res->ntuples;
  146. if (res->res != NULL) {
  147. PQclear(res->res);
  148. }
  149. res->res = PQgetResult(res->handle);
  150. if (res->res) {
  151. res->ntuples = PQntuples(res->res);
  152. while (res->ntuples == 0) {
  153. /* if we got an empty result, clear it, wait a mo, try
  154. * again */
  155. PQclear(res->res);
  156. apr_sleep(100000); /* 0.1 secs */
  157. res->res = PQgetResult(res->handle);
  158. if (res->res) {
  159. res->ntuples = PQntuples(res->res);
  160. }
  161. else {
  162. return -1;
  163. }
  164. }
  165. if (res->sz == 0) {
  166. res->sz = PQnfields(res->res);
  167. }
  168. }
  169. else {
  170. return -1;
  171. }
  172. }
  173. }
  174. return 0;
  175. }
  176. static const char *dbd_pgsql_get_entry(const apr_dbd_row_t *row, int n)
  177. {
  178. return PQgetvalue(row->res->res, row->n, n);
  179. }
  180. static const char *dbd_pgsql_error(apr_dbd_t *sql, int n)
  181. {
  182. return PQerrorMessage(sql->conn);
  183. }
  184. static int dbd_pgsql_query(apr_dbd_t *sql, int *nrows, const char *query)
  185. {
  186. PGresult *res;
  187. int ret;
  188. if (sql->trans && sql->trans->errnum) {
  189. return sql->trans->errnum;
  190. }
  191. res = PQexec(sql->conn, query);
  192. if (res) {
  193. ret = PQresultStatus(res);
  194. if (dbd_pgsql_is_success(ret)) {
  195. /* ugh, making 0 return-success doesn't fit */
  196. ret = 0;
  197. }
  198. *nrows = atoi(PQcmdTuples(res));
  199. PQclear(res);
  200. }
  201. else {
  202. ret = PGRES_FATAL_ERROR;
  203. }
  204. if (sql->trans) {
  205. sql->trans->errnum = ret;
  206. }
  207. return ret;
  208. }
  209. static const char *dbd_pgsql_escape(apr_pool_t *pool, const char *arg,
  210. apr_dbd_t *sql)
  211. {
  212. size_t len = strlen(arg);
  213. char *ret = apr_palloc(pool, 2*(len + 1));
  214. PQescapeString(ret, arg, len);
  215. return ret;
  216. }
  217. static int dbd_pgsql_prepare(apr_pool_t *pool, apr_dbd_t *sql,
  218. const char *query, const char *label,
  219. apr_dbd_prepared_t **statement)
  220. {
  221. char *sqlcmd;
  222. char *sqlptr;
  223. size_t length;
  224. size_t i = 0;
  225. const char *args[QUERY_MAX_ARGS];
  226. size_t alen;
  227. int ret;
  228. PGresult *res;
  229. char *pgquery;
  230. char *pgptr;
  231. if (!*statement) {
  232. *statement = apr_palloc(pool, sizeof(apr_dbd_prepared_t));
  233. }
  234. (*statement)->nargs = 0;
  235. /* Translate from apr_dbd to native query format */
  236. for (sqlptr = (char*)query; *sqlptr; ++sqlptr) {
  237. if (sqlptr[0] == '%') {
  238. if (isalpha(sqlptr[1])) {
  239. ++(*statement)->nargs;
  240. }
  241. else if (sqlptr[1] == '%') {
  242. ++sqlptr;
  243. }
  244. }
  245. }
  246. length = strlen(query) + 1;
  247. if ((*statement)->nargs > 8) {
  248. length += (*statement)->nargs - 8;
  249. }
  250. pgptr = pgquery = apr_palloc(pool, length) ;
  251. for (sqlptr = (char*)query; *sqlptr; ++sqlptr) {
  252. if ((sqlptr[0] == '%') && isalpha(sqlptr[1])) {
  253. *pgptr++ = '$';
  254. if (i < 9) {
  255. *pgptr++ = '1' + i;
  256. }
  257. else {
  258. *pgptr++ = '0' + ((i+1)/10);
  259. *pgptr++ = '0' + ((i+1)%10);
  260. }
  261. switch (*++sqlptr) {
  262. case 'd':
  263. args[i] = "integer";
  264. break;
  265. case 's':
  266. args[i] = "varchar";
  267. break;
  268. default:
  269. args[i] = "varchar";
  270. break;
  271. }
  272. length += 1 + strlen(args[i]);
  273. ++i;
  274. }
  275. else if ((sqlptr[0] == '%') && (sqlptr[1] == '%')) {
  276. /* reduce %% to % */
  277. *pgptr++ = *sqlptr++;
  278. }
  279. else {
  280. *pgptr++ = *sqlptr;
  281. }
  282. }
  283. *pgptr = 0;
  284. if (!label) {
  285. /* don't really prepare; use in execParams instead */
  286. (*statement)->prepared = 0;
  287. (*statement)->name = apr_pstrdup(pool, pgquery);
  288. return 0;
  289. }
  290. (*statement)->name = apr_pstrdup(pool, label);
  291. /* length of SQL query that prepares this statement */
  292. length = 8 + strlen(label) + 2 + 4 + length + 1;
  293. sqlcmd = apr_palloc(pool, length);
  294. sqlptr = sqlcmd;
  295. memcpy(sqlptr, "PREPARE ", 8);
  296. sqlptr += 8;
  297. length = strlen(label);
  298. memcpy(sqlptr, label, length);
  299. sqlptr += length;
  300. if ((*statement)->nargs > 0) {
  301. memcpy(sqlptr, " (",2);
  302. sqlptr += 2;
  303. for (i=0; i < (*statement)->nargs; ++i) {
  304. alen = strlen(args[i]);
  305. memcpy(sqlptr, args[i], alen);
  306. sqlptr += alen;
  307. *sqlptr++ = ',';
  308. }
  309. sqlptr[-1] = ')';
  310. }
  311. memcpy(sqlptr, " AS ", 4);
  312. sqlptr += 4;
  313. memcpy(sqlptr, pgquery, strlen(pgquery));
  314. sqlptr += strlen(pgquery);
  315. *sqlptr = 0;
  316. res = PQexec(sql->conn, sqlcmd);
  317. if ( res ) {
  318. ret = PQresultStatus(res);
  319. if (dbd_pgsql_is_success(ret)) {
  320. ret = 0;
  321. }
  322. /* Hmmm, do we do this here or register it on the pool? */
  323. PQclear(res);
  324. }
  325. else {
  326. ret = PGRES_FATAL_ERROR;
  327. }
  328. (*statement)->prepared = 1;
  329. return ret;
  330. }
  331. static int dbd_pgsql_pquery(apr_pool_t *pool, apr_dbd_t *sql,
  332. int *nrows, apr_dbd_prepared_t *statement,
  333. int nargs, const char **values)
  334. {
  335. int ret;
  336. PGresult *res;
  337. if (sql->trans && sql->trans->errnum) {
  338. return sql->trans->errnum;
  339. }
  340. if (statement->prepared) {
  341. res = PQexecPrepared(sql->conn, statement->name, nargs, values, 0, 0,
  342. 0);
  343. }
  344. else {
  345. res = PQexecParams(sql->conn, statement->name, nargs, 0, values, 0, 0,
  346. 0);
  347. }
  348. if (res) {
  349. ret = PQresultStatus(res);
  350. if (dbd_pgsql_is_success(ret)) {
  351. ret = 0;
  352. }
  353. *nrows = atoi(PQcmdTuples(res));
  354. PQclear(res);
  355. }
  356. else {
  357. ret = PGRES_FATAL_ERROR;
  358. }
  359. if (sql->trans) {
  360. sql->trans->errnum = ret;
  361. }
  362. return ret;
  363. }
  364. static int dbd_pgsql_pvquery(apr_pool_t *pool, apr_dbd_t *sql,
  365. int *nrows, apr_dbd_prepared_t *statement,
  366. va_list args)
  367. {
  368. const char **values;
  369. int i;
  370. if (sql->trans && sql->trans->errnum) {
  371. return sql->trans->errnum;
  372. }
  373. values = apr_palloc(pool, sizeof(*values) * statement->nargs);
  374. for (i = 0; i < statement->nargs; i++) {
  375. values[i] = apr_pstrdup(pool, va_arg(args, const char*));
  376. }
  377. return dbd_pgsql_pquery(pool, sql, nrows, statement,
  378. statement->nargs, values);
  379. }
  380. static int dbd_pgsql_pselect(apr_pool_t *pool, apr_dbd_t *sql,
  381. apr_dbd_results_t **results,
  382. apr_dbd_prepared_t *statement,
  383. int seek, int nargs, const char **values)
  384. {
  385. PGresult *res;
  386. int rv;
  387. int ret = 0;
  388. if (sql->trans && sql->trans->errnum) {
  389. return sql->trans->errnum;
  390. }
  391. if (seek) { /* synchronous query */
  392. if (statement->prepared) {
  393. res = PQexecPrepared(sql->conn, statement->name, nargs, values, 0,
  394. 0, 0);
  395. }
  396. else {
  397. res = PQexecParams(sql->conn, statement->name, nargs, 0, values, 0,
  398. 0, 0);
  399. }
  400. if (res) {
  401. ret = PQresultStatus(res);
  402. if (dbd_pgsql_is_success(ret)) {
  403. ret = 0;
  404. }
  405. else {
  406. PQclear(res);
  407. }
  408. }
  409. else {
  410. ret = PGRES_FATAL_ERROR;
  411. }
  412. if (ret != 0) {
  413. if (sql->trans) {
  414. sql->trans->errnum = ret;
  415. }
  416. return ret;
  417. }
  418. if (!*results) {
  419. *results = apr_pcalloc(pool, sizeof(apr_dbd_results_t));
  420. }
  421. (*results)->res = res;
  422. (*results)->ntuples = PQntuples(res);
  423. (*results)->sz = PQnfields(res);
  424. (*results)->random = seek;
  425. apr_pool_cleanup_register(pool, res, clear_result,
  426. apr_pool_cleanup_null);
  427. }
  428. else {
  429. if (statement->prepared) {
  430. rv = PQsendQueryPrepared(sql->conn, statement->name, nargs, values,
  431. 0, 0, 0);
  432. }
  433. else {
  434. rv = PQsendQueryParams(sql->conn, statement->name, nargs, 0,
  435. values, 0, 0, 0);
  436. }
  437. if (rv == 0) {
  438. if (sql->trans) {
  439. sql->trans->errnum = 1;
  440. }
  441. return 1;
  442. }
  443. if (!*results) {
  444. *results = apr_pcalloc(pool, sizeof(apr_dbd_results_t));
  445. }
  446. (*results)->random = seek;
  447. (*results)->handle = sql->conn;
  448. }
  449. if (sql->trans) {
  450. sql->trans->errnum = ret;
  451. }
  452. return ret;
  453. }
  454. static int dbd_pgsql_pvselect(apr_pool_t *pool, apr_dbd_t *sql,
  455. apr_dbd_results_t **results,
  456. apr_dbd_prepared_t *statement,
  457. int seek, va_list args)
  458. {
  459. const char **values;
  460. int i;
  461. if (sql->trans && sql->trans->errnum) {
  462. return sql->trans->errnum;
  463. }
  464. values = apr_palloc(pool, sizeof(*values) * statement->nargs);
  465. for (i = 0; i < statement->nargs; i++) {
  466. values[i] = apr_pstrdup(pool, va_arg(args, const char*));
  467. }
  468. return dbd_pgsql_pselect(pool, sql, results, statement,
  469. seek, statement->nargs, values) ;
  470. }
  471. static int dbd_pgsql_start_transaction(apr_pool_t *pool, apr_dbd_t *handle,
  472. apr_dbd_transaction_t **trans)
  473. {
  474. int ret = 0;
  475. PGresult *res;
  476. /* XXX handle recursive transactions here */
  477. res = PQexec(handle->conn, "BEGIN TRANSACTION");
  478. if (res) {
  479. ret = PQresultStatus(res);
  480. if (dbd_pgsql_is_success(ret)) {
  481. ret = 0;
  482. if (!*trans) {
  483. *trans = apr_pcalloc(pool, sizeof(apr_dbd_transaction_t));
  484. }
  485. }
  486. PQclear(res);
  487. (*trans)->handle = handle;
  488. handle->trans = *trans;
  489. }
  490. else {
  491. ret = PGRES_FATAL_ERROR;
  492. }
  493. return ret;
  494. }
  495. static int dbd_pgsql_end_transaction(apr_dbd_transaction_t *trans)
  496. {
  497. PGresult *res;
  498. int ret = -1; /* no transaction is an error cond */
  499. if (trans) {
  500. if (trans->errnum) {
  501. trans->errnum = 0;
  502. res = PQexec(trans->handle->conn, "ROLLBACK");
  503. }
  504. else {
  505. res = PQexec(trans->handle->conn, "COMMIT");
  506. }
  507. if (res) {
  508. ret = PQresultStatus(res);
  509. if (dbd_pgsql_is_success(ret)) {
  510. ret = 0;
  511. }
  512. PQclear(res);
  513. }
  514. else {
  515. ret = PGRES_FATAL_ERROR;
  516. }
  517. trans->handle->trans = NULL;
  518. }
  519. return ret;
  520. }
  521. static apr_dbd_t *dbd_pgsql_open(apr_pool_t *pool, const char *params)
  522. {
  523. apr_dbd_t *sql;
  524. PGconn *conn = PQconnectdb(params);
  525. /* if there's an error in the connect string or something we get
  526. * back a * bogus connection object, and things like PQreset are
  527. * liable to segfault, so just close it out now. it would be nice
  528. * if we could give an indication of why we failed to connect... */
  529. if (PQstatus(conn) != CONNECTION_OK) {
  530. PQfinish(conn);
  531. return NULL;
  532. }
  533. sql = apr_pcalloc (pool, sizeof (*sql));
  534. sql->conn = conn;
  535. return sql;
  536. }
  537. static apr_status_t dbd_pgsql_close(apr_dbd_t *handle)
  538. {
  539. PQfinish(handle->conn);
  540. return APR_SUCCESS;
  541. }
  542. static apr_status_t dbd_pgsql_check_conn(apr_pool_t *pool,
  543. apr_dbd_t *handle)
  544. {
  545. if (PQstatus(handle->conn) != CONNECTION_OK) {
  546. PQreset(handle->conn);
  547. if (PQstatus(handle->conn) != CONNECTION_OK) {
  548. return APR_EGENERAL;
  549. }
  550. }
  551. return APR_SUCCESS;
  552. }
  553. static int dbd_pgsql_select_db(apr_pool_t *pool, apr_dbd_t *handle,
  554. const char *name)
  555. {
  556. return APR_ENOTIMPL;
  557. }
  558. static void *dbd_pgsql_native(apr_dbd_t *handle)
  559. {
  560. return handle->conn;
  561. }
  562. static int dbd_pgsql_num_cols(apr_dbd_results_t* res)
  563. {
  564. return res->sz;
  565. }
  566. static int dbd_pgsql_num_tuples(apr_dbd_results_t* res)
  567. {
  568. if (res->random) {
  569. return res->ntuples;
  570. }
  571. else {
  572. return -1;
  573. }
  574. }
  575. APU_DECLARE_DATA const apr_dbd_driver_t apr_dbd_pgsql_driver = {
  576. "pgsql",
  577. NULL,
  578. dbd_pgsql_native,
  579. dbd_pgsql_open,
  580. dbd_pgsql_check_conn,
  581. dbd_pgsql_close,
  582. dbd_pgsql_select_db,
  583. dbd_pgsql_start_transaction,
  584. dbd_pgsql_end_transaction,
  585. dbd_pgsql_query,
  586. dbd_pgsql_select,
  587. dbd_pgsql_num_cols,
  588. dbd_pgsql_num_tuples,
  589. dbd_pgsql_get_row,
  590. dbd_pgsql_get_entry,
  591. dbd_pgsql_error,
  592. dbd_pgsql_escape,
  593. dbd_pgsql_prepare,
  594. dbd_pgsql_pvquery,
  595. dbd_pgsql_pvselect,
  596. dbd_pgsql_pquery,
  597. dbd_pgsql_pselect,
  598. };
  599. #endif