diff options
Diffstat (limited to 'odb/sqlite')
-rw-r--r-- | odb/sqlite/query.cxx | 103 | ||||
-rw-r--r-- | odb/sqlite/query.hxx | 97 | ||||
-rw-r--r-- | odb/sqlite/query.txx | 17 |
3 files changed, 151 insertions, 66 deletions
diff --git a/odb/sqlite/query.cxx b/odb/sqlite/query.cxx index a97dff2..4a2082b 100644 --- a/odb/sqlite/query.cxx +++ b/odb/sqlite/query.cxx @@ -150,41 +150,100 @@ namespace odb query& query:: operator+= (const query& q) { - size_t n (clause_.size ()); - - if (n != 0 && clause_[n - 1] != ' ' && - !q.clause_.empty () && q.clause_[0] != ' ') - clause_ += ' '; - - clause_ += q.clause_; + clause_.insert (clause_.end (), q.clause_.begin (), q.clause_.end ()); *parameters_ += *q.parameters_; - return *this; } void query:: - add (details::shared_ptr<query_param> p) + append (const string& q, clause_part::kind_type k) { - size_t n (clause_.size ()); + if (k == clause_part::native && + !clause_.empty () && + clause_.back ().kind == clause_part::native) + { + string& s (clause_.back ().part); + + char first (!q.empty () ? q[0] : ' '); + char last (!s.empty () ? s[s.size () - 1] : ' '); - if (n != 0 && clause_[n - 1] != ' ') - clause_ += ' '; + // We don't want extra spaces after '(' as well as before ',' + // and ')'. + // + if (last != ' ' && last != '(' && + first != ' ' && first != ',' && first != ')') + s += ' '; - clause_ += '?'; + s += q; + } + else + clause_.push_back (clause_part (k, q)); + } + + void query:: + add (details::shared_ptr<query_param> p) + { + clause_.push_back (clause_part (clause_part::param)); parameters_->add (p); } - std::string query:: - clause () const + string query:: + clause (string const& table) const { - if (clause_.empty () || - clause_.compare (0, 6, "WHERE ") == 0 || - clause_.compare (0, 9, "ORDER BY ") == 0 || - clause_.compare (0, 9, "GROUP BY ") == 0 || - clause_.compare (0, 7, "HAVING ") == 0) - return clause_; + string r; + + for (clause_type::const_iterator i (clause_.begin ()), + end (clause_.end ()); i != end; ++i) + { + char last (!r.empty () ? r[r.size () - 1] : ' '); + + switch (i->kind) + { + case clause_part::column: + { + if (last != ' ' && last != '(') + r += ' '; + + if (i->part[0] == '.') + r += table; + + r += i->part; + break; + } + case clause_part::param: + { + if (last != ' ' && last != '(') + r += ' '; + + r += '?'; + break; + } + case clause_part::native: + { + // We don't want extra spaces after '(' as well as before ',' + // and ')'. + // + const string& p (i->part); + char first (!p.empty () ? p[0] : ' '); + + if (last != ' ' && last != '(' && + first != ' ' && first != ',' && first != ')') + r += ' '; + + r += p; + break; + } + } + } + + 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 " + clause_; + return "WHERE " + r; } } } diff --git a/odb/sqlite/query.hxx b/odb/sqlite/query.hxx index 98189ab..3179bda 100644 --- a/odb/sqlite/query.hxx +++ b/odb/sqlite/query.hxx @@ -115,15 +115,33 @@ namespace odb class LIBODB_SQLITE_EXPORT query { public: + struct clause_part + { + enum kind_type + { + column, + param, + native + }; + + clause_part (kind_type k): kind (k) {} + clause_part (kind_type k, const std::string& p): kind (k), part (p) {} + + kind_type kind; + std::string part; + }; + query () : parameters_ (new (details::shared) query_params) { } explicit - query (const std::string& q) - : clause_ (q), parameters_ (new (details::shared) query_params) + query (const std::string& q, + clause_part::kind_type k = clause_part::native) + : parameters_ (new (details::shared) query_params) { + clause_.push_back (clause_part (k, q)); } template <typename T> @@ -152,7 +170,7 @@ namespace odb public: std::string - clause () const; + clause (std::string const& default_table) const; binding& parameters_binding () const; @@ -182,12 +200,7 @@ namespace odb query& operator+= (const std::string& q) { - size_t n (clause_.size ()); - - if (n != 0 && clause_[n - 1] != ' ' && !q.empty () && q[0] != ' ') - clause_ += ' '; - - clause_ += q; + append (q, clause_part::native); return *this; } @@ -216,12 +229,17 @@ namespace odb void append (ref_bind<T>); + void + append (const std::string&, clause_part::kind_type); + private: void add (details::shared_ptr<query_param>); private: - std::string clause_; + typedef std::vector<clause_part> clause_type; + + clause_type clause_; details::shared_ptr<query_params> parameters_; }; @@ -394,7 +412,7 @@ namespace odb query is_null () const { - query q (name_); + query q (name_, query::clause_part::column); q += "IS NULL"; return q; } @@ -402,7 +420,7 @@ namespace odb query is_not_null () const { - query q (name_); + query q (name_, query::clause_part::column); q += "IS NOT NULL"; return q; } @@ -438,7 +456,7 @@ namespace odb query equal (val_bind<T> v) const { - query q (name_); + query q (name_, query::clause_part::column); q += "="; q.append<T, ID> (v); return q; @@ -455,7 +473,7 @@ namespace odb query equal (ref_bind<T> r) const { - query q (name_); + query q (name_, query::clause_part::column); q += "="; q.append<T, ID> (r); return q; @@ -523,7 +541,7 @@ namespace odb query unequal (val_bind<T> v) const { - query q (name_); + query q (name_, query::clause_part::column); q += "!="; q.append<T, ID> (v); return q; @@ -540,7 +558,7 @@ namespace odb query unequal (ref_bind<T> r) const { - query q (name_); + query q (name_, query::clause_part::column); q += "!="; q.append<T, ID> (r); return q; @@ -608,7 +626,7 @@ namespace odb query less (val_bind<T> v) const { - query q (name_); + query q (name_, query::clause_part::column); q += "<"; q.append<T, ID> (v); return q; @@ -625,7 +643,7 @@ namespace odb query less (ref_bind<T> r) const { - query q (name_); + query q (name_, query::clause_part::column); q += "<"; q.append<T, ID> (r); return q; @@ -693,7 +711,7 @@ namespace odb query greater (val_bind<T> v) const { - query q (name_); + query q (name_, query::clause_part::column); q += ">"; q.append<T, ID> (v); return q; @@ -710,7 +728,7 @@ namespace odb query greater (ref_bind<T> r) const { - query q (name_); + query q (name_, query::clause_part::column); q += ">"; q.append<T, ID> (r); return q; @@ -778,7 +796,7 @@ namespace odb query less_equal (val_bind<T> v) const { - query q (name_); + query q (name_, query::clause_part::column); q += "<="; q.append<T, ID> (v); return q; @@ -795,7 +813,7 @@ namespace odb query less_equal (ref_bind<T> r) const { - query q (name_); + query q (name_, query::clause_part::column); q += "<="; q.append<T, ID> (r); return q; @@ -863,7 +881,7 @@ namespace odb query greater_equal (val_bind<T> v) const { - query q (name_); + query q (name_, query::clause_part::column); q += ">="; q.append<T, ID> (v); return q; @@ -880,7 +898,7 @@ namespace odb query greater_equal (ref_bind<T> r) const { - query q (name_); + query q (name_, query::clause_part::column); q += ">="; q.append<T, ID> (r); return q; @@ -947,9 +965,9 @@ namespace odb // (void) (sizeof (type_instance<T> () == type_instance<T2> ())); - query q (name_); + query q (name_, query::clause_part::column); q += "="; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -961,9 +979,9 @@ namespace odb // (void) (sizeof (type_instance<T> () != type_instance<T2> ())); - query q (name_); + query q (name_, query::clause_part::column); q += "!="; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -975,9 +993,9 @@ namespace odb // (void) (sizeof (type_instance<T> () < type_instance<T2> ())); - query q (name_); + query q (name_, query::clause_part::column); q += "<"; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -989,9 +1007,9 @@ namespace odb // (void) (sizeof (type_instance<T> () > type_instance<T2> ())); - query q (name_); + query q (name_, query::clause_part::column); q += ">"; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -1003,9 +1021,9 @@ namespace odb // (void) (sizeof (type_instance<T> () <= type_instance<T2> ())); - query q (name_); + query q (name_, query::clause_part::column); q += "<="; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -1017,9 +1035,9 @@ namespace odb // (void) (sizeof (type_instance<T> () >= type_instance<T2> ())); - query q (name_); + query q (name_, query::clause_part::column); q += ">="; - q += c.name (); + q.append (c.name (), query::clause_part::column); return q; } @@ -1222,6 +1240,13 @@ namespace odb : object_traits<T>::query_type (qc) { } + + std::string + clause () const + { + return object_traits<T>::query_type::clause ( + object_traits<T>::table_name); + } }; } diff --git a/odb/sqlite/query.txx b/odb/sqlite/query.txx index 3144348..6af80e7 100644 --- a/odb/sqlite/query.txx +++ b/odb/sqlite/query.txx @@ -13,13 +13,14 @@ namespace odb template <database_type_id ID> query:: query (const query_column<bool, ID>& c) - : clause_ (c.name ()), - parameters_ (new (details::shared) query_params) + : parameters_ (new (details::shared) query_params) { + clause_.push_back (clause_part (clause_part::column, c.name ())); + // Cannot use IS TRUE here since database type can be a non- // integral type. // - clause_ += " = "; + clause_.push_back (clause_part (clause_part::native, "=")); append<bool, ID> (val_bind<bool> (true)); } @@ -29,7 +30,7 @@ namespace odb query query_column<T, ID>:: in (const T& v1, const T& v2) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; q.append<T, ID> (val_bind<T> (v1)); q += ","; @@ -42,7 +43,7 @@ namespace odb query query_column<T, ID>:: in (const T& v1, const T& v2, const T& v3) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; q.append<T, ID> (val_bind<T> (v1)); q += ","; @@ -57,7 +58,7 @@ namespace odb query query_column<T, ID>:: in (const T& v1, const T& v2, const T& v3, const T& v4) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; q.append<T, ID> (val_bind<T> (v1)); q += ","; @@ -74,7 +75,7 @@ namespace odb query query_column<T, ID>:: in (const T& v1, const T& v2, const T& v3, const T& v4, const T& v5) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; q.append<T, ID> (val_bind<T> (v1)); q += ","; @@ -94,7 +95,7 @@ namespace odb query query_column<T, ID>:: in_range (I begin, I end) const { - query q (name_); + query q (name_, query::clause_part::column); q += "IN ("; for (I i (begin); i != end; ++i) |