Exemplo n.º 1
0
/**
 * Query a table for specified rows.
 * \param _h structure representing database connection
 * \param _k key names
 * \param _op operators
 *\param  _v values of the keys that must match
 * \param _c column names to return
 * \param _n number of key=values pairs to compare
 * \param _nc number of columns to return
 * \param _o order by the specified column
 * \param _r pointer to a structure representing the result
 * \return zero on success, negative value on failure
 */
int db_mysql_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
	     const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
	     const db_key_t _o, db1_res_t** _r)
{
	return db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r,
	db_mysql_val2str, db_mysql_submit_query, db_mysql_store_result);
}
Exemplo n.º 2
0
/*
 * Query table for specified rows
 * _h: structure representing database connection
 * _k: key names
 * _op: operators
 * _v: values of the keys that must match
 * _c: column names to return
 * _n: number of key=values pairs to compare
 * _nc: number of columns to return
 * _o: order by the specified column
 */
int db_oracle_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
		const db_val_t* _v, const db_key_t* _c, int _n, int _nc,
		const db_key_t _o, db1_res_t** _r)
{
	query_data_t cb;
	OCIStmt* reshp;
	int rc;

	if (!_h || !CON_TABLE(_h) || !_r) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	cb._rs = &reshp;
	cb._v  = _v;
	cb._n  = _n;
	cb._w  = NULL;
	cb._nw = 0;
	CON_ORA(_h)->pqdata = &cb;
	CON_ORA(_h)->bindpos = 0;
	rc = db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r,
		db_oracle_val2str, db_oracle_submit_query, db_oracle_store_result);
	CON_ORA(_h)->pqdata = NULL;	/* paranoid for next call */
	return rc;
}
Exemplo n.º 3
0
/*
 * Query table for specified rows
 * _con: structure representing database connection
 * _k: key names
 * _op: operators
 * _v: values of the keys that must match
 * _c: column names to return
 * _n: nmber of key=values pairs to compare
 * _nc: number of columns to return
 * _o: order by the specified column
 */
int db_postgres_query(const db_con_t* _h, const db_key_t* _k,
	const db_op_t* _op, const db_val_t* _v, const db_key_t* _c, const int _n,
	const int _nc, const db_key_t _o, db_res_t** _r)
{
	CON_RESET_CURR_PS(_h); /* no prepared statements support */
	return db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r,
		db_postgres_val2str, db_postgres_submit_query,
		db_postgres_store_result);
}
Exemplo n.º 4
0
int db_sqlite_query(const db_con_t* _h, const db_key_t* _k, const db_op_t* _op,
	     const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
	     const db_key_t _o, db_res_t** _r)
{
	int ret=-1;
#ifdef SQLITE_BIND
	db_ps_t ps;

	CON_SET_CURR_PS(_h, &ps);
#else
	CON_RESET_CURR_PS(_h);
#endif
	CON_RAW_QUERY(_h) = 0;

	ret = db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, NULL,
		db_sqlite_val2str, db_sqlite_submit_dummy_query, NULL);
	if (ret != 0) {
		if (_r)
			*_r = NULL;
		return ret;
	}

	if (db_copy_rest_of_count(&query_holder, &count_str)) {
		LM_ERR("failed to build row counter query\n");
		return -1;
	}


again:
	ret=sqlite3_prepare_v2(CON_CONNECTION(_h),
				query_holder.s, query_holder.len, &CON_SQLITE_PS(_h), NULL);

	if (ret==SQLITE_BUSY)
		goto again;
	if (ret!=SQLITE_OK)
		LM_ERR("failed to prepare: (%s)\n", sqlite3_errmsg(CON_CONNECTION(_h)));

#ifdef SQLITE_BIND
	if (db_sqlite_bind_values(CON_SQLITE_PS(_h), _v, _n) != SQLITE_OK) {
		LM_ERR("failed to bind values\n");
		return -1;
	}
#endif

	if (_r) {
		ret = db_sqlite_store_result(_h, _r, _v, _n);
		CON_SQLITE_PS(_h) = NULL;
	} else {
		/* need to fetch now the total number of rows in query
		 * because later won't have the query string */
		CON_PS_ROWS(_h) = db_sqlite_get_query_rows(_h, &count_str, _v, _n);
	}

	return ret;
}
Exemplo n.º 5
0
int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t * handle, str * table, db_key_t* _k, db_op_t* _o,
                    db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) {
	int i;
	int working_c[DB_NUM];
	int working_r[DB_NUM];
	int errors = 0;
	int w;

	if(!handle || !table || !table->s) {
		LM_ERR("NULL pointer in parameter.\n");
		return -1;
	}

	if(db_use_transactions) {
		for(i=0; i<DB_NUM; i++) {
			working_c[i] = 0;
			working_r[i] = 0;
		}

		if(ul_db_tran_start(handle, working_r) < 0) {
			LM_ERR("error during starting transaction"
			    " on table %.*s with id %i.\n", table->len, table->s, handle->id);
			w = get_working_sum(working_r, DB_NUM);
			if(db_check_policy(DB_POL_MOD, w, handle->working) < 0) {
				ul_db_tran_rollback(handle, working_r);
				return -1;
			}
		}

		for(i=0; i<DB_NUM; i++) {
			working_c[i] = working_r[i];
			if((handle->db[i].status == DB_ON) && (working_c[i])) {
				if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, table, _k, _o, _v, _uk, _uv, _n, _un) < 0) {
					LM_ERR("error during querying "
					    "table %.*s with id %i on db %i.\n",
					    table->len, table->s, handle->id, i);
					if(db_handle_error(handle, handle->db[i].no) < 0) {
						LM_CRIT("could not handle error on db %i, handle, %i\n",
						    handle->id, handle->db[i].no);
					}
					errors++;
					working_c[i] = 0;
				} else {
					working_r[i] = 0;
				}
			}
		}

		w = get_working_sum(working_c, DB_NUM);
		if(errors > 0) {
			ul_db_tran_rollback(handle, working_r);
			if(db_check_policy(DB_POL_MOD, w, handle->working) < 0) {
				ul_db_tran_rollback(handle, working_c);
				return -1;
			}
		}
		return ul_db_tran_commit(handle, working_c);
	} else {
		for(i=0; i<DB_NUM; i++) {
			if(handle->db[i].status == DB_ON) {
				if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, table, _k, _o, _v, _uk, _uv, _n, _un) < 0) {
					if(db_handle_error(handle, handle->db[i].no) < 0) {
						LM_CRIT("could not handle error on db %i, handle, %i\n",
						    handle->id, handle->db[i].no);
					}
					return -1;
				}
			}
		}
		return 0;
	}
}