Пример #1
0
/*
** Execute zSql on database db. Return an error code.
*/
static int execSql(sqlite3 *db, const char *zSql){
  sqlite3_stmt *pStmt;
  if( !zSql ){
    return SQLITE_NOMEM;
  }
  if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
    return sqlite3_errcode(db);
  }
  while( SQLITE_ROW==sqlite3_step(pStmt) ){}
  return sqlite3_finalize(pStmt);
}
Пример #2
0
static tb_database_sql_statement_ref_t tb_database_sqlite3_statement_init(tb_database_sql_impl_t* database, tb_char_t const* sql)
{
    // check
    tb_database_sqlite3_t* sqlite = tb_database_sqlite3_cast(database);
    tb_assert_and_check_return_val(sqlite && sqlite->database && sql, tb_null);

    // init statement
    sqlite3_stmt* statement = tb_null;
    if (SQLITE_OK != sqlite3_prepare_v2(sqlite->database, sql, -1, &statement, 0))
    {
        // save state
        sqlite->base.state = tb_database_sqlite3_state_from_errno(sqlite3_errcode(sqlite->database));

        // trace
        tb_trace_e("statement: init %s failed, error[%d]: %s", sql, sqlite3_errcode(sqlite->database), sqlite3_errmsg(sqlite->database));
    }

    // ok?
    return (tb_database_sql_statement_ref_t)statement;
}
Пример #3
0
static int _fs_create_internal(char *err_msg, sqlite3 *db, const char *name,
                               uint64_t size, uint64_t sim_pool_id)
{
    int rc = LSM_ERR_OK;
    char size_str[_BUFF_SIZE];
    lsm_hash *sim_pool = NULL;
    char sim_pool_id_str[_BUFF_SIZE];
    uint64_t element_type = 0;

    assert(db != NULL);
    assert(name != NULL);

    size = _db_blk_size_rounding(size);
    if (_pool_has_enough_free_size(db, sim_pool_id, size) == false) {
        rc = LSM_ERR_NOT_ENOUGH_SPACE;
        _lsm_err_msg_set(err_msg, "Insufficient space in pool");
        goto out;
    }
    _snprintf_buff(err_msg, rc, out, size_str, "%" PRIu64, size);
    _snprintf_buff(err_msg, rc, out, sim_pool_id_str, "%" PRIu64, sim_pool_id);
    _good(_db_sim_pool_of_sim_id(err_msg, db, sim_pool_id, &sim_pool),
          rc, out);
    /* Check whether pool support creating fs. */
    _good(_str_to_uint64(err_msg, lsm_hash_string_get(sim_pool, "element_type"),
                         &element_type),
          rc, out);
    if (! (element_type & LSM_POOL_ELEMENT_TYPE_FS)) {
        rc = LSM_ERR_NO_SUPPORT;
        _lsm_err_msg_set(err_msg, "Specified pool does not support fs "
                         "creation");
        goto out;
    }
    rc = _db_data_add(err_msg, db, _DB_TABLE_FSS,
                      "name", name,
                      "total_space", size_str,
                      "consumed_size", size_str,
                      "free_space", size_str,
                      "pool_id", sim_pool_id_str,
                      NULL);

    if (rc != LSM_ERR_OK) {
        if (sqlite3_errcode(db) == SQLITE_CONSTRAINT) {
            rc = LSM_ERR_NAME_CONFLICT;
            _lsm_err_msg_set(err_msg, "FS name '%s' in use", name);
        }
        goto out;
    }

 out:
    if (sim_pool != NULL)
        lsm_hash_free(sim_pool);

    return rc;
}
Пример #4
0
static tb_bool_t tb_database_sqlite3_rollback(tb_database_sql_impl_t* database)
{
    // check
    tb_database_sqlite3_t* sqlite = tb_database_sqlite3_cast(database);
    tb_assert_and_check_return_val(sqlite && sqlite->database, tb_false);

    // done rollback
    if (SQLITE_OK != sqlite3_exec(sqlite->database, "rollback;", tb_null, tb_null, tb_null))
    {
        // save state
        sqlite->base.state = tb_database_sqlite3_state_from_errno(sqlite3_errcode(sqlite->database));

        // trace
        tb_trace_e("rollback: failed, error[%d]: %s", sqlite3_errcode(sqlite->database), sqlite3_errmsg(sqlite->database));
        return tb_false;
    }

    // ok
    return tb_true;
}
Пример #5
0
SQLiteException::SQLiteException(const std::string& mess, sqlite3* sqlite) : SQLException("")
{
    if (sqlite) {
        std::ostringstream o;
        o << mess << "\n" << sqlite3_errcode(sqlite) << ": " << sqlite3_errmsg(sqlite);
        message = o.str();
    }
    else
        message = mess;

}
Пример #6
0
void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, int errcode) {
	if (SQLITE_OK == errcode) {
		errcode = sqlite3_errcode(handle);
	}

	const char* errmsg = sqlite3_errmsg(handle);

	jclass exClass = (*env)->FindClass(env, "com/mlsdev/sqlite/SQLiteIcuException");

	(*env)->ThrowNew(env, exClass, errmsg);
}
Пример #7
0
void
sqlite_error(C4Runtime *c4, const char *file, int line_num)
{
    const char *errmsg;

    errmsg = sqlite3_errmsg(c4->sql->db);
    fprintf(stderr, "SQLITE ERROR: \"%s\" (code = %d), at %s:%d\n",
            errmsg, sqlite3_errcode(c4->sql->db), file, line_num);
    print_backtrace();
    exit(1);
}
Пример #8
0
static JSBool
js_db_last_error_code(JSContext *cx, JSObject *obj, uintN argc,
                      jsval *argv, jsval *rval)
{
    js_db_t *jd = JS_GetPrivate(cx, obj);

    if(js_db_check(cx, jd))
        return JS_FALSE;

    *rval = INT_TO_JSVAL(sqlite3_errcode(jd->jd_db));
    return JS_TRUE;
}
Пример #9
0
            string session::last_error() const
            {
                if (db_ == nullptr) {
                    return string();
                }
                ostringstream buf;

                buf << sqlite3_errcode(db_.get());
                buf << ": " << sqlite3_errmsg(db_.get());

                return buf.str();
            }
