Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::query_builder Class Reference

Universal query builder that adapts to different database types. More...

#include <query_builder.h>

Collaboration diagram for database::query_builder:
Collaboration graph

Public Member Functions

 query_builder (database_types db_type=database_types::none)
 
 ~query_builder ()=default
 
 query_builder (query_builder &&) noexcept=default
 
query_builderoperator= (query_builder &&) noexcept=default
 
 query_builder (const query_builder &)=delete
 
query_builderoperator= (const query_builder &)=delete
 
query_builderfor_database (database_types db_type)
 
query_builderselect (const std::vector< std::string > &columns)
 
query_builderfrom (const std::string &table)
 
query_builderwhere (const std::string &field, const std::string &op, const core::database_value &value)
 
query_builderwhere (const query_condition &condition)
 
query_builderjoin (const std::string &table, const std::string &condition, join_type type=join_type::inner)
 
query_builderorder_by (const std::string &column, sort_order order=sort_order::asc)
 
query_buildergroup_by (const std::vector< std::string > &columns)
 
query_buildergroup_by (const std::string &column)
 
query_builderhaving (const std::string &condition)
 
query_builderlimit (size_t count)
 
query_builderoffset (size_t count)
 
query_builderinsert_into (const std::string &table)
 
query_buildervalues (const std::map< std::string, core::database_value > &data)
 
query_buildervalues (const std::vector< std::map< std::string, core::database_value > > &rows)
 
query_builderupdate (const std::string &table)
 
query_builderset (const std::string &field, const core::database_value &value)
 
query_builderset (const std::map< std::string, core::database_value > &data)
 
query_builderdelete_from (const std::string &table)
 
query_buildercollection (const std::string &name)
 
query_builderkey (const std::string &key)
 
std::string build () const
 
core::database_result execute (core::database_backend *db) const
 
void reset ()
 
database_types get_database_type () const
 

Private Member Functions

void ensure_dialect ()
 

Private Attributes

database_types db_type_
 
std::unique_ptr< query_dialectdialect_
 

Detailed Description

Universal query builder that adapts to different database types.

This class provides a unified interface for building queries across different database backends (PostgreSQL, SQLite, MongoDB, Redis) using the Strategy pattern.

Thread Safety

  • NOT thread-safe. Each thread should use its own instance.
  • Create separate builders for each thread or protect with external mutex.

Memory Efficiency

  • Only allocates ONE dialect instance per builder.
  • Dialect is lazily initialized when first needed.

Example Usage

// SQL query
auto query = builder
.select({"id", "name"})
.from("users")
.where("active", "=", true)
.order_by("name")
.limit(10)
.build();
// Switch database type
builder.for_database(database_types::sqlite);
auto sqlite_query = builder.select({"*"}).from("users").build();
Universal query builder that adapts to different database types.
std::string build() const
query_builder & where(const std::string &field, const std::string &op, const core::database_value &value)
query_builder & limit(size_t count)
query_builder & from(const std::string &table)
query_builder & order_by(const std::string &column, sort_order order=sort_order::asc)
@ sqlite
Indicates a SQLite database.
@ postgres
Indicates a PostgreSQL database.

Definition at line 104 of file query_builder.h.

Constructor & Destructor Documentation

◆ query_builder() [1/3]

database::query_builder::query_builder ( database_types db_type = database_types::none)
explicit

Definition at line 163 of file query_builder.cpp.

164 : db_type_(db_type)
165 {
167 }

References ensure_dialect().

Here is the call graph for this function:

◆ ~query_builder()

database::query_builder::~query_builder ( )
default

◆ query_builder() [2/3]

database::query_builder::query_builder ( query_builder && )
defaultnoexcept

◆ query_builder() [3/3]

database::query_builder::query_builder ( const query_builder & )
delete

Member Function Documentation

◆ build()

◆ collection()

query_builder & database::query_builder::collection ( const std::string & name)

Definition at line 339 of file query_builder.cpp.

340 {
342 if (dialect_) {
343 dialect_->set_collection(name);
344 }
345 return *this;
346 }

References dialect_, and ensure_dialect().

Here is the call graph for this function:

◆ delete_from()

query_builder & database::query_builder::delete_from ( const std::string & table)

Definition at line 330 of file query_builder.cpp.

