/*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;
}
bool
FileLogHandler::checkParams() {
  if(m_pLogFile == NULL)
  {
    setErrorStr("Log file cannot be null.");
    return false;
  }
  return true;
}
bool
FileLogHandler::setParam(const BaseString &param, const BaseString &value){
  if(param == "filename")
    return setFilename(value);
  if(param == "maxsize")
    return setMaxSize(value);
  if(param == "maxfiles")
    return setMaxFiles(value);
  setErrorStr("Invalid parameter");
  return false;
}
bool
FileLogHandler::setMaxFiles(const BaseString &files) {
  char *end;
  long val = strtol(files.c_str(), &end, 0);
  if(files.c_str() == end || val < 1)
  {
    setErrorStr("Invalid maximum number of files");
    return false;
  }
  m_maxNoFiles = val;

  return true;
}
bool
FileLogHandler::setMaxSize(const BaseString &size) {
  char *end;
  long val = strtol(size.c_str(), &end, 0); /* XXX */
  if(size.c_str() == end || val < 0)
  {
    setErrorStr("Invalid file size");
    return false;
  }
  if(end[0] == 'M')
    val *= 1024*1024;
  if(end[0] == 'k')
    val *= 1024;

  m_maxFileSize = val;

  return true;
}
int DBConnection_SQLITE::basicExec(const char *q, unsigned int *rows)
{
	char *err;
	int t_return_value = 0;

	if (rows == NULL)
		t_return_value = sqlite3_exec(mDB.getHandle(), q, 0, 0, &err);
	else 
	{
		int t_changed_row_count;
		t_changed_row_count = 0;
		sqlite3_update_hook(mDB.getHandle(),dataChangeCallback, &t_changed_row_count);

		*rows = 0;
		t_return_value = sqlite3_exec(mDB.getHandle(), q, exec_callback, rows, &err);

		int t_changed_rows;
		t_changed_rows = sqlite3_changes(mDB.getHandle());

		sqlite3_update_hook(mDB.getHandle(), NULL, NULL);
		
		// OK-2007-07-13: NOTE: When executing a delete query with no WHERE clause, SQLite deletes
		// and recreates the table, meaning that the row count will end up being 0 when it shouldnt be.

		// If *rows != 0 then rows was populated by the exec_callback function, which implies that the query
		// has returned a result set. As we are only executing the query here, we return 0 as the number of affected rows.
		if (*rows != 0)
			*rows = 0;
		else
			*rows = t_changed_row_count;
	}

	if (t_return_value != SQLITE_OK) 
	{
		mIsError = true;
		setErrorStr(err);
		sqlite3_free(err);
	}

	return t_return_value;
}
/*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;
}
Bool DBConnection_SQLITE::connect(char **args, int numargs)
{
	Bool ret = False;


	if(!isConnected && numargs > 1) {
		char *fname;
		string bhash;
		string mash;

		// MW-2008-07-29: [[ Bug 6429 ]] Can't connect to databases with accents in the path.
		//   This occurs because the SQLite functions expect UTF-8 and we are currently passing
		//   then native encoded strings. The os_path_to_native_utf8 function returns a native
		//   path encoded as UTF-8.
		fname = os_path_to_native_utf8(args[0]);
		
		try
		{
			mDB.setDatabase(fname);
			if(mDB.connect() == DB_CONNECTION_NONE) 
			{
				ret = False;
			}
			else
			{
				ret = True;
				isConnected = True;
			}
		}
		catch(DbErrors &e)
		{
			mIsError = true;
			setErrorStr(e.getMsg());
		}
		free(fname);
	}
	return ret;
}
void DBConnection_SQLITE::getTables(char *buffer, int *bufsize)
{
	int rowseplen = 1;
	char rowsep[] = "\n";
	char **result;
	int rows;
	int rv;
	char *errmsg;
	int maxlen = *bufsize;
	int usedlen = 0;
	char *bufptr = buffer;
	
	// OK-2010-02-18: [[Bug 8620]] - Calculate the buffer size properly...
	if (buffer)
		memset(buffer,0,*bufsize);

	rv = sqlite3_get_table(mDB.getHandle(),
		"SELECT name FROM sqlite_master "
		"WHERE type IN ('table','view') "
		"UNION ALL "
		"SELECT name FROM sqlite_temp_master "
		"WHERE type IN ('table','view') "
		"ORDER BY 1",
		&result, &rows, 0, &errmsg);

	if (errmsg)
	{
		mIsError = true;
		setErrorStr(errmsg);
		sqlite3_free(errmsg);
	}

	
	if (rv == SQLITE_OK)
	{
		for(int i = 1; i <= rows; i++)
		{
			if (result[i] == 0) 
				i = rows + 1; // break
			else
			{
				int l = strlen(result[i]);
				if (buffer && ((l + usedlen + rowseplen + 1) > maxlen))
					i = rows + 1; // break
				else
				{
					if (!buffer)
					{
						usedlen = usedlen + l + rowseplen;
					}
					else
					{
						memcpy(bufptr, result[i], l);
						bufptr += l;
						usedlen += l;

						memcpy(bufptr, rowsep, rowseplen);
						bufptr += rowseplen;
						usedlen += rowseplen;
					}
				}
			}
		}

		// OK-2010-02-18: [[Bug 8621]] - Replace trailing return with null.
		if ((rows != 0) && buffer && (usedlen < maxlen))
			*(bufptr - 1) = '\0';
	}
	sqlite3_free_table(result);
	*bufsize = usedlen + 1;
}
Beispiel #10
0
DBConnection_SQLITE::~DBConnection_SQLITE()
{
	setErrorStr(0);
	disconnect();
}