db_mod.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * $Id$
  3. *
  4. * Postgres module interface
  5. *
  6. * Portions Copyright (C) 2001-2003 FhG FOKUS
  7. * Copyright (C) 2003 August.Net Services, LLC
  8. * Portions Copyright (C) 2005 iptelorg GmbH
  9. *
  10. * This file is part of ser, a free SIP server.
  11. *
  12. * ser is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * For a license to use the ser software under conditions
  18. * other than those described here, or to purchase support for this
  19. * software, please contact iptel.org by e-mail at the following addresses:
  20. * [email protected]
  21. *
  22. * ser is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. #include <stdio.h>
  32. #include "../../sr_module.h"
  33. #include "dbase.h"
  34. MODULE_VERSION
  35. int connect_timeout = 0; /* Default is unlimited */
  36. int reconnect_attempts = 2; /* How many times should the module try to reconnect if
  37. * the connection is lost. 0 disables reconnecting */
  38. /*
  39. * Postgres database module interface
  40. */
  41. static cmd_export_t cmds[]={
  42. {"db_use_table", (cmd_function)pg_use_table, 2, 0, 0},
  43. {"db_init", (cmd_function)pg_init, 1, 0, 0},
  44. {"db_close", (cmd_function)pg_close, 2, 0, 0},
  45. {"db_query", (cmd_function)pg_query, 2, 0, 0},
  46. {"db_raw_query", (cmd_function)pg_raw_query, 2, 0, 0},
  47. {"db_free_result", (cmd_function)pg_db_free_result, 2, 0, 0},
  48. {"db_insert", (cmd_function)pg_insert, 2, 0, 0},
  49. {"db_delete", (cmd_function)pg_delete, 2, 0, 0},
  50. {"db_update", (cmd_function)pg_update, 2, 0, 0},
  51. {0, 0, 0, 0, 0}
  52. };
  53. /*
  54. * Exported parameters
  55. */
  56. static param_export_t params[] = {
  57. {"connect_timeout", PARAM_INT, &connect_timeout },
  58. {"reconnect_attempts", PARAM_INT, &reconnect_attempts},
  59. {0, 0, 0}
  60. };
  61. /*
  62. * create table test (
  63. * bool_col BOOL NULL,
  64. * int2_col INT2 NULL,
  65. * int4_col INT4 NULL,
  66. * int8_col INT8 NULL,
  67. * float4_col FLOAT4 NULL,
  68. * float8_col FLOAT8 NULL,
  69. * timestamp_col TIMESTAMP NULL,
  70. * char_col CHAR NULL,
  71. * text_col TEXT NULL,
  72. * bpchar_col BPCHAR NULL,
  73. * varchar_col VARCHAR(255) NULL,
  74. * bytea_col BYTEA NULL,
  75. * bit_col BIT(32) NULL,
  76. * varbit_col VARBIT NULL
  77. * );
  78. *
  79. * insert into test (bool_col, int2_col, int4_col, int8_col, float4_col, float8_col,
  80. * timestamp_col, char_col, text_col, bpchar_col, varchar_col,
  81. * bytea_col, bit_col, varbit_col)
  82. * values
  83. * (true, 22, 23, 24, 25.21, 25.22, '1999-10-18 21:35:00', 'a',
  84. * 'ab', 'a', 'abcde', 'abcdddd', B'00110011001100110011001100110011',
  85. * b'10101010101010101010101010101010');
  86. */
  87. #if 0
  88. static int pg_test(void)
  89. {
  90. int row, col;
  91. db_res_t* res;
  92. db_con_t* con;
  93. struct tm* tt;
  94. db_key_t keys[1];
  95. db_val_t vals[1];
  96. con = pg_init("postgres://ser:heslo@localhost/ser");
  97. if (!con) {
  98. ERR("Unable to connect database\n");
  99. return -1;
  100. }
  101. INFO("Successfuly connected\n");
  102. pg_use_table(con, "test");
  103. keys[0] = "int4_col";
  104. vals[0].type = DB_INT;
  105. vals[0].nul = 1;
  106. vals[0].val.int_val = 1;
  107. pg_query(con, keys, 0, vals, 0, 1, 0, 0, &res);
  108. if (!res) {
  109. ERR("No result received\n");
  110. return -1;
  111. }
  112. if (!res->n) {
  113. ERR("Result contains no rows\n");
  114. return -1;
  115. } else {
  116. INFO("Result contains %d rows\n", res->n);
  117. }
  118. INFO("Result contains %d columns\n", res->col.n);
  119. for(row = 0; row < res->n; row++) {
  120. for(col = 0; col < res->col.n; col++) {
  121. switch(res->col.types[col]) {
  122. case DB_INT:
  123. INFO("INT(%d)", res->rows[row].values[col].val.int_val);
  124. break;
  125. case DB_FLOAT:
  126. INFO("FLOAT(%f)", res->rows[row].values[col].val.float_val);
  127. break;
  128. case DB_DOUBLE:
  129. INFO("DOUBLE(%f)", res->rows[row].values[col].val.double_val);
  130. break;
  131. case DB_STRING:
  132. INFO("STRING(%s)", res->rows[row].values[col].val.string_val);
  133. break;
  134. case DB_STR:
  135. INFO("STR(%.*s)", res->rows[row].values[col].val.str_val.len, res->rows[row].values[col].val.str_val.s);
  136. break;
  137. case DB_DATETIME:
  138. tt = gmtime(&res->rows[row].values[col].val.time_val);
  139. INFO("DATETIME(%s)", asctime(tt));
  140. break;
  141. case DB_BLOB:
  142. INFO("BLOB(%.*s)", res->rows[row].values[col].val.str_val.len, res->rows[row].values[col].val.str_val.s);
  143. break;
  144. case DB_BITMAP:
  145. INFO("INT(%x)", res->rows[row].values[col].val.bitmap_val);
  146. break;
  147. default:
  148. ERR("Unsupported column type\n");
  149. return -1;
  150. }
  151. }
  152. }
  153. pg_close(con);
  154. return -1;
  155. }
  156. #endif
  157. struct module_exports exports = {
  158. "postgres",
  159. cmds,
  160. 0, /* RPC methods */
  161. params, /* module parameters */
  162. 0, /* module initialization function */
  163. 0, /* response function*/
  164. 0, /* destroy function */
  165. 0, /* oncancel function */
  166. 0 /* per-child init function */
  167. };