331 {
333 if (dialect_) {
334 dialect_->set_delete_table(table);
335 }
336 return *this;
337 }

References dialect_, and ensure_dialect().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ensure_dialect()

void database::query_builder::ensure_dialect ( )
private

Definition at line 395 of file query_builder.cpp.

396 {
399 }
400 }
std::unique_ptr< query_dialect > create_dialect(database_types type)
Factory function to create appropriate dialect for database type.
@ none
No specific database type is set.

References database::create_dialect(), db_type_, dialect_, and database::none.

Referenced by collection(), delete_from(), for_database(), from(), group_by(), having(), insert_into(), join(), key(), limit(), offset(), order_by(), query_builder(), select(), set(), set(), update(), values(), values(), where(), and where().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute()

core::database_result database::query_builder::execute ( core::database_backend * db) const

Definition at line 365 of file query_builder.cpp.

366 {
367 if (!db) {
368 return {};
369 }
370
371 std::string query = build();
372 if (query.empty()) {
373 return {};
374 }
375
376 auto result = db->select_query(query);
377 if (result.is_err()) {
378 return {};
379 }
380 return result.value();
381 }

References build(), and database::core::database_backend::select_query().

Here is the call graph for this function:

◆ for_database()

query_builder & database::query_builder::for_database ( database_types db_type)

Definition at line 169 of file query_builder.cpp.

170 {
171 if (db_type_ != db_type) {
172 db_type_ = db_type;
173 dialect_.reset();
175 }
176 return *this;
177 }

References db_type_, dialect_, and ensure_dialect().

Referenced by main(), database::tests::TEST_F(), database::tests::TEST_F(), and database::tests::TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ from()

query_builder & database::query_builder::from ( const std::string & table)

Definition at line 188 of file query_builder.cpp.

189 {
191 if (dialect_) {
192 dialect_->set_from_table(table);
193 }
194 return *this;
195 }

References dialect_, and ensure_dialect().

Referenced by database::tests::TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_database_type()

database_types database::query_builder::get_database_type ( ) const

Definition at line 390 of file query_builder.cpp.

391 {
392 return db_type_;
393 }

References db_type_.

◆ group_by() [1/2]

query_builder & database::query_builder::group_by ( const std::string & column)

Definition at line 242 of file query_builder.cpp.

243 {
244 return group_by(std::vector<std::string>{column});
245 }
query_builder & group_by(const std::vector< std::string > &columns)

References group_by().

Here is the call graph for this function:

◆ group_by() [2/2]

query_builder & database::query_builder::group_by ( const std::vector< std::string > & columns)

Definition at line 233 of file query_builder.cpp.

234 {
236 if (dialect_) {
237 dialect_->set_group_by(columns);
238 }
239 return *this;
240 }

References dialect_, and ensure_dialect().

