Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Code quality (clearer code), included dependent headers
Browse files Browse the repository at this point in the history
* Removed some unnecessary constructors that are just forwarding their arguments to initialize members
* Qualify enum values by their original enum class name instead of using `decltype`
* Spelled out type for SQLite database connection instead of using `auto`
* Moved `storage_t<>::drop_trigger()` to base class `storage_base`, changed signature to accept a `std::string`
  • Loading branch information
trueqbit committed Apr 25, 2022
1 parent 0dceffe commit b548905
Show file tree
Hide file tree
Showing 22 changed files with 1,110 additions and 124 deletions.
4 changes: 0 additions & 4 deletions dev/alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ namespace sqlite_orm {
using column_type = C;

column_type column;

alias_column_t() {}

alias_column_t(column_type column_) : column(std::move(column_)) {}
};

template<class T, class SFINAE = void>
Expand Down
1 change: 1 addition & 0 deletions dev/column_names_getter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <functional> // std::reference_wrapper

#include "error_code.h"
#include "serializer_context.h"
#include "select_constraints.h"
#include "util.h"

Expand Down
5 changes: 0 additions & 5 deletions dev/conditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,6 @@ namespace sqlite_orm {
struct order_by_base {
int asc_desc = 0; // 1: asc, -1: desc
std::string _collate_argument;

order_by_base() = default;

order_by_base(decltype(asc_desc) asc_desc_, decltype(_collate_argument) _collate_argument_) :
asc_desc(asc_desc_), _collate_argument(move(_collate_argument_)) {}
};

struct order_by_string {
Expand Down
31 changes: 13 additions & 18 deletions dev/constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,22 @@ namespace sqlite_orm {

inline std::ostream& operator<<(std::ostream& os, foreign_key_action action) {
switch(action) {
case decltype(action)::no_action:
case foreign_key_action::no_action:
os << "NO ACTION";
break;
case decltype(action)::restrict_:
case foreign_key_action::restrict_:
os << "RESTRICT";
break;
case decltype(action)::set_null:
case foreign_key_action::set_null:
os << "SET NULL";
break;
case decltype(action)::set_default:
case foreign_key_action::set_default:
os << "SET DEFAULT";
break;
case decltype(action)::cascade:
case foreign_key_action::cascade:
os << "CASCADE";
break;
case decltype(action)::none:
case foreign_key_action::none:
break;
}
return os;
Expand Down Expand Up @@ -229,7 +229,7 @@ namespace sqlite_orm {
}

operator bool() const {
return this->_action != decltype(this->_action)::none;
return this->_action != foreign_key_action::none;
}
};

Expand Down Expand Up @@ -301,8 +301,6 @@ namespace sqlite_orm {

tuple_type columns;

foreign_key_intermediate_t(tuple_type columns_) : columns(std::move(columns_)) {}

template<class... Rs>
foreign_key_t<std::tuple<Cs...>, std::tuple<Rs...>> references(Rs... refs) {
return {std::move(this->columns), std::make_tuple(std::forward<Rs>(refs)...)};
Expand All @@ -311,22 +309,19 @@ namespace sqlite_orm {
#endif

struct collate_constraint_t {
internal::collate_argument argument = internal::collate_argument::binary;

collate_constraint_t(internal::collate_argument argument_) : argument(argument_) {}
collate_argument argument = collate_argument::binary;

operator std::string() const {
std::string res = "COLLATE " + this->string_from_collate_argument(this->argument);
return res;
return "COLLATE " + this->string_from_collate_argument(this->argument);
}

static std::string string_from_collate_argument(internal::collate_argument argument) {
static std::string string_from_collate_argument(collate_argument argument) {
switch(argument) {
case decltype(argument)::binary:
case collate_argument::binary:
return "BINARY";
case decltype(argument)::nocase:
case collate_argument::nocase:
return "NOCASE";
case decltype(argument)::rtrim:
case collate_argument::rtrim:
return "RTRIM";
}
throw std::system_error{orm_error_code::invalid_collate_argument_enum};
Expand Down
2 changes: 1 addition & 1 deletion dev/error_code.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <sqlite3.h>
#include <system_error> // std::error_code, std::system_error
#include <string> // std::string
#include <sqlite3.h>
#include <stdexcept>
#include <sstream> // std::ostringstream
#include <type_traits>
Expand Down
2 changes: 1 addition & 1 deletion dev/function.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include <sqlite3.h>
#include <type_traits>
#include <string> // std::string
#include <tuple> // std::tuple
#include <functional> // std::function
#include <algorithm> // std::min
#include <cstddef>
#include <sqlite3.h>

#include "cxx_polyfill.h"

Expand Down
19 changes: 11 additions & 8 deletions dev/implementations/storage_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*/
#pragma once
#include <type_traits> // std::is_same
#include <sstream>
#include <functional> // std::reference_wrapper, std::cref
#include <algorithm> // std::find_if

#include "../storage.h"
#include "../dbstat.h"
Expand All @@ -26,10 +29,10 @@ namespace sqlite_orm {
auto res = sync_schema_result::already_in_sync;

auto schema_stat = this->schema_status(tImpl, db, preserve);
if(schema_stat != decltype(schema_stat)::already_in_sync) {
if(schema_stat == decltype(schema_stat)::new_table_created) {
if(schema_stat != sync_schema_result::already_in_sync) {
if(schema_stat == sync_schema_result::new_table_created) {
this->create_table(db, tImpl.table.name, tImpl);
res = decltype(res)::new_table_created;
res = sync_schema_result::new_table_created;
} else {
if(schema_stat == sync_schema_result::old_columns_removed ||
schema_stat == sync_schema_result::new_columns_added ||
Expand All @@ -51,11 +54,11 @@ namespace sqlite_orm {
for(auto& tableInfo: dbTableInfo) {
this->drop_column(db, tImpl.table.name, tableInfo.name);
}
res = decltype(res)::old_columns_removed;
res = sync_schema_result::old_columns_removed;
#else
// extra table columns than storage columns
this->backup_table(db, tImpl, {});
res = decltype(res)::old_columns_removed;
res = sync_schema_result::old_columns_removed;
#endif
}

Expand All @@ -68,19 +71,19 @@ namespace sqlite_orm {
this->add_column(tImpl.table.name, column, db);
});
}
res = decltype(res)::new_columns_added;
res = sync_schema_result::new_columns_added;
}

if(schema_stat == sync_schema_result::new_columns_added_and_old_columns_removed) {

// remove extra columns
this->backup_table(db, tImpl, columnsToAdd);
res = decltype(res)::new_columns_added_and_old_columns_removed;
res = sync_schema_result::new_columns_added_and_old_columns_removed;
}
} else if(schema_stat == sync_schema_result::dropped_and_recreated) {
this->drop_table_internal(tImpl.table.name, db);
this->create_table(db, tImpl.table.name, tImpl);
res = decltype(res)::dropped_and_recreated;
res = sync_schema_result::dropped_and_recreated;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dev/iterator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <memory> // std::shared_ptr, std::unique_ptr, std::make_shared
#include <sqlite3.h>
#include <memory> // std::shared_ptr, std::unique_ptr, std::make_shared
#include <type_traits> // std::decay
#include <utility> // std::move
#include <cstddef> // std::ptrdiff_t
Expand Down
3 changes: 3 additions & 0 deletions dev/mapped_type_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ namespace sqlite_orm {
struct mapped_type_proxy<T, std::enable_if_t<std::is_base_of<alias_tag, T>::value>> {
using type = typename T::type;
};

template<class T>
using mapped_type_proxy_t = typename mapped_type_proxy<T>::type;
}
}
15 changes: 8 additions & 7 deletions dev/pragma.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <string> // std::string
#include <sqlite3.h>
#include <string> // std::string
#include <functional> // std::function
#include <memory> // std::shared_ptr
#include <vector> // std::vector
#include <sstream>

#include "error_code.h"
#include "util.h"
Expand Down Expand Up @@ -119,7 +120,7 @@ namespace sqlite_orm {

std::vector<sqlite_orm::table_info> table_info(const std::string& tableName) const {
auto connection = this->get_connection();
auto db = connection.get();
sqlite3* db = connection.get();

std::vector<sqlite_orm::table_info> result;
auto query = "PRAGMA table_info(" + quote_identifier(tableName) + ")";
Expand All @@ -135,9 +136,9 @@ namespace sqlite_orm {
std::string type = argv[index++];
bool notnull = !!std::atoi(argv[index++]);
std::string dflt_value = argv[index] ? argv[index] : "";
index++;
++index;
auto pk = std::atoi(argv[index++]);
res.emplace_back(cid, name, type, notnull, dflt_value, pk);
res.emplace_back(cid, move(name), move(type), notnull, move(dflt_value), pk);
}
return 0;
},
Expand All @@ -159,9 +160,9 @@ namespace sqlite_orm {
template<class T>
T get_pragma(const std::string& name) {
auto connection = this->get_connection();
sqlite3* db = connection.get();
auto query = "PRAGMA " + name;
T result;
auto db = connection.get();
auto rc = sqlite3_exec(db, query.c_str(), getPragmaCallback<T>, &result, nullptr);
if(rc == SQLITE_OK) {
return result;
Expand All @@ -182,7 +183,7 @@ namespace sqlite_orm {
}
std::stringstream ss;
ss << "PRAGMA " << name << " = " << value;
internal::perform_void_exec(db, ss.str());
perform_void_exec(db, ss.str());
}

void set_pragma(const std::string& name, const sqlite_orm::journal_mode& value, sqlite3* db = nullptr) {
Expand All @@ -192,7 +193,7 @@ namespace sqlite_orm {
}
std::stringstream ss;
ss << "PRAGMA " << name << " = " << internal::to_string(value);
internal::perform_void_exec(db, ss.str());
perform_void_exec(db, ss.str());
}
};
}
Expand Down
4 changes: 4 additions & 0 deletions dev/start_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ __pragma(push_macro("max"))
#define SQLITE_ORM_CONSTEVAL constexpr
#endif

#if __cpp_aggregate_paren_init >= 201902L
#define SQLITE_ORM_AGGREGATE_PAREN_INIT
#endif

#if __cplusplus >= 201703L // C++17 or later
#if __has_include(<optional>)
#define SQLITE_ORM_OPTIONAL_SUPPORTED
Expand Down
2 changes: 1 addition & 1 deletion dev/statement_finalizer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <memory> // std::unique_ptr
#include <sqlite3.h>
#include <memory> // std::unique_ptr
#include <type_traits> // std::integral_constant

namespace sqlite_orm {
Expand Down
15 changes: 7 additions & 8 deletions dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ namespace sqlite_orm {
iterate_ast(sel.conditions, collector);
join_iterator<Args...>()([&collector, &context](const auto& c) {
using original_join_type = typename std::decay<decltype(c)>::type::join_type::type;
using cross_join_type = typename internal::mapped_type_proxy<original_join_type>::type;
using cross_join_type = mapped_type_proxy_t<original_join_type>;
auto crossJoinedTableName = lookup_table_name<cross_join_type>(context.impl);
auto tableAliasString = alias_extractor<original_join_type>::get();
std::pair<std::string, std::string> tableNameWithAlias{std::move(crossJoinedTableName),
Expand Down Expand Up @@ -1931,9 +1931,8 @@ namespace sqlite_orm {
if(index > 0) {
ss << ", ";
}
ss << streaming_identifier(
lookup_table_name<typename mapped_type_proxy<from_type>::type>(context.impl),
alias_extractor<from_type>::get());
ss << streaming_identifier(lookup_table_name<mapped_type_proxy_t<from_type>>(context.impl),
alias_extractor<from_type>::get());
++index;
});
return ss.str();
Expand Down Expand Up @@ -2176,7 +2175,7 @@ namespace sqlite_orm {
std::string operator()(const statement_type& l, const Ctx& context) const {
std::stringstream ss;
ss << static_cast<std::string>(l) << " "
<< streaming_identifier(lookup_table_name<typename mapped_type_proxy<T>::type>(context.impl),
<< streaming_identifier(lookup_table_name<mapped_type_proxy_t<T>>(context.impl),
alias_extractor<T>::get())
<< serialize(l.constraint, context);
return ss.str();
Expand Down Expand Up @@ -2205,7 +2204,7 @@ namespace sqlite_orm {
std::string operator()(const statement_type& l, const Ctx& context) const {
std::stringstream ss;
ss << static_cast<std::string>(l) << " "
<< streaming_identifier(lookup_table_name<typename mapped_type_proxy<T>::type>(context.impl),
<< streaming_identifier(lookup_table_name<mapped_type_proxy_t<T>>(context.impl),
alias_extractor<T>::get())
<< " " << serialize(l.constraint, context);
return ss.str();
Expand All @@ -2220,7 +2219,7 @@ namespace sqlite_orm {
std::string operator()(const statement_type& l, const Ctx& context) const {
std::stringstream ss;
ss << static_cast<std::string>(l) << " "
<< streaming_identifier(lookup_table_name<typename mapped_type_proxy<T>::type>(context.impl),
<< streaming_identifier(lookup_table_name<mapped_type_proxy_t<T>>(context.impl),
alias_extractor<T>::get())
<< " " << serialize(l.constraint, context);
return ss.str();
Expand All @@ -2235,7 +2234,7 @@ namespace sqlite_orm {
std::string operator()(const statement_type& l, const Ctx& context) const {
std::stringstream ss;
ss << static_cast<std::string>(l) << " "
<< streaming_identifier(lookup_table_name<typename mapped_type_proxy<T>::type>(context.impl),
<< streaming_identifier(lookup_table_name<mapped_type_proxy_t<T>>(context.impl),
alias_extractor<T>::get())
<< " " << serialize(l.constraint, context);
return ss.str();
Expand Down
3 changes: 0 additions & 3 deletions dev/static_magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ namespace sqlite_orm {
decltype(auto) static_if(const T& t) {
return static_if(std::integral_constant<bool, B>{}, t, empty_callable());
}

template<typename T>
using static_not = std::integral_constant<bool, !T::value>;
}

}
Loading

0 comments on commit b548905

Please sign in to comment.