Exemple #1
0
/*
 * 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;
}
Exemple #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: nmber 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, rv;
	if (!_c) {
		off = snprintf(sql_buf, SQL_BUF_LEN,
			"select * from %s ", CON_TABLE(_h));
	} else {
		off = snprintf(sql_buf, SQL_BUF_LEN, "select ");
		off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc);
		off += snprintf(sql_buf + off, SQL_BUF_LEN - off,
			"from %s ", CON_TABLE(_h));
	}
	if (_n) {
		off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "where ");
		off += print_where(sql_buf + off, SQL_BUF_LEN - off,
			_k, _op, _v, _n);
	}
	if (_o) {
		off += snprintf(sql_buf + off, SQL_BUF_LEN - off,
			"order by %s", _o);
	}

	if(begin_transaction(_h, sql_buf)) return(-1);
	if (submit_query(_h, sql_buf) < 0) {
		LOG(L_ERR, "db_query(): Error while submitting query\n");
		return -2;
	}
	rv = get_result(_h, _r);
	free_query(_h);
	commit_transaction(_h);
	return(rv);
}
Exemple #3
0
int db_do_delete( db_con_t* _h,  db_key_t* _k,  db_op_t* _o,
	 db_val_t* _v,  int _n, int (*val2str) ( db_con_t*,
	 db_val_t*, char*, int*), int (*submit_query)( db_con_t* _h,
	 str* _c),str *query_holder)
{
	int off, ret;
	str  sql_str;
	char sql_buf[SQL_BUF_LEN];

	if (!_h || !val2str || (!submit_query && !query_holder)) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	ret = snprintf(sql_buf, SQL_BUF_LEN, "delete from %.*s", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
	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 = db_print_where(_h, sql_buf + off,
				SQL_BUF_LEN - off, _k, _o, _v, _n, val2str);
		if (ret < 0) return -1;
		off += ret;
	}
	if (off + 1 > SQL_BUF_LEN) goto error;
	sql_buf[off] = '\0';
	sql_str.s = sql_buf;
	sql_str.len = off;

	if (submit_query)
	{
		if (submit_query(_h, &sql_str) < 0) {
			LM_ERR("error while submitting query\n");
			return -2;
		}
	}
	else
	{
		query_holder->s = pkg_malloc(off);
		if (!query_holder->s)
		{
			LM_ERR("no more pkg mem\n");
			return -2;
		}
		memcpy(query_holder->s,sql_buf,off);
		query_holder->len = off;
	}
	return 0;

error:
	LM_ERR("error while preparing delete operation\n");
	return -1;
}
Exemple #4
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, "db_query: Error while submitting query\n");
		return -2;
	}

	return store_result(_h, _r);

 error:
	LOG(L_ERR, "db_query: Error in snprintf\n");
	return -1;
}
Exemple #5
0
static void do_query(ruli_res_t *res_ctx, const char *name, int cname)
{
  int  name_len = strlen(name);
  char *dname_buf;
  int  dname_buf_len;
  int  dname_len;
  char *i;

  dname_buf_len = ruli_dname_encode_size(name, name_len);

  /* Allocate buffer */
  dname_buf = (char *) ruli_malloc(dname_buf_len);
  if (!dname_buf) {
    fprintf(stderr, 
	    "%s: do_query(): ruli_malloc(%d) failed: %s\n",
	    prog_name, dname_buf_len, strerror(errno));
    
    return;
  }
  
  i = ruli_dname_encode(dname_buf, dname_buf_len, name, name_len);
  if (!i) {
    const int DOM_BUFSZ = 256;
    char      dom_buf[DOM_BUFSZ];
    int       dom_len = RULI_MIN(name_len, DOM_BUFSZ - 1);
    
    memcpy(dom_buf, name, dom_len);
    dom_buf[dom_len] = '\0';
    
    fprintf(stderr, 
	    "%s: do_query(): can't encode domain: (total_len=%d displaying=%d) %s\n", 
	    prog_name, name_len, dom_len, dom_buf);
    
    return;
  }
  dname_len = i - dname_buf;

  /*
   * Send query
   */
  {
    ruli_host_t *qry = submit_query(res_ctx, dname_buf, dname_len,
				    name, name_len, cname);
    if (!qry) {
      fprintf(stderr, 
	      "%s: do_query(): could not send query\n",
	      prog_name);
      
      return;
    }
  }

}
Exemple #6
0
/*
 * Execute a raw SQL query
 */