Пример #10
0
ikptr
ik_sqlite3_errcode (ikptr s_conn, ikpcb * pcb)
{
#ifdef HAVE_SQLITE3_ERRCODE
  sqlite3 *	conn = IK_SQLITE_CONNECTION(s_conn);
  int		rv;
  rv = sqlite3_errcode(conn);
  return ika_integer_from_int(pcb, rv);
#else
  feature_failure(__func__);
#endif
}
Пример #11
0
void CSQLite3Query::freeRes(void)
{
    if (m_pVM && m_bOwnVM)
    {
        int iRtn = sqlite3_finalize(m_pVM);
        m_pVM = NULL;
        if (iRtn != SQLITE_OK)
        {
            Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
        }
    }
}
Пример #12
0
void
SQLiteConnectionBackend::open(SqlDialect *dialect, const SqlSource &source)
{
    close();
    ScopedLock lock(drv_->conn_mux_);
    own_handle_ = true;
    sqlite3_open(NARROW(source.db()).c_str(), &conn_);
    if (SQLITE_OK != sqlite3_errcode(conn_)) {
        const char *err = sqlite3_errmsg(conn_);
        throw DBError(WIDEN(err));
    }
}
SWIGEXPORT jint JNICALL Java_com_almworks_sqlite4java__1SQLiteSwiggedJNI_sqlite3_1errcode(JNIEnv *jenv, jclass jcls, jlong jarg1) {
  jint jresult = 0 ;
  sqlite3 *arg1 = (sqlite3 *) 0 ;
  int result;
  
  (void)jenv;
  (void)jcls;
  arg1 = *(sqlite3 **)&jarg1; 
  result = (int)sqlite3_errcode(arg1);
  jresult = (jint)result; 
  return jresult;
}
Пример #14
0
void CSQLite3Statement::freeVM(void)
{
    if (NULL != m_pVM)
    {
        int iRtn = sqlite3_finalize(m_pVM);
        m_pVM = NULL;
        if (iRtn != SQLITE_OK)
        {
            Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
        }
    }
}
Пример #15
0
void CSQLite3Statement::bindBlob(const int iField, const unsigned char* blobValue, const size_t iLen)
{
    checkVM();
    CheckParam(iField);

    int iRtn = sqlite3_bind_blob(m_pVM, iField + 1,
        (const void*)blobValue, (int)iLen, SQLITE_TRANSIENT);
    if (iRtn != SQLITE_OK)
    {
        Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
    }
}
Пример #16
0
void CSQLite3Link::open(CDBUrl &objDBUrl)
{
    int iRtn =  0;

    iRtn = sqlite3_open(objDBUrl.getDB().c_str(), &m_pDB);
    if (iRtn != SQLITE_OK)
    {
        Q_EXCEPTION(sqlite3_errcode(m_pDB), "%s", sqlite3_errmsg(m_pDB));
    }

    setBusyTimeout(m_iBusyTimeoutMs);
}
Пример #17
0
int _pdo_sqlite_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line) /* {{{ */
{
	pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
	pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
	pdo_sqlite_error_info *einfo = &H->einfo;

	einfo->errcode = sqlite3_errcode(H->db);
	einfo->file = file;
	einfo->line = line;

	if (einfo->errcode != SQLITE_OK) {
		if (einfo->errmsg) {
			pefree(einfo->errmsg, dbh->is_persistent);
		}
		einfo->errmsg = pestrdup((char*)sqlite3_errmsg(H->db), dbh->is_persistent);
	} else { /* no error */
		strncpy(*pdo_err, PDO_ERR_NONE, sizeof(PDO_ERR_NONE));
		return 0;
	}
	switch (einfo->errcode) {
		case SQLITE_NOTFOUND:
			strncpy(*pdo_err, "42S02", sizeof("42S02"));
			break;

		case SQLITE_INTERRUPT:
			strncpy(*pdo_err, "01002", sizeof("01002"));
			break;

		case SQLITE_NOLFS:
			strncpy(*pdo_err, "HYC00", sizeof("HYC00"));
			break;

		case SQLITE_TOOBIG:
			strncpy(*pdo_err, "22001", sizeof("22001"));
			break;

		case SQLITE_CONSTRAINT:
			strncpy(*pdo_err, "23000", sizeof("23000"));
			break;

		case SQLITE_ERROR:
		default:
			strncpy(*pdo_err, "HY000", sizeof("HY000"));
			break;
	}

	if (!dbh->methods) {
		zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s",
				*pdo_err, einfo->errcode, einfo->errmsg);
	}

	return einfo->errcode;
}
Пример #18
0
bool conflate_delete_private(conflate_handle_t *handle,
                             const char *k, const char *filename)
{
    bool rv = false;
    int err = 0, deleted = 0;
    sqlite3 *db = NULL;
    sqlite3_stmt *del = NULL;

    if ((err = open_and_initialize_db(handle, filename, &db)) != SQLITE_OK) {
        goto finished;
    }

    assert(db != NULL);

    if ((err = sqlite3_prepare_v2(db, DEL_PRIV, strlen(DEL_PRIV),
                                  &del, NULL)) != SQLITE_OK) {
        goto finished;
    }

    sqlite3_bind_text(del, 1, k, strlen(k), SQLITE_TRANSIENT);

    deleted = run_mod_steps(handle, db, del);
    CONFLATE_LOG(handle, LOG_LVL_DEBUG, "Removed %d records", deleted);
    rv = deleted >= 0;

 finished:
    err = sqlite3_errcode(db);
    if (err != SQLITE_OK && err != SQLITE_DONE) {
        CONFLATE_LOG(handle, LOG_LVL_ERROR, "DB error %d:  %s",
                     sqlite3_errcode(db), sqlite3_errmsg(db));
    }

    if (del) {
        sqlite3_finalize(del);
    }

    sqlite3_close(db);

    return rv;
}
Пример #19
0
CDBQuery* CSQLite3Link::execQuery(const char* szSQL)
{
    if (NULL == szSQL)
    {
        Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_NULLPOINTER);
    }

    checkDB();

    CSQLite3Query *pQuery = NULL;

    sqlite3_stmt* pVM = compile(szSQL);
    int iRtn = sqlite3_step(pVM);
    if (SQLITE_DONE == iRtn)
    {
        try
        {
            pQuery = new CSQLite3Query(m_pDB, pVM, true);
        }
        catch(std::bad_alloc &)
        {
            (void)sqlite3_finalize(pVM);
            Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_ALLOCMEMORY);
        }

        return pQuery;
    }
    else if (SQLITE_ROW == iRtn)
    {
        // at least 1 row
        try
        {
            pQuery = new CSQLite3Query(m_pDB, pVM, false);
        }
        catch(std::bad_alloc &)
        {
            (void)sqlite3_finalize(pVM);
            Q_EXCEPTION(Q_ERROR_DATABASE, "%s", Q_EXCEPTION_ALLOCMEMORY);
        }

        return pQuery;
    }
    else
    {
        int iErrorCode = sqlite3_errcode(m_pDB);
        std::string strErrorMsg = sqlite3_errmsg(m_pDB);

        (void)sqlite3_finalize(pVM);

        Q_EXCEPTION(iErrorCode, "%s", strErrorMsg.c_str());
    }
}
Пример #20
0
static tb_size_t tb_database_sqlite3_result_row_iterator_next(tb_iterator_ref_t iterator, tb_size_t itor)
{
    // check
    tb_database_sqlite3_result_t* result = (tb_database_sqlite3_result_t*)iterator;
    tb_assert(result);
    tb_assert_and_check_return_val(itor < result->count, result->count);

    // statement result?
    if (result->statement)
    {
        // step statement
        tb_int_t ok = sqlite3_step(result->statement);

        // end?
        if (ok != SQLITE_ROW) 
        {
            // reset it 
            if (SQLITE_OK != sqlite3_reset(result->statement))
            {
                // the sqlite
                tb_database_sqlite3_t* sqlite = (tb_database_sqlite3_t*)iterator->priv;
                if (sqlite)
                {
                    // save state
                    sqlite->base.state = tb_database_sqlite3_state_from_errno(sqlite3_errcode(sqlite->database));

                    // trace
                    tb_trace_e("statement: reset failed, error[%d]: %s", sqlite3_errcode(sqlite->database), sqlite3_errmsg(sqlite->database));
                }
            }

            // tail
            return result->count;
        }
    }

    // next
    return itor + 1;
}
Пример #21
0
void SQLiteStorage::connect()
{
    if (SQLITE_OK != sqlite3_open(db_name_.c_str(), &connection_))
    {
        throw sqlite3_errcode(connection_);
    }
    if (create_)
    {
        create_tables();
        return;
    }
    load();
}
Пример #22
0
void
FileState::DeleteFile(const std::string& filename)
{
  sqlite3_stmt* stmt;
  sqlite3_prepare_v2(m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
  sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC);

  _LOG_DEBUG("Delete " << filename);

  sqlite3_step(stmt);
  _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db));
  sqlite3_finalize(stmt);
}
Пример #23
0
IoObject *IoSQLite3_errorMessage(IoSQLite3 *self, IoObject *locals, IoMessage *m)
{
	/*doc SQLite3  error
	Results a string containing the current error. If there is no error, Nil is returned. 
	*/

	if (sqlite3_errcode(DATA(self)->db) == SQLITE_OK)
	{
		return IONIL(self);
	}

	return IOSYMBOL(IoSQLite3_error(self));
}
Пример #24
0
bool CSQLiteDatabase::open()
{
    if (sqlite3_open_v2(_conn_str.c_str(), &_db_handler, SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
    {
        int errcode = sqlite3_errcode(_db_handler);
        LOG(ERROR) << "Fail to open " << _dbname << " ERROR("
            << errcode << "): " << sqlite3_errmsg(_db_handler);

        return false;
    }

    return true;
}
Пример #25
0
bool conflate_save_private(conflate_handle_t *handle,
                           const char *k, const char *v, const char *filename)
{
    bool rv = false;
    int err = 0;
    sqlite3 *db = NULL;
    sqlite3_stmt *ins = NULL;

    if ((err = open_and_initialize_db(handle, filename, &db)) != SQLITE_OK) {
        goto finished;
    }

    assert(db != NULL);

    if ((err = sqlite3_prepare_v2(db, INS_PRIV, strlen(INS_PRIV),
                                  &ins, NULL)) != SQLITE_OK) {
        goto finished;
    }

    sqlite3_bind_text(ins, 1, k, strlen(k), SQLITE_TRANSIENT);
    sqlite3_bind_text(ins, 2, v, strlen(v), SQLITE_TRANSIENT);

    rv = run_mod_steps(handle, db, ins) == 1;

 finished:
    err = sqlite3_errcode(db);
    if (err != SQLITE_OK && err != SQLITE_DONE) {
        CONFLATE_LOG(handle, LOG_LVL_ERROR, "DB error %d:  %s",
                     sqlite3_errcode(db), sqlite3_errmsg(db));
    }

    if (ins) {
        sqlite3_finalize(ins);
    }

    sqlite3_close(db);

    return rv;
}
Пример #26
0
/* perfexpert_database_connect */
int perfexpert_database_connect(sqlite3 **db, const char *file) {
    sqlite3_backup *pBackup;
    sqlite3 *disk_db;
    char *error = NULL;

    /* Check if file exists */
    if (-1 == access(file, F_OK)) {
        OUTPUT(("%s (%s)", _ERROR((char *)"file not found"), file));
        goto CLEAN_UP;
    }

    /* Open the DB on disk */
    if (SQLITE_OK != sqlite3_open(file, &disk_db)) {
        OUTPUT(("%s (%s), %s", _ERROR((char *)"openning database"), file,
                sqlite3_errmsg(disk_db)));
        goto CLEAN_UP;
    }

    /* Open the DB in memory */
    if (SQLITE_OK != sqlite3_open(":memory:", db)) {
        OUTPUT(("%s (%s)", _ERROR((char *)"openning in-memory database"),
                sqlite3_errmsg(*db)));
        goto CLEAN_UP;
    }

    /* Copy the data from disk DB to in-memory DB */
    pBackup = sqlite3_backup_init(*db, "main", disk_db, "main");
    if (pBackup) {
        (void)sqlite3_backup_step(pBackup, -1);
        (void)sqlite3_backup_finish(pBackup);
    }
    if (SQLITE_OK != sqlite3_errcode(*db)) {
        goto CLEAN_UP;
    }

    /* Enable foreign keys */
    if (SQLITE_OK != sqlite3_exec(*db, "PRAGMA foreign_keys = ON;", NULL, NULL, &error)) {
        OUTPUT(("%s %s", _ERROR("SQL error (enabling foreign keys)"), error));
        sqlite3_free(error);
        goto CLEAN_UP;
    }

    OUTPUT_VERBOSE((4, "      connected to %s", file));

    return PERFEXPERT_SUCCESS;

CLEAN_UP:
    sqlite3_close(*db);
    sqlite3_close(disk_db);
    return PERFEXPERT_ERROR;
}
Пример #27
0
FileItemPtr
ActionLog::LookupAction(const std::string& filename, sqlite3_int64 version, const Buffer& filehash)
{
  sqlite3_stmt* stmt;
  sqlite3_prepare_v2(m_db,
                     "SELECT device_name, seq_no, strftime('%s', file_mtime), file_chmod, file_seg_num, file_hash "
                     " FROM ActionLog "
                     " WHERE action = 0 AND "
                     "       filename=? AND "
                     "       version=? AND "
                     "       is_prefix(?, file_hash)=1",
                     -1, &stmt, 0);
  _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db));

  sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC);
  sqlite3_bind_int64(stmt, 2, version);
  sqlite3_bind_blob(stmt, 3, filehash.buf(), filehash.size(), SQLITE_STATIC);

  FileItemPtr fileItem;

  if (sqlite3_step(stmt) == SQLITE_ROW) {
    fileItem = make_shared<FileItem>();
    fileItem->set_filename(filename);
    fileItem->set_device_name(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
    fileItem->set_seq_no(sqlite3_column_int64(stmt, 1));
    fileItem->set_mtime(sqlite3_column_int64(stmt, 2));
    fileItem->set_mode(sqlite3_column_int64(stmt, 3));
    fileItem->set_seg_num(sqlite3_column_int64(stmt, 4));

    fileItem->set_file_hash(sqlite3_column_blob(stmt, 5), sqlite3_column_bytes(stmt, 5));
  }

  _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE || sqlite3_errcode(m_db) != SQLITE_ROW ||
                    sqlite3_errcode(m_db) != SQLITE_OK,
                  sqlite3_errmsg(m_db));

  return fileItem;
}
Пример #28
0
bool SqQuery::Execute()
{
	int rc;

	/* If we don't have a result set and we have a column count, 
	 * create a result set pre-emptively.  This is in case there
	 * are no rows in the upcoming result set.
	 */
	if (!m_pResults && m_ColCount)
	{
		m_pResults = new SqResults(this);
	}

	/* If we've got results, throw them away */
	if (m_pResults)
	{
		m_pResults->ResetResultCount();
	}

	/* Fetch each row, if any */
	while ((rc = sqlite3_step(m_pStmt)) == SQLITE_ROW)
	{
		/* This should NEVER happen but we're being safe. */
		if (!m_pResults)
		{
			m_pResults = new SqResults(this);
		}
		m_pResults->PushResult();
	}

	sqlite3 *db = m_pParent->GetDb();
	if (rc != SQLITE_OK && rc != SQLITE_DONE && rc == sqlite3_errcode(db))
	{
		/* Something happened... */
		m_LastErrorCode = rc;
		m_LastError.assign(sqlite3_errmsg(db));
		m_AffectedRows = 0;
		m_InsertID = 0;
	} else {
		m_LastErrorCode = SQLITE_OK;
		m_AffectedRows = (unsigned int)sqlite3_changes(db);
		m_InsertID = (unsigned int)sqlite3_last_insert_rowid(db);
	}

	/* Reset everything for the next execute */
	sqlite3_reset(m_pStmt);
	sqlite3_clear_bindings(m_pStmt);

	return (m_LastErrorCode == SQLITE_OK);
}
Пример #29
0
int sqlite_pragma (sqlite3* db, const char* request){
    // get current database version of schema
    static sqlite3_stmt *stmt_pragma;
    int rc=0;

    if( (rc=sqlite3_prepare_v2(db, request, -1, &stmt_pragma, NULL)) == SQLITE_OK) {
        while(sqlite3_step(stmt_pragma) == SQLITE_ROW);
    }
    else{
	rc = sqlite3_errcode(db);
    }
    sqlite3_finalize(stmt_pragma);
    return rc;
}
Пример #30
0
void IoSQLite3_showError(IoSQLite3 *self)
{
	int status = sqlite3_errcode(DATA(self)->db);

	if (status != SQLITE_OK)
	{
		const char *error = IoSQLite3_error(self);

		if (DATA(self)->debugOn)
		{
			IoState_print_(IOSTATE, "*** IoSQLite3 error '%s' ***\n", error);
		}
	}
}