예제 #1
0
파일: sqlite3_db.cpp 프로젝트: respu/arg3db
        void sqlite3_db::open()
        {

            if (db_ != NULL) return;

            if (sqlite3_open(filename_.c_str(), &db_) != SQLITE_OK)
                throw database_exception(last_error());
        }
예제 #2
0
            statement::resultset_type statement::results()
            {
                if (sess_ == nullptr) {
                    throw database_exception("sqlite statement results invalid database");
                }

                return resultset_type(make_shared<resultset>(sess_, stmt_));
            }
예제 #3
0
            bool session::execute(const string &sql)
            {
                if (db_ == nullptr) {
                    throw database_exception("database is not open");
                }

                return !mysql_real_query(db_.get(), sql.c_str(), sql.length());
            }
예제 #4
0
            std::shared_ptr<resultset_impl> session::query(const string &sql)
            {
                sqlite3_stmt *stmt;

                if (db_ == nullptr) {
                    throw database_exception("session::execute database not open");
                }

                if (sqlite3_prepare_v2(db_.get(), sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
                    throw database_exception(last_error());
                }

                if (cache_level() == cache::ResultSet) {
                    return make_shared<cached_resultset>(shared_from_this(), shared_ptr<sqlite3_stmt>(stmt, helper::stmt_delete()));
                } else {
                    return make_shared<resultset>(shared_from_this(), shared_ptr<sqlite3_stmt>(stmt, helper::stmt_delete()));
                }
            }
예제 #5
0
            void statement::prepare(const string &sql)
            {
                if (!sess_ || !sess_->db_) {
                    throw database_exception("database not open");
                }

                if (is_valid()) {
                    log::warn("sql statement prepare not valid");
                    return;
                }

                sqlite3_stmt *temp;

                if (sqlite3_prepare_v2(sess_->db_.get(), sql.c_str(), -1, &temp, NULL) != SQLITE_OK) {
                    throw database_exception(sess_->last_error());
                }

                stmt_ = shared_ptr<sqlite3_stmt>(temp, helper::stmt_delete());
            }
예제 #6
0
 void statement::reset()
 {
     if (!is_valid()) {
         log::warn("sqlite statement reset invalid");
         return;
     }
     if (sqlite3_reset(stmt_.get()) != SQLITE_OK) {
         throw database_exception(sess_->last_error());
     }
 }
예제 #7
0
        std::shared_ptr<session> sqldb::create_session(const uri &uri)
        {
            auto factory = instance()->factories_[uri.protocol];

            if (factory == nullptr) {
                throw database_exception("unknown database " + uri.value);
            }

            return std::make_shared<session>(factory->create(uri));
        }
예제 #8
0
            std::shared_ptr<resultset_impl> session::query(const string &sql)
            {
                MYSQL_RES *res = nullptr;

                if (db_ == nullptr) {
                    throw database_exception("database is not open");
                }

                if (mysql_real_query(db_.get(), sql.c_str(), sql.length())) {
                    throw database_exception(last_error());
                }

                res = mysql_store_result(db_.get());

                if (res == nullptr && mysql_field_count(db_.get()) != 0) {
                    throw database_exception(last_error());
                }

                return make_shared<resultset>(shared_from_this(), shared_ptr<MYSQL_RES>(res, helper::res_delete()));
            }
예제 #9
0
파일: sqlite3_db.cpp 프로젝트: respu/arg3db
        resultset sqlite3_db::execute(const string &sql)
        {
            sqlite3_stmt *stmt;

            if (sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt, NULL) != SQLITE_OK)
                throw database_exception(last_error());

            resultset set(make_shared<sqlite3_resultset>(this, stmt));

            set.next();

            return set;
        }
예제 #10
0
 void connection::open(std::string const & db, sqlite::open_mode open_mode){
     boost::system::error_code ec;
     bool exists = boost::filesystem::exists(db, ec);
     exists = exists && !ec;
     switch(open_mode) {
       case sqlite::open_mode::open_readonly:
          if (!exists) {
             throw database_exception(
                 "Read-only database '" + db + "' does not exist"
             );
         }
         open(db, true);    // read-only
         return;        // we already opened it!
         case sqlite::open_mode::open_existing:
             if(!exists) {
                 throw database_exception(
                     "Database '" + db + "' does not exist"
                 );
             }
             break;
         case sqlite::open_mode::always_create:
             if(exists) {
                 boost::filesystem::remove(db, ec);
                 if(ec) {
                     throw database_system_error(
                         "Failed to remove existing database '" + db + "'",
                         ec.value()
                     );
                 }
             }
             // fall-through
         case sqlite::open_mode::open_or_create:
             // Default behaviour
             break;
         default:
             break;
     };
     open(db);
 }
예제 #11
0
            void session::open(int flags)
            {
                if (db_ != nullptr) {
                    return;
                }

                sqlite3 *conn = nullptr;

                if (sqlite3_open_v2(connection_info().path.c_str(), &conn, flags, nullptr) != SQLITE_OK) {
                    throw database_exception(last_error());
                }

                db_ = shared_ptr<sqlite3>(conn, helper::close_db());
            }