int db_raw_query(db_con_t* _h, char* _s, db_res_t** _r)
{
	int rv;

	if(begin_transaction(_h, sql_buf)) return(-1);
	if (submit_query(_h, _s) < 0) {
		LOG(L_ERR, "db_raw_query(): Error while submitting query\n");
		return -2;
	}
	rv = get_result(_h, _r);
	free_query(_h);
	commit_transaction(_h);
	return(rv);
}
Exemple #7
0
int db_do_update(const db_con_t* _h, const db_key_t* _k, const db_op_t* _o,
	const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n,
	const int _un, 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;

	if (!_h || !_uk || !_uv || !_un || !val2str || !submit_query) {
		LM_ERR("invalid parameter value\n");
		goto err_exit;
	}

	ret = snprintf(sql_buf, SQL_BUF_LEN, "update %.*s set ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
	if (ret < 0 || ret >= SQL_BUF_LEN) goto error;
	off = ret;

	ret = db_print_set(_h, sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un, val2str);
	if (ret < 0) goto err_exit;
	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 = db_print_where(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n, val2str);
		if (ret < 0) goto err_exit;
		off += ret;
	}
	if (off + 1 > SQL_BUF_LEN) goto error;
	sql_buf[off] = '\0';
	sql_str.s = sql_buf;
	sql_str.len = off;

	if (submit_query(_h, &sql_str) < 0) {
		LM_ERR("error while submitting query\n");
		CON_OR_RESET(_h);
		return -2;
	}	

	CON_OR_RESET(_h);
	return 0;

error:
	LM_ERR("error while preparing update operation\n");
err_exit:
	CON_OR_RESET(_h);
	return -1;
}
Exemple #8
0
/*
 * 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;
}
Exemple #9
0
int db_do_replace( db_con_t* _h,  db_key_t* _k,  db_val_t* _v,
	 int _n, int (*val2str) ( db_con_t*,  db_val_t*, char*,
	int*), int (*submit_query)( db_con_t* _h,  str* _c))
{
	int off, ret;
	str  sql_str;
	char sql_buf[SQL_BUF_LEN];

	if (!_h || !_k || !_v || !val2str|| !submit_query) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	ret = snprintf(sql_buf, SQL_BUF_LEN, "replace %.*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) 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 = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n,
	val2str);
	if (ret < 0) return -1;
	off += ret;

	if (off + 2 > SQL_BUF_LEN) goto error;
	sql_buf[off++] = ')';
	sql_buf[off] = '\0';
	sql_str.s = sql_buf;
	sql_str.len = off;

	if (submit_query(_h, &sql_str) < 0) {
	        LM_ERR("error while submitting query\n");
		return -2;
	}
	return 0;

 error:
	LM_ERR("error while preparing replace operation\n");
	return -1;
}
Exemple #10
0
/*
 * 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;
	off = snprintf(sql_buf, SQL_BUF_LEN, "delete from %s", CON_TABLE(_h));
	if (_n) {
		off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where ");
		off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k,
			_o, _v, _n);
	}
	if(begin_transaction(_h, sql_buf)) return(-1);
	if (submit_query(_h, sql_buf) < 0) {
		LOG(L_ERR, "db_delete(): Error while deleting\n");
		return -2;
	}
	free_query(_h);
	commit_transaction(_h);
	return(0);
}
bool db_query_widget::init(const db_connection *cnn, const QString &SQL)
{
  pv_cnn = cnn;
  pv_SQL = SQL;

  pv_vlayout = new QVBoxLayout(this);
  lb_title = new QLabel(tr("SQL query widget - Connection: " ) + pv_cnn->get_cnn_name());
  pv_vlayout->addWidget(lb_title);
  pv_table = new QTableWidget(this);
  pv_vlayout->addWidget(pv_table);
  le_sql = new db_mem_lineedit;
  pv_vlayout->addWidget(le_sql);
  pv_hlayout = new QHBoxLayout(this);
  pv_vlayout->addLayout(pv_hlayout);
  pb_submit = new QPushButton(tr("&Submit"));
  pv_hlayout->addWidget(pb_submit);
  pb_html = new QPushButton(tr("&Export HTML"));
  pv_hlayout->addWidget(pb_html);
  pb_pdf = new QPushButton(tr("Export PDF"));
  pv_hlayout->addWidget(pb_pdf);
  lb_status = new QLabel(tr("Ready"));
  pv_vlayout->addWidget(lb_status);

  //pv_printer = new QPrinter;
  //pv_painter = new QPainter;

  pv_text_doc = new QTextDocument(this);
  te_text = new QTextEdit;
  //te_text->show();

  pv_table->setEditTriggers(QAbstractItemView::NoEditTriggers);

  pv_query = new QSqlQuery(pv_cnn->get_db());
  if(!pv_SQL.isEmpty()){
    new_query(pv_SQL);
  }

  //pv_label.init(cnn);

  connect(pb_submit, SIGNAL(clicked()),this, SLOT(submit_query()));
  connect(pb_html, SIGNAL(clicked()),this, SLOT(export_html()));
  connect(pb_pdf, SIGNAL(clicked()),this, SLOT(export_pdf()));
  return true;
}
Exemple #12
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;
	off = snprintf(sql_buf, SQL_BUF_LEN, "insert into %s (", CON_TABLE(_h));
	off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n);
	off += snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values (");
	off += print_values(sql_buf + off, SQL_BUF_LEN - off, _v, _n);
	*(sql_buf + off++) = ')';
	*(sql_buf + off) = '\0';

	if(begin_transaction(_h, sql_buf)) return(-1);
	if (submit_query(_h, sql_buf) < 0) {
		LOG(L_ERR, "db_insert(): Error while inserting\n");
		return -2;
	}
	free_query(_h);
	commit_transaction(_h);
	return(0);
}
Exemple #13
0
static inline int db_do_submit_query(const db1_con_t* _h, const str *_query,
		int (*submit_query)(const db1_con_t*, const str*))
{
	int ret;
	unsigned int ms = 0;

	if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0))
		ms = TICKS_TO_MS(get_ticks_raw());

	ret = submit_query(_h, _query);

	if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0)) {
		ms = TICKS_TO_MS(get_ticks_raw()) - ms;
		if(ms >= cfg_get(core, core_cfg, latency_limit_action)) {
				LOG(cfg_get(core, core_cfg, latency_log),
					"alert - query execution too long [%u ms] for [%.*s]\n",
				   ms, _query->len<50?_query->len:50, _query->s);
		}
	}

	return ret;
}
Exemple #14
0
/*
 * 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;
	off = snprintf(sql_buf, SQL_BUF_LEN, "update %s set ", CON_TABLE(_h));
	off += print_set(sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un);
	if (_n) {
		off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where ");
		off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k,
			_o, _v, _n);
		*(sql_buf + off) = '\0';
	}

	if(begin_transaction(_h, sql_buf)) return(-1);
	if (submit_query(_h, sql_buf) < 0) {
		LOG(L_ERR, "db_update(): Error while updating\n");
		return -2;
	}
	free_query(_h);
	commit_transaction(_h);
	return(0);
}
Exemple #15
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;
}
Exemple #16
0
/*
 * 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;
}
Exemple #17
0
int db_do_raw_query(const db_con_t* _h, const str* _s, db_res_t** _r,
	int (*submit_query)(const db_con_t* _h, const str* _c),
	int (*store_result)(const db_con_t* _h, db_res_t** _r))
{
	if (!_h || !_s || !submit_query || !store_result) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (submit_query(_h, _s) < 0) {
		LM_ERR("error while submitting query\n");
		return -2;
	}

	if(_r) {
		int tmp = store_result(_h, _r);
		if (tmp < 0) {
			LM_ERR("error while storing result");
			return tmp;
		}
	}
	return 0;
}
Exemple #18
0
int db_do_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 (*val2str) (const db_con_t*,
	const db_val_t*, char*, int* _len), int (*submit_query)(const db_con_t*,
	const str*), int (*store_result)(const db_con_t* _h, db_res_t** _r))
{
	int off, ret;

	if (!_h || !val2str || !submit_query || (_r && !store_result)) {
		LM_ERR("invalid parameter value\n");
		goto err_exit;
	}

	if (!_c) {
		ret = snprintf(sql_buf, SQL_BUF_LEN, "select * from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
		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 = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc);
		if (ret < 0) goto err_exit;
		off += ret;

		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
		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 = db_print_where(_h, sql_buf + off,
				SQL_BUF_LEN - off, _k, _op, _v, _n, val2str);
		if (ret < 0) goto err_exit;
		off += ret;
	}
	if (_o) {
		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " order by %.*s", _o->len, _o->s);
		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
		off += ret;
	}
	/*
	 * Null-terminate the string for the postgres driver. Its query function
	 * don't support a length parameter, so they need this for the correct
	 * function of strlen. This zero is not included in the 'str' length.
	 * We need to check the length here, otherwise we could overwrite the buffer
	 * boundaries if off is equal to SQL_BUF_LEN.
	 */
	if (off + 1 >= SQL_BUF_LEN) goto error;
	sql_buf[off + 1] = '\0';
	sql_str.s = sql_buf;
	sql_str.len = off;

	if (submit_query(_h, &sql_str) < 0) {
		LM_ERR("error while submitting query - [%.*s]\n",sql_str.len,sql_str.s);
		goto err_exit;
	}

	if(_r) {
		int tmp = store_result(_h, _r);
		if (tmp < 0) {
			LM_ERR("error while storing result for query [%.*s]\n",sql_str.len,sql_str.s);
			CON_OR_RESET(_h);
			return tmp;
		}
	}

	CON_OR_RESET(_h);
	return 0;

