db_filters.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2009, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 // Author(s): Lorenz Moesenlechner
00036 
00037 #ifndef _DB_FILTERS_H_
00038 #define _DB_FILTERS_H_
00039 
00040 #include <boost/lexical_cast.hpp>
00041 #include <boost/format.hpp>
00042 
00043 #include <string>
00044 
00045 #include "database_interface/db_field.h"
00046 
00058 namespace database_interface
00059 {
00060 
00061 template<typename T>
00062 inline std::string toString(const T &data)
00063 {
00064   return boost::lexical_cast<std::string>(data);
00065 }
00066 
00067 template<>
00068 inline std::string toString<double>(const double &data)
00069 {
00070   return (boost::format("%.5f") % data).str();
00071 }
00072 
00073 struct FilterClause
00074 {
00075   std::string clause_;
00076 
00077   FilterClause() {}
00078   FilterClause(const std::string clause)
00079     : clause_(clause) {}
00080 };
00081 
00082 struct dbField
00083 {
00084   std::string name_;
00085 
00086   dbField(const std::string name)
00087     : name_(name) {}
00088 };
00089 
00090 // All operators return a FilterClause that contains a string
00091 // representation of the specific operation. To extend it, you need
00092 // to implement an operator for the classes DBField<T> and dbField
00093 // in all different possible combinations. To get a string
00094 // representation of the arbitrary datatype T, boost::lexical_cast
00095 // is used.
00096   
00097 // Operator <
00098 template<typename T>
00099 FilterClause operator<(const DBField<T> &lhs, const T &rhs)
00100 {
00101   return FilterClause(lhs.getName() + " < '" + toString(rhs) + "'");
00102 }
00103     
00104 template<typename T>
00105 FilterClause operator<(const T &lhs, const DBField<T> &rhs)
00106 {
00107   return FilterClause("'" + toString(lhs) + "' < " + rhs.getName());
00108 }
00109 
00110 template<typename T>
00111 FilterClause operator<(const DBField<T> &lhs, const DBField<T> &rhs)
00112 {
00113   return FilterClause(lhs.getName() + " < " + rhs.getName());
00114 }
00115 
00116 template<typename T>
00117 FilterClause operator<(const dbField &lhs, const T &rhs)
00118 {
00119   return FilterClause(lhs.name_ + " < '" + toString(rhs) + "'");
00120 }
00121 
00122 template<typename T>
00123 FilterClause operator<(const T &lhs, const dbField &rhs)
00124 {
00125   return FilterClause("'" + toString(lhs) + "' < " + rhs.name_);
00126 }
00127 
00128 template<typename T>
00129 FilterClause operator<(const dbField &lhs, const dbField &rhs)
00130 {
00131   return FilterClause(lhs.name_ + " < " + rhs.name_);
00132 }
00133 
00134 // Opetrator <=
00135 template<typename T>
00136 FilterClause operator<=(const DBField<T> &lhs, const T &rhs)
00137 {
00138   return FilterClause(lhs.getName() + " <= '" + toString(rhs) + "'");
00139 }
00140     
00141 template<typename T>
00142 FilterClause operator<=(const T &lhs, const DBField<T> &rhs)
00143 {
00144   return FilterClause("'" + toString(lhs) + "' <= " + rhs.getName());
00145 }
00146 
00147 template<typename T>
00148 FilterClause operator<=(const DBField<T> &lhs, const DBField<T> &rhs)
00149 {
00150   return FilterClause(lhs.getName() + " <= " + rhs.getName());
00151 }
00152 
00153 template<typename T>
00154 FilterClause operator<=(const dbField &lhs, const T &rhs)
00155 {
00156   return FilterClause(lhs.name_ + " <= '" + toString(rhs) + "'");
00157 }
00158 
00159 template<typename T>
00160 FilterClause operator<=(const T &lhs, const dbField &rhs)
00161 {
00162   return FilterClause("'" + toString(lhs) + "' <= " + rhs.name_);
00163 }
00164 
00165 template<typename T>
00166 FilterClause operator<=(const dbField &lhs, const dbField &rhs)
00167 {
00168   return FilterClause(lhs.name_ + " <= " + rhs.name_);
00169 }
00170 
00171 // Operator >
00172 template<typename T>
00173 FilterClause operator>(const DBField<T> &lhs, const T &rhs)
00174 {
00175   return FilterClause(lhs.getName() + " > '" + toString(rhs) + "'");
00176 }
00177     
00178 template<typename T>
00179 FilterClause operator>(const T &lhs, const DBField<T> &rhs)
00180 {
00181   return FilterClause("'" + toString(lhs) + "' > " + rhs.getName());
00182 }
00183 
00184 template<typename T>
00185 FilterClause operator>(const DBField<T> &lhs, const DBField<T> &rhs)
00186 {
00187   return FilterClause(lhs.getName() + " > " + rhs.getName());
00188 }
00189 
00190 template<typename T>
00191 FilterClause operator>(const dbField &lhs, const T &rhs)
00192 {
00193   return FilterClause(lhs.name_ + " > '" + toString(rhs) + "'");
00194 }
00195 
00196 template<typename T>
00197 FilterClause operator>(const T &lhs, const dbField &rhs)
00198 {
00199   return FilterClause("'" + toString(lhs) + "' > " + rhs.name_);
00200 }
00201 
00202 template<typename T>
00203 FilterClause operator>(const dbField &lhs, const dbField &rhs)
00204 {
00205   return FilterClause(lhs.name_ + " > " + rhs.name_);
00206 }
00207 
00208 // Opetrator >=
00209 template<typename T>
00210 FilterClause operator>=(const DBField<T> &lhs, const T &rhs)
00211 {
00212   return FilterClause(lhs.getName() + " >= '" + toString(rhs) + "'");
00213 }
00214     
00215 template<typename T>
00216 FilterClause operator>=(const T &lhs, const DBField<T> &rhs)
00217 {
00218   return FilterClause("'" + toString(lhs) + "' >= " + rhs.getName());
00219 }
00220 
00221 template<typename T>
00222 FilterClause operator>=(const DBField<T> &lhs, const DBField<T> &rhs)
00223 {
00224   return FilterClause(lhs.getName() + " >= " + rhs.getName());
00225 }
00226 
00227 template<typename T>
00228 FilterClause operator>=(const dbField &lhs, const T &rhs)
00229 {
00230   return FilterClause(lhs.name_ + " >= '" + toString(rhs) + "'");
00231 }
00232 
00233 template<typename T>
00234 FilterClause operator>=(const T &lhs, const dbField &rhs)
00235 {
00236   return FilterClause("'" + toString(lhs) + "' >= " + rhs.name_);
00237 }
00238 
00239 template<typename T>
00240 FilterClause operator>=(const dbField &lhs, const dbField &rhs)
00241 {
00242   return FilterClause(lhs.name_ + " >= " + rhs.name_);
00243 }
00244 
00245 // Operator ==
00246 template<typename T>
00247 FilterClause operator==(const DBField<T> &lhs, const T &rhs)
00248 {
00249   return FilterClause(lhs.getName() + " == '" + toString(rhs) + "'");
00250 }
00251     
00252 template<typename T>
00253 FilterClause operator==(const T &lhs, const DBField<T> &rhs)
00254 {
00255   return FilterClause("'" + toString(lhs) + "' == " + rhs.getName());
00256 }
00257 
00258 template<typename T>
00259 FilterClause operator==(const DBField<T> &lhs, const DBField<T> &rhs)
00260 {
00261   return FilterClause(lhs.getName() + " == " + rhs.getName());
00262 }
00263 
00264 template<typename T>
00265 FilterClause operator==(const dbField &lhs, const T &rhs)
00266 {
00267   return FilterClause(lhs.name_ + " == '" + toString(rhs) + "'");
00268 }
00269 
00270 template<typename T>
00271 FilterClause operator==(const T &lhs, const dbField &rhs)
00272 {
00273   return FilterClause("'" + toString(lhs) + "' == " + rhs.name_);
00274 }
00275 
00276 template<typename T>
00277 FilterClause operator==(const dbField &lhs, const dbField &rhs)
00278 {
00279   return FilterClause(lhs.name_ + " == " + rhs.name_);
00280 }
00281 
00282 // Operator !=
00283 template<typename T>
00284 FilterClause operator!=(const DBField<T> &lhs, const T &rhs)
00285 {
00286   return FilterClause(lhs.getName() + " != '" + toString(rhs) + "'");
00287 }
00288 
00289 template<typename T>
00290 FilterClause operator!=(const T &lhs, const DBField<T> &rhs)
00291 {
00292   return FilterClause("'" + toString(lhs) + "' != " + rhs.getName());
00293 }
00294 
00295 template<typename T>
00296 FilterClause operator!=(const DBField<T> &lhs, const DBField<T> &rhs)
00297 {
00298   return FilterClause(lhs.getName() + " != " + rhs.getName());
00299 }
00300 
00301 template<typename T>
00302 FilterClause operator!=(const dbField &lhs, const T &rhs)
00303 {
00304   return FilterClause(lhs.name_ + " != '" + toString(rhs) + "'");
00305 }
00306 
00307 template<typename T>
00308 FilterClause operator!=(const T &lhs, const dbField &rhs)
00309 {
00310   return FilterClause("'" + toString(lhs) + "' != " + rhs.name_);
00311 }
00312 
00313 template<typename T>
00314 FilterClause operator!=(const dbField &lhs, const dbField &rhs)
00315 {
00316   return FilterClause(lhs.name_ + " != " + rhs.name_);
00317 }
00318 
00319 // Combination clauses (and, or, ...)
00320 inline FilterClause operator&&(const FilterClause &lhs, const FilterClause &rhs)
00321 {
00322   return FilterClause(" ( " + lhs.clause_ + " AND " + rhs.clause_ + " )");
00323 }
00324 
00325 inline FilterClause operator||(const FilterClause &lhs, const FilterClause &rhs)
00326 {
00327   return FilterClause(" ( " + lhs.clause_ + " OR " + rhs.clause_ + " )");
00328 }
00329 
00330 } //namespace
00331 
00332 #endif


sql_database
Author(s): Matei Ciocarlie and Lorenz Mosenlechner
autogenerated on Fri Aug 28 2015 13:11:16