db_mod.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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", INT_PARAM, &connect_timeout },
  58. {"reconnect_attempts", INT_PARAM, &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. static int pg_test(void)
  88. {
  89. int row, col;
  90. db_res_t* res;
  91. db_con_t* con;
  92. struct tm* tt;
  93. con = pg_init("postgres://ser:heslo@localhost/ser");
  94. if (!con) {
  95. ERR("Unable to connect database\n");
  96. return -1;
  97. }
  98. INFO("Successfuly connected\n");
  99. pg_use_table(con, "test");
  100. pg_query(con, 0, 0, 0, 0, 0, 0, 0, &res);
  101. if (!res) {
  102. ERR("No result received\n");
  103. return -1;
  104. }
  105. if (!res->n) {
  106. ERR("Result contains no rows\n");
  107. return -1;
  108. } else {
  109. INFO("Result contains %d rows\n", res->n);
  110. }
  111. INFO("Result contains %d columns\n", res->col.n);
  112. for(row = 0; row < res->n; row++) {
  113. for(col = 0; col < res->col.n; col++) {
  114. switch(res->col.types[col]) {
  115. case DB_INT:
  116. INFO("INT(%d)", res->rows[row].values[col].val.int_val);
  117. break;
  118. case DB_DOUBLE:
  119. INFO("DOUBLE(%f)", res->rows[row].values[col].val.double_val);
  120. break;
  121. case DB_STRING:
  122. INFO("STRING(%s)", res->rows[row].values[col].val.string_val);
  123. break;
  124. case DB_STR:
  125. INFO("STR(%.*s)", res->rows[row].values[col].val.str_val.len, res->rows[row].values[col].val.str_val.s);
  126. break;
  127. case DB_DATETIME:
  128. tt = gmtime(&res->rows[row].values[col].val.time_val);
  129. INFO("DATETIME(%s)", asctime(tt));
  130. break;
  131. case DB_BLOB:
  132. INFO("BLOB(%.*s)", res->rows[row].values[col].val.str_val.len, res->rows[row].values[col].val.str_val.s);
  133. break;
  134. case DB_BITMAP:
  135. INFO("INT(%x)", res->rows[row].values[col].val.bitmap_val);
  136. break;
  137. default:
  138. ERR("Unsupported column type\n");
  139. return -1;
  140. }
  141. }
  142. }
  143. pg_close(con);
  144. return -1;
  145. }
  146. struct module_exports exports = {
  147. "postgres",
  148. cmds,
  149. 0, /* RPC methods */
  150. params, /* module parameters */
  151. 0, /* module initialization function */
  152. 0, /* response function*/
  153. 0, /* destroy function */
  154. 0, /* oncancel function */
  155. 0 /* per-child init function */
  156. };