7 #define sql_update pgsql_update 8 #define sql_select pgsql_select 9 #define exit_nicely(conn) do { PQfinish(conn); } while(0) 11 static Octstr *sqlbox_logtable;
12 static Octstr *sqlbox_insert_table;
18 static DBPool *pool = NULL;
26 static void pgsql_update(
const Octstr *sql)
32 #if defined(SQLBOX_TRACE) 38 error(0,
"PGSQL: Database pool got no connection! DB update failed!");
43 status = PQresultStatus(res);
45 case PGRES_BAD_RESPONSE:
46 case PGRES_NONFATAL_ERROR:
47 case PGRES_FATAL_ERROR:
48 error (0,
"PGSQL: %s", PQresultErrorMessage(res));
58 static PGresult *pgsql_select(
const Octstr *sql)
63 #if defined(SQLBOX_TRACE) 69 error(0,
"PGSQL: Database pool got no connection! DB operation failed!");
74 switch (PQresultStatus(res)) {
75 case PGRES_EMPTY_QUERY:
76 case PGRES_BAD_RESPONSE:
77 case PGRES_NONFATAL_ERROR:
78 case PGRES_FATAL_ERROR:
79 error(0,
"PGSQL: %s", PQresultErrorMessage(res));
89 void sqlbox_configure_pgsql(
Cfg*
cfg)
95 panic(0,
"SQLBOX: PGSQL: group 'sqlbox' is not specified!");
98 if (sqlbox_logtable == NULL) {
99 panic(0,
"No 'sql-log-table' not configured.");
102 if (sqlbox_insert_table == NULL) {
103 panic(0,
"No 'sql-insert-table' not configured.");
107 sql =
octstr_format(SQLBOX_PGSQL_CREATE_LOG_TABLE, sqlbox_logtable);
110 sql =
octstr_format(SQLBOX_PGSQL_CREATE_INSERT_TABLE, sqlbox_insert_table);
116 static Octstr *get_numeric_value_or_return_null(
long int num)
124 static Octstr *get_string_value_or_return_null(
Octstr *str)
137 #define st_num(x) (stuffer[stuffcount++] = get_numeric_value_or_return_null(x)) 138 #define st_str(x) (stuffer[stuffcount++] = get_string_value_or_return_null(x)) 146 sql =
octstr_format(SQLBOX_PGSQL_INSERT_QUERY, sqlbox_logtable, st_str(momt), st_str(
msg->sms.sender),
147 st_str(
msg->sms.receiver), st_str(
msg->sms.udhdata), st_str(
msg->sms.msgdata), st_num(
msg->sms.time),
148 st_str(
msg->sms.smsc_id), st_str(
msg->sms.service), st_str(
msg->sms.account), st_num(
msg->sms.sms_type),
149 st_num(
msg->sms.mclass), st_num(
msg->sms.mwi), st_num(
msg->sms.coding), st_num(
msg->sms.compress),
150 st_num(
msg->sms.validity), st_num(
msg->sms.deferred), st_num(
msg->sms.dlr_mask), st_str(
msg->sms.dlr_url),
151 st_num(
msg->sms.pid), st_num(
msg->sms.alt_dcs), st_num(
msg->sms.rpi), st_str(
msg->sms.charset),
152 st_str(
msg->sms.boxc_id), st_str(
msg->sms.binfo), st_str(
msg->sms.meta_data), st_str(
msg->sms.foreign_id));
155 while (stuffcount > 0) {
166 #define octstr_null_create(x) (octstr_create(PQgetvalue(res, 0, x))) 167 #define atol_null(x) ((PQgetisnull(res, 0, x) == 0) ? atol(PQgetvalue(res, 0, x)) : -1) 168 Msg *pgsql_fetch_msg()
174 sql =
octstr_format(SQLBOX_PGSQL_SELECT_QUERY, sqlbox_insert_table);
175 #if defined(SQLBOX_TRACE) 178 res = pgsql_select(sql);
183 if (PQntuples(res) >= 1) {
184 id = octstr_null_create(0);
188 msg->sms.foreign_id = octstr_null_create(0);
189 msg->sms.sender = octstr_null_create(2);
190 msg->sms.receiver = octstr_null_create(3);
191 msg->sms.udhdata = octstr_null_create(4);
192 msg->sms.msgdata = octstr_null_create(5);
193 msg->sms.time = atol_null(6);
194 msg->sms.smsc_id = octstr_null_create(7);
195 msg->sms.service = octstr_null_create(8);
196 msg->sms.account = octstr_null_create(9);
198 msg->sms.sms_type = atol_null(11);
199 msg->sms.mclass = atol_null(12);
200 msg->sms.mwi = atol_null(13);
201 msg->sms.coding = atol_null(14);
202 msg->sms.compress = atol_null(15);
203 msg->sms.validity = atol_null(16);
204 msg->sms.deferred = atol_null(17);
205 msg->sms.dlr_mask = atol_null(18);
206 msg->sms.dlr_url = octstr_null_create(19);
207 msg->sms.pid = atol_null(20);
208 msg->sms.alt_dcs = atol_null(21);
209 msg->sms.rpi = atol_null(22);
210 msg->sms.charset = octstr_null_create(23);
211 msg->sms.binfo = octstr_null_create(25);
212 msg->sms.meta_data = octstr_null_create(26);
213 if ((PQgetvalue(res, 0, 24)) == NULL) {
217 msg->sms.boxc_id= octstr_null_create(24);
220 delet =
octstr_format(SQLBOX_PGSQL_DELETE_QUERY, sqlbox_insert_table,
id);
221 #if defined(SQLBOX_TRACE) 239 Octstr *pgsql_host, *pgsql_user, *pgsql_pass, *pgsql_db, *pgsql_id;
241 long pool_size, pgsql_port;
251 panic(0,
"SQLBOX: PGSQL: group 'sqlbox' is not specified!");
254 panic(0,
"SQLBOX: PGSQL: directive 'id' is not specified!");
272 panic(0,
"SQLBOX: PGSQL: connection settings for id '%s' are not specified!",
283 panic(0,
"SQLBOX: PGSQL: directive 'host' is not specified!");
285 panic(0,
"SQLBOX: PGSQL: directive 'username' is not specified!");
287 panic(0,
"SQLBOX: PGSQL: directive 'password' is not specified!");
289 panic(0,
"SQLBOX: PGSQL: directive 'database' is not specified!");
295 db_conf = gw_malloc(
sizeof(
DBConf));
316 panic(0,
"SQLBOX: PGSQL: database pool has no connections!");
void error(int err, const char *fmt,...)
int(* sql_fetch_msg_list)(List *, long)
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
long dbpool_conn_count(DBPool *p)
DBPool * dbpool_create(enum db_type db_type, DBConf *conf, unsigned int connections)
gw_assert(wtls_machine->packet_to_send !=NULL)
#define cfg_get(grp, varname)
void(* sql_save_list)(List *, Octstr *, int)
#define octstr_get_cstr(ostr)
static struct pid_list * found
void dbpool_conn_produce(DBPoolConn *conn)
Octstr * octstr_imm(const char *cstr)
void * gwlist_extract_first(List *list)
#define octstr_duplicate(ostr)
List * cfg_get_multi_group(Cfg *cfg, Octstr *name)
Octstr * octstr_format(const char *fmt,...)
void octstr_destroy(Octstr *ostr)
#define octstr_create(cstr)
void dbpool_destroy(DBPool *p)
void debug(const char *place, int err, const char *fmt,...)
int cfg_get_integer(long *n, CfgGroup *grp, Octstr *varname)
DBPoolConn * dbpool_conn_consume(DBPool *p)
CfgGroup * cfg_get_single_group(Cfg *cfg, Octstr *name)
static XMLRPCDocument * msg
void(* sql_save_msg)(Msg *, Octstr *)
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)