40 #ifdef SWITCH_HAVE_PGSQL
45 struct switch_pgsql_handle {
57 struct switch_pgsql_result {
59 ExecStatusType status;
68 #ifdef SWITCH_HAVE_PGSQL
71 if (!(new_handle = malloc(
sizeof(*new_handle)))) {
75 memset(new_handle, 0,
sizeof(*new_handle));
77 if (!(new_handle->dsn = strdup(dsn))) {
83 new_handle->con = NULL;
84 new_handle->affected_rows = 0;
101 #ifdef SWITCH_HAVE_PGSQL
106 char *err_str = NULL;
108 int code = 0, recon = 0;
111 max_tries = handle->num_retries;
128 PQconsumeInput(handle->con);
130 if (PQstatus(handle->con) == CONNECTION_BAD) {
133 PQreset(handle->con);
134 if (PQstatus(handle->con) == CONNECTION_BAD) {
139 handle->sock = PQsocket(handle->con);
166 if (PQstatus(handle->con) == CONNECTION_BAD) {
168 PQreset(handle->con);
169 if (PQstatus(handle->con) == CONNECTION_OK) {
172 handle->sock = PQsocket(handle->con);
218 #ifdef SWITCH_HAVE_PGSQL
220 handle->num_retries = num_retries;
227 #ifdef SWITCH_HAVE_PGSQL
234 PQfinish(handle->con);
248 #ifdef SWITCH_HAVE_PGSQL
252 handle->sql = strdup(sql);
253 if (!PQsendQuery(handle->con, sql)) {
269 #ifdef SWITCH_HAVE_PGSQL
271 PGcancel *cancel = NULL;
274 cancel = PQgetCancel(handle->con);
275 if(!PQcancel(cancel, err_buf, 256)) {
279 PQfreeCancel(cancel);
289 #ifdef SWITCH_HAVE_PGSQL
293 unsigned int usec = msec * 1000;
295 struct pollfd fds[2] = { {0} };
304 if (PQconsumeInput(handle->con)) {
306 if (PQisBusy(handle->con)) {
311 int wait_time = (usec - (ctime - start)) / 1000;
312 fds[0].fd = handle->sock;
313 fds[0].events |= POLLIN;
314 fds[0].events |= POLLERR;
315 fds[0].events |= POLLNVAL;
316 fds[0].events |= POLLHUP;
317 fds[0].events |= POLLPRI;
318 fds[0].events |= POLLRDNORM;
319 fds[0].events |= POLLRDBAND;
322 if ((poll_res = poll(&fds[0], 1, wait_time)) > 0 ) {
323 if (fds[0].revents & POLLHUP || fds[0].revents & POLLNVAL) {
326 }
else if (fds[0].revents & POLLERR) {
329 }
else if (fds[0].revents & POLLIN || fds[0].revents & POLLPRI || fds[0].revents & POLLRDNORM || fds[0].revents & POLLRDBAND) {
331 if (PQconsumeInput(handle->con)) {
332 if (PQstatus(handle->con) == CONNECTION_BAD) {
339 if (!PQisBusy(handle->con)) {
352 }
else if (poll_res == -1) {
359 if (ctime - start > usec) {
384 res->result = PQgetResult(handle->con);
387 res->status = PQresultStatus(res->result);
388 switch(res->status) {
389 #if POSTGRESQL_MAJOR_VERSION >= 9 && POSTGRESQL_MINOR_VERSION >= 2
390 case PGRES_SINGLE_TUPLE:
393 case PGRES_TUPLES_OK:
395 res->rows = PQntuples(res->result);
396 handle->affected_rows = res->rows;
397 res->cols = PQnfields(res->result);
400 #if POSTGRESQL_MAJOR_VERSION >= 9 && POSTGRESQL_MINOR_VERSION >= 1
401 case PGRES_COPY_BOTH:
406 case PGRES_COMMAND_OK:
408 case PGRES_EMPTY_QUERY:
410 case PGRES_BAD_RESPONSE:
412 case PGRES_NONFATAL_ERROR:
414 case PGRES_FATAL_ERROR:
416 res->err = PQresultErrorMessage(res->result);
441 #ifdef SWITCH_HAVE_PGSQL
447 if ((*result)->result) {
448 PQclear((*result)->result);
459 #ifdef SWITCH_HAVE_PGSQL
472 }
else if (res->result) {
473 char *affected_rows = PQcmdTuples(res->result);
475 if (!
zstr(affected_rows)) {
476 handle->affected_rows = atoi(affected_rows);
491 #ifdef SWITCH_HAVE_PGSQL
499 handle->con = PQconnectdb(handle->dsn);
500 if (PQstatus(handle->con) != CONNECTION_OK) {
514 handle->sock = PQsocket(handle->con);
524 #ifdef SWITCH_HAVE_PGSQL
529 handle->affected_rows = 0;
540 switch (result->status) {
541 #if POSTGRESQL_MAJOR_VERSION >= 9 && POSTGRESQL_MINOR_VERSION >= 2
542 case PGRES_SINGLE_TUPLE:
545 case PGRES_COMMAND_OK:
546 case PGRES_TUPLES_OK:
554 if (handle->affected_rows <= 0) {
558 val = PQgetvalue(result->result, 0, 0);
559 strncpy(resbuf, val, len);
579 #ifdef SWITCH_HAVE_PGSQL
580 char *err_str = NULL, *er = NULL;
585 handle->affected_rows = 0;
587 if (!db_is_up(handle)) {
588 er = strdup(
"Database is not up!");
594 er = strdup(
"Error sending BEGIN!");
603 er = strdup(
"Error sending BEGIN!");
610 er = strdup(
"Error sending query!");
624 err_str = strdup((
char *)
"SQL ERROR!");
651 #ifdef SWITCH_HAVE_PGSQL
667 #ifdef SWITCH_HAVE_PGSQL
668 char *err_str = NULL;
669 int row = 0, col = 0, err_cnt = 0;
672 handle->affected_rows = 0;
683 if (result && !
zstr(result->err)) {
686 if (!
zstr(err_str)) {
693 while (result != NULL) {
695 for (row = 0; row < result->rows; ++row) {
699 names = calloc(result->cols,
sizeof(*names));
700 vals = calloc(result->cols,
sizeof(*vals));
704 for (col = 0; col < result->cols; ++col) {
708 tmp = PQfname(result->result, col);
711 names[col] = malloc(len+1);
712 names[col][len] =
'\0';
713 strncpy(names[col], tmp, len);
715 len = PQgetlength(result->result, row, col);
716 vals[col] = malloc(len+1);
717 vals[col][len] =
'\0';
718 tmp = PQgetvalue(result->result, row, col);
719 strncpy(vals[col], tmp, len);
728 if (callback(pdata, result->cols, vals, names)) {
733 for (col = 0; col < result->cols; ++col) {
744 if (result && !
zstr(result->err)) {
747 if (!
zstr(err_str)) {
766 #ifdef SWITCH_HAVE_PGSQL
789 #ifdef SWITCH_HAVE_PGSQL
798 #ifdef SWITCH_HAVE_PGSQL
812 #ifdef SWITCH_HAVE_PGSQL
813 return handle->affected_rows;
821 #ifdef SWITCH_HAVE_PGSQL
830 #ifdef SWITCH_HAVE_PGSQL
844 #ifdef SWITCH_HAVE_PGSQL
846 PGresult *tmp = NULL;
850 while ((tmp = PQgetResult(handle->con)) != NULL) {
867 #ifdef SWITCH_HAVE_PGSQL
868 char * err_str = NULL;
870 if(!PQsendQuery(handle->con,
"COMMIT")) {
877 if(!PQsendQuery(handle->con,
"ROLLBACK")) {
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
#define switch_strdup(ptr, s)
#define SWITCH_CHANNEL_LOG
switch_pgsql_status_t switch_pgsql_SQLEndTran(switch_pgsql_handle_t *handle, switch_bool_t commit)
struct switch_pgsql_handle switch_pgsql_handle_t
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
switch_pgsql_status_t switch_pgsql_handle_exec_detailed(const char *file, const char *func, int line, switch_pgsql_handle_t *handle, const char *sql, char **err)
void switch_pgsql_set_num_retries(switch_pgsql_handle_t *handle, int num_retries)
Sets the number of retries if the PGSQL connection fails.
void switch_pgsql_free_result(switch_pgsql_result_t **result)
#define DEFAULT_PGSQL_RETRIES
int(* switch_core_db_callback_func_t)(void *pArg, int argc, char **argv, char **columnNames)
switch_pgsql_state_t switch_pgsql_handle_get_state(switch_pgsql_handle_t *handle)
switch_pgsql_status_t switch_pgsql_handle_callback_exec_detailed(const char *file, const char *func, int line, switch_pgsql_handle_t *handle, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
Execute the sql query and issue a callback for each row returned.
switch_pgsql_status_t switch_pgsql_SQLSetAutoCommitAttr(switch_pgsql_handle_t *handle, switch_bool_t on)
struct switch_pgsql_result switch_pgsql_result_t
switch_pgsql_status_t switch_pgsql_handle_exec_string_detailed(const char *file, const char *func, int line, switch_pgsql_handle_t *handle, const char *sql, char *resbuf, size_t len, char **err)
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
switch_pgsql_status_t switch_pgsql_cancel_real(const char *file, const char *func, int line, switch_pgsql_handle_t *handle)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
switch_pgsql_status_t switch_pgsql_flush(switch_pgsql_handle_t *handle)
#define switch_pgsql_finish_results(handle)
switch_pgsql_status_t switch_pgsql_finish_results_real(const char *file, const char *func, int line, switch_pgsql_handle_t *handle)
switch_pgsql_status_t switch_pgsql_send_query(switch_pgsql_handle_t *handle, const char *sql)
switch_pgsql_status_t switch_pgsql_handle_disconnect(switch_pgsql_handle_t *handle)
Disconnects a PGSQL connection from the database.
#define switch_pgsql_next_result(h, r)
#define switch_pgsql_handle_exec_base(handle, sql, err)
#define switch_str_nil(s)
Make a null string a blank string instead.
void switch_pgsql_handle_destroy(switch_pgsql_handle_t **handlep)
#define switch_pgsql_cancel(handle)
char * switch_pgsql_handle_get_error(switch_pgsql_handle_t *handle)
switch_pgsql_handle_t * switch_pgsql_handle_new(const char *dsn)
Create a new handle for the PGSQL connection.
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
switch_pgsql_status_t switch_pgsql_handle_exec_base_detailed(const char *file, const char *func, int line, switch_pgsql_handle_t *handle, const char *sql, char **err)
switch_bool_t switch_pgsql_available(void)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_stristr(const char *instr, const char *str)
int switch_pgsql_handle_affected_rows(switch_pgsql_handle_t *handle)
switch_pgsql_status_t switch_pgsql_next_result_timed(switch_pgsql_handle_t *handle, switch_pgsql_result_t **result_out, int msec)
switch_pgsql_status_t switch_pgsql_handle_connect(switch_pgsql_handle_t *handle)
Connect to the database specified by the DSN passed to the switch_pgsql_handle_new() call which initi...