00001 #ifndef MYSQLPP_COMPARE_H
00002 #define MYSQLPP_COMPARE_H
00003
00006
00007 #include "row.h"
00008
00009 #include <cstring>
00010 #include <functional>
00011
00012 namespace mysqlpp {
00013
00015
00016 template <class BinaryPred, class CmpType>
00017 class MysqlCmp : public std::unary_function<const Row&, bool>
00018 {
00019 protected:
00020 unsigned int index;
00021 BinaryPred func;
00022 CmpType cmp2;
00023 public:
00024 MysqlCmp(uint i, const BinaryPred &f, const CmpType &c) : index(i),func(f),cmp2(c) {}
00025 bool operator () (const Row& cmp1) const {return func(cmp2,cmp1[this->index]);}
00026 };
00027
00028
00030
00031 template <class BinaryPred>
00032 class MysqlCmpCStr : public MysqlCmp<BinaryPred, const char *>
00033 {
00034 public:
00035 MysqlCmpCStr(uint i, const BinaryPred &f, const char* c) : MysqlCmp<BinaryPred, const char *> (i,f,c) {}
00036 bool operator () (const Row& cmp1) const
00037 {return MysqlCmp<BinaryPred, const char*>::func(
00038 MysqlCmp<BinaryPred, const char*>::cmp2, cmp1[this->index]);}
00039 };
00040
00055 template <class BinaryPred, class CmpType>
00056 MysqlCmp <BinaryPred, CmpType>
00057 mysql_cmp(uint i, const BinaryPred &func, const CmpType &cmp2)
00058 {
00059 return MysqlCmp<BinaryPred, CmpType>(i, func, cmp2);
00060 }
00061
00062 typedef std::binary_function<const char*, const char*, bool> bin_char_pred;
00063
00067 struct cstr_equal_to : bin_char_pred {
00068 bool operator () (const char *x, const char *y) const
00069 {return !std::strcmp(x,y);}
00070 };
00071
00075 struct cstr_not_equal_to : bin_char_pred {
00076 bool operator () (const char *x, const char *y) const
00077 {return std::strcmp(x,y) != 0;}
00078 };
00079
00083 struct cstr_less : bin_char_pred {
00084 bool operator () (const char *x, const char *y) const
00085 {return std::strcmp(x,y) > 0; }
00086 };
00087
00091 struct cstr_less_equal : bin_char_pred {
00092 bool operator () (const char *x, const char *y) const
00093 {return std::strcmp(x,y) >= 0; }
00094 };
00095
00099 struct cstr_greater : bin_char_pred {
00100 bool operator () (const char *x, const char *y) const
00101 {return std::strcmp(x,y) < 0; }
00102 };
00103
00107 struct cstr_greater_equal : bin_char_pred {
00108 bool operator () (const char *x, const char *y) const
00109 {return std::strcmp(x,y) <= 0; }
00110 };
00111
00122 template <class BinaryPred>
00123 MysqlCmpCStr <BinaryPred>
00124 mysql_cmp_cstr (uint i, const BinaryPred &func, const char *cmp2) {
00125 return MysqlCmpCStr<BinaryPred>(i, func, cmp2);
00126 }
00127
00128 }
00129
00130 #endif
00131