Ejemplo n.º 1
0
VALUE do_sqlite3_cCommand_execute_non_query(int argc, VALUE *argv, VALUE self) {
  VALUE query = data_objects_build_query_from_args(self, argc, argv);
  VALUE connection = rb_iv_get(self, "@connection");
  VALUE sqlite3_connection = rb_iv_get(connection, "@connection");

  if (sqlite3_connection == Qnil) {
    rb_raise(eConnectionError, "This connection has already been closed.");
  }

  sqlite3 *db = NULL;

  Data_Get_Struct(sqlite3_connection, sqlite3, db);

  struct timeval start;
  char *error_message;
  int status;

  gettimeofday(&start, NULL);
  status = sqlite3_exec(db, rb_str_ptr_readonly(query), 0, 0, &error_message);

  if (status != SQLITE_OK) {
    do_sqlite3_raise_error(self, db, query);
  }

  data_objects_debug(connection, query, &start);

  int affected_rows = sqlite3_changes(db);
  do_int64 insert_id = sqlite3_last_insert_rowid(db);

  return rb_funcall(cSqlite3Result, ID_NEW, 3, self, INT2NUM(affected_rows), INT2NUM(insert_id));
}
Ejemplo n.º 2
0
bool PDOSqliteStatement::executer() {
  if (executed && !m_done) {
    sqlite3_reset(m_stmt);
  }

  m_done = 0;
  switch (sqlite3_step(m_stmt)) {
  case SQLITE_ROW:
    m_pre_fetched = 1;
    column_count = sqlite3_data_count(m_stmt);
    return true;

  case SQLITE_DONE:
    column_count = sqlite3_column_count(m_stmt);
    row_count = sqlite3_changes(m_db);
    sqlite3_reset(m_stmt);
    m_done = 1;
    return true;

  case SQLITE_ERROR:
    sqlite3_reset(m_stmt);
  case SQLITE_MISUSE:
  case SQLITE_BUSY:
  default:
    handleError(__FILE__, __LINE__);
    return false;
  }
}
Ejemplo n.º 3
0
int CppSQLite3Statement::execDML()
{
	checkDB();
	checkVM();

	const char* szError=0;

	int nRet = sqlite3_step(mpVM);

	if (nRet == SQLITE_DONE)
	{
		int nRowsChanged = sqlite3_changes(mpDB);

		nRet = sqlite3_reset(mpVM);

		if (nRet != SQLITE_OK)
		{
			szError = sqlite3_errmsg(mpDB);
			throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
		}

		return nRowsChanged;
	}
	else
	{
		nRet = sqlite3_reset(mpVM);
		szError = sqlite3_errmsg(mpDB);
		throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG);
	}
}
Ejemplo n.º 4
0
static int pdo_sqlite_stmt_execute(pdo_stmt_t *stmt)
{
	pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;

	if (stmt->executed && !S->done) {
		sqlite3_reset(S->stmt);
	}

	S->done = 0;
	switch (sqlite3_step(S->stmt)) {
		case SQLITE_ROW:
			S->pre_fetched = 1;
			stmt->column_count = sqlite3_data_count(S->stmt);
			return 1;

		case SQLITE_DONE:
			stmt->column_count = sqlite3_column_count(S->stmt);
			stmt->row_count = sqlite3_changes(S->H->db);
			sqlite3_reset(S->stmt);
			S->done = 1;
			return 1;

		case SQLITE_ERROR:
			sqlite3_reset(S->stmt);
		case SQLITE_MISUSE:
		case SQLITE_BUSY:
		default:
			pdo_sqlite_error_stmt(stmt);
			return 0;
	}
}
Ejemplo n.º 5
0
// Execute a one-step query with no expected result
int Statement::exec()
{
    if (false == mbDone)
    {
        const int ret = sqlite3_step(mStmtPtr);
        if (SQLITE_DONE == ret) // the statement has finished executing successfully
        {
            mbOk = false;
            mbDone = true;
        }
        else if (SQLITE_ROW == ret)
        {
            mbOk = false;
            mbDone = false;
            throw SQLite::Exception("exec() does not expect results. Use executeStep.");
        }
        else
        {
            mbOk = false;
            mbDone = false;
            throw SQLite::Exception(mStmtPtr, ret);
        }
    }
    else
    {
        throw SQLite::Exception("Statement need to be reseted.");
    }

    // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE)
    return sqlite3_changes(mStmtPtr);
}
Ejemplo n.º 6
0
static int sql_execute(struct sql_data* sql, int (*callback)(void* ptr, int argc, char **argv, char **colName), void* ptr, const char* sql_fmt, ...)
{
	va_list args;
	char query[1024];
	char* errMsg;
	int rc;

	va_start(args, sql_fmt);
	vsnprintf(query, sizeof(query), sql_fmt, args);

#ifdef DEBUG_SQL
	printf("SQL: %s\n", query);
#endif

	rc = sqlite3_exec(sql->db, query, callback, ptr, &errMsg);
	if (rc != SQLITE_OK)
	{
#ifdef DEBUG_SQL
		fprintf(stderr, "ERROR: %s\n", errMsg);
#endif
		sqlite3_free(errMsg);
		return -rc;
	}

	rc = sqlite3_changes(sql->db);
	return rc;
}
Ejemplo n.º 7
0
static int
es_db_changes(duk_context *ctx)
{
  es_sqlite_t *es = es_resource_get(ctx, 0, &es_resource_sqlite);
  duk_push_int(ctx, sqlite3_changes(es->es_db));
  return 1;
}
Ejemplo n.º 8
0
void TransactionStream::AddTransaction(std::uint64_t account, const char * date_s, const char * amount_s) {
	long long amount = std::strtoll(amount_s, NULL, 10);
	if (amount == 0) return; //ignore
	char date[11];
	reformat_date(date_s, date);
	check_date(date);
	sqlite3_bind_int64(sql_update, 1, amount);
	sqlite3_bind_text(sql_update, 2, date, 11, SQLITE_STATIC);
	sqlite3_bind_int64(sql_update, 3, account);
	if (sqlite3_step(sql_update) != SQLITE_DONE) {
		throw sql_error(sqlite3_errmsg(connection));
	}
	if (sqlite3_changes(connection) == 0) {
		sqlite3_bind_int64(sql_insert, 1, account);
		sqlite3_bind_int64(sql_insert, 2, amount);
		sqlite3_bind_text(sql_insert, 3, date, 11, SQLITE_STATIC);
		if (sqlite3_step(sql_insert) != SQLITE_DONE) {
			throw sql_error(sqlite3_errmsg(connection));
		}
		sqlite3_clear_bindings(sql_insert);
		sqlite3_reset(sql_insert);
	}
	sqlite3_clear_bindings(sql_update);
	sqlite3_reset(sql_update);
}
Ejemplo n.º 9
0
/**
 * @param [in] reg        the registry to delete the metadata from
 * @param [in] key        the metadata key to delete
 * @param [out] errPtr    on error, a description of the error that occurred
 * @return                true if success; false if failure
 */
