Exemplo n.º 1
0
int SQLiteStatement::prepare()
{
    ASSERT(!m_isPrepared);

    MutexLocker databaseLock(m_database.databaseMutex());
    if (m_database.isInterrupted())
        return SQLITE_INTERRUPT;

    const void* tail = 0;
    LOG(SQLDatabase, "SQL - prepare - %s", m_query.ascii().data());
    String strippedQuery = m_query.stripWhiteSpace();
    const UChar* nullTermed = strippedQuery.deprecatedCharactersWithNullTermination();
    int error = sqlite3_prepare16_v2(m_database.sqlite3Handle(), nullTermed, -1, &m_statement, &tail);

    // Starting with version 3.6.16, sqlite has a patch (http://www.sqlite.org/src/ci/256ec3c6af)
    // that should make sure sqlite3_prepare16_v2 doesn't return a SQLITE_SCHEMA error.
    // If we're using an older sqlite version, try to emulate the patch.
    if (error == SQLITE_SCHEMA) {
      sqlite3_finalize(m_statement);
      error = sqlite3_prepare16_v2(m_database.sqlite3Handle(), m_query.deprecatedCharactersWithNullTermination(), -1, &m_statement, &tail);
    }

    if (error != SQLITE_OK)
        LOG(SQLDatabase, "sqlite3_prepare16 failed (%i)\n%s\n%s", error, m_query.ascii().data(), sqlite3_errmsg(m_database.sqlite3Handle()));
    const UChar* ch = static_cast<const UChar*>(tail);
    if (ch && *ch)
        error = SQLITE_ERROR;
#ifndef NDEBUG
    m_isPrepared = error == SQLITE_OK;
#endif
    return error;
}
Exemplo n.º 2
0
object sqlite3::compile(flusspferd::string sql_in, value bind ) {
    local_root_scope scope;

    size_t n_bytes = sql_in.length() * 2;
    sqlite3_stmt * sth = 0;
    js_char16_t * tail = 0; // uncompiled part of the sql (when multiple stmts)
    
    if (sqlite3_prepare16_v2(db, sql_in.data(), n_bytes, &sth, (const void**)&tail) != SQLITE_OK)
    {
        raise_sqlite_error(db);
    }

    object cursor = create<sqlite3_cursor>(fusion::make_vector(sth));

    string tail_str;
    if (tail) {
        tail_str = string(tail);
    }
    
    string sql = sql_in.substr( 0, sql_in.size() - tail_str.size() );

    cursor.define_property("sql", sql);
    cursor.define_property("tail", tail_str);        

    if ( !bind.is_undefined_or_null() ) {
        cursor.call("bind", bind );
    }
    
