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

157 lines
4.0 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2004 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.0 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_0.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Edin Kadribasic <edink@emini.dk> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "pdo/php_pdo.h"
  26. #include "pdo/php_pdo_driver.h"
  27. #include "php_pdo_pgsql.h"
  28. #include "php_pdo_pgsql_int.h"
  29. static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
  30. {
  31. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  32. int i;
  33. if (S->result) {
  34. /* free the resource */
  35. PQclear(S->result);
  36. S->result = NULL;
  37. }
  38. if(S->cols) {
  39. efree(S->cols);
  40. S->cols = NULL;
  41. }
  42. efree(S);
  43. return 1;
  44. }
  45. static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
  46. {
  47. pdo_dbh_t *dbh = stmt->dbh;
  48. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  49. pdo_pgsql_db_handle *H = S->H;
  50. ExecStatusType status;
  51. if (stmt->executed) {
  52. /* ensure that we free any previous unfetched results */
  53. if(S->result) {
  54. PQclear(S->result);
  55. S->result = NULL;
  56. }
  57. }
  58. S->result = PQexec(H->server, stmt->active_query_string);
  59. status = PQresultStatus(S->result);
  60. if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
  61. pdo_pgsql_error_stmt(stmt, status);
  62. return 0;
  63. }
  64. if(!stmt->executed) {
  65. stmt->column_count = (int) PQnfields(S->result);
  66. S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
  67. }
  68. if (status == PGRES_COMMAND_OK) {
  69. stmt->row_count = (long)atoi(PQcmdTuples(S->result));
  70. } else {
  71. stmt->row_count = (long)PQntuples(S->result);
  72. }
  73. return 1;
  74. }
  75. static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
  76. enum pdo_param_event event_type TSRMLS_DC)
  77. {
  78. return 1;
  79. }
  80. static int pgsql_stmt_fetch(pdo_stmt_t *stmt TSRMLS_DC)
  81. {
  82. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  83. if (S->current_row < stmt->row_count) {
  84. S->current_row++;
  85. return 1;
  86. } else {
  87. return 0;
  88. }
  89. }
  90. static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
  91. {
  92. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  93. struct pdo_column_data *cols = stmt->columns;
  94. if (!S->result) {
  95. return 0;
  96. }
  97. cols[colno].name = estrdup(PQfname(S->result, colno));
  98. cols[colno].namelen = strlen(cols[colno].name);
  99. cols[colno].maxlen = PQfsize(S->result, colno);
  100. cols[colno].precision = PQfmod(S->result, colno);
  101. cols[colno].param_type = PDO_PARAM_STR;
  102. return 1;
  103. }
  104. static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len TSRMLS_DC)
  105. {
  106. pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
  107. if (!S->result) {
  108. return 0;
  109. }
  110. /* We have already increased count by 1 in pgsql_stmt_fetch() */
  111. *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
  112. *len = PQgetlength(S->result, S->current_row - 1, colno);
  113. return 1;
  114. }
  115. struct pdo_stmt_methods pgsql_stmt_methods = {
  116. pgsql_stmt_dtor,
  117. pgsql_stmt_execute,
  118. pgsql_stmt_fetch,
  119. pgsql_stmt_describe,
  120. pgsql_stmt_get_col,
  121. pgsql_stmt_param_hook
  122. };
  123. /*
  124. * Local variables:
  125. * tab-width: 4
  126. * c-basic-offset: 4
  127. * End:
  128. * vim600: noet sw=4 ts=4 fdm=marker
  129. * vim<600: noet sw=4 ts=4
  130. */