int reg_del_metadata(reg_registry* reg, const char* key, reg_error* errPtr) {
    int result = 1;
    sqlite3_stmt* stmt = NULL;
    char* query = "DELETE FROM registry.metadata WHERE key=?";
    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
            && (sqlite3_bind_text(stmt, 1, key, -1, SQLITE_STATIC) == SQLITE_OK)) {
        int r;
        do {
            r = sqlite3_step(stmt);
            switch (r) {
                case SQLITE_DONE:
                    if (sqlite3_changes(reg->db) == 0) {
                        reg_throw(errPtr, REG_INVALID, "no such metadata key");
                        result = 0;
                    } else {
                        sqlite3_reset(stmt);
                    }
                    break;
                case SQLITE_BUSY:
                    break;
                default:
                    reg_sqlite_error(reg->db, errPtr, query);
                    result = 0;
                    break;
            }
        } while (r == SQLITE_BUSY);
    } else {
        reg_sqlite_error(reg->db, errPtr, query);
        result = 0;
    }
    if (stmt) {
        sqlite3_finalize(stmt);
    }
    return result;
}
Ejemplo n.º 10
0
GError*
__destroy_container(struct sqlx_sqlite3_s *sq3, struct hc_url_s *url,
                    gboolean force, gboolean *done)
{
    GError *err = NULL;
    gint count_actions = 0;
    struct sqlx_repctx_s *repctx = NULL;

    EXTRA_ASSERT(sq3 != NULL);
    EXTRA_ASSERT(sq3->db != NULL);

    err = sqlx_transaction_begin(sq3, &repctx);
    if (NULL != err)
        return err;

    if (force) {
        __exec_cid (sq3->db, "DELETE FROM services WHERE cid = ?", hc_url_get_id (url));
        count_actions += sqlite3_changes(sq3->db);
        __exec_cid (sq3->db, "DELETE FROM properties WHERE cid = ?", hc_url_get_id (url));
        count_actions += sqlite3_changes(sq3->db);
    } else {
        guint count_services = 0, count_properties = 0;

        /* No forced op, we count the services belonging to the container. */
        err = __count_FK(sq3, url, "services", &count_services);
        if (!err)
            err = __count_FK(sq3, url, "properties", &count_properties);

        /* If any service is found, this is an error. */
        if (!err && count_services > 0)
            err = NEWERROR(CODE_USER_INUSE, "User still linked to services");
        if (!err && count_properties > 0)
            err = NEWERROR(CODE_USER_INUSE, "User still has properties");
    }

    if (!err) {
        __exec_cid(sq3->db, "DELETE FROM users WHERE cid = ?", hc_url_get_id (url));
        count_actions += sqlite3_changes(sq3->db);
    }

    *done = !err && (count_actions > 0);

    if (!err && !*done)
        err = NEWERROR(CODE_USER_NOTFOUND, "User not found");

    return sqlx_transaction_end(repctx, err);
}
Ejemplo n.º 11
0
// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...).
int Database::exec(const char* apQueries)
{
    const int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL);
    check(ret);

    // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE only)
    return sqlite3_changes(mpSQLite);
}
Ejemplo n.º 12
0
void object::test<1>()
{
    ensure( "open", se.is_open() );
    ensure( "no active txn", !se.active_txn() );
    ensure( "impl", se.impl() != 0 );

    sqlite3_changes(se.impl()); // test native function call
}
Ejemplo n.º 13
0
/* call-seq: changes
 *
 * Returns the number of changes made to this database instance by the last
 * operation performed. Note that a "delete from table" without a where
 * clause will not affect this value.
 */
