diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-16 16:03:25 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-16 16:03:25 +0200 |
commit | 2a270b8424d91e58b17b5d5f77ce9f135198569a (patch) | |
tree | da5414edd881e55823f5c425801d361d219e6ed9 | |
parent | 7fec1734882ab0bf92ee1d21d898d7ee3d8f07bf (diff) |
Support for views; integrated part
-rw-r--r-- | odb/mysql/query.cxx | 50 | ||||
-rw-r--r-- | odb/mysql/query.hxx | 71 | ||||
-rw-r--r-- | odb/mysql/traits.hxx | 26 | ||||
-rw-r--r-- | odb/mysql/view-result.txx | 2 | ||||
-rw-r--r-- | odb/mysql/view-statements.hxx | 17 |
5 files changed, 140 insertions, 26 deletions
diff --git a/odb/mysql/query.cxx b/odb/mysql/query.cxx index 14477d6..c87fee5 100644 --- a/odb/mysql/query.cxx +++ b/odb/mysql/query.cxx @@ -166,6 +166,47 @@ namespace odb return r; } + const char* query:: + clause_prefix () const + { + if (!clause_.empty ()) + { + const clause_part& p (clause_.front ()); + + if (p.kind == clause_part::native) + { + const string& s (p.part); + string::size_type n; + + // It is easier to compare to upper and lower-case versions + // rather than getting involved with the portable case- + // insensitive string comparison mess. + // + if (s.compare (0, (n = 5), "WHERE") == 0 || + s.compare (0, (n = 5), "where") == 0 || + s.compare (0, (n = 6), "SELECT") == 0 || + s.compare (0, (n = 6), "select") == 0 || + s.compare (0, (n = 8), "ORDER BY") == 0 || + s.compare (0, (n = 8), "order by") == 0 || + s.compare (0, (n = 8), "GROUP BY") == 0 || + s.compare (0, (n = 8), "group by") == 0 || + s.compare (0, (n = 6), "HAVING") == 0 || + s.compare (0, (n = 6), "having") == 0) + { + // It either has to be an exact match, or there should be + // a whitespace following the keyword. + // + if (s.size () == n || s[n] == ' ' || s[n] =='\t') + return ""; + } + } + + return "WHERE "; + } + + return ""; + } + string query:: clause () const { @@ -212,14 +253,7 @@ namespace odb } } - if (r.empty () || - r.compare (0, 6, "WHERE ") == 0 || - r.compare (0, 9, "ORDER BY ") == 0 || - r.compare (0, 9, "GROUP BY ") == 0 || - r.compare (0, 7, "HAVING ") == 0) - return r; - else - return "WHERE " + r; + return clause_prefix () + r; } } } diff --git a/odb/mysql/query.hxx b/odb/mysql/query.hxx index 57d43a1..4314938 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -106,6 +106,23 @@ namespace odb { } + // True or false literal. + // + explicit + query (bool v) + : binding_ (0, 0) + { + clause_.push_back ( + clause_part (clause_part::native, v ? "TRUE" : "FALSE")); + } + + explicit + query (const char* native) + : binding_ (0, 0) + { + clause_.push_back (clause_part (clause_part::native, native)); + } + explicit query (const std::string& native) : binding_ (0, 0) @@ -147,6 +164,9 @@ namespace odb std::string clause () const; + const char* + clause_prefix () const; + binding& parameters_binding () const; @@ -1031,6 +1051,45 @@ namespace odb const char* column_; }; + // Provide operator+() for using columns to construct native + // query fragments (e.g., ORDER BY). + // + template <typename T, database_type_id ID> + inline query + operator+ (const query_column<T, ID>& c, const std::string& s) + { + query q (c.table (), c.column ()); + q += s; + return q; + } + + template <typename T, database_type_id ID> + inline query + operator+ (const std::string& s, const query_column<T, ID>& c) + { + query q (s); + q.append (c.table (), c.column ()); + return q; + } + + template <typename T, database_type_id ID> + inline query + operator+ (const query_column<T, ID>& c, const query& q) + { + query r (c.table (), c.column ()); + r += q; + return r; + } + + template <typename T, database_type_id ID> + inline query + operator+ (const query& q, const query_column<T, ID>& c) + { + query r (q); + r.append (c.table (), c.column ()); + return r; + } + // // template <typename T, database_type_id> @@ -1801,6 +1860,18 @@ namespace odb } explicit + query (bool v) + : query_selector<T>::type (v) + { + } + + explicit + query (const char* q) + : query_selector<T>::type (q) + { + } + + explicit query (const std::string& q) : query_selector<T>::type (q) { diff --git a/odb/mysql/traits.hxx b/odb/mysql/traits.hxx index b03bbe1..d10b414 100644 --- a/odb/mysql/traits.hxx +++ b/odb/mysql/traits.hxx @@ -448,6 +448,26 @@ namespace odb { }; + template <std::size_t n> + struct default_value_traits<char[n], id_string>: c_string_value_traits + { + }; + + template <std::size_t n> + struct default_value_traits<char[n], id_decimal>: c_string_value_traits + { + }; + + template <std::size_t n> + struct default_value_traits<char[n], id_enum>: c_string_value_traits + { + }; + + template <std::size_t n> + struct default_value_traits<char[n], id_set>: c_string_value_traits + { + }; + // std::vector<char> (buffer) specialization. // template <> @@ -584,6 +604,12 @@ namespace odb { static const database_type_id db_type_id = id_string; }; + + template <std::size_t n> + struct default_type_traits<char[n]> + { + static const database_type_id db_type_id = id_string; + }; } } diff --git a/odb/mysql/view-result.txx b/odb/mysql/view-result.txx index 7adf036..94d9ae6 100644 --- a/odb/mysql/view-result.txx +++ b/odb/mysql/view-result.txx @@ -40,7 +40,7 @@ namespace odb odb::database& db (this->database ()); view_traits::callback (db, view, callback_event::pre_load); - view_traits::init (view, statements_.image ()); + view_traits::init (view, statements_.image (), db); view_traits::callback (db, view, callback_event::post_load); } diff --git a/odb/mysql/view-statements.hxx b/odb/mysql/view-statements.hxx index ee384e1..04df484 100644 --- a/odb/mysql/view-statements.hxx +++ b/odb/mysql/view-statements.hxx @@ -33,8 +33,6 @@ namespace odb typedef typename view_traits::pointer_type pointer_type; typedef typename view_traits::image_type image_type; - typedef mysql::select_statement query_statement_type; - public: view_statements (connection_type&); @@ -73,19 +71,6 @@ namespace odb return image_truncated_; } - query_statement_type& - query_statement () - { - if (query_ == 0) - query_.reset ( - new (details::shared) query_statement_type ( - conn_, - view_traits::query_statement, - image_binding_)); - - return *query_; - } - private: view_statements (const view_statements&); view_statements& operator= (const view_statements&); @@ -96,8 +81,6 @@ namespace odb binding image_binding_; MYSQL_BIND image_bind_[view_traits::column_count]; my_bool image_truncated_[view_traits::column_count]; - - details::shared_ptr<query_statement_type> query_; }; } } |