    return cursor;
}
Exemplo n.º 3
0
sqlite3_command::sqlite3_command(sqlite3_connection &con, const wchar_t *sql) : con(con),refs(0) {
	const wchar_t *tail=NULL;
	if(sqlite3_prepare16_v2(con.db, sql, -1, &this->stmt, (const void**)&tail)!=SQLITE_OK)
		throw database_error(con);

	this->argc=sqlite3_column_count(this->stmt);
}
static jlong nativePrepareStatement(JNIEnv* env, jclass clazz, jlong connectionPtr,
        jstring sqlString) {
    SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);

    jsize sqlLength = env->GetStringLength(sqlString);
    const jchar* sql = env->GetStringCritical(sqlString, NULL);
    sqlite3_stmt* statement;
    int err = sqlite3_prepare16_v2(connection->db,
            sql, sqlLength * sizeof(jchar), &statement, NULL);
    env->ReleaseStringCritical(sqlString, sql);

    if (err != SQLITE_OK) {
        // Error messages like 'near ")": syntax error' are not
        // always helpful enough, so construct an error string that
        // includes the query itself.
        const char *query = env->GetStringUTFChars(sqlString, NULL);
        char *message = (char*) malloc(strlen(query) + 50);
        if (message) {
            strcpy(message, ", while compiling: "); // less than 50 chars
            strcat(message, query);
        }
        env->ReleaseStringUTFChars(sqlString, query);
        throw_sqlite3_exception(env, connection->db, message);
        free(message);
        return 0;
    }

    ALOGV("Prepared statement %p on connection %p", statement, connection->db);
    return reinterpret_cast<jlong>(statement);
}
Exemplo n.º 5
0
sqlite3_command::sqlite3_command(sqlite3_connection &con, const std::wstring &sql) : con(con),refs(0) {
	const wchar_t *tail=NULL;
	if(sqlite3_prepare16_v2(con.db, sql.data(), (int)sql.length()*2, &this->stmt, (const void**)&tail)!=SQLITE_OK)
		throw database_error(con);

	this->argc=sqlite3_column_count(this->stmt);
}
//This function attempts to execute sqlite3_prepare_v2() or sqlite3_prepare16_v2() in an "out of memory loop".
//If the prepare call fails, the statement handle is expected to be NULL.
void StmtHandleTest(TStmtType aStmtType)
	{
	TEST(TheSqliteDb != NULL);
	for(TInt failingAllocationNo=1;;++failingAllocationNo)
		{
		__UHEAP_SETFAIL(RHeap::EFailNext, failingAllocationNo);
		sqlite3_stmt* stmt = NULL;
		TInt err = SQLITE_NOMEM;
		if(aStmtType == EStmt8)
			{
			_LIT8(KSelectSqlZ,"SELECT * FROM A\x0");
			err = sqlite3_prepare_v2(TheSqliteDb, (const char*)(KSelectSqlZ().Ptr()), -1, &stmt, NULL);
			}
		else
			{
			_LIT(KSelectSqlZ,"SELECT * FROM A\x0");
			err = sqlite3_prepare16_v2(TheSqliteDb, (const char*)(KSelectSqlZ().Ptr()), -1, &stmt, NULL);
			}
		__UHEAP_SETFAIL(RHeap::ENone, 0);
		if(err != SQLITE_OK)
			{//The statement handle should be NULL if err is not SQLITE_OK
			TEST(!stmt);
			}
		else
			{
			TEST(stmt != NULL);
			sqlite3_finalize(stmt);
			break;
			}
		}
	}
Exemplo n.º 7
0
bool SQLiteResult::prepare(const QString &query)
{
    if (!driver() || !driver()->isOpen() || driver()->isOpenError())
        return false;

    d->cleanup();

    setSelect(false);

    const void *pzTail = NULL;

#if (SQLITE_VERSION_NUMBER >= 3003011)
    int res = sqlite3_prepare16_v2(d->access, query.constData(), (query.size() + 1) * sizeof(QChar),
                                   &d->stmt, &pzTail);
#else
    int res = sqlite3_prepare16(d->access, query.constData(), (query.size() + 1) * sizeof(QChar),
                                &d->stmt, &pzTail);
#endif

    if (res != SQLITE_OK) {
#if defined(SQLITEDRIVER_DEBUG)
        trace(NULL, query.toUtf8().constData());
#endif
        setLastError(qMakeError(d->access, QCoreApplication::translate("SQLiteResult",
                     "Unable to execute statement"), QSqlError::StatementError, res));
        d->finalize();
        return false;
    } else if (pzTail && !stringFromUnicode(reinterpret_cast<const QChar *>(pzTail)).trimmed().isEmpty()) {
        setLastError(qMakeError(d->access, QCoreApplication::translate("SQLiteResult",
            "Unable to execute multiple statements at a time"), QSqlError::StatementError, SQLITE_MISUSE));
        d->finalize();
        return false;
    }
    return true;
}
Exemplo n.º 8
0
bool QSpatiaLiteResult::prepare(const QString &query)
{
    if (!driver() || !driver()->isOpen() || driver()->isOpenError())
        return false;

    d->cleanup();

    setSelect(false);

    const void *pzTail = NULL;

#if (SQLITE_VERSION_NUMBER >= 3003011)
    int res = sqlite3_prepare16_v2(d->access, query.constData(), (query.size() + 1) * sizeof(QChar),
                                   &d->stmt, &pzTail);
#else
    int res = sqlite3_prepare16(d->access, query.constData(), (query.size() + 1) * sizeof(QChar),
                                &d->stmt, &pzTail);
#endif

    if (res != SQLITE_OK) {
        setLastError(qMakeError(d->access, QCoreApplication::translate("QSpatiaLiteResult",
                     "Unable to execute statement"), QSqlError::StatementError, res));
        d->finalize();
        return false;
    } else if (pzTail && !QString::fromUtf16( (const ushort *) pzTail ).trimmed().isEmpty()) {
        setLastError(qMakeError(d->access, QCoreApplication::translate("QSpatiaLiteResult",
            "Unable to execute multiple statements at a time"), QSqlError::StatementError, SQLITE_MISUSE));
        d->finalize();
        return false;
    }
    return true;
}
Exemplo n.º 9
0
/*
* Class:     com_sabo_sqlite_SQLiteDatabase
* Method:    nativeExecute
* Signature: (JLjava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_sabo_sqlite_SQLiteDatabase_nativeExecute
(JNIEnv *env, jobject clazz, jlong connectionPtr, jstring sql) {
	const jchar *zSql;
	int nBytes;
	int rc;
	sqlite3 *pDb;
	sqlite3_stmt *pStmt;

	assert(sql != NULL && connectionPtr != 0);
	zSql = env->GetStringCritical(sql, NULL);
	nBytes = env->GetStringLength(sql) * 2;
	assert(zSql != NULL);
	pDb = (sqlite3 *)connectionPtr;
	rc = sqlite3_prepare16_v2(pDb, zSql, nBytes, &pStmt, NULL);
	env->ReleaseStringCritical(sql, zSql);
	if (rc != SQLITE_OK) {
		sqlite3_finalize(pStmt);
		return (jint)rc;
	}
	rc = sqlite3_step(pStmt);
	if (rc == SQLITE_DONE) rc = SQLITE_OK;
	sqlite3_finalize(pStmt);
	
	return (jint)rc;	
}
Exemplo n.º 10
0
bool
O2DatDB::
select(O2DatRec &out, const wchar_t *domain, const wchar_t *bbsname, const wchar_t *datname)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select by domain bbsname datname");
#endif

	bool ret = true;
	sqlite3 *db = NULL;
	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql =
		L"select"
		COLUMNS
		L" from dat"
		L" where domain = ?"
		L"   and bbsname = ?"
		L"   and datname = ?;";

	sqlite3_stmt *stmt = NULL;
	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	if (!bind(db, stmt, 1, domain))
		goto error;
	if (!bind(db, stmt, 2, bbsname))
		goto error;
	if (!bind(db, stmt, 3, datname))
		goto error;

	err = sqlite3_step(stmt);
	if (err != SQLITE_ROW && err != SQLITE_DONE)
		goto error;

	if (err == SQLITE_DONE)
		ret = false;
	if (err == SQLITE_ROW)
		get_columns(stmt, out);

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;

	return (ret);

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return false;
}
Exemplo n.º 11
0
SQLiteSTMT::SQLiteSTMT(sqlite3* db, const void* zSql, int nByte, const void** pzTail) {
  this->db = db;
  handle = 0;
  int ret;
  if ((ret = sqlite3_prepare16_v2(db, zSql, nByte, &handle, pzTail)) != SQLITE_OK || !handle) {
	Close();
	throw ret;
  }
}
Exemplo n.º 12
0
size_t
O2DatDB::
select(const wchar_t *sql, SQLResultList &out)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select");
#endif
	wstrarray cols;

	sqlite3 *db = NULL;
	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	sqlite3_stmt *stmt = NULL;
	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_reset(stmt);

	err = sqlite3_step(stmt);
	if (err != SQLITE_ROW && err != SQLITE_DONE)
		goto error;

	if (out.empty()) {
		//一行目
		cols.clear();
		get_column_names(stmt, cols);
		out.push_back(cols);
		cols.clear();
		get_columns(stmt, cols);
		out.push_back(cols);
	}
	while (sqlite3_step(stmt) == SQLITE_ROW) {
		//2行目以降
		cols.clear();
		get_columns(stmt, cols);
		out.push_back(cols);
	}

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;

	return (out.size());

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return (0);
}
Exemplo n.º 13
0
/**
	request : 'db -> sql:string -> 'result
	<doc>Executes the SQL request and returns its result</doc>
**/
HL_PRIM sqlite_result *HL_NAME(request)(sqlite_database *db, vbyte *sql ) {
	sqlite_result *r;
	const char *tl;
	int i,j;

	r = (sqlite_result*)hl_gc_alloc_finalizer(sizeof(sqlite_result));
	r->finalize = HL_NAME(finalize_result);
	r->db = db;
	
	if( sqlite3_prepare16_v2(db->db, sql, -1, &r->r, &tl) != SQLITE_OK ) {
		HL_NAME(error)(db->db, false);
	}

	if( *tl ) {
		sqlite3_finalize(r->r);
		hl_error("SQLite error: Cannot execute several SQL requests at the same time");
	}

	r->ncols = sqlite3_column_count(r->r);
	r->names = (int*)hl_gc_alloc(sizeof(int)*r->ncols);
	r->bools = (int*)hl_gc_alloc(sizeof(int)*r->ncols);
	r->first = 1;
	r->done = 0;
	for(i=0;i<r->ncols;i++) {
		int id = hl_hash_gen((uchar*)sqlite3_column_name16(r->r,i), true);
		const char *dtype = sqlite3_column_decltype(r->r,i);
		for(j=0;j<i;j++)
			if( r->names[j] == id ) {
				if( strcmp(sqlite3_column_name16(r->r,i), sqlite3_column_name16(r->r,j)) == 0 ) {
					sqlite3_finalize(r->r);
					hl_buffer *b = hl_alloc_buffer();
					hl_buffer_str(b, USTR("SQLite error: Same field is two times in the request: "));
					hl_buffer_str(b, (uchar*)sql);

					hl_error_msg(hl_buffer_content(b, NULL));
				} else {
					hl_buffer *b = hl_alloc_buffer();
					hl_buffer_str(b, USTR("SQLite error: Same field ids for: "));
					hl_buffer_str(b, sqlite3_column_name16(r->r,i));
					hl_buffer_str(b, USTR(" and "));
					hl_buffer_str(b, sqlite3_column_name16(r->r,j));
					
					sqlite3_finalize(r->r);
					hl_error_msg(hl_buffer_content(b, NULL));
				}
			}
		r->names[i] = id;
		r->bools[i] = dtype?(strcmp(dtype,"BOOL") == 0):0;
	}
	// changes in an update/delete
	if( db->last != NULL )
		HL_NAME(finalize_request)(db->last, false);
	
	db->last = r;
	return db->last;
}
Exemplo n.º 14
0
bool
O2DatDB::
select(O2DatRecList &out, time_t publish_tt, size_t limit)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select lastpublish");
#endif

	sqlite3 *db = NULL;
	sqlite3_stmt *stmt = NULL;
	O2DatRec rec;

	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql =
		L"select"
		COLUMNS
		L" from dat"
		L" where lastpublish < ?"
		L" order by lastpublish"
		L" limit ?;";

	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	if (!bind(db, stmt, 1, time(NULL)-publish_tt))
		goto error;
	if (!bind(db, stmt, 2, limit))
		goto error;

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		get_columns(stmt, rec);
		out.push_back(rec);
	}

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;

	return true;

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return false;
}
Exemplo n.º 15
0
static JSVAL sqlite_prepare16_v2(JSARGS args) {
    HandleScope scope;
    Local<External>wrap = Local<External>::Cast(args[0]);
    sqlite3 *db = (sqlite3 *)wrap->Value();
    String::Utf8Value zSql(args[1]->ToString());
    int nByte = args[2]->IntegerValue()	;
    sqlite3_stmt *stmt;
    if (!sqlite3_prepare16_v2(db, *zSql, nByte, &stmt, NULL)) {
        return scope.Close(String::New(sqlite3_errmsg(db)));
    }
    return scope.Close(External::New(stmt));
}
Exemplo n.º 16
0
bool
O2DatDB::
select(O2DatRecList &out, const wchar_t *domain, const wchar_t *bbsname)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select domain bbsname order by datname");
#endif

	sqlite3 *db = NULL;
	sqlite3_stmt *stmt = NULL;
	O2DatRec rec;

	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql =
		L"select"
		COLUMNS
		L" from dat"
		L" where domain = ?"
		L"   and bbsname = ?"
		L" order by datname;";

	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	if (!bind(db, stmt, 1, domain))
		goto error;
	if (!bind(db, stmt, 2, bbsname))
		goto error;

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		get_columns(stmt, rec);
		out.push_back(rec);
	}

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;
	return true;

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return false;
}
Exemplo n.º 17
0
BOOL SQLiteCommand::SetCommandText(LPCTSTR lpSql)
{
#ifdef  UNICODE 
	if (sqlite3_prepare16_v2(m_pSqlite->m_db, lpSql, -1, &m_pStmt, NULL) != SQLITE_OK)
#else
	if (sqlite3_prepare_v2(m_pSqlite->m_db, lpSql, -1, &m_pStmt, NULL) != SQLITE_OK)
#endif
	{
		return FALSE;
	}
	return TRUE;
}
Exemplo n.º 18
0
int SQLiteStatement::prepare()
{
    ASSERT(!m_isPrepared);
    const void* tail;
    LOG(SQLDatabase, "SQL - prepare - %s", m_query.ascii().data());
    int error = sqlite3_prepare16_v2(m_database.sqlite3Handle(), m_query.charactersWithNullTermination(), -1, &m_statement, &tail);
    if (error != SQLITE_OK)
        LOG(SQLDatabase, "sqlite3_prepare16 failed (%i)\n%s\n%s", error, m_query.ascii().data(), sqlite3_errmsg(m_database.sqlite3Handle()));
#ifndef NDEBUG
    m_isPrepared = error == SQLITE_OK;
#endif
    return error;
}
Exemplo n.º 19
0
bool
O2DatDB::
select(O2DatRec &out)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select random 1");
#endif

	bool ret = true;
	sqlite3 *db = NULL;
	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql =
		L"select"
		COLUMNS
		L" from dat"
		L" order by random() limit 1;";

	sqlite3_stmt *stmt = NULL;
	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_reset(stmt);

	err = sqlite3_step(stmt);
	if (err != SQLITE_ROW && err != SQLITE_DONE)
		goto error;

	if (err == SQLITE_DONE)
		ret = false;
	if (err == SQLITE_ROW)
		get_columns(stmt, out);

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;

	return (ret);

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return false;
}
Exemplo n.º 20
0
uint64
O2DatDB::
select_datcount(wstrnummap &out)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select datcount group by domain bbsname");
#endif

	sqlite3 *db = NULL;
	sqlite3_stmt *stmt = NULL;
	wstring domain_bbsname;
	uint64 total = 0;
	uint64 num;

	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql =
		L"select domain, bbsname, count(*) from dat group by domain, bbsname;";

	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		domain_bbsname = (wchar_t*)sqlite3_column_text16(stmt, 0);
		domain_bbsname += L":";
		domain_bbsname += (wchar_t*)sqlite3_column_text16(stmt, 1);
		num = sqlite3_column_int64(stmt, 2);

		out.insert(wstrnummap::value_type(domain_bbsname, num));
		total += num;
	}

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;
	return (total);

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return false;
}
/* public native void native_execSQL(String sql); */
static void native_execSQL(JNIEnv* env, jobject object, jstring sqlString)
{
    int err;
    int stepErr;
    sqlite3_stmt * statement = NULL;
    sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
    jchar const * sql = env->GetStringChars(sqlString, NULL);
    jsize sqlLen = env->GetStringLength(sqlString);

    if (sql == NULL || sqlLen == 0) {
        jniThrowException(env, "java/lang/IllegalArgumentException", "You must supply an SQL string");
        return;
    }

    err = sqlite3_prepare16_v2(handle, sql, sqlLen * 2, &statement, NULL);

    env->ReleaseStringChars(sqlString, sql);

    if (err != SQLITE_OK) {
        char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
        LOGE("Failure %d (%s) on %p when preparing '%s'.\n", err, sqlite3_errmsg(handle), handle, sql8);
        throw_sqlite3_exception(env, handle, sql8);
        env->ReleaseStringUTFChars(sqlString, sql8);
        return;
    }

    stepErr = sqlite3_step(statement);
    err = sqlite3_finalize(statement);

    if (stepErr != SQLITE_DONE) {
        if (stepErr == SQLITE_ROW) {
            throw_sqlite3_exception(env, "Queries cannot be performed using execSQL(), use query() instead.");
        } else {
            char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
            LOGE("Failure %d (%s) on %p when executing '%s'\n", err, sqlite3_errmsg(handle), handle, sql8);
            throw_sqlite3_exception(env, handle, sql8);
            env->ReleaseStringUTFChars(sqlString, sql8);

        }
    } else
#ifndef DB_LOG_STATEMENTS
    IF_LOGV()
#endif
    {
        char const * sql8 = env->GetStringUTFChars(sqlString, NULL);
        LOGV("Success on %p when executing '%s'\n", handle, sql8);
        env->ReleaseStringUTFChars(sqlString, sql8);
    }
}
Exemplo n.º 22
0
uint64
O2DatDB::
select_publishcount(time_t publish_tt)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select datcount by lastpublish");
#endif

	sqlite3 *db = NULL;
	sqlite3_stmt *stmt = NULL;

	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql = L"select count(*) from dat where lastpublish > ?;";

	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	if (!bind(db, stmt, 1, time(NULL)-publish_tt))
		goto error;

	err = sqlite3_step(stmt);
	if (err != SQLITE_ROW && err != SQLITE_DONE)
		goto error;

	uint64 count = sqlite3_column_int64(stmt,0);

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;

	return (count);

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return (0);
}
Exemplo n.º 23
0
// 查询
SQLiteDataReader SQLite::ExcuteQuery(LPCTSTR lpSql)
{
	if (lpSql == NULL)
	{
		return FALSE;
	}
	sqlite3_stmt* stmt;
#ifdef  UNICODE 
	if (sqlite3_prepare16_v2(m_db, lpSql, -1, &stmt, NULL) != SQLITE_OK)
#else
	if (sqlite3_prepare_v2(m_db, lpSql, -1, &stmt, NULL) != SQLITE_OK)
#endif
	{
		return FALSE;
	}
	return SQLiteDataReader(stmt);
}
Exemplo n.º 24
0
bool
O2DatDB::
select(O2DatRecList &out)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select all");
#endif

	sqlite3 *db = NULL;
	sqlite3_stmt *stmt = NULL;
	O2DatRec rec;

	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql =
		L"select"
		COLUMNS
		L" from dat;";

	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		get_columns(stmt, rec);
		out.push_back(rec);
	}

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;
	return true;

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return false;
}
Exemplo n.º 25
0
bool
O2DatDB::
remove(const hashT &hash)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("remove");
#endif

	sqlite3 *db = NULL;
	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql =
		L"delete from dat where hash = ?;";

	sqlite3_stmt *stmt = NULL;
	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	if (!bind(db, stmt, 1, hash))
		goto error;

	err = sqlite3_step(stmt);
	if (err != SQLITE_ROW && err != SQLITE_DONE)
		goto error;

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;

	return true;

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return false;
}
Exemplo n.º 26
0
bool
O2DatDB::
update_lastpublish(const hashT &hash)
{
	sqlite3 *db = NULL;
	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql =
		L"update or replace dat"
		L"   set lastpublish = ?"
		L" where hash = ?;";

	sqlite3_stmt *stmt = NULL;
	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	if (!bind(db, stmt, 1, time(NULL)))
		goto error;
	if (!bind(db, stmt, 2, hash))
		goto error;

	err = sqlite3_step(stmt);
	if (err != SQLITE_ROW && err != SQLITE_DONE)
		goto error;

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;

	return true;

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return false;
}
Exemplo n.º 27
0
BOOL SQLite::ExcuteNonQuery(LPCTSTR lpSql)
{
	if (lpSql == NULL)
	{
		return FALSE;
	}
	sqlite3_stmt* stmt;
#ifdef  UNICODE 
	if (sqlite3_prepare16_v2(m_db, lpSql, -1, &stmt, NULL) != SQLITE_OK)
#else
	if (sqlite3_prepare_v2(m_db, lpSql, -1, &stmt, NULL) != SQLITE_OK)
#endif
	{
		return FALSE;
	}
	sqlite3_step(stmt);

	return (sqlite3_finalize(stmt) == SQLITE_OK) ? TRUE : FALSE;
}
Exemplo n.º 28
0
uint64
O2DatDB::
select_totaldisksize(void)
{
#if TRACE_SQL_EXEC_TIME
	stopwatch sw("select totakdisksize");
#endif

	sqlite3 *db = NULL;
	sqlite3_stmt *stmt = NULL;

	int err = sqlite3_open16(dbfilename.c_str(), &db);
	if (err != SQLITE_OK)
		goto error;
	sqlite3_busy_timeout(db, 5000);

	wchar_t *sql = L"select sum(disksize) from dat;";

	err = sqlite3_prepare16_v2(db, sql, wcslen(sql)*2, &stmt, NULL);
	if (err != SQLITE_OK)
		goto error;

	err = sqlite3_step(stmt);
	if (err != SQLITE_ROW && err != SQLITE_DONE)
		goto error;

	uint64 totalsize = sqlite3_column_int64(stmt,0);

	sqlite3_finalize(stmt);
	stmt = NULL;

	err = sqlite3_close(db);
	if (err != SQLITE_OK)
		goto error;

	return (totalsize);

error:
	log(db);
	if (stmt) sqlite3_finalize(stmt);
	if (db) sqlite3_close(db);
	return (0);
}
Exemplo n.º 29
0
/*
* Class:     com_sabo_sqlite_SQLiteDatabase_SQLiteQuery
* Method:    nativeStatement
* Signature: (JLjava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_com_sabo_sqlite_SQLiteDatabase_00024SQLiteQuery_nativeStatement
(JNIEnv *env, jobject clazz, jlong connectionPtr, jstring sql) {
	const jchar *zSql;
	int rc;
	int nBytes;
	sqlite3 *pDb;
	sqlite3_stmt *pStmt;

	assert(connectionPtr != 0);
	assert(sql != NULL);
	pDb = (sqlite3 *)connectionPtr;
	zSql = env->GetStringCritical(sql, NULL);
	nBytes = env->GetStringLength(sql) * 2;
	assert(zSql != NULL);
	rc = sqlite3_prepare16_v2(pDb, zSql, nBytes, &pStmt, NULL);
	env->ReleaseStringCritical(sql, zSql);
	if (rc != SQLITE_OK) return (jlong)0;

	return (jlong)pStmt;
}
sqlite3_stmt * compile(JNIEnv* env, jobject object,
                       sqlite3 * handle, jstring sqlString)
{
    int err;
    jchar const * sql;
    jsize sqlLen;
    sqlite3_stmt * statement = GET_STATEMENT(env, object);

    // Make sure not to leak the statement if it already exists
    if (statement != NULL) {
        sqlite3_finalize(statement);
        env->SetIntField(object, gStatementField, 0);
    }

    // Compile the SQL
    sql = env->GetStringChars(sqlString, NULL);
    sqlLen = env->GetStringLength(sqlString);
    err = sqlite3_prepare16_v2(handle, sql, sqlLen * 2, &statement, NULL);
    env->ReleaseStringChars(sqlString, sql);

    if (err == SQLITE_OK) {
        // Store the statement in the Java object for future calls
        LOGV("Prepared statement %p on %p", statement, handle);
        env->SetIntField(object, gStatementField, (int)statement);
        return statement;
    } else {
        // Error messages like 'near ")": syntax error' are not
        // always helpful enough, so construct an error string that
        // includes the query itself.
        const char *query = env->GetStringUTFChars(sqlString, NULL);
        char *message = (char*) malloc(strlen(query) + 50);
        if (message) {
            strcpy(message, ", while compiling: "); // less than 50 chars
            strcat(message, query);
        }
        env->ReleaseStringUTFChars(sqlString, query);
        throw_sqlite3_exception(env, handle, message);
        free(message);
        return NULL;
    }
}