0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Added compile time tests for date and date_time

This commit is contained in:
rbock 2015-10-13 21:25:10 +02:00
parent d1a3ab596a
commit 56d312a59b
8 changed files with 553 additions and 32 deletions

View File

@ -44,13 +44,15 @@ namespace sqlpp
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_rhs_comparison_operand_t, "invalid rhs operand in comparison"); SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_rhs_comparison_operand_t, "invalid rhs operand in comparison");
template <typename LhsValueType, typename RhsType> template <typename LhsValueType, typename RhsType>
using check_rhs_comparison_operand_t = static_check_t< using check_rhs_comparison_operand_t =
(is_expression_t<RhsType>::value // expressions are OK static_check_t<(is_expression_t<sqlpp::wrap_operand_t<RhsType>>::value // expressions are OK
or or
is_multi_expression_t<RhsType>::value) // multi-expressions like ANY are OK for comparisons, too is_multi_expression_t<sqlpp::wrap_operand_t<RhsType>>::value) // multi-expressions like ANY are
and // OK for comparisons, too
LhsValueType::template _is_valid_operand<RhsType>::value, // the correct value type is required, of course and
assert_valid_rhs_comparison_operand_t>; LhsValueType::template _is_valid_operand<
sqlpp::wrap_operand_t<RhsType>>::value, // the correct value type is required, of course
assert_valid_rhs_comparison_operand_t>;
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_in_arguments_t, "at least one operand of in() is not valid"); SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_in_arguments_t, "at least one operand of in() is not valid");

View File

@ -98,8 +98,11 @@ types = {
'mediumblob': 'blob', 'mediumblob': 'blob',
'longblob': 'blob', 'longblob': 'blob',
'bool': 'boolean', 'bool': 'boolean',
'boolean': 'boolean',
'double': 'floating_point', 'double': 'floating_point',
'float': 'floating_point', 'float': 'floating_point',
'date' : 'date',
'datetime' : 'date_time',
} }
# PROCESS DDL # PROCESS DDL
@ -113,6 +116,7 @@ namespace = sys.argv[3]
ddlFile = open(pathToDdl, 'r') ddlFile = open(pathToDdl, 'r')
header = open(pathToHeader, 'w') header = open(pathToHeader, 'w')
print('// generated by ' + ' '.join(sys.argv), file=header)
print('#ifndef '+get_include_guard_name(namespace, pathToHeader), file=header) print('#ifndef '+get_include_guard_name(namespace, pathToHeader), file=header)
print('#define '+get_include_guard_name(namespace, pathToHeader), file=header) print('#define '+get_include_guard_name(namespace, pathToHeader), file=header)
print('', file=header) print('', file=header)

View File

