diff options
Diffstat (limited to 'lib/statement.h')
| -rw-r--r-- | lib/statement.h | 130 |
1 files changed, 65 insertions, 65 deletions
| diff --git a/lib/statement.h b/lib/statement.h index 8188ec0..aa56568 100644 --- a/lib/statement.h +++ b/lib/statement.h | |||
| @@ -11,23 +11,23 @@ | |||
| 11 | #include "filter.h" | 11 | #include "filter.h" |
| 12 | 12 | ||
| 13 | namespace verbly { | 13 | namespace verbly { |
| 14 | 14 | ||
| 15 | class filter; | 15 | class filter; |
| 16 | 16 | ||
| 17 | class statement { | 17 | class statement { |
| 18 | public: | 18 | public: |
| 19 | 19 | ||
| 20 | statement(object context, filter queryFilter); | 20 | statement(object context, filter queryFilter); |
| 21 | 21 | ||
| 22 | std::string getQueryString(std::list<std::string> select, bool random, int limit) const; | 22 | std::string getQueryString(std::list<std::string> select, bool random, int limit) const; |
| 23 | 23 | ||
| 24 | std::list<binding> getBindings() const; | 24 | std::list<binding> getBindings() const; |
| 25 | 25 | ||
| 26 | private: | 26 | private: |
| 27 | 27 | ||
| 28 | class join { | 28 | class join { |
| 29 | public: | 29 | public: |
| 30 | 30 | ||
| 31 | join( | 31 | join( |
| 32 | bool outer, | 32 | bool outer, |
| 33 | std::string foreignTableName, | 33 | std::string foreignTableName, |
| @@ -43,37 +43,37 @@ namespace verbly { | |||
| 43 | foreignColumn_(std::move(foreignColumn)) | 43 | foreignColumn_(std::move(foreignColumn)) |
| 44 | { | 44 | { |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | bool isOuterJoin() const | 47 | bool isOuterJoin() const |
| 48 | { | 48 | { |
| 49 | return outer_; | 49 | return outer_; |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | const std::string& getForeignTableName() const | 52 | const std::string& getForeignTableName() const |
| 53 | { | 53 | { |
| 54 | return foreignTableName_; | 54 | return foreignTableName_; |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | const std::string& getJoinTable() const | 57 | const std::string& getJoinTable() const |
| 58 | { | 58 | { |
| 59 | return joinTable_; | 59 | return joinTable_; |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | const std::string& getJoinColumn() const | 62 | const std::string& getJoinColumn() const |
| 63 | { | 63 | { |
| 64 | return joinColumn_; | 64 | return joinColumn_; |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | const std::string& getForeignTable() const | 67 | const std::string& getForeignTable() const |
| 68 | { | 68 | { |
| 69 | return foreignTable_; | 69 | return foreignTable_; |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | const std::string& getForeignColumn() const | 72 | const std::string& getForeignColumn() const |
| 73 | { | 73 | { |
| 74 | return foreignColumn_; | 74 | return foreignColumn_; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | private: | 77 | private: |
| 78 | bool outer_ = false; | 78 | bool outer_ = false; |
| 79 | std::string foreignTableName_; | 79 | std::string foreignTableName_; |
| @@ -81,11 +81,11 @@ namespace verbly { | |||
| 81 | std::string joinColumn_; | 81 | std::string joinColumn_; |
| 82 | std::string foreignTable_; | 82 | std::string foreignTable_; |
| 83 | std::string foreignColumn_; | 83 | std::string foreignColumn_; |
| 84 | 84 | ||
| 85 | }; | 85 | }; |
| 86 | 86 | ||
| 87 | friend std::ostream& operator<<(std::ostream& oss, const join& j); | 87 | friend std::ostream& operator<<(std::ostream& oss, const join& j); |
| 88 | 88 | ||
| 89 | class condition { | 89 | class condition { |
| 90 | public: | 90 | public: |
| 91 | enum class type { | 91 | enum class type { |
| @@ -93,7 +93,7 @@ namespace verbly { | |||
| 93 | singleton, | 93 | singleton, |
| 94 | group | 94 | group |
| 95 | }; | 95 | }; |
| 96 | 96 | ||
| 97 | enum class comparison { | 97 | enum class comparison { |
| 98 | equals, | 98 | equals, |
| 99 | does_not_equal, | 99 | does_not_equal, |
| @@ -106,57 +106,57 @@ namespace verbly { | |||
| 106 | is_not_null, | 106 | is_not_null, |
| 107 | is_null | 107 | is_null |
| 108 | }; | 108 | }; |
| 109 | 109 | ||
| 110 | // Copy and move constructors | 110 | // Copy and move constructors |
| 111 | 111 | ||
| 112 | condition(const condition& other); | 112 | condition(const condition& other); |
| 113 | condition(condition&& other); | 113 | condition(condition&& other); |
| 114 | 114 | ||
| 115 | // Assignment | 115 | // Assignment |
| 116 | 116 | ||
| 117 | condition& operator=(condition other); | 117 | condition& operator=(condition other); |
| 118 | 118 | ||
| 119 | // Swap | 119 | // Swap |
| 120 | 120 | ||
| 121 | friend void swap(condition& first, condition& second); | 121 | friend void swap(condition& first, condition& second); |
| 122 | 122 | ||
| 123 | // Destructor | 123 | // Destructor |
| 124 | 124 | ||
| 125 | ~condition(); | 125 | ~condition(); |
| 126 | 126 | ||
| 127 | // Accessors | 127 | // Accessors |
| 128 | 128 | ||
| 129 | type getType() const | 129 | type getType() const |
| 130 | { | 130 | { |
| 131 | return type_; | 131 | return type_; |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | // Empty | 134 | // Empty |
| 135 | 135 | ||
| 136 | condition(); | 136 | condition(); |
| 137 | 137 | ||
| 138 | // Singleton | 138 | // Singleton |
| 139 | 139 | ||
| 140 | condition(std::string table, std::string column, bool isNull); | 140 | condition(std::string table, std::string column, bool isNull); |
| 141 | 141 | ||
| 142 | condition(std::string table, std::string column, comparison comp, binding value); | 142 | condition(std::string table, std::string column, comparison comp, binding value); |
| 143 | 143 | ||
| 144 | // Group | 144 | // Group |
| 145 | 145 | ||
| 146 | explicit condition(bool orlogic); | 146 | explicit condition(bool orlogic); |
| 147 | 147 | ||
| 148 | condition& operator+=(condition n); | 148 | condition& operator+=(condition n); |
| 149 | 149 | ||
| 150 | condition& operator&=(condition n); | 150 | condition& operator&=(condition n); |
| 151 | 151 | ||
| 152 | const std::list<condition>& getChildren() const; | 152 | const std::list<condition>& getChildren() const; |
| 153 | 153 | ||
| 154 | // Utility | 154 | // Utility |
| 155 | 155 | ||
| 156 | std::string toSql() const; | 156 | std::string toSql() const; |
| 157 | 157 | ||
| 158 | std::list<binding> flattenBindings() const; | 158 | std::list<binding> flattenBindings() const; |
| 159 | 159 | ||
| 160 | private: | 160 | private: |
| 161 | union { | 161 | union { |
| 162 | struct { | 162 | struct { |
| @@ -172,12 +172,12 @@ namespace verbly { | |||
| 172 | }; | 172 | }; |
| 173 | type type_; | 173 | type type_; |
| 174 | }; | 174 | }; |
| 175 | 175 | ||
| 176 | friend void swap(condition& first, condition& second); | 176 | friend void swap(condition& first, condition& second); |
| 177 | 177 | ||
| 178 | class with { | 178 | class with { |
| 179 | public: | 179 | public: |
| 180 | 180 | ||
| 181 | with( | 181 | with( |
| 182 | std::string identifier, | 182 | std::string identifier, |
| 183 | field f, | 183 | field f, |
| @@ -195,42 +195,42 @@ namespace verbly { | |||
| 195 | recursive_(recursive) | 195 | recursive_(recursive) |
| 196 | { | 196 | { |
| 197 | } | 197 | } |
| 198 | 198 | ||
| 199 | const std::string& getIdentifier() const | 199 | const std::string& getIdentifier() const |
| 200 | { | 200 | { |
| 201 | return identifier_; | 201 | return identifier_; |
| 202 | } | 202 | } |
| 203 | 203 | ||
| 204 | field getField() const | 204 | field getField() const |
| 205 | { | 205 | { |
| 206 | return field_; | 206 | return field_; |
| 207 | } | 207 | } |
| 208 | 208 | ||
| 209 | std::string getTableForId(std::string identifier) const | 209 | std::string getTableForId(std::string identifier) const |
| 210 | { | 210 | { |
| 211 | return tables_.at(identifier); | 211 | return tables_.at(identifier); |
| 212 | } | 212 | } |
| 213 | 213 | ||
| 214 | const std::string& getTopTable() const | 214 | const std::string& getTopTable() const |
| 215 | { | 215 | { |
| 216 | return topTable_; | 216 | return topTable_; |
| 217 | } | 217 | } |
| 218 | 218 | ||
| 219 | const condition& getCondition() const | 219 | const condition& getCondition() const |
| 220 | { | 220 | { |
| 221 | return topCondition_; | 221 | return topCondition_; |
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | const std::list<join>& getJoins() const | 224 | const std::list<join>& getJoins() const |
| 225 | { | 225 | { |
| 226 | return joins_; | 226 | return joins_; |
| 227 | } | 227 | } |
| 228 | 228 | ||
| 229 | bool isRecursive() const | 229 | bool isRecursive() const |
| 230 | { | 230 | { |
| 231 | return recursive_; | 231 | return recursive_; |
| 232 | } | 232 | } |
| 233 | 233 | ||
| 234 | private: | 234 | private: |
| 235 | std::string identifier_; | 235 | std::string identifier_; |
| 236 | field field_; | 236 | field field_; |
| @@ -239,9 +239,9 @@ namespace verbly { | |||
| 239 | condition topCondition_; | 239 | condition topCondition_; |
| 240 | std::list<join> joins_; | 240 | std::list<join> joins_; |
| 241 | bool recursive_; | 241 | bool recursive_; |
| 242 | 242 | ||
| 243 | }; | 243 | }; |
| 244 | 244 | ||
| 245 | static constexpr const char* getTableForContext(object context) | 245 | static constexpr const char* getTableForContext(object context) |
| 246 | { | 246 | { |
| 247 | return (context == object::notion) ? "notions" | 247 | return (context == object::notion) ? "notions" |
| @@ -253,22 +253,22 @@ namespace verbly { | |||
| 253 | : (context == object::pronunciation) ? "pronunciations" | 253 | : (context == object::pronunciation) ? "pronunciations" |
| 254 | : throw std::domain_error("Provided context has no associated table"); | 254 | : throw std::domain_error("Provided context has no associated table"); |
| 255 | } | 255 | } |
| 256 | 256 | ||
| 257 | static const std::list<field> getSelectForContext(object context); | 257 | static const std::list<field> getSelectForContext(object context); |
| 258 | 258 | ||
| 259 | statement(std::string tableName, filter clause, int nextTableId = 0, int nextWithId = 0); | 259 | statement(std::string tableName, filter clause, int nextTableId = 0, int nextWithId = 0); |
| 260 | 260 | ||
| 261 | condition parseFilter(filter queryFilter); | 261 | condition parseFilter(filter queryFilter); |
| 262 | 262 | ||
| 263 | std::string instantiateTable(std::string name); | 263 | std::string instantiateTable(std::string name); |
| 264 | 264 | ||
| 265 | std::string instantiateWith(std::string name); | 265 | std::string instantiateWith(std::string name); |
| 266 | 266 | ||
| 267 | condition integrate(statement subStmt, bool cte = false); | 267 | condition integrate(statement subStmt, bool cte = false); |
| 268 | 268 | ||
| 269 | int nextTableId_; | 269 | int nextTableId_; |
| 270 | int nextWithId_; | 270 | int nextWithId_; |
| 271 | 271 | ||
| 272 | std::map<std::string, std::string> tables_; | 272 | std::map<std::string, std::string> tables_; |
| 273 | std::string topTable_; | 273 | std::string topTable_; |
| 274 | std::list<join> joins_; | 274 | std::list<join> joins_; |
| @@ -276,7 +276,7 @@ namespace verbly { | |||
| 276 | condition topCondition_; | 276 | condition topCondition_; |
| 277 | 277 | ||
| 278 | }; | 278 | }; |
| 279 | 279 | ||
| 280 | }; | 280 | }; |
| 281 | 281 | ||
| 282 | #endif /* end of include guard: STATEMENT_H_29F51659 */ | 282 | #endif /* end of include guard: STATEMENT_H_29F51659 */ |
