fetch.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*!
  2. \file db/driver/postgres/fetch.c
  3. \brief DBMI - Low Level PostgreSQL database driver - fetch data
  4. \todo implement time zone handling
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Radim Blazek
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <grass/dbmi.h>
  12. #include <grass/glocale.h>
  13. #include "globals.h"
  14. #include "proto.h"
  15. int db__driver_fetch(dbCursor * cn, int position, int *more)
  16. {
  17. cursor *c;
  18. dbToken token;
  19. dbTable *table;
  20. int i;
  21. /* get cursor token */
  22. token = db_get_cursor_token(cn);
  23. /* get the cursor by its token */
  24. if (!(c = (cursor *) db_find_token(token))) {
  25. append_error(_("Cursor not found"));
  26. report_error();
  27. return DB_FAILED;
  28. }
  29. /* fetch on position */
  30. switch (position) {
  31. case DB_NEXT:
  32. c->row++;
  33. break;
  34. case DB_CURRENT:
  35. break;
  36. case DB_PREVIOUS:
  37. c->row--;
  38. break;
  39. case DB_FIRST:
  40. c->row = 0;
  41. break;
  42. case DB_LAST:
  43. c->row = c->nrows - 1;
  44. break;
  45. };
  46. G_debug(3, "row = %d nrows = %d", c->row, c->nrows);
  47. if (c->row < 0 || c->row >= c->nrows) {
  48. *more = 0;
  49. return DB_OK;
  50. }
  51. *more = 1;
  52. /* get the data out of the descriptor into the table */
  53. table = db_get_cursor_table(cn);
  54. for (i = 0; i < c->ncols; i++) {
  55. int col, gpgtype, sqltype;
  56. dbColumn *column;
  57. dbValue *value;
  58. col = c->cols[i]; /* known cols */
  59. column = db_get_table_column(table, i);
  60. gpgtype = db_get_column_host_type(column);
  61. sqltype = db_get_column_sqltype(column);
  62. value = db_get_column_value(column);
  63. db_zero_string(&value->s);
  64. /* Is null? */
  65. if (PQgetisnull(c->res, c->row, col)) {
  66. value->isNull = 1;
  67. continue;
  68. }
  69. else {
  70. value->isNull = 0;
  71. }
  72. G_debug(3, "row %d, col %d, gpgtype %d, sqltype %d: val = '%s'",
  73. c->row, col, gpgtype, sqltype, PQgetvalue(c->res, c->row,
  74. col));
  75. switch (gpgtype) {
  76. int ns, tz;
  77. case PG_TYPE_CHAR:
  78. case PG_TYPE_BPCHAR:
  79. case PG_TYPE_VARCHAR:
  80. case PG_TYPE_TEXT:
  81. db_set_string(&(value->s), PQgetvalue(c->res, c->row, col));
  82. break;
  83. case PG_TYPE_BIT:
  84. case PG_TYPE_INT2:
  85. case PG_TYPE_INT4:
  86. case PG_TYPE_INT8:
  87. case PG_TYPE_SERIAL:
  88. case PG_TYPE_OID:
  89. value->i = atoi(PQgetvalue(c->res, c->row, col));
  90. break;
  91. case PG_TYPE_FLOAT4:
  92. case PG_TYPE_FLOAT8:
  93. case PG_TYPE_NUMERIC:
  94. value->d = atof(PQgetvalue(c->res, c->row, col));
  95. break;
  96. /* Note: we have set DATESTYLE TO ISO in db_driver_open_select_cursor() so datetime
  97. * format should be ISO */
  98. case PG_TYPE_DATE:
  99. /* Example: '1999-01-25' */
  100. ns = sscanf(PQgetvalue(c->res, c->row, col), "%4d-%2d-%2d",
  101. &(value->t.year), &(value->t.month), &(value->t.day));
  102. if (ns != 3) {
  103. append_error(_("Unable to scan date:"));
  104. append_error(PQgetvalue(c->res, c->row, col));
  105. report_error();
  106. return DB_FAILED;
  107. }
  108. value->t.hour = 0;
  109. value->t.minute = 0;
  110. value->t.seconds = 0.0;
  111. break;
  112. case PG_TYPE_TIME:
  113. /* Example: '04:05:06.25', '04:05:06' */
  114. ns = sscanf(PQgetvalue(c->res, c->row, col), "%2d:%2d:%lf",
  115. &(value->t.hour), &(value->t.minute),
  116. &(value->t.seconds));
  117. if (ns != 3) {
  118. append_error(_("Unable to scan time:"));
  119. append_error(PQgetvalue(c->res, c->row, col));
  120. report_error();
  121. return DB_FAILED;
  122. }
  123. value->t.year = 0;
  124. value->t.month = 0;
  125. value->t.day = 0;
  126. break;
  127. case PG_TYPE_TIMESTAMP:
  128. /* Example: '1999-01-25 04:05:06.25+01', '1999-01-25 04:05:06' */
  129. ns = sscanf(PQgetvalue(c->res, c->row, col),
  130. "%4d-%2d-%2d %2d:%2d:%lf%3d", &(value->t.year),
  131. &(value->t.month), &(value->t.day), &(value->t.hour),
  132. &(value->t.minute), &(value->t.seconds), &tz);
  133. if (ns == 7) {
  134. append_error(_("Unable to scan timestamp (no idea how to process time zone):"));
  135. append_error(PQgetvalue(c->res, c->row, col));
  136. report_error();
  137. return DB_FAILED;
  138. }
  139. else if (ns < 6) {
  140. append_error(_("Unable to scan timestamp (not enough arguments):"));
  141. append_error(PQgetvalue(c->res, c->row, col));
  142. report_error();
  143. return DB_FAILED;
  144. }
  145. break;
  146. case PG_TYPE_BOOL:
  147. if (strcmp(PQgetvalue(c->res, c->row, col), "t") == 0)
  148. db_set_string(&(value->s), "1");
  149. else if (strcmp(PQgetvalue(c->res, c->row, col), "f") == 0)
  150. db_set_string(&(value->s), "0");
  151. else
  152. G_warning(_("Unable to recognize boolean value"));
  153. break;
  154. }
  155. }
  156. G_debug(3, "Row fetched");
  157. return DB_OK;
  158. }
  159. int db__driver_get_num_rows(dbCursor * cn)
  160. {
  161. cursor *c;
  162. dbToken token;
  163. /* get cursor token */
  164. token = db_get_cursor_token(cn);
  165. /* get the cursor by its token */
  166. if (!(c = (cursor *) db_find_token(token))) {
  167. append_error(_("Taken not found"));
  168. report_error();
  169. return DB_FAILED;
  170. }
  171. return (c->nrows);
  172. }