示例#1
0
/*Method to execute sql statements like SELECT and return Cursor
Inputs:
query- string containing sql query
qlength - length of query (for binary data). if 0 then assume null terminated.
Output: NULL cursor on error
*/
DBCursor *DBConnection_SQLITE::sqlQuery(char *query, DBString *args, int numargs, int p_rows)
{
	DBCursor_SQLITE *ret = 0;
	unsigned int qlength;
	char *newquery = (char *)query;

#ifndef NDEBUG
	MDEBUG0("SQLite::sqlQuery\n");
	MDEBUG("Numargs=[%d]\n", numargs);
	MDEBUG("Query=[%s]\n", query);
	
	for(int i = 0; i < numargs; i++) {
		MDEBUG("Args[%d]=[%s]\n", i);
	}
#endif
	
	if (!isConnected)
		return NULL;

	//if null terminated (qlength = 0) then calculate length of query
	qlength = strlen(query);
	//execute query and check for error

	if(numargs) {
		int newsize;
		newquery = BindVariables(query, qlength, args, numargs, newsize);
		qlength = newsize;
	}

	try {
		ret = new DBCursor_SQLITE(mDB);
		Dataset *ds = ret->getDataset();

		ds->query(newquery);
		//try to open cursor..on error delete invalid cursor object and exit with error
		if(!ret->open((DBConnection *)this)) {
			delete ret;
			ret = 0;
			mIsError = true;
			setErrorStr("Unable to open query");
		} else
			addCursor(ret); //add to cursor list

	} catch(DbErrors &e) {
		MDEBUG0("\n\n --- CAUGHT ERROR --- \n");
		mIsError = true;
		setErrorStr(e.getMsg());
		delete ret;
		ret = 0;
	}

	if (numargs)
		delete newquery;

	return ret;
}
示例#2
0
/*Method to execute quick and fast sql statements like UPDATE and INSERT
Inputs:
query- string containing sql query
qlength - length of query (for binary data). if 0 then assume null terminated.
affectedrows - will recieve number of rows updated or inserted
Output: False on error
*/
Bool DBConnection_SQLITE::sqlExecute(char *query, DBString *args, int numargs, unsigned int &affectedrows)
{
	MDEBUG0("SQLite::sqlExecute\n");
	Bool ret = True;

	if (!isConnected)
		return ret;
	else
	{
		char *newquery = query;
		int qlength = strlen(query);

		MDEBUG("args=%d, numargs=%d\n", args != 0);

		if(numargs > 0)
		{
			int newsize;
			newquery = BindVariables(query, qlength, args, numargs, newsize);
			qlength = newsize;
		}

		int rv = basicExec(newquery, &affectedrows);
		if(rv != SQLITE_OK)
		{
			// MW-2008-07-29: [[ Bug 6639 ]] Executing a query doesn't return meaningful error messages.
			//   Make sure we only use a generic string if an error hasn't been set.
			if (!mIsError)
			{
				mIsError = true;
				setErrorStr("Unable to execute query");
			}
			ret = False;
		}
		else
			mIsError = false;
	}
	
	return ret;
}
示例#3
0
Cda_Def *DBConnection_ORACLE::ExecuteQuery(char *p_query, DBString *p_arguments, int p_argument_count)
{
	int t_affected_rows;
	t_affected_rows = 0;

	unsigned int t_query_length;
	t_query_length = strlen(p_query);

	Cda_Def *t_cursor;
	t_cursor = new Cda_Def;

	bool t_success;
	t_success = true;

	sword t_error;
	if (t_success)
	{
		t_error = oopen(t_cursor, getLDA(), (text *)0, -1, -1, NULL, -1);
		if (t_error != 0)
			t_success = false;
	}

	char *t_parsed_query;
	t_parsed_query = p_query;

	PlaceholderMap t_placeholder_map;

	if (p_argument_count != 0)
	{
		t_placeholder_map . length = 0;
		t_placeholder_map . elements = new int[p_argument_count + 1];

		DBBuffer t_query_buffer(t_query_length + 1);
		t_success = processQuery(p_query, t_query_buffer, queryCallback, &t_placeholder_map);

		t_query_length = t_query_buffer . getSize();
		t_parsed_query = t_query_buffer . grab();
	}

	if (t_success)
	{
		if (oparse(t_cursor, (text *)t_parsed_query, t_query_length, DEFER_PARSE, PARSE_V7_LNG) != 0)
			t_success = false;
	}

	if (t_success)
	{
		if (!BindVariables(t_cursor, p_arguments, p_argument_count, &t_placeholder_map))
			t_success = false;
	}

	if (t_success)
	{
		t_error = oexec(t_cursor);
		if (t_error != 0)
			t_success = false;
	}

	if (!t_success)
	{
		delete t_cursor;
		t_cursor = NULL;
	}

	return t_cursor;
}