Referenced by group_by(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ having()

query_builder & database::query_builder::having ( const std::string & condition)

Definition at line 247 of file query_builder.cpp.

248 {
250 if (dialect_) {
251 dialect_->set_having(condition);
252 }
253 return *this;
254 }

References dialect_, and ensure_dialect().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ insert_into()

query_builder & database::query_builder::insert_into ( const std::string & table)

Definition at line 274 of file query_builder.cpp.

275 {
277 if (dialect_) {
278 dialect_->set_insert_table(table);
279 }
280 return *this;
281 }

References dialect_, and ensure_dialect().

Referenced by main(), and database::tests::TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ join()

query_builder & database::query_builder::join ( const std::string & table,
const std::string & condition,
join_type type = join_type::inner )

Definition at line 215 of file query_builder.cpp.

216 {
218 if (dialect_) {
219 dialect_->add_join(table, condition, type);
220 }
221 return *this;
222 }

References dialect_, and ensure_dialect().

Referenced by main(), and database::tests::TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ key()

query_builder & database::query_builder::key ( const std::string & key)

Definition at line 348 of file query_builder.cpp.

349 {
351 if (dialect_) {
352 dialect_->set_key(key);
353 }
354 return *this;
355 }
query_builder & key(const std::string &key)

References dialect_, ensure_dialect(), and key().

Referenced by key().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ limit()

query_builder & database::query_builder::limit ( size_t count)

Definition at line 256 of file query_builder.cpp.

257 {
259 if (dialect_) {
260 dialect_->set_limit(count);
261 }
262 return *this;
263 }

References dialect_, and ensure_dialect().

Referenced by main(), database::tests::TEST_F(), database::tests::TEST_F(), database::tests::TEST_F(), database::tests::TEST_F(), database::tests::TEST_F(), database::tests::TEST_F(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ offset()

query_builder & database::query_builder::offset ( size_t count)

Definition at line 265 of file query_builder.cpp.

266 {
268 if (dialect_) {
269 dialect_->set_offset(count);
270 }
271 return *this;
272 }

References dialect_, and ensure_dialect().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [1/2]

query_builder & database::query_builder::operator= ( const query_builder & )
delete

◆ operator=() [2/2]

query_builder & database::query_builder::operator= ( query_builder && )
defaultnoexcept

◆ order_by()

query_builder & database::query_builder::order_by ( const std::string & column,
sort_order order = sort_order::asc )

Definition at line 224 of file query_builder.cpp.

225 {
227 if (dialect_) {
228 dialect_->add_order_by(column, order);
229 }
230 return *this;
231 }

References dialect_, and ensure_dialect().

Referenced by main(), database::tests::TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

void database::query_builder::reset ( )

Definition at line 383 of file query_builder.cpp.

384 {
385 if (dialect_) {
386 dialect_->reset();
387 }
388 }

References dialect_.

Referenced by main(), database::tests::TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ select()

query_builder & database::query_builder::select ( const std::vector< std::string > & columns)

◆ set() [1/2]

query_builder & database::query_builder::set ( const std::map< std::string, core::database_value > & data)

Definition at line 321 of file query_builder.cpp.

322 {
324 if (dialect_) {
325 dialect_->set_update_data(data);
326 }
327 return *this;
328 }

References dialect_, and ensure_dialect().

Here is the call graph for this function:

◆ set() [2/2]

query_builder & database::query_builder::set ( const std::string & field,
const core::database_value & value )

Definition at line 310 of file query_builder.cpp.

311 {
313 if (dialect_) {
314 std::map<std::string, core::database_value> data;
315 data[field] = value;
316 dialect_->set_update_data(data);
317 }
318 return *this;
319 }

References dialect_, and ensure_dialect().

Referenced by main(), and database::tests::TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update()

query_builder & database::query_builder::update ( const std::string & table)

Definition at line 301 of file query_builder.cpp.

302 {
304 if (dialect_) {
305 dialect_->set_update_table(table);
306 }
307 return *this;
308 }

References dialect_, and ensure_dialect().

Referenced by main(), and database::tests::TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ values() [1/2]

query_builder & database::query_builder::values ( const std::map< std::string, core::database_value > & data)

Definition at line 283 of file query_builder.cpp.

284 {
286 if (dialect_) {
287 dialect_->set_insert_data(data);
288 }
289 return *this;
290 }

References dialect_, and ensure_dialect().

Referenced by main(), and database::tests::TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ values() [2/2]

query_builder & database::query_builder::values ( const std::vector< std::map< std::string, core::database_value > > & rows)

Definition at line 292 of file query_builder.cpp.

293 {
295 if (dialect_) {
296 dialect_->set_insert_rows(rows);
297 }
298 return *this;
299 }

References dialect_, and ensure_dialect().

Here is the call graph for this function:

◆ where() [1/2]

query_builder & database::query_builder::where ( const query_condition & condition)

Definition at line 206 of file query_builder.cpp.

207 {
209 if (dialect_) {
210 dialect_->add_where_condition(condition);
211 }
212 return *this;
213 }

References dialect_, and ensure_dialect().

Here is the call graph for this function:

◆ where() [2/2]

query_builder & database::query_builder::where ( const std::string & field,
const std::string & op,
const core::database_value & value )

Definition at line 197 of file query_builder.cpp.

198 {
200 if (dialect_) {
201 dialect_->add_where_condition(field, op, value);
202 }
203 return *this;
204 }

References dialect_, and ensure_dialect().

Referenced by main(), database::tests::TEST_F(), database::tests::TEST_F(), database::tests::TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ db_type_

database_types database::query_builder::db_type_
private

Definition at line 160 of file query_builder.h.

Referenced by ensure_dialect(), for_database(), and get_database_type().

◆ dialect_

std::unique_ptr<query_dialect> database::query_builder::dialect_
private

The documentation for this class was generated from the following files: