dbase.c

来自「性能优秀的SIP Proxy」· C语言 代码 · 共 746 行 · 第 1/2 页

C
746
字号
	res->tail = (unsigned long)con;	return res;	err:	if (id) free_db_id(id);	if (res) pkg_free(res);	return 0;}/* * Shut down database module * No function should be called after this */void db_close(db_con_t* _h){	struct pool_con* con;	if (!_h)	{		LOG(L_ERR, "db_close: Invalid parameter value\n");		return;	}	con = (struct pool_con*)_h->tail;	if (pool_remove(con) != 0)	{		free_connection((struct my_con*)con);	}	pkg_free(_h);}/* * Retrieve result set */static int store_result(db_con_t* _h, db_res_t** _r){	if ((!_h) || (!_r))	{		LOG(L_ERR, "store_result: Invalid parameter value\n");		return -1;	}	*_r = new_result();	if (*_r == 0)	{		LOG(L_ERR, "store_result: No memory left\n");		return -2;	}	if (convert_result(_h, *_r) < 0)	{		LOG(L_ERR, "store_result: Error while converting result\n");		pkg_free(*_r);		*_r = 0;		return -4;	}	return 0;}/* * Release a result set from memory */int db_free_result(db_con_t* _h, db_res_t* _r){	if ((!_h) || (!_r))	{		LOG(L_ERR, "db_free_result: Invalid parameter value\n");		return -1;	}	if (free_result(_r) < 0)	{		LOG(L_ERR, "db_free_result: Unable to free result structure\n");		return -1;	}	SQLFreeHandle(SQL_HANDLE_STMT, CON_RESULT(_h));	CON_RESULT(_h) = 0;	return 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_query(db_con_t* _h, db_key_t* _k, db_op_t* _op,db_val_t* _v, db_key_t* _c, int _n, int _nc,db_key_t _o, db_res_t** _r){	int off, ret;	if (!_h)	{		LOG(L_ERR, "db_query: Invalid parameter value\n");		return -1;	}	if (!_c)	{		ret = snprintf(sql_buf, SQL_BUF_LEN, "select * from %s ", CON_TABLE(_h));		if (ret < 0 || ret >= SQL_BUF_LEN) goto error;		off = ret;	}	else	{		ret = snprintf(sql_buf, SQL_BUF_LEN, "select ");		if (ret < 0 || ret >= SQL_BUF_LEN) goto error;		off = ret;		ret = print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc);		if (ret < 0) return -1;		off += ret;		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %s ", CON_TABLE(_h));		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;		off += ret;	}	if (_n)	{		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "where ");		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;		off += ret;		ret = print_where(&CON_CONNECTION(_h), sql_buf + off, SQL_BUF_LEN - off, _k, _op, _v, _n);		if (ret < 0) return -1;;		off += ret;	}	if (_o)	{		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "order by %s", _o);		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;		off += ret;	}	*(sql_buf + off) = '\0';	if (submit_query(_h, sql_buf) < 0)	{		LOG(L_ERR, "unixodbc:db_query: Error while submitting query\n");		return -2;	}	return store_result(_h, _r);	error:	LOG(L_ERR, "unixodbc:db_query: Error in snprintf\n");	return -1;}/* * Execute a raw SQL query */int db_raw_query(db_con_t* _h, char* _s, db_res_t** _r){	if ((!_h) || (!_s))	{		LOG(L_ERR, "db_raw_query: Invalid parameter value\n");		return -1;	}	if (submit_query(_h, _s) < 0)	{		LOG(L_ERR, "db_raw_query: Error while submitting query\n");		return -2;	}	if(_r)		return store_result(_h, _r);	return 0;}/* * Insert a row into specified table * _h: structure representing database connection * _k: key names * _v: values of the keys * _n: number of key=value pairs */int db_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n){	int off, ret;	if ((!_h) || (!_k) || (!_v) || (!_n))	{		LOG(L_ERR, "db_insert: Invalid parameter value\n");		return -1;	}	ret = snprintf(sql_buf, SQL_BUF_LEN, "insert into %s (", CON_TABLE(_h));	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;	off = ret;	ret = print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n);	if (ret < 0) return -1;	off += ret;	ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values (");	if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;	off += ret;	ret = print_values(&CON_CONNECTION(_h), sql_buf + off, SQL_BUF_LEN - off, _v, _n);	if (ret < 0) return -1;	off += ret;	*(sql_buf + off++) = ')';	*(sql_buf + off) = '\0';	if (submit_query(_h, sql_buf) < 0)	{		LOG(L_ERR, "db_insert: Error while submitting query\n");		return -2;	}	return 0;	error:	LOG(L_ERR, "db_insert: Error in snprintf\n");	return -1;}/* * Delete a row from the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _n: number of key=value pairs */int db_delete(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n){	int off, ret;	if (!_h)	{		LOG(L_ERR, "db_delete: Invalid parameter value\n");		return -1;	}	ret = snprintf(sql_buf, SQL_BUF_LEN, "delete from %s", CON_TABLE(_h));	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;	off = ret;	if (_n)	{		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " where ");		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;		off += ret;		ret = print_where(&CON_CONNECTION(_h), sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n);		if (ret < 0) return -1;		off += ret;	}	*(sql_buf + off) = '\0';	if (submit_query(_h, sql_buf) < 0)	{		LOG(L_ERR, "db_delete: Error while submitting query\n");		return -2;	}	return 0;	error:	LOG(L_ERR, "db_delete: Error in snprintf\n");	return -1;}/* * Update some rows in the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _uk: updated columns * _uv: updated values of the columns * _n: number of key=value pairs * _un: number of columns to update */int db_update(db_con_t* _h, 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 off, ret;	if ((!_h) || (!_uk) || (!_uv) || (!_un))	{		LOG(L_ERR, "db_update: Invalid parameter value\n");		return -1;	}	ret = snprintf(sql_buf, SQL_BUF_LEN, "update %s set ", CON_TABLE(_h));	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;	off = ret;	ret = print_set(&CON_CONNECTION(_h), sql_buf + off, SQL_BUF_LEN - off,			_uk, _uv, _un);	if (ret < 0) return -1;	off += ret;	if (_n)	{		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " where ");		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;		off += ret;		ret = print_where(&CON_CONNECTION(_h), sql_buf + off,				SQL_BUF_LEN - off, _k, _o, _v, _n);		if (ret < 0) return -1;		off += ret;	}	*(sql_buf + off) = '\0';	if (submit_query(_h, sql_buf) < 0)	{		LOG(L_ERR, "db_update: Error while submitting query\n");		return -2;	}	return 0;	error:	LOG(L_ERR, "db_update: Error in snprintf\n");	return -1;}/* * Just like insert, but replace the row if it exists */int db_replace(db_con_t* handle, db_key_t* keys, db_val_t* vals, int n){	int off, ret;	if (!handle || !keys || !vals)	{		LOG(L_ERR, "db_replace: Invalid parameter value\n");		return -1;	}	ret = snprintf(sql_buf, SQL_BUF_LEN, "replace %s (", CON_TABLE(handle));	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;	off = ret;	ret = print_columns(sql_buf + off, SQL_BUF_LEN - off, keys, n);	if (ret < 0) return -1;	off += ret;	ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values (");	if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;	off += ret;	ret = print_values(&CON_CONNECTION(handle), sql_buf + off,			SQL_BUF_LEN - off, vals, n);	if (ret < 0) return -1;	off += ret;	*(sql_buf + off++) = ')';	*(sql_buf + off) = '\0';	if (submit_query(handle, sql_buf) < 0)	{		LOG(L_ERR, "db_replace: Error while submitting query\n");		return -2;	}	return 0;	error:	LOG(L_ERR, "db_replace: Error in snprintf\n");	return -1;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?