static VALUE changes(VALUE self)
{
  sqlite3RubyPtr ctx;
  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  return INT2NUM(sqlite3_changes(ctx->db));
}
static jint nativeExecuteForChangedRowCount(JNIEnv* env, jclass clazz,
        jlong connectionPtr, jlong statementPtr) {
    SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);
    sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);

    int err = executeNonQuery(env, connection, statement);
    return err == SQLITE_DONE ? sqlite3_changes(connection->db) : -1;
}
Ejemplo n.º 15
0
SQLiteCountProxy::operator int() const
{
    // If a separate thread makes changes on the same database connection
    // while sqlite3_changes() is running then the value returned is
    // unpredictable and not meaningful.

    return sqlite3_changes(_db);
}
Ejemplo n.º 16
0
/*
** Implementation of the changes() SQL function.  The return value is the
** same as the sqlite3_changes() API function.
*/
static void changes(
  sqlite3_context *context,
  int arg,
  sqlite3_value **argv
){
  sqlite3 *db = sqlite3_user_data(context);
  sqlite3_result_int(context, sqlite3_changes(db));
}
Ejemplo n.º 17
0
/*
 * Nr of changes
 */
static ERL_NIF_TERM
do_changes(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
{
    int changes = sqlite3_changes(conn->db);

    ERL_NIF_TERM changes_term = enif_make_int64(env, changes);
    return make_ok_tuple(env, changes_term);
}
Ejemplo n.º 18
0
int CSqldalImpl::RowsAffected()
{
	if ( ! m_pDBConn )
	{
		WARN("%s called on closed DB.", __FUNCTION__) ;
		return 0;
	}
	return sqlite3_changes(m_pDBConn) ;
}
Ejemplo n.º 19
0
static uint64_t sqlite3_odbx_rows_affected( odbx_result_t* result )
{
	if( result->handle != NULL )
	{
		return (uint64_t) sqlite3_changes( (sqlite3*) result->handle->generic );
	}

	return 0;
}
Ejemplo n.º 20
0
int s_sqlite::exec_statement (std::string query)
{
    connect();
    rc = sqlite3_exec (db, query.c_str(), 0, 0, &errormsj);
    if (rc != SQLITE_OK)
        throw genexception (errormsj);
    disconnect();
    return sqlite3_changes (db);
}
Ejemplo n.º 21
0
//-----------------
int Statement::executeUpdate(const std::string& sql)
{
	int rc = sqlite3_exec(mhdbc->hdbc, (const char*)sql.c_str(), 0, 0, NULL);
	if( rc !=SQLITE_OK)
	{
		throw SQLException( (const char*)sqlite3_errmsg( mhdbc->hdbc ) );
	}
	return sqlite3_changes(mhdbc->hdbc);
}
Ejemplo n.º 22
0
int64_t PDOSqliteConnection::doer(const String& sql) {
  char *errmsg = NULL;
  if (sqlite3_exec(m_db, sql.data(), NULL, NULL, &errmsg) != SQLITE_OK) {
    handleError(__FILE__, __LINE__);
    if (errmsg) sqlite3_free(errmsg);
    return -1;
  }
  return sqlite3_changes(m_db);
}
Ejemplo n.º 23
0
/*
** Implementation of the changes() SQL function.  The return value is the
** same as the sqlite3_changes() API function.
*/
static void changes(
  sqlite3_context *context,
  int NotUsed,
  sqlite3_value **NotUsed2
){
  sqlite3 *db = sqlite3_context_db_handle(context);
  UNUSED_PARAMETER2(NotUsed, NotUsed2);
  sqlite3_result_int(context, sqlite3_changes(db));
}
static jlong nativeExecuteForLastInsertedRowId(JNIEnv* env, jclass clazz,
        jlong connectionPtr, jlong statementPtr) {
    SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr);
    sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr);

    int err = executeNonQuery(env, connection, statement);
    return err == SQLITE_DONE && sqlite3_changes(connection->db) > 0
            ? sqlite3_last_insert_rowid(connection->db) : -1;
}
Ejemplo n.º 25
0
/**
@SYMTestCaseID			SYSLIB-SQL-UT-3433
@SYMTestCaseDesc		Test for DEF104744 - RSqlStatement::Next() SQL Server crashes on ORDER BY clause.
						The test creates a database with a table with 30 integer columns, then inserts 100
						records. After that, sets the soft heap limit to be very low - 10K 
						(to get sqlite3_release_memory() called by SQLITE ), creates a statement object 
						and attempts to retrieve the inserted records in descending order.
@SYMTestPriority		High
@SYMTestActions			Test for DEF104744 - RSqlStatement::Next() SQL Server crashes on ORDER BY clause.
@SYMTestExpectedResults Test must not fail
@SYMDEF					DEF104744
*/
void DEF104744()
	{
	(void)TheFs.Delete(KTestDatabase);
	TheSqliteDb = NULL;
	TInt err = sqlite3_open((const char*)KTestDatabaseZ().Ptr(), &TheSqliteDb);
	TEST2(err, SQLITE_OK);
	
	_LIT8(KCreateTblSqlZ, "CREATE TABLE A1(F1 INTEGER,F2 INTEGER,F3 INTEGER,F4 INTEGER,F5 INTEGER,F6 INTEGER,F7 INTEGER,F8 INTEGER,F9 INTEGER,F10 INTEGER,F11 INTEGER,F12 INTEGER,F13 INTEGER,F14 INTEGER,F15 INTEGER,F16 INTEGER,F17 INTEGER,F18 INTEGER,F19 INTEGER,F20 INTEGER,F21 INTEGER,F22 INTEGER,F23 INTEGER,F24 INTEGER,F25 INTEGER,F26 INTEGER,F27 INTEGER,F28 INTEGER,F29 INTEGER,F30 INTEGER)\x0");
	err = sqlite3_exec(TheSqliteDb, (const char*)KCreateTblSqlZ().Ptr(), 0, 0, 0);
	TEST2(err, SQLITE_OK);

	//Insert a 100 records
	const TInt KTestRecCnt = 100;
	_LIT8(KInsertSqlZ, "INSERT INTO A1(F1,F2 ,F3 ,F4 ,F5 ,F6 ,F7 ,F8 ,F9 ,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,F24,F25,F26,F27,F28,F29,F30) VALUES(:Prm1,:Prm2,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296)\x0");
	sqlite3_stmt* stmt1 = NULL;
	err = sqlite3_prepare_v2(TheSqliteDb, (const char*)(KInsertSqlZ().Ptr()), -1, &stmt1, NULL);
	TEST2(err, SQLITE_OK);
	
	_LIT8(KBeginSqlZ, "BEGIN\x0");
	err = sqlite3_exec(TheSqliteDb, (const char*)KBeginSqlZ().Ptr(), 0, 0, 0);
	TEST2(err, SQLITE_OK);
	
	for(TInt i=0;i<KTestRecCnt;++i)
		{ 
		err = sqlite3_bind_int(stmt1, 1, i);
		TEST2(err, SQLITE_OK);
		err = sqlite3_bind_int(stmt1, 2, i + 1);
		TEST2(err, SQLITE_OK);
		err = sqlite3_step(stmt1);
		TEST2(err, SQLITE_DONE);
		err = sqlite3_reset(stmt1);
		TEST2(err, SQLITE_OK);
		TInt cnt = sqlite3_changes(TheSqliteDb);
		TEST2(cnt, 1);
		}
		
	_LIT8(KCommitSqlZ, "COMMIT\x0");
	err = sqlite3_exec(TheSqliteDb, (const char*)KCommitSqlZ().Ptr(), 0, 0, 0);
	TEST2(err, SQLITE_OK);
	sqlite3_finalize(stmt1);

	sqlite3_soft_heap_limit(10 * 1024);//Setting very low soft heap limit - 10K

	// Get the inserted record data in descending order.
	sqlite3_stmt* stmt2 = NULL;
	_LIT8(KSelectSqlZ,"SELECT * FROM A1 ORDER BY F1 DESC");
	err = sqlite3_prepare_v2(TheSqliteDb, (const char*)(KSelectSqlZ().Ptr()), -1, &stmt2, NULL);
	TEST2(err, SQLITE_OK);
	err = sqlite3_step(stmt2);
	TEST2(err, SQLITE_ROW);
	sqlite3_finalize(stmt2);

	sqlite3_close(TheSqliteDb); 
	TheSqliteDb = NULL;
	(void)TheFs.Delete(KTestDatabase);
	}
