Skip to content

Commit f0e2ea0

Browse files
committed
Amended working with table references
* Provided overloads of 'storage' methods and expression factory functions that query tables * Provided a table reference for the SQLite master 'schema' table * Rounded off unit tests
1 parent a1eecad commit f0e2ea0

File tree

8 files changed

+364
-63
lines changed

8 files changed

+364
-63
lines changed

dev/mapped_type_proxy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace sqlite_orm {
1111
namespace internal {
1212

1313
/**
14-
* If T is a recordset alias then the typename mapped_type_proxy<T>::type is the unqualified aliased type,
14+
* If T is a table reference or recordset alias then the typename mapped_type_proxy<T>::type is the unqualified aliased type,
1515
* otherwise unqualified T.
1616
*/
1717
template<class T, class SFINAE = void>

dev/prepared_statement.h

Lines changed: 93 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include <iterator> // std::iterator_traits
66
#include <string> // std::string
77
#include <type_traits> // std::integral_constant, std::declval
8-
#include <utility> // std::pair
8+
#include <utility> // std::move, std::forward, std::pair
9+
#include <tuple> // std::tuple
910

1011
#include "functional/cxx_universal.h"
1112
#include "functional/cxx_type_traits_polyfill.h"
@@ -596,10 +597,21 @@ namespace sqlite_orm {
596597
*/
597598
template<class T, class... Ids>
598599
internal::remove_t<T, Ids...> remove(Ids... ids) {
599-
std::tuple<Ids...> idsTuple{std::forward<Ids>(ids)...};
600-
return {std::move(idsTuple)};
600+
return {{std::forward<Ids>(ids)...}};
601601
}
602602

603+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
604+
/**
605+
* Create a remove statement
606+
* `table` is an explicitly specified table reference of a mapped object to be extracted.
607+
* Usage: remove<user_table>(5);
608+
*/
609+
template<orm_table_reference auto table, class... Ids>
610+
auto remove(Ids... ids) {
611+
return remove<internal::auto_decay_table_ref_t<table>>(std::forward<Ids>(ids)...);
612+
}
613+
#endif
614+
603615
/**
604616
* Create an update statement.
605617
* T is an object type mapped to a storage.
@@ -625,12 +637,12 @@ namespace sqlite_orm {
625637
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
626638
/**
627639
* Create a get statement.
628-
* T is an object type mapped to a storage.
629-
* Usage: get<User>(5);
640+
* `table` is an explicitly specified table reference of a mapped object to be extracted.
641+
* Usage: get<user_table>(5);
630642
*/
631-
template<orm_table_reference auto als, class... Ids>
643+
template<orm_table_reference auto table, class... Ids>
632644
auto get(Ids... ids) {
633-
return get<internal::mapped_type_proxy_t<decltype(als)>>(std::forward<Ids>(ids)...);
645+
return get<internal::auto_decay_table_ref_t<table>>(std::forward<Ids>(ids)...);
634646
}
635647
#endif
636648

@@ -644,6 +656,18 @@ namespace sqlite_orm {
644656
return {{std::forward<Ids>(ids)...}};
645657
}
646658

659+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
660+
/**
661+
* Create a get pointer statement.
662+
* `table` is an explicitly specified table reference of a mapped object to be extracted.
663+
* Usage: get_pointer<user_table>(5);
664+
*/
665+
template<orm_table_reference auto table, class... Ids>
666+
auto get_pointer(Ids... ids) {
667+
return get_pointer<internal::auto_decay_table_ref_t<table>>(std::forward<Ids>(ids)...);
668+
}
669+
#endif
670+
647671
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
648672
/**
649673
* Create a get optional statement.
@@ -656,6 +680,18 @@ namespace sqlite_orm {
656680
}
657681
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
658682

683+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
684+
/**
685+
* Create a get optional statement.
686+
* `table` is an explicitly specified table reference of a mapped object to be extracted.
687+
* Usage: get_optional<user_table>(5);
688+
*/
689+
template<orm_table_reference auto table, class... Ids>
690+
auto get_optional(Ids... ids) {
691+
return get_pointer<internal::auto_decay_table_ref_t<table>>(std::forward<Ids>(ids)...);
692+
}
693+
#endif
694+
659695
/**
660696
* Create a remove all statement.
661697
* T is an object type mapped to a storage.
@@ -665,13 +701,24 @@ namespace sqlite_orm {
665701
internal::remove_all_t<T, Args...> remove_all(Args... args) {
666702
using args_tuple = std::tuple<Args...>;
667703
internal::validate_conditions<args_tuple>();
668-
args_tuple conditions{std::forward<Args>(args)...};
669-
return {std::move(conditions)};
704+
return {{std::forward<Args>(args)...}};
705+
}
706+
707+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
708+
/**
709+
* Create a remove all statement.
710+
* `table` is an explicitly specified table reference of a mapped object to be extracted.
711+
* Usage: storage.remove_all<user_table>(...);
712+
*/
713+
template<orm_table_reference auto table, class... Args>
714+
auto remove_all(Args... args) {
715+
return remove_all<internal::auto_decay_table_ref_t<table>>(std::forward<Args>(args)...);
670716
}
717+
#endif
671718

672719
/**
673720
* Create a get all statement.
674-
* T is an object type mapped to a storage.
721+
* T is an object mapped to a storage or a table alias.
675722
* R is a container type. std::vector<T> is default
676723
* Usage: storage.prepare(get_all<User>(...));
677724
*/
@@ -685,15 +732,15 @@ namespace sqlite_orm {
685732
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
686733
/**
687734
* Create a get all statement.
688-
* `als` is an explicitly specified table proxy of an object to be extracted.
735+
* `mapped` is an explicitly specified table reference or alias of a mapped object to be extracted.
689736
* `R` is the container return type, which must have a `R::push_back(T&&)` method, and defaults to `std::vector<T>`
690737
* Usage: storage.get_all<sqlite_schema>(...);
691738
*/
692-
template<orm_refers_to_table auto als,
693-
class R = std::vector<internal::mapped_type_proxy_t<decltype(als)>>,
739+
template<orm_refers_to_table auto mapped,
740+
class R = std::vector<internal::mapped_type_proxy_t<decltype(mapped)>>,
694741
class... Args>
695742
auto get_all(Args&&... conditions) {
696-
return get_all<internal::auto_decay_table_ref_t<als>, R>(std::forward<Args>(conditions)...);
743+
return get_all<internal::auto_decay_table_ref_t<mapped>, R>(std::forward<Args>(conditions)...);
697744
}
698745
#endif
699746

@@ -706,23 +753,37 @@ namespace sqlite_orm {
706753
static_assert(internal::is_set<S>::value, "first argument in update_all can be either set or dynamic_set");
707754
using args_tuple = std::tuple<Wargs...>;
708755
internal::validate_conditions<args_tuple>();
709-
args_tuple conditions{std::forward<Wargs>(wh)...};
710-
return {std::move(set), std::move(conditions)};
756+
return {std::move(set), {std::forward<Wargs>(wh)...}};
711757
}
712758

713759
/**
714760
* Create a get all pointer statement.
715761
* T is an object type mapped to a storage.
716762
* R is a container return type. std::vector<std::unique_ptr<T>> is default
717763
* Usage: storage.prepare(get_all_pointer<User>(...));
718-
*/
764+
*/
719765
template<class T, class R = std::vector<std::unique_ptr<T>>, class... Args>
720766
internal::get_all_pointer_t<T, R, Args...> get_all_pointer(Args... conditions) {
721767
using conditions_tuple = std::tuple<Args...>;
722768
internal::validate_conditions<conditions_tuple>();
723769
return {{std::forward<Args>(conditions)...}};
724770
}
725771

772+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
773+
/**
774+
* Create a get all pointer statement.
775+
* `table` is an explicitly specified table reference of a mapped object to be extracted.
776+
* R is a container return type. std::vector<std::unique_ptr<T>> is default
777+
* Usage: storage.prepare(get_all_pointer<user_table>(...));
778+
*/
779+
template<orm_table_reference auto table,
780+
class R = std::vector<internal::auto_decay_table_ref_t<table>>,
781+
class... Args>
782+
auto get_all_pointer(Args... conditions) {
783+
return get_all_pointer<internal::auto_decay_table_ref_t<table>, R>(std::forward<Args>(conditions)...);
784+
}
785+
#endif
786+
726787
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
727788
/**
728789
* Create a get all optional statement.
@@ -737,4 +798,19 @@ namespace sqlite_orm {
737798
return {{std::forward<Args>(conditions)...}};
738799
}
739800
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
801+
802+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
803+
/**
804+
* Create a get all optional statement.
805+
* `table` is an explicitly specified table reference of a mapped object to be extracted.
806+
* R is a container return type. std::vector<std::optional<T>> is default
807+
* Usage: storage.get_all_optional<user_table>(...);
808+
*/
809+
template<orm_table_reference auto table,
810+
class R = std::vector<internal::auto_decay_table_ref_t<table>>,
811+
class... Args>
812+
auto get_all_optional(Args&&... conditions) {
813+
return get_all_optional<internal::auto_decay_table_ref_t<table>, R>(std::forward<Args>(conditions)...);
814+
}
815+
#endif
740816
}

dev/sqlite_schema_table.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "schema/column.h"
66
#include "schema/table.h"
7+
#include "column_pointer.h"
78
#include "alias.h"
89

910
namespace sqlite_orm {
@@ -38,6 +39,7 @@ namespace sqlite_orm {
3839
}
3940

4041
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
41-
inline constexpr auto sqlite_schema = "sqlite_schema"_alias.for_<sqlite_master>();
42+
inline constexpr orm_table_reference auto sqlite_master_table = c<sqlite_master>();
43+
inline constexpr orm_table_alias auto sqlite_schema = "sqlite_schema"_alias.for_<sqlite_master>();
4244
#endif
4345
}

dev/storage.h

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ namespace sqlite_orm {
252252
}
253253

254254
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
255-
template<orm_refers_to_table auto table, class... Args>
255+
template<orm_refers_to_table auto mapped, class... Args>
256256
auto iterate(Args&&... args) {
257-
return this->iterate<auto_decay_table_ref_t<table>>(std::forward<Args>(args)...);
257+
return this->iterate<mapped_type_proxy_t<decltype(mapped)>>(std::forward<Args>(args)...);
258258
}
259259
#endif
260260

@@ -291,6 +291,13 @@ namespace sqlite_orm {
291291
this->execute(statement);
292292
}
293293

294+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
295+
template<orm_table_reference auto table, class... Args>
296+
void remove_all(Args&&... args) {
297+
return this->remove_all<auto_decay_table_ref_t<table>>(std::forward<Args>(args)...);
298+
}
299+
#endif
300+
294301
/**
295302
* Delete routine.
296303
* O is an object's type. Must be specified explicitly.
@@ -303,6 +310,13 @@ namespace sqlite_orm {
303310
this->execute(statement);
304311
}
305312

313+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
314+
template<orm_table_reference auto table, class... Ids>
315+
void remove(Ids... ids) {
316+
return this->remove<auto_decay_table_ref_t<table>>(std::forward<Ids>(ids)...);
317+
}
318+
#endif
319+
306320
/**
307321
* Update routine. Sets all non primary key fields where primary key is equal.
308322
* O is an object type. May be not specified explicitly cause it can be deduced by
@@ -360,18 +374,17 @@ namespace sqlite_orm {
360374
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
361375
/**
362376
* SELECT * routine.
363-
* `als` is an explicitly specified table proxy of an object to be extracted.
377+
* `mapped` is an explicitly specified table reference or alias of an object to be extracted.
364378
* `R` is the container return type, which must have a `R::push_back(O&&)` method, and defaults to `std::vector<O>`
365379
* @return All objects stored in database.
366380
* @example: storage.get_all<sqlite_schema, std::list<sqlite_master>>(); - SELECT sqlite_schema.* FROM sqlite_master AS sqlite_schema
367381
*/
368-
template<orm_refers_to_table auto als,
369-
class R = std::vector<mapped_type_proxy_t<decltype(als)>>,
382+
template<orm_refers_to_table auto mapped,
383+
class R = std::vector<mapped_type_proxy_t<decltype(mapped)>>,
370384
class... Args>
371385
R get_all(Args&&... args) {
372-
using A = decltype(als);
373-
this->assert_mapped_type<mapped_type_proxy_t<A>>();
374-
auto statement = this->prepare(sqlite_orm::get_all<als, R>(std::forward<Args>(args)...));
386+
this->assert_mapped_type<mapped_type_proxy_t<decltype(mapped)>>();
387+
auto statement = this->prepare(sqlite_orm::get_all<mapped, R>(std::forward<Args>(args)...));
375388
return this->execute(statement);
376389
}
377390
#endif
@@ -391,6 +404,15 @@ namespace sqlite_orm {
391404
return this->execute(statement);
392405
}
393406

407+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
408+
template<orm_table_reference auto table,
409+
class R = std::vector<std::unique_ptr<auto_decay_table_ref_t<table>>>,
410+
class... Args>
411+
auto get_all_pointer(Args&&... args) {
412+
return this->get_all_pointer<auto_decay_table_ref_t<table>>(std::forward<Args>(args)...);
413+
}
414+
#endif
415+
394416
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
395417
/**
396418
* SELECT * routine.
@@ -408,6 +430,15 @@ namespace sqlite_orm {
408430
}
409431
#endif
410432

433+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
434+
template<orm_table_reference auto table,
435+
class R = std::vector<std::optional<auto_decay_table_ref_t<table>>>,
436+
class... Args>
437+
auto get_all_optional(Args&&... conditions) {
438+
return this->get_all_optional<auto_decay_table_ref_t<table>>(std::forward<Args>(conditions)...);
439+
}
440+
#endif
441+
411442
/**
412443
* Select * by id routine.
413444
* throws std::system_error{orm_error_code::not_found} if object not found with given
@@ -441,6 +472,13 @@ namespace sqlite_orm {
441472
return this->execute(statement);
442473
}
443474

475+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
476+
template<orm_table_reference auto table, class... Ids>
477+
auto get_pointer(Ids... ids) {
478+
return this->get_pointer<auto_decay_table_ref_t<table>>(std::forward<Ids>(ids)...);
479+
}
480+
#endif
481+
444482
/**
445483
* A previous version of get_pointer() that returns a shared_ptr
446484
* instead of a unique_ptr. New code should prefer get_pointer()
@@ -472,6 +510,13 @@ namespace sqlite_orm {
472510
}
473511
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
474512

513+
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
514+
template<orm_table_reference auto table, class... Ids>
515+
auto get_optional(Ids... ids) {
516+
return this->get_optional<auto_decay_table_ref_t<table>>(std::forward<Ids>(ids)...);
517+
}
518+
#endif
519+
475520
/**
476521
* SELECT COUNT(*) https://www.sqlite.org/lang_aggfunc.html#count
477522
* @return Number of O object in table.
@@ -785,7 +830,7 @@ namespace sqlite_orm {
785830

786831
template<class It, class Projection = polyfill::identity>
787832
void replace_range(It from, It to, Projection project = {}) {
788-
using O = std::decay_t<decltype(polyfill::invoke(std::declval<Projection>(), *std::declval<It>()))>;
833+
using O = std::decay_t<decltype(polyfill::invoke(project, *from))>;
789834
this->assert_mapped_type<O>();
790835
if(from == to) {
791836
return;

0 commit comments

Comments
 (0)