예제 #12
0
        column sqlite3_row::column(const string &name) const
        {
            assert(!name.empty());

            for (size_t i = 0; i < size_; i++)
            {
                const char *col_name = sqlite3_column_name(stmt_, i);

                if (name == col_name)
                {
                    return column(i);
                }
            }
            throw database_exception("unknown column '" + name + "'");
        }
예제 #13
0
            bool session::execute(const string &sql)
            {
                sqlite3_stmt *stmt;

                if (db_ == nullptr) {
                    throw database_exception("session::execute database not open");
                }

                if (sqlite3_prepare_v2(db_.get(), sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
                    return false;
                }

                int res = sqlite3_step(stmt);

                sqlite3_finalize(stmt);

                return res == SQLITE_OK || res == SQLITE_ROW || res == SQLITE_DONE;
            }
예제 #14
0
파일: modify_query.cpp 프로젝트: ryjen/db
        int modify_query::execute()
        {
            if (!is_valid()) {
                throw database_exception("Invalid modify query");
            }

            prepare(to_sql());

            bool success = stmt_->result();

            if (success) {
                numChanges_ = stmt_->last_number_of_changes();
            } else {
                numChanges_ = 0;
            }

            reset();

            return numChanges_;
        }
예제 #15
0
        void mysql_stmt_resultset::get_results()
        {
            if (stmt_ == NULL || bindings_ != NULL || status_ != -1)
                return;

            if ((status_ = mysql_stmt_execute(stmt_)))
            {
                throw database_exception(last_stmt_error(stmt_));
            }

            metadata_ = mysql_stmt_result_metadata(stmt_);

            if (metadata_ == NULL)
                throw database_exception("No result data found.");

            columnCount_ = mysql_num_fields(metadata_);

            bindings_ = (MYSQL_BIND *) calloc(columnCount_, sizeof(MYSQL_BIND));

            auto fields = mysql_fetch_fields(metadata_);

            for (auto i = 0; i < columnCount_; i++)
            {
                switch (fields[i].type)
                {
                case MYSQL_TYPE_INT24:
                    bindings_[i].buffer_type = MYSQL_TYPE_LONGLONG;
                    break;
                case MYSQL_TYPE_DECIMAL:
                case MYSQL_TYPE_NEWDECIMAL:
                    bindings_[i].buffer_type = MYSQL_TYPE_DOUBLE;
                    break;
                case MYSQL_TYPE_BIT:
                    bindings_[i].buffer_type = MYSQL_TYPE_TINY;
                    break;
                case MYSQL_TYPE_YEAR:
                    break;
                case MYSQL_TYPE_VAR_STRING:
                    bindings_[i].buffer_type = MYSQL_TYPE_STRING;
                    break;
                case MYSQL_TYPE_SET:
                case MYSQL_TYPE_ENUM:
                case MYSQL_TYPE_GEOMETRY:
                    break;
                default:
                    bindings_[i].buffer_type = fields[i].type;
                    break;
                }
                bindings_[i].is_null = (my_bool *) calloc(1, sizeof(my_bool));
                bindings_[i].is_unsigned = 0;
                bindings_[i].error = 0;
                bindings_[i].buffer_length = fields[i].length;
                bindings_[i].length = (size_t *) calloc(1, sizeof(size_t));
                bindings_[i].buffer = calloc(1, fields[i].length);
            }

            if (mysql_stmt_bind_result(stmt_, bindings_) != 0)
            {
                throw database_exception(last_stmt_error(stmt_));
            }
        }
예제 #16
0
 void connection::access_check(){
     if(!handle)
         throw database_exception("Database is not open.");
 }
예제 #17
0
 void sqlite3_statement::reset()
 {
     if (sqlite3_reset(stmt_) != SQLITE_OK)
         throw database_exception(db_->last_error());
 }
예제 #18
0
파일: record.cpp 프로젝트: ryjen/db
 /*!
  * @param schema the schema to operate on
  * @param columnName the name of the id column in the schema
  */
 record::record(const std::shared_ptr<schema_type> &schema) : schema_(schema) {
     if (schema_ == nullptr) {
         throw database_exception("no schema for record");
     }
 }
예제 #19
0
 statement::statement(const std::shared_ptr<sqlite::session> &sess) : sess_(sess), stmt_(nullptr), bound_(0)
 {
     if (sess_ == NULL) {
         throw database_exception("no database provided to sqlite3 statement");
     }
 }
예제 #20
0
파일: row.cpp 프로젝트: ryjen/db
 row::row(const shared_ptr<row_impl> &impl) : impl_(impl) {
     if (impl_ == nullptr) {
         throw database_exception("No implementation provided for row");
     }
 }
예제 #21
0
 boost::int64_t connection::get_last_insert_rowid(){
     if(!handle)
         throw database_exception("Database is not open.");
     return static_cast<boost::int64_t>(
             sqlite3_last_insert_rowid(handle));
 }