/* * Insert a row into specified table * _con: structure representing database connection * _k: key names * _v: values of the keys * _n: number of key=value pairs */ int db_postgres_insert(const db_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n) { db_res_t* _r = NULL; CON_RESET_CURR_PS(_h); /* no prepared statements support */ int tmp = db_do_insert(_h, _k, _v, _n, db_postgres_val2str, db_postgres_submit_query); if (submit_func_called) { /* finish the async query, * otherwise the next query will not complete */ /* only call this if the DB API has effectively called * our submit_query function * * in case of insert queueing, * it may postpone calling the insert func until * enough rows have piled up */ if (db_postgres_store_result(_h, &_r) != 0) LM_WARN("unexpected result returned\n"); submit_func_called = 0; } if (_r) db_free_result(_r); if (CON_HAS_INSLIST(_h)) CON_RESET_INSLIST(_h); return tmp; }
/* * Insert a row into specified table * _con: structure representing database connection * _k: key names * _v: values of the keys * _n: number of key=value pairs */ int db_postgres_insert(const db_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n) { db_res_t* _r = NULL; CON_RESET_CURR_PS(_h); /* no prepared statements support */ /* This needs to be reset before each call to db_do_insert. This is only used by inserts, but as a side effect delete and updates will set it to 1 without resetting it. */ submit_func_called = 0; int tmp = db_do_insert(_h, _k, _v, _n, db_postgres_val2str, db_postgres_submit_query); /* For bulk queries the insert may not be submitted until enough rows are queued */ if (submit_func_called) { /* Query was submitted. Result must be handled. */ if (db_postgres_store_result(_h, &_r) != 0) LM_WARN("unexpected result returned\n"); } if (_r) db_free_result(_r); if (CON_HAS_INSLIST(_h)) CON_RESET_INSLIST(_h); return tmp; }
int db_do_insert(const db_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n, int (*val2str) (const db_con_t*, const db_val_t*, char*, int*), int (*submit_query)(const db_con_t* _h, const str* _c)) { int off, ret,i,no_rows=0; db_val_t **buffered_rows = NULL; if (!_h || !_k || !_v || !_n || !val2str || !submit_query) { LM_ERR("invalid parameter value\n"); return -1; } /* insert buffering is enabled ? */ if (CON_HAS_INSLIST(_h) && !CON_HAS_PS(_h)) { LM_DBG("inlist %p\n",CON_HAS_INSLIST(_h)); if (IS_INSTANT_FLUSH(_h)) { LM_DBG("timer wishing to flush \n"); /* if caller signals it's flush time ( timer, etc ), * detach rows in queue * the caller is holding the lock at this point */ no_rows = ql_detach_rows_unsafe(_h->ins_list,&buffered_rows); CON_FLUSH_RESET(_h,_h->ins_list); if (no_rows == -1) { LM_ERR("failed to detach rows for insertion\n"); goto error; } if (no_rows > 0) goto build_query; else { /* caller wanted to make sure that everything if flushed * but queue is empty */ return 0; } } /* if connection has prepared statement, leave the row insertion to the proper module func, as the submit_query func provided is a dummy one*/ if ( (no_rows = ql_row_add(_h->ins_list,_v,&buffered_rows)) < 0) { LM_ERR("failed to insert row to buffered list \n"); goto error; } LM_DBG("no rows = %d\n",no_rows); if (no_rows == 0) { /* wait for queries to pile up */ return 0; } } build_query: ret = snprintf(sql_buf, SQL_BUF_LEN, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); if (ret < 0) goto error; off += ret; if (CON_HAS_INSLIST(_h)) { if (buffered_rows != NULL || CON_HAS_PS(_h)) { /* if we have to insert now, build the query * * if a prep stmt is provided, * build a prep stmt with query_buffer_size elements */ if (CON_HAS_PS(_h)) no_rows = query_buffer_size; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values"); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; for (i=0;i<no_rows;i++) { sql_buf[off++]='('; ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, CON_HAS_PS(_h)?_v:buffered_rows[i], _n, val2str); if (ret < 0) goto error; off += ret; sql_buf[off++]=')'; if (i != (no_rows -1)) sql_buf[off++]=','; /* if we have a PS, leave the function handling prep stmts in the module to free the rows once it's done */ if (!CON_HAS_PS(_h)) { shm_free(buffered_rows[i]); buffered_rows[i] = NULL; } } if (off + 1 > SQL_BUF_LEN) goto error0; sql_buf[off] = '\0'; sql_str.s = sql_buf; sql_str.len = off; goto submit; } else { /* wait for queries to pile up */ return 0; } } else { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error0; off += ret; ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n, val2str); if (ret < 0) goto error0; off += ret; if (off + 2 > SQL_BUF_LEN) goto error0; } sql_buf[off++] = ')'; sql_buf[off] = '\0'; sql_str.s = sql_buf; sql_str.len = off; submit: if (submit_query(_h, &sql_str) < 0) { LM_ERR("error while submitting query\n"); return -2; } return 0; error: cleanup_rows(buffered_rows); error0: LM_ERR("error while preparing insert operation\n"); return -1; }