@ -0,0 +1,290 @@
// generated by ../scripts/ddl2cpp ../test_static_asserts/AssertTables.sql ../test_static_asserts/AssertTables test
#ifndef TEST_ASSERTTABLES_H
#define TEST_ASSERTTABLES_H
#include <sqlpp11/table.h>
#include <sqlpp11/column_types.h>
#include <sqlpp11/char_sequence.h>
namespace test
{
namespace TabAllTypes_
{
struct SomeString
{
struct _alias_t
{
static constexpr const char _literal[] = "some_string";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T someString;
T& operator()()
{
return someString;
}
const T& operator()() const
{
return someString;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::varchar, sqlpp::tag::can_be_null>;
};
struct SomeInt
{
struct _alias_t
{
static constexpr const char _literal[] = "some_int";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T someInt;
T& operator()()
{
return someInt;
}
const T& operator()() const
{
return someInt;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::bigint, sqlpp::tag::can_be_null>;
};
struct SomeFloat
{
struct _alias_t
{
static constexpr const char _literal[] = "some_float";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T someFloat;
T& operator()()
{
return someFloat;
}
const T& operator()() const
{
return someFloat;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::floating_point, sqlpp::tag::can_be_null>;
};
struct SomeBool
{
struct _alias_t
{
static constexpr const char _literal[] = "some_bool";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T someBool;
T& operator()()
{
return someBool;
}
const T& operator()() const
{
return someBool;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::boolean, sqlpp::tag::can_be_null>;
};
struct SomeDate
{
struct _alias_t
{
static constexpr const char _literal[] = "some_date";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T someDate;
T& operator()()
{
return someDate;
}
const T& operator()() const
{
return someDate;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::date, sqlpp::tag::can_be_null>;
};
struct SomeDateTime
{
struct _alias_t
{
static constexpr const char _literal[] = "some_date_time";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T someDateTime;
T& operator()()
{
return someDateTime;
}
const T& operator()() const
{
return someDateTime;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::date_time, sqlpp::tag::can_be_null>;
};
struct OtherInt
{
struct _alias_t
{
static constexpr const char _literal[] = "other_int";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T otherInt;
T& operator()()
{
return otherInt;
}
const T& operator()() const
{
return otherInt;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::bigint, sqlpp::tag::can_be_null>;
};
struct OtherFloat
{
struct _alias_t
{
static constexpr const char _literal[] = "other_float";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T otherFloat;
T& operator()()
{
return otherFloat;
}
const T& operator()() const
{
return otherFloat;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::floating_point, sqlpp::tag::can_be_null>;
};
struct OtherBool
{
struct _alias_t
{
static constexpr const char _literal[] = "other_bool";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T otherBool;
T& operator()()
{
return otherBool;
}
const T& operator()() const
{
return otherBool;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::boolean, sqlpp::tag::can_be_null>;
};
struct OtherDate
{
struct _alias_t
{
static constexpr const char _literal[] = "other_date";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T otherDate;
T& operator()()
{
return otherDate;
}
const T& operator()() const
{
return otherDate;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::date, sqlpp::tag::can_be_null>;
};
struct OtherDateTime
{
struct _alias_t
{
static constexpr const char _literal[] = "other_date_time";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T otherDateTime;
T& operator()()
{
return otherDateTime;
}
const T& operator()() const
{
return otherDateTime;
}
};
};
using _traits = sqlpp::make_traits<sqlpp::date_time, sqlpp::tag::can_be_null>;
};
}
struct TabAllTypes : sqlpp::table_t<TabAllTypes,
TabAllTypes_::SomeString,
TabAllTypes_::SomeInt,
TabAllTypes_::SomeFloat,
TabAllTypes_::SomeBool,
TabAllTypes_::SomeDate,
TabAllTypes_::SomeDateTime,
TabAllTypes_::OtherInt,
TabAllTypes_::OtherFloat,
TabAllTypes_::OtherBool,
TabAllTypes_::OtherDate,
TabAllTypes_::OtherDateTime>
{
struct _alias_t
{
static constexpr const char _literal[] = "tab_all_types";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T tabAllTypes;
T& operator()()
{
return tabAllTypes;
}
const T& operator()() const
{
return tabAllTypes;
}
};
};
};
}
#endif

View File

@ -0,0 +1,43 @@
--
-- Copyright (c) 2015-2015, Roland Bock
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without modification,
-- are permitted provided that the following conditions are met:
--
-- Redistributions of source code must retain the above copyright notice, this
-- list of conditions and the following disclaimer.
--
-- Redistributions in binary form must reproduce the above copyright notice, this
-- list of conditions and the following disclaimer in the documentation and/or
-- other materials provided with the distribution.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
-- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
-- ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--
CREATE TABLE tab_all_types
(
some_string varchar(255),
some_int bigint,
some_float double,
some_bool bool,
some_date date,
some_date_time datetime
other_string varchar(255),
other_int bigint,
other_float double,
other_bool bool,
other_date date,
other_date_time datetime
);

View File

@ -30,4 +30,6 @@ endfunction()
test_compile(aggregates) test_compile(aggregates)
test_compile(insert) test_compile(insert)
test_compile(date)
test_compile(date_time)

View File

@ -0,0 +1,90 @@
/*
* Copyright (c) 2015-2015, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <MockDb.h>
#include "AssertTables.h"
#include <sqlpp11/sqlpp11.h>
namespace
{
constexpr auto t = test::TabAllTypes{};
template <typename T>
void print_type_on_error(std::true_type)
{
}
template <typename T>
void print_type_on_error(std::false_type)
{
T::_print_me_;
}
template <typename Assert, typename Operand>
void static_check_comparison(const Operand& operand)
{
using CheckResult = sqlpp::check_rhs_comparison_operand_t<decltype(t.someDate), Operand>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
static_assert(ExpectedCheckResult::value, "Unexpected check result");
print_type_on_error<CheckResult>(ExpectedCheckResult{});
using ReturnType = sqlpp::detail::make_type_set_t<
decltype(t.someDate < operand), decltype(t.someDate <= operand), decltype(t.someDate == operand),
decltype(t.someDate != operand), decltype(t.someDate >= operand), decltype(t.someDate > operand),
decltype(t.someDate.in(operand)), decltype(t.someDate.in(operand, operand)),
decltype(t.someDate.not_in(operand)), decltype(t.someDate.not_in(operand, operand))>;
using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor
std::is_same<ReturnType, sqlpp::detail::type_set<sqlpp::bad_statement>>::value>;
static_assert(ExpectedReturnType::value, "Unexpected return type");
print_type_on_error<ReturnType>(ExpectedReturnType{});
}
void allowed_comparands()
{
static_check_comparison<sqlpp::consistent_t>(std::chrono::system_clock::now());
static_check_comparison<sqlpp::consistent_t>(t.someDate);
static_check_comparison<sqlpp::consistent_t>(t.someDateTime);
}
void disallowed_comparands()
{
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(17);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>('a');
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(std::string("a"));
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someString);
}
}
int main(int, char**)
{
allowed_comparands();
disallowed_comparands();
}

View File

@ -0,0 +1,90 @@
/*
* Copyright (c) 2015-2015, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <MockDb.h>
#include "AssertTables.h"
#include <sqlpp11/sqlpp11.h>
namespace
{
constexpr auto t = test::TabAllTypes{};
template <typename T>
void print_type_on_error(std::true_type)
{
}
template <typename T>
void print_type_on_error(std::false_type)
{
T::_print_me_;
}
template <typename Assert, typename Operand>
void static_check_comparison(const Operand& operand)
{
using CheckResult = sqlpp::check_rhs_comparison_operand_t<decltype(t.someDateTime), Operand>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
static_assert(ExpectedCheckResult::value, "Unexpected check result");
print_type_on_error<CheckResult>(ExpectedCheckResult{});
using ReturnType = sqlpp::detail::make_type_set_t<
decltype(t.someDateTime < operand), decltype(t.someDateTime <= operand), decltype(t.someDateTime == operand),
decltype(t.someDateTime != operand), decltype(t.someDateTime >= operand), decltype(t.someDateTime > operand),
decltype(t.someDateTime.in(operand)), decltype(t.someDateTime.in(operand, operand)),
decltype(t.someDateTime.not_in(operand)), decltype(t.someDateTime.not_in(operand, operand))>;
using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor
std::is_same<ReturnType, sqlpp::detail::type_set<sqlpp::bad_statement>>::value>;
static_assert(ExpectedReturnType::value, "Unexpected return type");
print_type_on_error<ReturnType>(ExpectedReturnType{});
}
void allowed_comparands()
{
static_check_comparison<sqlpp::consistent_t>(std::chrono::system_clock::now());
static_check_comparison<sqlpp::consistent_t>(t.someDate);
static_check_comparison<sqlpp::consistent_t>(t.someDateTime);
}
void disallowed_comparands()
{
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(17);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>('a');
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(std::string("a"));
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someString);
}
}
int main(int, char**)
{
allowed_comparands();
disallowed_comparands();
}