qicosmos / ormpp Goto Github PK
View Code? Open in Web Editor NEWmodern C++ ORM, C++17, support mysql, postgresql,sqlite
License: Apache License 2.0
modern C++ ORM, C++17, support mysql, postgresql,sqlite
License: Apache License 2.0
connection_pool:83 args_ = std::make_tuple(std::forward(args)...);这句话不适合sqlite3,因为只能传递一个参数
比如对于mysql初始化连接使用dao_t<dbng>::init 这个连接是单例,建议改成对象初始化方式,这样对于dao_t<dbng> 可以有多个连接实例,同时去掉了全局状态,现在代码中初始化了后dao_t<dbng> 类型可在任意地方创建实例引用全局状态,代码有点高耦合。
请问如果在多线程环境下,是否线程安全?
是否支持windows操作系统?
实际使用中,除sqlite外连接池还是不可或缺的,建议增加。
#include <'iostream'>
#include <'array'>
#include <'string'>
using namespace std;
template<'typename T'>
struct package
{
};
template<'typename T'>
struct test
{
static const int value = 0;
};
template<'std::size_t Size'>
struct test<package<std::array<char,Size>>>
{
static const int value = Size;
};
template<'typename U'>
auto _test_help(package obj)->int
{
cout<<typeid(U).name()<<endl;
return test<package>::value;
}
template<'int Size'>
auto _test_help(package<char[Size]>)->int
{
return test<package<std::array<char,Size>>>::value;
}
struct My
{
char a[20];
};
int main() {
char a[10]={'0'};
std::cout<<_test_help(package<decltype(My::a)>{})<<std::endl;
return 0;
}
struct test_tb {
int id;
char name[12];
};
REFLECTION(test_tb, id, name);
dbng< sqlite > sqlite;
TEST_REQUIRE(sqlite.create_datatable<test_tb>());
按照示例中的代码,发生编译错误,经过调试发现是char[]引起的
iguana\iguana\reflection.hpp(376): error C2440: “return”: 无法从“char [12]”转换为“char (&&)[12]”
iguana\iguana\reflection.hpp(377): error C3487: “void”: 所有返回表达式必须推导为相同类型: 以前为“char (&&)[12]”
主键是自增的,insert时不需要给值,该如何使用
看源码update的逻辑应该是先删除数据库里该条记录再插入该条记录最新信息,但是实际执行逻辑是先清空该表,再插入该条记录最新信息,最终update就只剩下一条数据了
Deleting a single row doesn't work.
Please show how.
It says there isn't an overridden function for that.
我定义对数据库操作的两个类,然后同时包含必要hpp文件报错,我知道是重复包含错误,想请问一下这个能不能在源码上解决?😳
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2005 "void __cdecl ormpp::get_sql_conditions(class std::basic_string<char,struct std::char_traits,class std::allocator > &)" (?get_sql_conditions@ormpp@@YAXAAV?$basic_string@DU?$char_traits@D@std@@v?$allocator@D@2@@std@@@z) 已经在 ExportBankUser.obj 中定义 DllMysqlOrm D:\vs2017_workspace\test_2017\DllMysqlOrm\exportFunc.obj 1
这是我使用以下代码类似进行相应的实验,并在线程池内部添加代码注入。
#ifndef MYSQL_CONNECTION_POOL
#define MYSQL_CONNECTION_POOL ormpp::connection_pool<ormpp::dbng<ormpp::mysql>>::instance()
#endif
void test1(int num) {
for (int i = 1; i <= 6; i++) {
{
std::lock_guard<std::mutex> lck (mtx);
}
auto conn = MYSQL_CONNECTION_POOL.get();
auto conn2 = MYSQL_CONNECTION_POOL.get();
{
std::lock_guard<std::mutex> lck (mtx);
std::cout << "id:" << num << " conn: " << &conn << " conn2: " << &conn2 << std::endl;
}
std::string sql_query = "WHERE status>=-3";
if (conn == nullptr) {
continue;
}
auto res = conn->query<ab>(sql_query);
{
std::lock_guard<std::mutex> lck (mtx);
std::cout << num << " : " << res.size() << std::endl;
}
MYSQL_CONNECTION_POOL.return_back(conn2);
MYSQL_CONNECTION_POOL.return_back(conn);
sleep(5);
}
}
发现线程池中的连接只会进行一次重连,之后都不会再进行重连。
你好,请问blob数据类型怎么存储呢?
gcc --version
gcc (GCC) 8.2.1 20181127
Copyright (C) 2018 Free Software Foundation, Inc.
ormpp/sqlite.hpp: In member function ‘constexpr int ormpp::sqlite::insert_impl(bool, const string&, const T&, Args&& ...)’:
ormpp/sqlite.hpp:355:24: error: ‘INT_MIN’ was not declared in this scope
return INT_MIN;
^~~~~~~
ormpp/sqlite.hpp:355:24: note: ‘INT_MIN’ is defined in header ‘<climits>’; did you forget to ‘#include <climits>’?
因课设需求,需要用到您的库,在编译成LIB文件后,我需要在工程中加入lib(main.lib)和哪些头文件
或者说有其他扩展的方法?
5.插入多条数据
template<typename T, typename... Args>
int update(const std::vector<T>& t, Args&&... args); // is insert() here?
multiple insert example:
person p = {1, "test1", 2};
person p1 = {2, "test2", 3};
person p2 = {3, "test3", 4};
std::vector<person> v1{p, p1, p2};
TEST_CHECK(mysql.insert(v1)==3);
TEST_CHECK(postgres.insert(v1)==3);
TEST_CHECK(sqlite.insert(v1)==3);
在作为ORM库用的时候,我们一般只需要某一个数据库,而此库的编译是需要三个后端数据的头文件,我觉得这有点不妥?
能不能增加编译选项来编译指定的数据库?
编译器:
Apple clang version 11.0.3 (clang-1103.0.32.62)
报错:
Variable of non-literal type 'std::string' (aka 'basic_string<char, char_traits, allocator>') cannot be defined in a constexpr function。
基本上都是这个错误,很多。想问一下是不是编译器不支持?
inline static std::map<std::string, std::string> auto_key_map_;
放到cpp文件中没有总量,应该是static变量的总量吧
查了一下原因,在utility.hpp文件的第150行
// template
// bool is_empty(const std::string& t){
// return t.empty();
// }
使用mysql是什么版本的,这边没找到mysql8.0+ 的C的头文件。 用的5.0的版本 +VS2019 ,编译报错。
ormpp现在支持建表时指定表名吗,同类型表可能会建多张表
比如我的表结构是
struct student
{
string name;
int age;
}
name为表的主键,该如何创建呢。
我按照你的例子创建出来错误提示是:BLOB/TEXT column 'name' used in key specification without a key length
我使用MySQL+Vs2019 测试 main.cpp 中的代码,发现TEST_CASE(orm_query) 部分 查找指定 “id=1” 失败,生成的 SQL 语句不含 “where”。 请确认
gcc版本 gcc8.1
ubuntu:16.04
Thread model: posix
gcc version 8.0.1 20180424 (experimental) [trunk revision 259590] (Ubuntu 8-20180424-0ubuntu1~16.04.1)
编译报错
~/ormpp/main.cpp:11:
~/ormpp/mysql.hpp: In instantiation of ‘ormpp::mysql::query(const Arg&, Args&& ...) [with T = std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>; Arg = std::__cxx11::basic_string<char>; Args = {}; std::enable_if_t<(! is_reflection_v<T>), std::vector<_Tp> > = std::vector<std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>, std::allocator<std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >]::<lambda(auto:8&, auto:9)> [with auto:8 = person; auto:9 = std::integral_constant<long unsigned int, 0>]’:
~/ormpp/iguana/iguana/reflection.hpp:471:28: required from ‘constexpr void iguana::for_each(std::tuple<_Tps ...>&, F&&, std::index_sequence<_Idx ...>) [with Args = {person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int}; F = ormpp::mysql::query(const Arg&, Args&& ...) [with T = std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>; Arg = std::__cxx11::basic_string<char>; Args = {}; std::enable_if_t<(! is_reflection_v<T>), std::vector<_Tp> > = std::vector<std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>, std::allocator<std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >]::<lambda(auto:8&, auto:9)>; long unsigned int ...Idx = {0, 1, 2}; std::index_sequence<_Idx ...> = std::integer_sequence<long unsigned int, 0, 1, 2>]’
~/ormpp/mysql.hpp:227:21: required from ‘constexpr std::enable_if_t<(! is_reflection_v<T>), std::vector<_Tp> > ormpp::mysql::query(const Arg&, Args&& ...) [with T = std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>; Arg = std::__cxx11::basic_string<char>; Args = {}; std::enable_if_t<(! is_reflection_v<T>), std::vector<_Tp> > = std::vector<std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>, std::allocator<std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >]’
~/ormpp/dbng.hpp:65:69: required from ‘std::vector<T> ormpp::dbng<DB>::query(Args&& ...) [with T = std::tuple<person, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>; Args = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >}; DB = ormpp::mysql]’
/home/liu/Study/ormpp/main.cpp:603:135: required from here
~/ormpp/mysql.hpp:227:55: internal compiler error: in tsubst_copy, at cp/pt.c:15325
iguana::for_each(tp, [&mp, &it](auto& item, auto i) {
Please submit a full bug report,
with preprocessed source if appropriate.
我认为对于char_array类型,应该将缓冲区直接指向该字段的地址,这样能减少一次拷贝 应当使用如下语句
param_binds[Idx].buffer = (t.*item);
文档中说:
注意:execute接口支持的原生sql语句是不带占位符的,是一条完整的sql语句。
能否支持下占位符,与 to be prepared 的参数?
您好,请问能否增加更多的数据类型
因为个人比较喜欢这个库,然后需要用到datetime,就自己看了好久增加的类型.
希望作者有空可以更新
使用mysql连接池首次客户端连接mysql服务端成功后,不操作大约五分钟,就会无法再连接上。
跟踪代码发现:在pool实例中get时候ping不通,然后重新启动连接。但是连接不上。
在ping不通时候,启动重新连接,代码有bug。如下:
在pool类,成员变量,args_,采用指针存储,重连时候指向的为无效区域,导致重连失败
std::tuple<const char*, const char*, const char*, const char*, int> args_;
后面修复成员变量无效问题,但是具体为什么会ping不通,没有找出原因。
服务端设置客户连接时长最长为8小时
如果把dbng.hpp 和sqlite.hpp放在同一个头文件里就会报编译重定义
multiple definition of `ormpp::get_sql_conditions(std::__cxx11::basic_string<char, std::char_traits, std::allocator >&)'
obj\Debug\main.o:D:\Code\codeprj\ormpp/../../database/ormpp-master/utility.hpp:192: first defined here
mysql数据库,connect后进行query报错
query语句: select cali_flag from db_test where msn='210617111005'
lasterror报错信息为:
0x019d10d7 "Lost connection to MySQL server during query"
还有(HY000)字样
此外,我在测试时还发现,如果connect后执行insert 然后update,再query就是正常的。但是如果就是去掉insert和update就会报上面的故障
请帮忙看看,多谢。
#ifdef ORMPP_ENABLE_PG std::string quota_name = "'" + std::string(iguana::get_name<T>()) + "'"; #else
应该将单引号去掉,不然报错
#ifdef ORMPP_ENABLE_PG std::string quota_name = std::string(iguana::get_name<T>()) ; #else
struct Title
{
int t;
std::string code;
};
struct person
{
int id;
std::string name;
int age;
Title title;
};
这时候 REFLECTION是什么样子的?
I test this lib to link Mysql in VC++2019. I run the main.cpp step by step, I found
code
mysql.delete_records("")
failed.
the reason is in Utilit.hpp 172,where is modified for VS2017.
Please Conform this issue.
默认的连接方式不能指定端口,connect默认使用3306端口。
我在用mysql.connect(ip, mysql_name, mysql_password, mysql_db, port_);进行连接时发现port_这个参数起不到作用,请问如果端口不是3306时我在哪里指定呢?
例如我只需要使用sqlite,在不安装mysql,postgresql的情况下也可编译此项目
sqlite 和 mysql 是不支持数组数据类型的;
而postgresql 是支持数组的。
不知道ormpp这里是否支持postgresql 里面有数组类型的操作?
比如
postgres.insert({"Apple", "round", ["red", "yellow", "green"]});
请问大大有没有支持数据库外键的计划?
因为项目需求的需要,我在尝试添加对mysql8.0的支持,因为大佬引入的是mysql的c api,所以我尝试改为c++的api,遇到了一个问题,你的type_mapping.hpp里的那些函数返回的都是字符串后面有一个sv,想问一下这个sv是什么?我没找到在哪块定义的
clang --version clang version 7.0.1 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
third_party/ormpp/sqlite.hpp:43:25: error: variable of non-literal type 'std::string' (aka 'basic_string<char>') cannot be defined in a constexpr function
std::string sql = generate_createtb_sql<T>(std::forward<Args>(args)...);
third_party/ormpp/sqlite.hpp:53:25: error: variable of non-literal type 'std::string' (aka 'basic_string<char>') cannot be defined in a constexpr function
std::string sql = auto_key_map_.empty()?generate_insert_sql<T>(false):generate_auto_insert_sql<T>(auto_key_map_, false);
third_party/ormpp/sqlite.hpp:357:18: error: variable of non-literal type 'ormpp::sqlite::guard_statment' cannot be defined in a constexpr function
auto guard = guard_statment(stmt_);
template<typename T, typename V>
void set_field_value(T& to_obj, std::string_view to_field_name, const V& from_v)
{
using M = decltype(iguana_reflect_members(to_obj));
auto tp = M::apply_impl();
constexpr auto Size = M::value();
auto index = iguana::get_index(to_field_name);
tuple_switch(index, tp, [&](auto& v) {
using type_v = decltype(std::declval().*std::declval<decltype(v)>());
to_obj.*v = (type_v)from_v;
}, std::make_index_sequence{});
}
///////////////////////////////////////////////
struct person
{
std::string name;
int age = 0;
};
REFLECTION(person, age, name);
int main()
{
person p;
iguana::set_field_value(p, "name", "Tom");
iguana::set_field_value(p, "age", 20);
return getchar();
}
windows环境:
windows10,vs2017,编译器最新,支持c++17.
问题描述:
例如结构体:
struct test_create_table
{
//__int64 id; //可以
long id; //不可以,编译报错
string name;
};
REFLECTION(test_create_table, id, name)
void test_creat()
{
try
{
dbng mysql;
mysql.connect("127.0.0.1", "root", "123456", "test");
ormpp_key key{ "id" };
ormpp_not_null not_null{ {"id"} };
ormpp_auto_key auto_key{ "id" };
mysql.create_datatable<test_create_table>(key, not_null);
}
catch (...)
{
cout << "create table fail" << endl;
}
}
报错如下:
1>ormpp_test.cpp
1>d:mayun_git\secondarycounter\ormpp_compile\ormpp-master\utility.hpp(84): error C2665: “ormpp::ormpp_mysql::type_to_name”: 7 个重载中没有一个可以转换所有参数类型
1>d:mayun_git\secondarycounter\ormpp_compile\ormpp-master\type_mapping.hpp(50): note: 可能是“std::string_view ormpp::ormpp_mysql::type_to_name(ormpp::identitystd::string) noexcept”
1>d:\mayun_git\secondarycounter\ormpp_compile\ormpp-master\type_mapping.hpp(49): note: 或 “std::string_view ormpp::ormpp_mysql::type_to_name(ormpp::identity<int64_t>) noexcept”
1>d:\mayun_git\secondarycounter\ormpp_compile\ormpp-master\type_mapping.hpp(48): note: 或 “std::string_view ormpp::ormpp_mysql::type_to_name(ormpp::identity) noexcept”
1>d:\mayun_git\secondarycounter\ormpp_compile\ormpp-master\type_mapping.hpp(47): note: 或 “std::string_view ormpp::ormpp_mysql::type_to_name(ormpp::identity) noexcept”
1>d:mayun_git\secondarycounter\ormpp_compile\ormpp-master\type_mapping.hpp(46): note: 或 “std::string_view ormpp::ormpp_mysql::type_to_name(ormpp::identity) noexcept”
1>d:mayun_git\secondarycounter\ormpp_compile\ormpp-master\type_mapping.hpp(45): note: 或 “std::string_view ormpp::ormpp_mysql::type_to_name(ormpp::identity) noexcept”
1>dmayun_git\secondarycounter\ormpp_compile\ormpp-master\type_mapping.hpp(44): note: 或 “std::string_view ormpp::ormpp_mysql::type_to_name(ormpp::identity) noexcept”
1>d:mayun_git\secondarycounter\ormpp_compile\ormpp-master\utility.hpp(84): note: 尝试匹配参数列表“(ormpp::identity)”时
\mayun_git\secondarycounter\ormpp_compile\iguana\iguana\reflection.hpp(476): note: 参见对正在编译的函数 模板 实例化“auto ormpp::get_type_names::<lambda_ce8993546f247617066e6a0f110d02f9>::operator ()<longtest_create_table::* const ,std::integral_constant<size_t,0>>(long test_create_table::* const &,std::integral_constant<size_t,0>) const”的引用
\mayun_git\secondarycounter\ormpp_compile\iguana\iguana\reflection.hpp(484): note: 参见对正在编译的函数 模板 实例化“void iguana::for_each<longtest_create_table::* ,std::stringtest_create_table::* ,_Ty,0,1>(const std::tuple<long test_create_table::* ,std::string test_create_table::* > &,F &&,std::integer_sequence<unsigned int,0,1>)”的引用
1> with
1> [
1> _Ty=ormpp::get_type_names::<lambda_ce8993546f247617066e6a0f110d02f9>,
1> F=ormpp::get_type_names::<lambda_ce8993546f247617066e6a0f110d02f9>
1> ]
1>\mayun_git\secondarycounter\ormpp_compile\ormpp-master\utility.hpp(98): note: 参见对正在编译的函数 模板 实例化“void iguana::for_each<test_create_table,ormpp::get_type_names::<lambda_ce8993546f247617066e6a0f110d02f9>>(T &&,F &&)”的引用
1> with
1> [
1> T=test_create_table,
1> F=ormpp::get_type_names::<lambda_ce8993546f247617066e6a0f110d02f9>
1> ]
1>mayun_git\secondarycounter\ormpp_compile\ormpp-master\mysql.hpp(381): note: 参见对正在编译的函数 模板 实例化“std::arraystd::string,2 ormpp::get_type_names(ormpp::DBType)”的引用
1> with
1> [
1> T=test_create_table
1> ]
1>dmayun_git\secondarycounter\ormpp_compile\ormpp-master\mysql.hpp(67): note: 参见对正在编译的函数 模板 实例化“std::string ormpp::mysql::generate_createtb_sql<T,ormpp_key&,ormpp_not_null&>(ormpp_key &,ormpp_not_null &)”的引用
1> with
1> [
1> T=test_create_table
1> ]
1>d:mayun_git\secondarycounter\ormpp_compile\ormpp-master\dbng.hpp(33): note: 参见对正在编译的函数 模板 实例化“bool ormpp::mysql::create_datatable<T,ormpp_key&,ormpp_not_null&>(ormpp_key &,ormpp_not_null &)”的引用
1> with
1> [
1> T=test_create_table
1> ]
1>d:\mayun_git\secondarycounter\ormpp_compile\ormpp-master\ormpp_test\ormpp_test.cpp(61): note: 参见对正在编译的函数 模板 实例化“bool ormpp::dbngormpp::mysql::create_datatable<test_create_table,ormpp_key&,ormpp_not_null&>(ormpp_key &,ormpp_not_null &)”的引用
1>dmayun_git\secondarycounter\ormpp_compile\ormpp-master\ormpp_test\ormpp_test.cpp(61): note: 参见对正在编译的函数 模板 实例化“bool ormpp::dbngormpp::mysql::create_datatable<test_create_table,ormpp_key&,ormpp_not_null&>(ormpp_key &,ormpp_not_null &)”的引用
1>d:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\type_traits(520): note: 参见对正在编译的 类 模板 实例化 "std::basic_string_view<char,std::char_traits>" 的引用
1>d:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\type_traits(1358): note: 参见对正在编译的 类 模板 实例化 "std::is_convertible<const _StringViewIsh (&),std::basic_string_view<char,std::char_traits>>" 的引用
1> with
1> [
1> _StringViewIsh=char [2]
1> ]
1>d:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\type_traits(1364): note: 参见对正在编译的 类 模板 实例化 "std::conjunction<std::is_convertible<const _StringViewIsh (&),std::basic_string_view<char,std::char_traits>>,std::negation<std::is_convertible<const _StringViewIsh (&),const _Elem *>>>" 的引用
1> with
1> [
1> _StringViewIsh=char [2],
1> _Elem=char
1> ]
1>d:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xstring(2105): note: 查看指向正在编译的变量模板“const bool conjunction_v<std::is_convertible<char const (&)[2],std::basic_string_view<char,std::char_traits > >,std::negation<std::is_convertible<char const (&)[2],char const *> > >”的引用
1>d:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xstring(3748): note: 参见对正在编译的 别名 模板 实例化 "_Is_string_view_ish<char[2]>" 的引用
1>已完成生成项目“ormpp_test.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
无法识别long类型?
This example (from README.md
) do not work: mysql.delete_records<person>();
. But this does: mysql.delete_records<person>("id > 0");
.
The check for an empty where-condition is commented out here:: https://github.com/qicosmos/ormpp/blob/master/utility.hpp#L172
If that is because a bug in VS2017, maybe wrap in an #ifdef _MSC_VER
or something.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.