Ejemplo n.º 26
0
static mrb_value
mrb_sqlite3_database_changes(mrb_state *mrb, mrb_value self) {
  mrb_value value_context = mrb_iv_get(mrb, self, mrb_intern(mrb, "context"));
  mrb_sqlite3_database* db = NULL;
  Data_Get_Struct(mrb, value_context, &mrb_sqlite3_database_type, db);
  if (!db) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }
  return mrb_fixnum_value(sqlite3_changes(db->db));
}
Ejemplo n.º 27
0
svn_error_t *
svn_sqlite__update(int *affected_rows, svn_sqlite__stmt_t *stmt)
{
  SVN_ERR(step_with_expectation(stmt, FALSE));

  if (affected_rows)
    *affected_rows = sqlite3_changes(stmt->db->db3);

  return svn_error_trace(svn_sqlite__reset(stmt));
}
Ejemplo n.º 28
0
static int dbd_sqlite3_query(apr_dbd_t *sql, int *nrows, const char *query)
{
    sqlite3_stmt *stmt = NULL;
    const char *tail = NULL;
    int ret = -1, length = 0;

    if (sql->trans && sql->trans->errnum) {
        return sql->trans->errnum;
    }

    length = strlen(query);
#if APR_HAS_THREADS
    apr_thread_mutex_lock(sql->mutex);
#endif

    do {
        int retry_count = 0;

        ret = sqlite3_prepare(sql->conn, query, length, &stmt, &tail);
        if (ret != SQLITE_OK) {
            sqlite3_finalize(stmt);
            break;
        }

        while(retry_count++ <= MAX_RETRY_COUNT) {
            ret = sqlite3_step(stmt);
            if (ret != SQLITE_BUSY)
                break;

#if APR_HAS_THREADS
            apr_thread_mutex_unlock(sql->mutex);
#endif
            apr_sleep(MAX_RETRY_SLEEP);
#if APR_HAS_THREADS
            apr_thread_mutex_lock(sql->mutex);
#endif
        }

        *nrows = sqlite3_changes(sql->conn);
        sqlite3_finalize(stmt);
        length -= (tail - query);
        query = tail;
    } while (length > 0);

    if (dbd_sqlite3_is_success(ret)) {
        ret = 0;
    }
#if APR_HAS_THREADS
    apr_thread_mutex_unlock(sql->mutex);
#endif
    if (sql->trans) {
        sql->trans->errnum = ret;
    }
    return ret;
}
Ejemplo n.º 29
0
static void HL_NAME(finalize_request)(sqlite_result *r, bool exc ) {
	r->first = 0;
	r->done = 1;
	if( r->ncols == 0 )
		r->count = sqlite3_changes(r->db->db);
	if( sqlite3_finalize(r->r) != SQLITE_OK && exc )
		hl_error("SQLite error: Could not finalize request");
	r->r = NULL;
	r->db->last = NULL;
	r->db = NULL;
}
Ejemplo n.º 30
0
uint32 Connection::doExecute(sqlite3 *connection, const String &sql)
{
	if(m_showSql)
		OS_LOG_NOTICE(sql);
	_check(connection);

	scoped_ptr<Result> result(OS_NEW Result(connection, m_cs));
	result->execute(sql);
	// Restituisce il numero di records affetti
	return sqlite3_changes(connection);
}