diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-08-10 11:17:52 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-08-10 11:17:52 +0200 |
commit | 0a589394a09ce5b3f16d902d657710a2886cc2fc (patch) | |
tree | 08f5bfa47a0d07b0dc20352706342e17f277759f /odb/mysql/statement.cxx | |
parent | c67f076c75a36877ce142dc43a1ed2292ab8117a (diff) |
Add query support
Diffstat (limited to 'odb/mysql/statement.cxx')
-rw-r--r-- | odb/mysql/statement.cxx | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/odb/mysql/statement.cxx b/odb/mysql/statement.cxx index e008d17..981c756 100644 --- a/odb/mysql/statement.cxx +++ b/odb/mysql/statement.cxx @@ -36,6 +36,133 @@ namespace odb mysql_stmt_close (stmt_); } + void statement:: + cancel () + { + } + + // query_statement + // + + query_statement:: + ~query_statement () + { + if (conn_.active () == this) + { + try + { + free_result (); + } + catch (...) + { + } + } + } + + query_statement:: + query_statement (connection& conn, + const string& query, + binding& image, + MYSQL_BIND* parameters) + : statement (conn), + image_ (image), + image_version_ (0), + parameters_ (parameters) + { + if (mysql_stmt_prepare (stmt_, query.c_str (), query.size ()) != 0) + throw database_exception (stmt_); + } + + void query_statement:: + execute () + { + if (statement* a = conn_.active ()) + a->cancel (); + + if (mysql_stmt_reset (stmt_)) + throw database_exception (stmt_); + + if (image_version_ != image_.version) + { + if (mysql_stmt_bind_result (stmt_, image_.bind)) + throw database_exception (stmt_); + + image_version_ = image_.version; + } + + if (parameters_ != 0) + { + // @@ versioning + // + if (mysql_stmt_bind_param (stmt_, parameters_)) + throw database_exception (stmt_); + } + + if (mysql_stmt_execute (stmt_)) + throw database_exception (stmt_); + + conn_.active (this); + } + + query_statement::result query_statement:: + fetch () + { + int r (mysql_stmt_fetch (stmt_)); + + switch (r) + { + case 0: + { + return success; + } + case 1: + { + throw database_exception (stmt_); + } + case MYSQL_NO_DATA: + { + return no_data; + } + case MYSQL_DATA_TRUNCATED: + { + return truncated; + } + } + } + + void query_statement:: + refetch () + { + // Re-fetch columns that were truncated. + // + for (size_t i (0); i < image_.count; ++i) + { + if (*image_.bind[i].error) + { + *image_.bind[i].error = 0; + + if (mysql_stmt_fetch_column ( + stmt_, image_.bind + i, static_cast<unsigned int> (i), 0)) + throw database_exception (stmt_); + } + } + } + + void query_statement:: + free_result () + { + if (mysql_stmt_free_result (stmt_)) + throw database_exception (stmt_); + + conn_.active (0); + } + + void query_statement:: + cancel () + { + free_result (); + } + // insert_statement // @@ -57,6 +184,9 @@ namespace odb void insert_statement:: execute () { + if (statement* a = conn_.active ()) + a->cancel (); + if (mysql_stmt_reset (stmt_)) throw database_exception (stmt_); @@ -92,6 +222,16 @@ namespace odb select_statement:: ~select_statement () { + if (conn_.active () == this) + { + try + { + free_result (); + } + catch (...) + { + } + } } select_statement:: @@ -112,6 +252,9 @@ namespace odb select_statement::result select_statement:: execute () { + if (statement* a = conn_.active ()) + a->cancel (); + if (mysql_stmt_reset (stmt_)) throw database_exception (stmt_); @@ -134,6 +277,8 @@ namespace odb if (mysql_stmt_execute (stmt_)) throw database_exception (stmt_); + conn_.active (this); + int r (mysql_stmt_fetch (stmt_)); switch (r) @@ -181,8 +326,17 @@ namespace odb { if (mysql_stmt_free_result (stmt_)) throw database_exception (stmt_); + + conn_.active (0); } + void select_statement:: + cancel () + { + free_result (); + } + + // update_statement // @@ -209,6 +363,9 @@ namespace odb void update_statement:: execute () { + if (statement* a = conn_.active ()) + a->cancel (); + if (mysql_stmt_reset (stmt_)) throw database_exception (stmt_); @@ -259,6 +416,9 @@ namespace odb void delete_statement:: execute () { + if (statement* a = conn_.active ()) + a->cancel (); + if (mysql_stmt_reset (stmt_)) throw database_exception (stmt_); |