error:
	LM_ERR("error while preparing query\n");
err_exit:
	CON_OR_RESET(_h);
	return -1;
}
Exemple #19
0
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;
}
Exemple #20
0
static void *on_stdin_read(oop_source *oop_src, int std_in, 
			   oop_event event, void *ctx)
{
  ruli_res_t *res_ctx = (ruli_res_t *) ctx;
  int        result;
  const int  QBUF_SIZE = sizeof(srv_qbuf_t);

  assert(std_in == 0);
  assert(event == OOP_READ);

  /*
   * Read stdin
   */

  result = read_stdin(std_in);

  switch (result) {
  case STDIN_READ_OK:
    break;

  case STDIN_READ_BLOCK:
    return OOP_CONTINUE;

  case STDIN_READ_EOF:
    /* Stop monitoring stdin */
    oop_src->cancel_fd(oop_src, std_in, OOP_READ);
    return OOP_CONTINUE;

  case STDIN_READ_ERROR:
    return OOP_HALT;

  case STDIN_READ_OVERFLOW:
    fprintf(stderr, 
	    "%s: on_stdin_read(): stdin read buffer overflow\n", 
	    prog_name);
    reset_stdin_buf();
    return OOP_CONTINUE;

  default:
    assert(0); /* NOT REACHED */
  }

  /*
   * Scan possible hostnames from stdin
   */
  for (;;) {

    srv_qbuf_t *qbuf;

    /* plain domain */
    const int domain_buf_size = IN_BUF_SIZE;
    char      domain[domain_buf_size];
    int       domain_len;
    
    /*
     * Parse hostname
     */

    result = get_next_domain(domain, domain_buf_size, &domain_len);
    
    /* If no domain found yet, keep waiting for one */
    if (result == PARSE_DOMAIN_NONE)
      return OOP_CONTINUE;
    
    /* Do not expect other errors, not even overflow */
    assert(!result);

    /*
     * Now we have the full domain name, submit a query for it
     */

    /*
     * Break full domain name in service + domain into qbuf
     */

    {
      char *past_end = domain + domain_len;
      char *i = domain;

      for (; i < past_end; ++i) {
	if (*i == '.') {
	  ++i;
	  if (i < past_end) {
	    if (*i != '_')
	      break;
	  }
	}
      }

      if (i >= past_end) {
	fprintf(stderr, 
		"%s: on_stdin_read(): could not split service/domain\n",
		prog_name);

	return OOP_CONTINUE;
      }

      /* Allocate qbuf */
      qbuf = (srv_qbuf_t *) malloc(QBUF_SIZE);
      if (!qbuf) {
	fprintf(stderr, 
		"%s: on_stdin_read(): could not allocate srv_qbuf_t: malloc(%d) failed\n",
		prog_name, QBUF_SIZE);
	
	return OOP_CONTINUE;
      }

      qbuf->txt_service_len = i - domain - 1;
      assert(qbuf->txt_service_len < QBUFSZ);
      memcpy(qbuf->txt_service, domain, qbuf->txt_service_len);
      qbuf->txt_service[qbuf->txt_service_len] = '\0';
      
#ifdef SRVSOLVER_DEBUG
      debug_dump_buf(stderr, 
		     "on_stdin_read(): txt_service=%s txt_service_len=%d", 
		     qbuf->txt_service, qbuf->txt_service_len);
#endif

      qbuf->txt_domain_len = past_end - i;
      assert(qbuf->txt_domain_len < QBUFSZ);
      memcpy(qbuf->txt_domain, i, qbuf->txt_domain_len);
      qbuf->txt_domain[qbuf->txt_domain_len] = '\0';

#ifdef SRVSOLVER_DEBUG
      debug_dump_buf(stderr, 
		     "on_stdin_read(): txt_domain=%s txt_domain_len=%d", 
		     qbuf->txt_domain, qbuf->txt_domain_len);
#endif
    }

    /*
     * Encode buffers in qbuf (txt => raw)
     */

    if (encode_srv_qbuf(qbuf)) {
      fprintf(stderr, 
	      "%s: on_stdin_read(): could not encode domain in srv_qbuf_t\n",
	      prog_name);

      free(qbuf);

      return OOP_CONTINUE;
    }
        
    /*
     * Send query
     */
    
    {
      ruli_srv_t *srv_qry = submit_query(res_ctx, qbuf);
      if (!srv_qry) {
	fprintf(stderr, 
		"%s: on_stdin_read(): could not send SRV query\n",
		prog_name);

	free(qbuf);
	
	return OOP_CONTINUE;
      }
    }
    
  } /* for loop */

  assert(0); /* NOT REACHED */

  return OOP_CONTINUE;
}
Exemple #21
0
int db_do_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 (*val2str) ( db_con_t*,
	 db_val_t*, char*, int* _len), int (*submit_query)( db_con_t*,
	 str*), int (*store_result)( db_con_t* _h, db_res_t** _r),str *query_holder)
{

	int off, ret;
	str  sql_str;
	char sql_buf[SQL_BUF_LEN];

	if (!_h || !val2str || (!submit_query && !query_holder) || (_r && !store_result)) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (!_c) {
		ret = snprintf(sql_buf, SQL_BUF_LEN, "select * from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
		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 = db_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)->len, CON_TABLE(_h)->s);
		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 = db_print_where(_h, sql_buf + off,
				SQL_BUF_LEN - off, _k, _op, _v, _n, val2str);
		if (ret < 0) return -1;;
		off += ret;
	}
	if (_o) {
		ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " order by %.*s", _o->len, _o->s);
		if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error;
		off += ret;
	}
	/*
	 * Null-terminate the string for the postgres driver. Its query function
	 * don't support a length parameter, so they need this for the correct
	 * function of strlen. This zero is not included in the 'str' length.
	 * We need to check the length here, otherwise we could overwrite the buffer
	 * boundaries if off is equal to SQL_BUF_LEN.
	 */
	if (off + 1 >= SQL_BUF_LEN) goto error;
	sql_buf[off + 1] = '\0';
	sql_str.s = sql_buf;
	sql_str.len = off;

	if (submit_query)
	{
		if (submit_query(_h, &sql_str) < 0) 
		{
			LM_ERR("error while submitting query\n");
			return -2;
		}
	}
	else
	{
		query_holder->s = pkg_malloc(off);
		if (!query_holder->s)
		{
			LM_ERR("no more pkg mem\n");
			return -2;
		}
		memcpy(query_holder->s,sql_buf,off);
		query_holder->len = off;
	}

	if(_r) {
		int tmp = store_result(_h, _r);
		if (tmp < 0) {
			LM_ERR("error while storing result\n");
			return tmp;
		}
	}
	return 0;

error:
	LM_ERR("error while preparing query\n");
	return -1;
}