From ea18e17618af2b065af6f558c0a11f6ea0837b09 Mon Sep 17 00:00:00 2001 From: "haifu.tian" Date: Mon, 28 Feb 2022 11:36:00 +0800 Subject: [PATCH 1/7] [dev] compile success --- include/common.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/common.h b/include/common.h index 1419b07..f6e2e95 100644 --- a/include/common.h +++ b/include/common.h @@ -7,6 +7,8 @@ #include #include +#include + #include #include #include From f80a78022cd71b3738c8aaec7fd19da183ecdbca Mon Sep 17 00:00:00 2001 From: "haifu.tian" Date: Mon, 28 Feb 2022 12:59:05 +0800 Subject: [PATCH 2/7] [dev] add checkCreateModel --- include/Database.h | 1 + include/bean_ptr_impl.hpp | 2 +- sample.cpp | 31 ++++++++++++++++++++++++++----- src/Database.cpp | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/include/Database.h b/include/Database.h index ac69ca8..fc05cb6 100644 --- a/include/Database.h +++ b/include/Database.h @@ -66,6 +66,7 @@ class Database : noncopyable std::vector checkModel(); void dropModel(); void createModel(); + void checkCreateModel(); template bean_ptr loadBean(sqlid_t objId); diff --git a/include/bean_ptr_impl.hpp b/include/bean_ptr_impl.hpp index 1bd55aa..42ca727 100644 --- a/include/bean_ptr_impl.hpp +++ b/include/bean_ptr_impl.hpp @@ -91,7 +91,7 @@ bean_ptr::bean_ptr() template bean_ptr::operator bool() const { - return get_id()!=Database::NULL_ID; + return const_cast< bean_ptr* >(this)->get_id()!=Database::NULL_ID; } template template diff --git a/sample.cpp b/sample.cpp index 82b4f5d..dc084ce 100644 --- a/sample.cpp +++ b/sample.cpp @@ -6,6 +6,8 @@ #include using namespace std; +#define TEST_OLD_DB 1 + class Person{ friend class hiberlite::access; template @@ -28,10 +30,16 @@ void createDB() hiberlite::Database db("sample.db"); //register bean classes db.registerBeanClass(); + +#if TEST_OLD_DB + std::vector tableNames = db.checkModel(); + db.checkCreateModel(); +#else //drop all tables beans will use db.dropModel(); //create those tables again with proper schema db.createModel(); +#endif const char* names[5]={"Stanley Marsh", "Kyle Broflovski", "Eric Theodore Cartman", "Kenneth McCormick", "Leopold Stotch"}; @@ -59,7 +67,7 @@ void printDB() cout << "found " << v.size() << " persons in the database:\n"; for(size_t j=0;jbio.size();i++) @@ -70,14 +78,27 @@ void printDB() void modifyDB() { + cout << string( 15, '=' ) + "\nmodify the DB\n"; hiberlite::Database db("sample.db"); db.registerBeanClass(); vector< hiberlite::bean_ptr > v=db.getAllBeans(); - cout << v[0]->name << " will be deleted.\n"; - v[0].destroy(); - cout << v[1]->name << " becomes 1 year older.\n\n"; - v[1]->age+=1; + + if( v.size() > 0 ) { + cout << v[0]->name << " will be deleted.\n"; + v[0].destroy(); + + if( v[0] ) { + cout << "error: v[0] should be null!"; + } + } + + if( v.size() > 1 ) { + cout << v[1]->name << " becomes 1 year older.\n\n"; + v[1]->age += 1; + } + + vector< hiberlite::bean_ptr > v2 = db.getAllBeans(); } int main() diff --git a/src/Database.cpp b/src/Database.cpp index e876d25..1813885 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -45,8 +45,17 @@ void Database::close() std::vector Database::checkModel() { + if( !mx ) + throw std::logic_error( "register bean classes first" ); + //TODO checkModel std::vector ans; + Model mdl = mx->getModel(); + for( Model::iterator it = mdl.begin(); it != mdl.end(); it++ ) { + Table& t = it->second; + ans.push_back( t.name ); + } + return ans; } @@ -87,6 +96,31 @@ void Database::createModel() } } +void Database::checkCreateModel() +{ + if( !mx ) + throw std::logic_error( "register bean classes first" ); + Model mdl = mx->getModel(); + for( Model::iterator it = mdl.begin(); it != mdl.end(); it++ ) { + Table& t = it->second; + std::string query = "CREATE TABLE IF NOT EXISTS " + t.name + " ("; + bool needComma = false; + for( std::map::iterator c = t.columns.begin(); c != t.columns.end(); c++ ) { + if( needComma ) + query += ", "; + needComma = true; + Column& col = c->second; + query += col.name + " "; + if( col.name == HIBERLITE_PRIMARY_KEY_COLUMN ) + query += "INTEGER PRIMARY KEY AUTOINCREMENT"; + else + query += col.storage_type; + } + query += ");"; + dbExecQuery( query ); + } +} + sqlid_t Database::allocId(shared_connection c, std::string table) { //THREAD critical call From 56bd00d44de6ff77cf25933a9813ad715e7da584 Mon Sep 17 00:00:00 2001 From: "haifu.tian" Date: Mon, 28 Feb 2022 13:15:58 +0800 Subject: [PATCH 3/7] [dev] add msvc v141 --- Build/hiberlite.sln | 71 ++++++++++++++- Build/hiberlite.vcxproj | 142 ++++++++++++++++++++++++++++++ Build/hiberlite_test.vcxproj | 162 +++++++++++++++++++++++++++++++++++ Build/sample.vcxproj | 162 +++++++++++++++++++++++++++++++++++ Build/sqlite.vcxproj | 146 +++++++++++++++++++++++++++++++ sample.cpp | 2 +- 6 files changed, 682 insertions(+), 3 deletions(-) diff --git a/Build/hiberlite.sln b/Build/hiberlite.sln index 8ba2053..dc1aad6 100644 --- a/Build/hiberlite.sln +++ b/Build/hiberlite.sln @@ -1,8 +1,14 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.1778 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "catch_test", "catch_test.vcxproj", "{27B715B5-136F-5702-7C76-305E684DF2F2}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hiberlite", "hiberlite.vcxproj", "{1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}" + ProjectSection(ProjectDependencies) = postProject + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43} = {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hiberlite_test", "hiberlite_test.vcxproj", "{FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}" EndProject @@ -18,6 +24,12 @@ Global Release|native = Release|native Release|Win32 = Release|Win32 Release|x64 = Release|x64 + v141Debug|native = v141Debug|native + v141Debug|Win32 = v141Debug|Win32 + v141Debug|x64 = v141Debug|x64 + v141Release|native = v141Release|native + v141Release|Win32 = v141Release|Win32 + v141Release|x64 = v141Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {27B715B5-136F-5702-7C76-305E684DF2F2}.Debug|native.ActiveCfg = Debug native|Win32 @@ -32,6 +44,18 @@ Global {27B715B5-136F-5702-7C76-305E684DF2F2}.Release|Win32.Build.0 = Release|Win32 {27B715B5-136F-5702-7C76-305E684DF2F2}.Release|x64.ActiveCfg = Release|x64 {27B715B5-136F-5702-7C76-305E684DF2F2}.Release|x64.Build.0 = Release|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|native.ActiveCfg = Release native|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|native.Build.0 = Release native|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|Win32.ActiveCfg = Debug|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|Win32.Build.0 = Debug|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|x64.ActiveCfg = Debug|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|x64.Build.0 = Debug|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|native.ActiveCfg = Release native|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|native.Build.0 = Release native|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|Win32.ActiveCfg = Release|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|Win32.Build.0 = Release|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|x64.ActiveCfg = Release|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|x64.Build.0 = Release|x64 {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Debug|native.ActiveCfg = Debug native|Win32 {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Debug|native.Build.0 = Debug native|Win32 {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Debug|Win32.ActiveCfg = Debug|Win32 @@ -44,6 +68,16 @@ Global {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Release|Win32.Build.0 = Release|Win32 {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Release|x64.ActiveCfg = Release|x64 {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Release|x64.Build.0 = Release|x64 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Debug|native.ActiveCfg = v141Debug|Win32 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Debug|Win32.ActiveCfg = v141Debug|Win32 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Debug|Win32.Build.0 = v141Debug|Win32 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Debug|x64.ActiveCfg = v141Debug|x64 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Debug|x64.Build.0 = v141Debug|x64 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Release|native.ActiveCfg = v141Release|Win32 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Release|Win32.ActiveCfg = v141Release|Win32 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Release|Win32.Build.0 = v141Release|Win32 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Release|x64.ActiveCfg = v141Release|x64 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.v141Release|x64.Build.0 = v141Release|x64 {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.Debug|native.ActiveCfg = Debug native|Win32 {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.Debug|native.Build.0 = Debug native|Win32 {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.Debug|Win32.ActiveCfg = Debug|Win32 @@ -56,6 +90,16 @@ Global {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.Release|Win32.Build.0 = Release|Win32 {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.Release|x64.ActiveCfg = Release|x64 {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.Release|x64.Build.0 = Release|x64 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Debug|native.ActiveCfg = v141Debug|Win32 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Debug|Win32.ActiveCfg = v141Debug|Win32 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Debug|Win32.Build.0 = v141Debug|Win32 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Debug|x64.ActiveCfg = v141Debug|x64 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Debug|x64.Build.0 = v141Debug|x64 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Release|native.ActiveCfg = v141Release|Win32 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Release|Win32.ActiveCfg = v141Release|Win32 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Release|Win32.Build.0 = v141Release|Win32 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Release|x64.ActiveCfg = v141Release|x64 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E}.v141Release|x64.Build.0 = v141Release|x64 {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.Debug|native.ActiveCfg = Debug native|Win32 {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.Debug|native.Build.0 = Debug native|Win32 {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.Debug|Win32.ActiveCfg = Debug|Win32 @@ -68,6 +112,16 @@ Global {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.Release|Win32.Build.0 = Release|Win32 {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.Release|x64.ActiveCfg = Release|x64 {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.Release|x64.Build.0 = Release|x64 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Debug|native.ActiveCfg = v141Debug|Win32 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Debug|Win32.ActiveCfg = v141Debug|Win32 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Debug|Win32.Build.0 = v141Debug|Win32 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Debug|x64.ActiveCfg = v141Debug|x64 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Debug|x64.Build.0 = v141Debug|x64 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Release|native.ActiveCfg = v141Release|Win32 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Release|Win32.ActiveCfg = v141Release|Win32 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Release|Win32.Build.0 = v141Release|Win32 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Release|x64.ActiveCfg = v141Release|x64 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41}.v141Release|x64.Build.0 = v141Release|x64 {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.Debug|native.ActiveCfg = Debug native|Win32 {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.Debug|native.Build.0 = Debug native|Win32 {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.Debug|Win32.ActiveCfg = Debug|Win32 @@ -80,8 +134,21 @@ Global {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.Release|Win32.Build.0 = Release|Win32 {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.Release|x64.ActiveCfg = Release|x64 {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.Release|x64.Build.0 = Release|x64 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Debug|native.ActiveCfg = v141Debug|Win32 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Debug|Win32.ActiveCfg = v141Debug|Win32 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Debug|Win32.Build.0 = v141Debug|Win32 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Debug|x64.ActiveCfg = v141Debug|x64 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Debug|x64.Build.0 = v141Debug|x64 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Release|native.ActiveCfg = v141Release|Win32 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Release|Win32.ActiveCfg = v141Release|Win32 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Release|Win32.Build.0 = v141Release|Win32 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Release|x64.ActiveCfg = v141Release|x64 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43}.v141Release|x64.Build.0 = v141Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3F6A4405-92F7-42A7-AB5C-BF85E1EF73B4} + EndGlobalSection EndGlobal diff --git a/Build/hiberlite.vcxproj b/Build/hiberlite.vcxproj index 320f573..7b75571 100644 --- a/Build/hiberlite.vcxproj +++ b/Build/hiberlite.vcxproj @@ -33,6 +33,22 @@ Release x64 + + v141Debug + Win32 + + + v141Debug + x64 + + + v141Release + Win32 + + + v141Release + x64 + {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62} @@ -53,12 +69,24 @@ MultiByte v120 + + StaticLibrary + true + MultiByte + v141 + StaticLibrary true MultiByte v120 + + StaticLibrary + true + MultiByte + v141 + StaticLibrary false @@ -71,12 +99,24 @@ MultiByte v120 + + StaticLibrary + false + MultiByte + v141 + StaticLibrary false MultiByte v120 + + StaticLibrary + false + MultiByte + v141 + @@ -86,18 +126,30 @@ + + + + + + + + + + + + ..\bin\hiberlite\Debug\ @@ -111,12 +163,24 @@ hiberlite .lib + + ..\bin\hiberlite\Debug\ + ..\obj\hiberlite\Debug\x32\ + hiberlite + .lib + ..\bin\hiberlite\Debug\ ..\obj\hiberlite\Debug\x64\ hiberlite .lib + + ..\bin\hiberlite\Debug\ + ..\obj\hiberlite\Debug\x64\ + hiberlite + .lib + ..\bin\hiberlite\Release\ ..\obj\hiberlite\Release\native\ @@ -129,12 +193,24 @@ hiberlite .lib + + ..\bin\hiberlite\Release\ + ..\obj\hiberlite\Release\x32\ + hiberlite + .lib + ..\bin\hiberlite\Release\ ..\obj\hiberlite\Release\x64\ hiberlite .lib + + ..\bin\hiberlite\Release\ + ..\obj\hiberlite\Release\x64\ + hiberlite + .lib + NotUsing @@ -163,6 +239,20 @@ true + + + NotUsing + Level3 + DEBUG;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + EditAndContinue + Disabled + + + Windows + true + + NotUsing @@ -177,6 +267,20 @@ true + + + NotUsing + Level3 + DEBUG;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + ProgramDatabase + Disabled + + + Windows + true + + NotUsing @@ -215,6 +319,25 @@ true + + + NotUsing + Level3 + RELEASE;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + + + Windows + false + true + true + + NotUsing @@ -234,6 +357,25 @@ true + + + NotUsing + Level3 + RELEASE;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + + + Windows + false + true + true + + diff --git a/Build/hiberlite_test.vcxproj b/Build/hiberlite_test.vcxproj index 26dedc4..48b1e77 100644 --- a/Build/hiberlite_test.vcxproj +++ b/Build/hiberlite_test.vcxproj @@ -33,6 +33,22 @@ Release x64 + + v141Debug + Win32 + + + v141Debug + x64 + + + v141Release + Win32 + + + v141Release + x64 + {FC275FB3-E8F5-52B9-D189-C3DFBDF62E1E} @@ -53,12 +69,24 @@ MultiByte v120 + + Application + true + MultiByte + v141 + Application true MultiByte v120 + + Application + true + MultiByte + v141 + Application false @@ -71,12 +99,24 @@ MultiByte v120 + + Application + false + MultiByte + v141 + Application false MultiByte v120 + + Application + false + MultiByte + v141 + @@ -86,18 +126,30 @@ + + + + + + + + + + + + true @@ -113,6 +165,13 @@ hiberlite_test .exe + + true + ..\bin\hiberlite_test\Debug\ + ..\obj\hiberlite_test\Debug\x32\ + hiberlite_test + .exe + true ..\bin\hiberlite_test\Debug\ @@ -120,6 +179,13 @@ hiberlite_test .exe + + true + ..\bin\hiberlite_test\Debug\ + ..\obj\hiberlite_test\Debug\x64\ + hiberlite_test + .exe + false ..\bin\hiberlite_test\Release\ @@ -134,6 +200,13 @@ hiberlite_test .exe + + false + ..\bin\hiberlite_test\Release\ + ..\obj\hiberlite_test\Release\x32\ + hiberlite_test + .exe + false ..\bin\hiberlite_test\Release\ @@ -141,6 +214,13 @@ hiberlite_test .exe + + false + ..\bin\hiberlite_test\Release\ + ..\obj\hiberlite_test\Release\x64\ + hiberlite_test + .exe + NotUsing @@ -177,6 +257,24 @@ "$(TargetPath)" + + + NotUsing + Level3 + DEBUG;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + EditAndContinue + Disabled + + + Console + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -195,6 +293,24 @@ "$(TargetPath)" + + + NotUsing + Level3 + DEBUG;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + ProgramDatabase + Disabled + + + Console + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -241,6 +357,29 @@ "$(TargetPath)" + + + NotUsing + Level3 + RELEASE;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + + + Console + false + true + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -264,6 +403,29 @@ "$(TargetPath)" + + + NotUsing + Level3 + RELEASE;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + + + Console + false + true + true + mainCRTStartup + + + "$(TargetPath)" + + diff --git a/Build/sample.vcxproj b/Build/sample.vcxproj index 83087ab..d18493c 100644 --- a/Build/sample.vcxproj +++ b/Build/sample.vcxproj @@ -33,6 +33,22 @@ Release x64 + + v141Debug + Win32 + + + v141Debug + x64 + + + v141Release + Win32 + + + v141Release + x64 + {073E391B-F3DF-63F1-DC9A-7745C8DBEA41} @@ -53,12 +69,24 @@ MultiByte v120 + + Application + true + MultiByte + v141 + Application true MultiByte v120 + + Application + true + MultiByte + v141 + Application false @@ -71,12 +99,24 @@ MultiByte v120 + + Application + false + MultiByte + v141 + Application false MultiByte v120 + + Application + false + MultiByte + v141 + @@ -86,18 +126,30 @@ + + + + + + + + + + + + true @@ -113,6 +165,13 @@ sample .exe + + true + ..\bin\sample\Debug\ + ..\obj\sample\Debug\x32\ + sample + .exe + true ..\bin\sample\Debug\ @@ -120,6 +179,13 @@ sample .exe + + true + ..\bin\sample\Debug\ + ..\obj\sample\Debug\x64\ + sample + .exe + false ..\bin\sample\Release\ @@ -134,6 +200,13 @@ sample .exe + + false + ..\bin\sample\Release\ + ..\obj\sample\Release\x32\ + sample + .exe + false ..\bin\sample\Release\ @@ -141,6 +214,13 @@ sample .exe + + false + ..\bin\sample\Release\ + ..\obj\sample\Release\x64\ + sample + .exe + NotUsing @@ -177,6 +257,24 @@ "$(TargetPath)" + + + NotUsing + Level3 + DEBUG;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + EditAndContinue + Disabled + + + Console + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -195,6 +293,24 @@ "$(TargetPath)" + + + NotUsing + Level3 + DEBUG;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + ProgramDatabase + Disabled + + + Console + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -241,6 +357,29 @@ "$(TargetPath)" + + + NotUsing + Level3 + RELEASE;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + + + Console + false + true + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -264,6 +403,29 @@ "$(TargetPath)" + + + NotUsing + Level3 + RELEASE;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + + + Console + false + true + true + mainCRTStartup + + + "$(TargetPath)" + + diff --git a/Build/sqlite.vcxproj b/Build/sqlite.vcxproj index 22cab93..400fb57 100644 --- a/Build/sqlite.vcxproj +++ b/Build/sqlite.vcxproj @@ -33,6 +33,22 @@ Release x64 + + v141Debug + Win32 + + + v141Debug + x64 + + + v141Release + Win32 + + + v141Release + x64 + {F71C5A1C-E3BE-84F2-CC79-9846B8BA0B43} @@ -53,12 +69,24 @@ MultiByte v120 + + StaticLibrary + true + MultiByte + v141 + StaticLibrary true MultiByte v120 + + StaticLibrary + true + MultiByte + v141 + StaticLibrary false @@ -71,12 +99,24 @@ MultiByte v120 + + StaticLibrary + false + MultiByte + v141 + StaticLibrary false MultiByte v120 + + StaticLibrary + false + MultiByte + v141 + @@ -86,18 +126,30 @@ + + + + + + + + + + + + ..\bin\sqlite\Debug\ @@ -111,12 +163,24 @@ sqlite .lib + + ..\bin\sqlite\Debug\ + ..\obj\sqlite\Debug\x32\ + sqlite + .lib + ..\bin\sqlite\Debug\ ..\obj\sqlite\Debug\x64\ sqlite .lib + + ..\bin\sqlite\Debug\ + ..\obj\sqlite\Debug\x64\ + sqlite + .lib + ..\bin\sqlite\Release\ ..\obj\sqlite\Release\native\ @@ -129,12 +193,24 @@ sqlite .lib + + ..\bin\sqlite\Release\ + ..\obj\sqlite\Release\x32\ + sqlite + .lib + ..\bin\sqlite\Release\ ..\obj\sqlite\Release\x64\ sqlite .lib + + ..\bin\sqlite\Release\ + ..\obj\sqlite\Release\x64\ + sqlite + .lib + NotUsing @@ -165,6 +241,21 @@ true + + + NotUsing + Level3 + DEBUG;_DEBUG;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + EditAndContinue + Disabled + CompileAsC + + + Windows + true + + NotUsing @@ -180,6 +271,21 @@ true + + + NotUsing + Level3 + DEBUG;_DEBUG;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + ProgramDatabase + Disabled + CompileAsC + + + Windows + true + + NotUsing @@ -220,6 +326,26 @@ true + + + NotUsing + Level3 + RELEASE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + CompileAsC + + + Windows + false + true + true + + NotUsing @@ -240,6 +366,26 @@ true + + + NotUsing + Level3 + RELEASE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + CompileAsC + + + Windows + false + true + true + + diff --git a/sample.cpp b/sample.cpp index dc084ce..f08687d 100644 --- a/sample.cpp +++ b/sample.cpp @@ -6,7 +6,7 @@ #include using namespace std; -#define TEST_OLD_DB 1 +#define TEST_OLD_DB 0 class Person{ friend class hiberlite::access; From 3b87dc149d0dca43065dc851f994e7e598b8e497 Mon Sep 17 00:00:00 2001 From: "haifu.tian" Date: Tue, 5 Apr 2022 16:20:30 +0800 Subject: [PATCH 4/7] [misc] 1.catch_test add v141 2.add wstring to database --- Build/catch_test.vcxproj | 162 ++++++++++++++++++++++++++++++++++++++ Build/hiberlite.sln | 24 +++--- include/Database.h | 2 +- include/bean_ptr_impl.hpp | 2 +- include/common.h | 2 +- include/hiberdefs.h | 2 +- include/some_types.h | 41 +++++++++- sample.cpp | 23 +++++- src/Database.cpp | 2 +- 9 files changed, 238 insertions(+), 22 deletions(-) diff --git a/Build/catch_test.vcxproj b/Build/catch_test.vcxproj index a7648e8..8d524a9 100644 --- a/Build/catch_test.vcxproj +++ b/Build/catch_test.vcxproj @@ -33,6 +33,22 @@ Release x64 + + v141Debug + Win32 + + + v141Debug + x64 + + + v141Release + Win32 + + + v141Release + x64 + {27B715B5-136F-5702-7C76-305E684DF2F2} @@ -53,12 +69,24 @@ MultiByte v120 + + Application + true + MultiByte + v141 + Application true MultiByte v120 + + Application + true + MultiByte + v141 + Application false @@ -71,12 +99,24 @@ MultiByte v120 + + Application + false + MultiByte + v141 + Application false MultiByte v120 + + Application + false + MultiByte + v141 + @@ -86,18 +126,30 @@ + + + + + + + + + + + + true @@ -113,6 +165,13 @@ catch_test .exe + + true + ..\bin\catch_test\Debug\ + ..\obj\catch_test\Debug\x32\ + catch_test + .exe + true ..\bin\catch_test\Debug\ @@ -120,6 +179,13 @@ catch_test .exe + + true + ..\bin\catch_test\Debug\ + ..\obj\catch_test\Debug\x64\ + catch_test + .exe + false ..\bin\catch_test\Release\ @@ -134,6 +200,13 @@ catch_test .exe + + false + ..\bin\catch_test\Release\ + ..\obj\catch_test\Release\x32\ + catch_test + .exe + false ..\bin\catch_test\Release\ @@ -141,6 +214,13 @@ catch_test .exe + + false + ..\bin\catch_test\Release\ + ..\obj\catch_test\Release\x64\ + catch_test + .exe + NotUsing @@ -177,6 +257,24 @@ "$(TargetPath)" + + + NotUsing + Level3 + DEBUG;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + EditAndContinue + Disabled + + + Console + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -195,6 +293,24 @@ "$(TargetPath)" + + + NotUsing + Level3 + DEBUG;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + ProgramDatabase + Disabled + + + Console + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -241,6 +357,29 @@ "$(TargetPath)" + + + NotUsing + Level3 + RELEASE;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + + + Console + false + true + true + mainCRTStartup + + + "$(TargetPath)" + + NotUsing @@ -264,6 +403,29 @@ "$(TargetPath)" + + + NotUsing + Level3 + RELEASE;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;%(PreprocessorDefinitions) + ..\include;..\sqlite-amalgamation;..\Catch\single_include;%(AdditionalIncludeDirectories) + Full + true + true + false + true + + + Console + false + true + true + mainCRTStartup + + + "$(TargetPath)" + + diff --git a/Build/hiberlite.sln b/Build/hiberlite.sln index dc1aad6..d53c18b 100644 --- a/Build/hiberlite.sln +++ b/Build/hiberlite.sln @@ -44,18 +44,18 @@ Global {27B715B5-136F-5702-7C76-305E684DF2F2}.Release|Win32.Build.0 = Release|Win32 {27B715B5-136F-5702-7C76-305E684DF2F2}.Release|x64.ActiveCfg = Release|x64 {27B715B5-136F-5702-7C76-305E684DF2F2}.Release|x64.Build.0 = Release|x64 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|native.ActiveCfg = Release native|x64 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|native.Build.0 = Release native|x64 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|Win32.ActiveCfg = Debug|Win32 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|Win32.Build.0 = Debug|Win32 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|x64.ActiveCfg = Debug|x64 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|x64.Build.0 = Debug|x64 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|native.ActiveCfg = Release native|x64 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|native.Build.0 = Release native|x64 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|Win32.ActiveCfg = Release|Win32 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|Win32.Build.0 = Release|Win32 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|x64.ActiveCfg = Release|x64 - {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|x64.Build.0 = Release|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|native.ActiveCfg = v141Debug|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|native.Build.0 = v141Debug|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|Win32.ActiveCfg = v141Debug|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|Win32.Build.0 = v141Debug|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|x64.ActiveCfg = v141Debug|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Debug|x64.Build.0 = v141Debug|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|native.ActiveCfg = v141Release|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|native.Build.0 = v141Release|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|Win32.ActiveCfg = v141Release|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|Win32.Build.0 = v141Release|Win32 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|x64.ActiveCfg = v141Release|x64 + {27B715B5-136F-5702-7C76-305E684DF2F2}.v141Release|x64.Build.0 = v141Release|x64 {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Debug|native.ActiveCfg = Debug native|Win32 {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Debug|native.Build.0 = Debug native|Win32 {1DFD5A97-8967-0F0E-D2A5-C0B33E4FED62}.Debug|Win32.ActiveCfg = Debug|Win32 diff --git a/include/Database.h b/include/Database.h index fc05cb6..53258e2 100644 --- a/include/Database.h +++ b/include/Database.h @@ -1,4 +1,4 @@ -#ifndef DATABASE_H +#ifndef DATABASE_H #define DATABASE_H namespace hiberlite{ diff --git a/include/bean_ptr_impl.hpp b/include/bean_ptr_impl.hpp index 42ca727..d96d044 100644 --- a/include/bean_ptr_impl.hpp +++ b/include/bean_ptr_impl.hpp @@ -1,4 +1,4 @@ -namespace hiberlite{ +namespace hiberlite{ template real_bean::real_bean(const bean_key _key, C* _obj) : key(_key), obj(_obj), forgotten(false) diff --git a/include/common.h b/include/common.h index f6e2e95..f8c03fc 100644 --- a/include/common.h +++ b/include/common.h @@ -1,4 +1,4 @@ -#ifndef COMMON_H_ +#ifndef COMMON_H_ #define COMMON_H_ #include diff --git a/include/hiberdefs.h b/include/hiberdefs.h index 1293ce9..366b31e 100644 --- a/include/hiberdefs.h +++ b/include/hiberdefs.h @@ -1,4 +1,4 @@ -#ifndef HIBERDEFS_H_INCLUDED +#ifndef HIBERDEFS_H_INCLUDED #define HIBERDEFS_H_INCLUDED #if (defined _MSC_VER && _MSC_VER< 1600) diff --git a/include/some_types.h b/include/some_types.h index e5dbc70..361d34b 100644 --- a/include/some_types.h +++ b/include/some_types.h @@ -1,8 +1,24 @@ -#ifndef SOM_TYPES_H_INCLUDED +#ifndef SOM_TYPES_H_INCLUDED #define SOM_TYPES_H_INCLUDED + +#include +#include + namespace hiberlite{ + inline std::string wstringToUtf8( const std::wstring& str ) + { + std::wstring_convert > strCnv; + return strCnv.to_bytes( str ); + } + + inline std::wstring utf8ToWstring( const std::string& str ) + { + std::wstring_convert< std::codecvt_utf8 > strCnv; + return strCnv.from_bytes( str ); + } + template class stl_stream_adapter{ C& ct; @@ -154,6 +170,29 @@ inline void db_atom::bindValue(sqlite3_stmt* stmt, int col) { sqlite3_bind_text(stmt, col, val.c_str(), -1, SQLITE_TRANSIENT); } + +// std::wstring -> TEXT type + +template +void hibernate( A& ar, std::wstring& value, const unsigned int ) { + ar & db_atom( value ); +} + +template<> template +void db_atom::loadValue( Stmt& res, Arg& arg ) { + val = utf8ToWstring( std::string( ( const char* )(res.get_text( arg )) ) ); +} + +template<> +inline std::string db_atom::sqliteStorageClass() { + return "TEXT"; +} + +template<> +inline void db_atom::bindValue( sqlite3_stmt* stmt, int col ) { + sqlite3_bind_text( stmt, col, wstringToUtf8(val).c_str(), -1, SQLITE_TRANSIENT ); +} + // std::vector -> BLOB type template diff --git a/sample.cpp b/sample.cpp index f08687d..e29903d 100644 --- a/sample.cpp +++ b/sample.cpp @@ -1,4 +1,4 @@ -#include "hiberlite.h" +#include "hiberlite.h" #include #include @@ -13,14 +13,18 @@ class Person{ template void hibernate(Archive & ar) { - ar & HIBERLITE_NVP(name); + ar & HIBERLITE_NVP( name ); + ar & HIBERLITE_NVP( wname ); ar & HIBERLITE_NVP(age); - ar & HIBERLITE_NVP(bio); + ar & HIBERLITE_NVP( bio ); + ar & HIBERLITE_NVP( bio2 ); } public: string name; + wstring wname; double age; vector bio; + vector bio2; }; HIBERLITE_EXPORT_CLASS(Person) @@ -46,11 +50,16 @@ void createDB() for(unsigned int i=0;i<5;i++) { Person x; x.name=names[i%5]; + x.wname = hiberlite::utf8ToWstring( names[i % 5] ); x.age=14+i*0.1; x.bio.push_back("Hello"); x.bio.push_back("world"); x.bio.push_back("!"); + x.bio2.push_back( L"wHello" ); + x.bio2.push_back( L"wworld" ); + x.bio2.push_back( L"!" ); + hiberlite::bean_ptr p=db.copyBean(x); //create a managed copy of the object } } @@ -68,11 +77,17 @@ void printDB() for(size_t j=0;jbio.size();i++) i && cout << ", ", cout << v[j]->bio[i]; - cout << "}]\n"; + cout << "}\t"; + + cout << "bio2={"; + for( size_t i = 0; i < v[j]->bio2.size(); i++ ) + i && cout << ", ", cout << hiberlite::wstringToUtf8( v[j]->bio2[i]); + cout << "}]\n"; } } diff --git a/src/Database.cpp b/src/Database.cpp index 1813885..c501d76 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -1,4 +1,4 @@ - + #include "hiberlite.h" namespace hiberlite{ From 11277371b63622edaabb81dae91c8b8d8d3f1486 Mon Sep 17 00:00:00 2001 From: "haifu.tian" Date: Tue, 5 Apr 2022 22:12:47 +0800 Subject: [PATCH 5/7] =?UTF-8?q?[dev]=20=E5=8A=A0=E4=BA=86=E5=A4=96?= =?UTF-8?q?=E9=83=A8=E4=BD=BF=E7=94=A8=E4=BA=8B=E5=8A=A1=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E8=83=BD=E5=8A=A0=E5=BF=AB=E4=B8=80=E5=8D=8A?= =?UTF-8?q?=E7=9A=84=E9=80=9F=E5=BA=A6,=E4=BD=86=E8=BF=98=E6=98=AF?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E6=85=A2=EF=BC=8C=E8=BF=98=E8=83=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Build/hiberlite_test.vcxproj | 2 +- Build/sample.vcxproj | 2 +- include/Database.h | 33 +++++++- include/Database_tmpl_impl.hpp | 140 ++++++++++++++++++++++++++++----- include/Registry.h | 6 +- include/Registry_impl.hpp | 16 ++-- include/bean_ptr.h | 14 +++- include/bean_ptr_impl.hpp | 16 ++-- sample.cpp | 53 ++++++++++++- 9 files changed, 233 insertions(+), 49 deletions(-) diff --git a/Build/hiberlite_test.vcxproj b/Build/hiberlite_test.vcxproj index 48b1e77..f0b6191 100644 --- a/Build/hiberlite_test.vcxproj +++ b/Build/hiberlite_test.vcxproj @@ -308,7 +308,7 @@ mainCRTStartup - "$(TargetPath)" + rem "$(TargetPath)" diff --git a/Build/sample.vcxproj b/Build/sample.vcxproj index d18493c..ae37777 100644 --- a/Build/sample.vcxproj +++ b/Build/sample.vcxproj @@ -308,7 +308,7 @@ mainCRTStartup - "$(TargetPath)" + rem "$(TargetPath)" diff --git a/include/Database.h b/include/Database.h index 53258e2..1f7308c 100644 --- a/include/Database.h +++ b/include/Database.h @@ -5,6 +5,26 @@ namespace hiberlite{ class ModelExtractor; class UpdateBean; +class Database; + +namespace detail { + class DatabaseTransaction + { + public: + inline DatabaseTransaction(); + inline ~DatabaseTransaction(); + inline DatabaseTransaction( const DatabaseTransaction& ); + inline DatabaseTransaction& operator=( const DatabaseTransaction& ); + + private: + friend class Database; + inline DatabaseTransaction( shared_connection key, bool bRollback = true ); + + bool m_bValid = true; + //bool m_bRollback = true; + shared_connection m_connection; + }; +} class Database : noncopyable { @@ -12,6 +32,7 @@ class Database : noncopyable friend class UpdateBean; friend class KillChildren; friend class LoadBean; + friend class detail::DatabaseTransaction; template friend class real_bean; @@ -19,10 +40,10 @@ class Database : noncopyable ModelExtractor* mx; template - static void dbDelete(bean_key key, C& bean); + static void dbDelete(bean_key key, C& bean, bool bUseTransaction ); template - static void dbUpdate(bean_key key, C& bean); + static void dbUpdate(bean_key key, C& bean, bool bUseTransaction ); template static C* dbLoad(bean_key key); @@ -44,6 +65,8 @@ class Database : noncopyable template static void dbDeleteRows( shared_connection con, std::string table, std::string column, C value ); + inline bool shouldInsideUseTransaction() const; + public: static const sqlid_t NULL_ID=-1; @@ -68,6 +91,10 @@ class Database : noncopyable void createModel(); void checkCreateModel(); + /** 外部使用事务,内部就不再使用事务了,能快一点 */ + inline bool beginTransaction( bool bRollback = true ); + inline bool endTransaction(); + template bean_ptr loadBean(sqlid_t objId); @@ -85,6 +112,8 @@ class Database : noncopyable template bean_ptr manageBean(C* ptr); + + detail::DatabaseTransaction m_transaction; }; } //namespace hiberlite; diff --git a/include/Database_tmpl_impl.hpp b/include/Database_tmpl_impl.hpp index 7d82394..3cfb2fd 100644 --- a/include/Database_tmpl_impl.hpp +++ b/include/Database_tmpl_impl.hpp @@ -1,21 +1,103 @@ -#ifndef DATABASE_TMPL_IMPL_HPP_INCLUDED +#ifndef DATABASE_TMPL_IMPL_HPP_INCLUDED #define DATABASE_TMPL_IMPL_HPP_INCLUDED namespace hiberlite{ + namespace detail { + inline DatabaseTransaction::DatabaseTransaction() + : m_bValid( false ) + { + } + inline DatabaseTransaction::DatabaseTransaction( shared_connection connection, bool bRollback ) + : m_bValid(true) + , m_connection( connection ) { + if( bRollback ) { + try { + Database::dbExecQuery( connection, "ROLLBACK TRANSACTION;" ); + } + catch( ... ) {} + } + Database::dbExecQuery( connection, "BEGIN TRANSACTION;" ); + } + inline DatabaseTransaction::~DatabaseTransaction() { + if( m_bValid ) { + Database::dbExecQuery( m_connection, "COMMIT TRANSACTION;" ); + m_bValid = false; + } + } + inline DatabaseTransaction::DatabaseTransaction( const DatabaseTransaction& o ) { + m_connection = o.m_connection; + m_bValid = o.m_bValid; + const_cast< DatabaseTransaction& >( o ).m_bValid = false; + } + inline DatabaseTransaction& DatabaseTransaction::operator=( const DatabaseTransaction& o ) { + m_connection = o.m_connection; + m_bValid = o.m_bValid; + const_cast< DatabaseTransaction& >(o).m_bValid = false; + return *this; + } + } + + inline bool Database::beginTransaction( bool bRollback ) { + if( m_transaction.m_bValid ) { + return false; + } + else { + m_transaction = detail::DatabaseTransaction( con, bRollback ); + return true; + } + } + + inline bool Database::shouldInsideUseTransaction() const { + if( m_transaction.m_bValid ) { + return false; + } + else { + return true; + } + } + + inline bool Database::endTransaction() { + if( m_transaction.m_bValid ) { + m_transaction.~DatabaseTransaction(); + return true ; + } + else { + return false; + } + } + template -void Database::dbDelete(bean_key key, C& bean) +void Database::dbDelete(bean_key key, C& bean, bool bUseTransaction ) { - try{ - dbExecQuery(key.con,"ROLLBACK TRANSACTION;"); - }catch(...){} - dbExecQuery(key.con,"BEGIN TRANSACTION;"); + if( bUseTransaction ) + { + try { + dbExecQuery( key.con, "ROLLBACK TRANSACTION;" ); + } + catch( ... ) {} + dbExecQuery( key.con, "BEGIN TRANSACTION;" ); + + ChildKiller ck; + ck.killChildren( key, bean ); + dbDeleteRows( key.con, Database::getClassName(), HIBERLITE_PRIMARY_KEY_COLUMN, key.id ); + + dbExecQuery( key.con, "COMMIT TRANSACTION;" ); + } + else { + try { + //dbExecQuery( key.con, "ROLLBACK TRANSACTION;" ); + } + catch( ... ) {} + //dbExecQuery( key.con, "BEGIN TRANSACTION;" ); ChildKiller ck; - ck.killChildren(key,bean); - dbDeleteRows(key.con, Database::getClassName(), HIBERLITE_PRIMARY_KEY_COLUMN, key.id); + ck.killChildren( key, bean ); + dbDeleteRows( key.con, Database::getClassName(), HIBERLITE_PRIMARY_KEY_COLUMN, key.id ); - dbExecQuery(key.con,"COMMIT TRANSACTION;"); + //dbExecQuery( key.con, "COMMIT TRANSACTION;" ); + } + } template @@ -28,19 +110,37 @@ void Database::dbDeleteRows(shared_connection con, std::string table, std::strin template -void Database::dbUpdate(bean_key key, C& bean) +void Database::dbUpdate(bean_key key, C& bean, bool bUseTransaction ) { - try{ - dbExecQuery(key.con,"ROLLBACK TRANSACTION;"); - }catch(...){} - dbExecQuery(key.con,"BEGIN TRANSACTION;"); + if( bUseTransaction ) { + try { + dbExecQuery( key.con, "ROLLBACK TRANSACTION;" ); + } + catch( ... ) {} + dbExecQuery( key.con, "BEGIN TRANSACTION;" ); + + ChildKiller ck; + ck.killChildren( key, bean ); + BeanUpdater u; + u.update( key, bean ); + + dbExecQuery( key.con, "COMMIT TRANSACTION;" ); + } + else { + try { + //dbExecQuery( key.con, "ROLLBACK TRANSACTION;" ); + } + catch( ... ) {} + //dbExecQuery( key.con, "BEGIN TRANSACTION;" ); ChildKiller ck; - ck.killChildren(key,bean); + ck.killChildren( key, bean ); BeanUpdater u; - u.update(key, bean); + u.update( key, bean ); - dbExecQuery(key.con,"COMMIT TRANSACTION;"); + //dbExecQuery( key.con, "COMMIT TRANSACTION;" ); + } + } template @@ -68,14 +168,14 @@ inline bean_ptr Database::manageBean(C* ptr) { sqlid_t id=allocId( Database::getClassName() ); bean_key key(con,id); - dbUpdate(key,*ptr); - return Registry::createBeanPtr(key,ptr); + dbUpdate(key,*ptr, !m_transaction.m_bValid); + return Registry::createBeanPtr(this, key,ptr); } template inline bean_ptr Database::loadBean(sqlid_t id) { - return Registry::get( bean_key(con,id) ); + return Registry::get( this, bean_key(con,id) ); } template diff --git a/include/Registry.h b/include/Registry.h index 24bcc49..8935bff 100644 --- a/include/Registry.h +++ b/include/Registry.h @@ -1,4 +1,4 @@ -#ifndef REGISTRY_H_INCLUDED +#ifndef REGISTRY_H_INCLUDED #define REGISTRY_H_INCLUDED namespace hiberlite{ @@ -32,10 +32,10 @@ class Registry{ static void notifyRBDies(rb_pair* r); public: - static bean_ptr get(const bean_key key); + static bean_ptr get( Database* pDb, const bean_key key); static bool has(const bean_key key); - static bean_ptr createBeanPtr(bean_key key, C* obj); + static bean_ptr createBeanPtr( Database* pDb, bean_key key, C* obj); }; template diff --git a/include/Registry_impl.hpp b/include/Registry_impl.hpp index d84234f..fe79de3 100644 --- a/include/Registry_impl.hpp +++ b/include/Registry_impl.hpp @@ -1,4 +1,4 @@ -namespace hiberlite{ +namespace hiberlite{ template rb_pair::~rb_pair() @@ -18,12 +18,12 @@ bean_key rb_pair::get_key() } template -bean_ptr Registry::createBeanPtr(bean_key key, C* obj) +bean_ptr Registry::createBeanPtr( Database* pDb, bean_key key, C* obj) { if(key.id==Database::NULL_ID) - return bean_ptr(key,NULL); + return bean_ptr( pDb, key,NULL); - real_bean* rb=new real_bean(key,obj); + real_bean* rb=new real_bean( pDb, key,obj); rb_pair* para=new rb_pair(rb); if( rbpairs.find(key)!=rbpairs.end() ) @@ -31,19 +31,19 @@ bean_ptr Registry::createBeanPtr(bean_key key, C* obj) rbpairs[key]=para; - bean_ptr ans(key, para); + bean_ptr ans( pDb, key, para); return ans; } template -bean_ptr Registry::get(const bean_key key) +bean_ptr Registry::get( Database* pDb, const bean_key key) { typename std::map* >::iterator it; it=rbpairs.find(key); if(it==rbpairs.end()) - return createBeanPtr(key,NULL); + return createBeanPtr( pDb, key,NULL); else - return bean_ptr(key, it->second); + return bean_ptr( pDb, key, it->second); } template diff --git a/include/bean_ptr.h b/include/bean_ptr.h index 8e881ac..83f1a31 100644 --- a/include/bean_ptr.h +++ b/include/bean_ptr.h @@ -1,4 +1,4 @@ -#ifndef BEAN_PTR_H_INCLUDED +#ifndef BEAN_PTR_H_INCLUDED #define BEAN_PTR_H_INCLUDED namespace hiberlite{ @@ -27,16 +27,22 @@ class real_bean : noncopyable { void destroy(); bool destroyed() const { return forgotten; } + Database* database() const { + return m_pDb; + } + + protected: inline void loadLazy(); + Database* m_pDb = nullptr; bean_key key; C* obj; bool forgotten; private: friend class Registry; - real_bean(const bean_key _key, C* _obj); //only Registry can create the real_bean + real_bean( Database* pDb, const bean_key _key, C* _obj); //only Registry can create the real_bean }; template @@ -47,12 +53,12 @@ class bean_ptr : public shared_res< real_bean > void hibernate(Archive & ar); friend class Registry; - bean_ptr(bean_key k, rb_pair* rbpair); + bean_ptr( Database* pDb, bean_key k, rb_pair* rbpair); sqlid_t tmp_id; public: - bean_ptr(bean_key k); + bean_ptr( Database* pDb, bean_key k); bean_ptr(); diff --git a/include/bean_ptr_impl.hpp b/include/bean_ptr_impl.hpp index d96d044..4a4327c 100644 --- a/include/bean_ptr_impl.hpp +++ b/include/bean_ptr_impl.hpp @@ -1,7 +1,8 @@ namespace hiberlite{ template -real_bean::real_bean(const bean_key _key, C* _obj) : key(_key), obj(_obj), forgotten(false) +real_bean::real_bean( Database* pDb, const bean_key _key, C* _obj) + : key(_key), m_pDb( pDb ), obj(_obj), forgotten(false) {} template @@ -15,7 +16,7 @@ template void real_bean::destroy() { if(forgotten) return; - Database::dbDelete(key, *obj); + Database::dbDelete( key, *obj, m_pDb->shouldInsideUseTransaction() ); delete obj; forgotten=true; obj=NULL; @@ -28,7 +29,7 @@ void real_bean::save() { return; if(!obj) return; - Database::dbUpdate(key, *obj); + Database::dbUpdate( key, *obj, m_pDb->shouldInsideUseTransaction() ); } template @@ -61,7 +62,7 @@ inline void real_bean::loadLazy() } template -bean_ptr::bean_ptr(bean_key k, rb_pair* para) +bean_ptr::bean_ptr( Database* pDb, bean_key k, rb_pair* para) { this->takeRes(para); } @@ -79,9 +80,10 @@ bean_ptr& bean_ptr::operator=(const bean_ptr& other) } template -bean_ptr::bean_ptr(bean_key k) +bean_ptr::bean_ptr( Database* pDb, bean_key k) { - *this=Registry::get(k); + m_pDb = pDb; + *this=Registry::get( pDb, k); } template @@ -105,7 +107,7 @@ void bean_ptr::hibernate(Archive & ar) ar & hiberlite::sql_nvp< sqlid_t > ("id", tmp_id ); if(ar.is_loading()) - *this=Registry::get( bean_key(ar.getConnection(), tmp_id) ); + *this=Registry::get( res->getRes()->database(), bean_key(ar.getConnection(), tmp_id) ); } template diff --git a/sample.cpp b/sample.cpp index e29903d..defa14a 100644 --- a/sample.cpp +++ b/sample.cpp @@ -14,10 +14,10 @@ class Person{ void hibernate(Archive & ar) { ar & HIBERLITE_NVP( name ); - ar & HIBERLITE_NVP( wname ); + //ar & HIBERLITE_NVP( wname ); ar & HIBERLITE_NVP(age); ar & HIBERLITE_NVP( bio ); - ar & HIBERLITE_NVP( bio2 ); + //ar & HIBERLITE_NVP( bio2 ); } public: string name; @@ -64,6 +64,52 @@ void createDB() } } +/**数据量稍微一大,就慢的要命。 +https://blog.csdn.net/majiakun1/article/details/46607163 +加了外部使用事务的接口,能快一点 +*/ +void createDBmuch() +{ + hiberlite::Database db( "sample.db" ); + //register bean classes + db.registerBeanClass(); + +#if TEST_OLD_DB + std::vector tableNames = db.checkModel(); + db.checkCreateModel(); +#else + //drop all tables beans will use + db.dropModel(); + //create those tables again with proper schema + db.createModel(); +#endif + + const char* names[5] = { "Stanley Marsh", "Kyle Broflovski", "Eric Theodore Cartman", "Kenneth McCormick", "Leopold Stotch" }; + + db.beginTransaction(false); + + for( int j = 0; j < 100; ++j ) { + for( unsigned int i = 0; i < 5; i++ ) { + Person x; + x.name = names[i % 5] ; + x.name += " " + std::to_string(j); + x.wname = hiberlite::utf8ToWstring( names[i % 5] ); + x.age = 14 + i * 0.1; + x.bio.push_back( "Hello" ); + x.bio.push_back( "world" ); + x.bio.push_back( "!" ); + + x.bio2.push_back( L"wHello" ); + x.bio2.push_back( L"wworld" ); + x.bio2.push_back( L"!" ); + + hiberlite::bean_ptr p = db.copyBean( x ); //create a managed copy of the object + } + } + + db.endTransaction(); +} + void printDB() { hiberlite::Database db("sample.db"); @@ -118,7 +164,8 @@ void modifyDB() int main() { - createDB(); + //createDB(); + createDBmuch(); printDB(); modifyDB(); printDB(); From 5fd32773f888b64c3f319367f0e1da4aa12383ab Mon Sep 17 00:00:00 2001 From: "haifu.tian" Date: Sun, 10 Apr 2022 20:48:11 +0800 Subject: [PATCH 6/7] [dev] 1.add synchronous 2.transactionGuard --- include/Database.h | 59 ++++++++++-------- include/Database_tmpl_impl.hpp | 105 ++++++++++++++++++--------------- sample.cpp | 71 +++++++++++++++++++--- src/Database.cpp | 22 ++++++- 4 files changed, 174 insertions(+), 83 deletions(-) diff --git a/include/Database.h b/include/Database.h index 1f7308c..d81b052 100644 --- a/include/Database.h +++ b/include/Database.h @@ -7,24 +7,29 @@ class ModelExtractor; class UpdateBean; class Database; -namespace detail { - class DatabaseTransaction - { - public: - inline DatabaseTransaction(); - inline ~DatabaseTransaction(); - inline DatabaseTransaction( const DatabaseTransaction& ); - inline DatabaseTransaction& operator=( const DatabaseTransaction& ); - - private: - friend class Database; - inline DatabaseTransaction( shared_connection key, bool bRollback = true ); +class DatabaseTransaction +{ +public: + inline DatabaseTransaction(); + inline ~DatabaseTransaction(); + inline DatabaseTransaction( DatabaseTransaction& ); + inline DatabaseTransaction& operator=( DatabaseTransaction& ); + +private: + friend class Database; + inline DatabaseTransaction( Database* db, bool bRollback = true ); + inline bool isValid() const; + + //bool m_bRollback = true; + Database* m_pDb = nullptr; +}; - bool m_bValid = true; - //bool m_bRollback = true; - shared_connection m_connection; - }; -} +enum EDBSynchronous { + EDBSynchronous_Default, + EDBSynchronous_Full, + EDBSynchronous_Normal, + EDBSynchronous_Off, +}; class Database : noncopyable { @@ -32,7 +37,7 @@ class Database : noncopyable friend class UpdateBean; friend class KillChildren; friend class LoadBean; - friend class detail::DatabaseTransaction; + friend class DatabaseTransaction; template friend class real_bean; @@ -67,6 +72,9 @@ class Database : noncopyable inline bool shouldInsideUseTransaction() const; + int m_nOutTransactionNum = 0; + bool m_bOutbRollback = false; + public: static const sqlid_t NULL_ID=-1; @@ -74,10 +82,10 @@ class Database : noncopyable static std::string getClassName(); Database(); - Database(std::string fname); + Database(std::string fname, EDBSynchronous sync = EDBSynchronous_Default ); virtual ~Database(); - void open(std::string fname); + void open(std::string fname, EDBSynchronous ); void close(); template @@ -92,8 +100,9 @@ class Database : noncopyable void checkCreateModel(); /** 外部使用事务,内部就不再使用事务了,能快一点 */ - inline bool beginTransaction( bool bRollback = true ); - inline bool endTransaction(); + inline void beginTransaction( bool bRollback = true ); + inline void endTransaction(); + inline DatabaseTransaction transactionGuard( bool bRollback = true ); template bean_ptr loadBean(sqlid_t objId); @@ -107,13 +116,15 @@ class Database : noncopyable template bean_ptr copyBean(const C& c); + /** 不需要返回值的话,这个方法比 copyBean 高效 */ + template + void copyBeanToDB( const C& c ); + template bean_ptr createBean(); template bean_ptr manageBean(C* ptr); - - detail::DatabaseTransaction m_transaction; }; } //namespace hiberlite; diff --git a/include/Database_tmpl_impl.hpp b/include/Database_tmpl_impl.hpp index 3cfb2fd..0ef46aa 100644 --- a/include/Database_tmpl_impl.hpp +++ b/include/Database_tmpl_impl.hpp @@ -3,68 +3,70 @@ namespace hiberlite{ - namespace detail { - inline DatabaseTransaction::DatabaseTransaction() - : m_bValid( false ) - { + inline DatabaseTransaction::DatabaseTransaction() + : m_pDb( nullptr ) + { + } + inline DatabaseTransaction::DatabaseTransaction( Database* pDb, bool bRollback ) + : m_pDb(pDb) { + if( m_pDb ) { + m_pDb->beginTransaction( bRollback ); + } + } + inline DatabaseTransaction::~DatabaseTransaction() { + if( m_pDb ) { + m_pDb->endTransaction( ); + m_pDb = nullptr; } - inline DatabaseTransaction::DatabaseTransaction( shared_connection connection, bool bRollback ) - : m_bValid(true) - , m_connection( connection ) { + } + inline DatabaseTransaction::DatabaseTransaction( DatabaseTransaction& o ) { + m_pDb = o.m_pDb; + const_cast< DatabaseTransaction& >( o ).m_pDb = nullptr; + } + inline DatabaseTransaction& DatabaseTransaction::operator=( DatabaseTransaction& o ) { + m_pDb = o.m_pDb; + const_cast< DatabaseTransaction& >(o).m_pDb = nullptr; + return *this; + } + + bool DatabaseTransaction::isValid() const { + return m_pDb != nullptr; + } + + inline void Database::beginTransaction( bool bRollback ){ + ++m_nOutTransactionNum; + if( m_nOutTransactionNum == 1 ) { if( bRollback ) { try { - Database::dbExecQuery( connection, "ROLLBACK TRANSACTION;" ); + Database::dbExecQuery( this->con, "ROLLBACK TRANSACTION;" ); } catch( ... ) {} } - Database::dbExecQuery( connection, "BEGIN TRANSACTION;" ); - } - inline DatabaseTransaction::~DatabaseTransaction() { - if( m_bValid ) { - Database::dbExecQuery( m_connection, "COMMIT TRANSACTION;" ); - m_bValid = false; - } - } - inline DatabaseTransaction::DatabaseTransaction( const DatabaseTransaction& o ) { - m_connection = o.m_connection; - m_bValid = o.m_bValid; - const_cast< DatabaseTransaction& >( o ).m_bValid = false; - } - inline DatabaseTransaction& DatabaseTransaction::operator=( const DatabaseTransaction& o ) { - m_connection = o.m_connection; - m_bValid = o.m_bValid; - const_cast< DatabaseTransaction& >(o).m_bValid = false; - return *this; + Database::dbExecQuery( this->con, "BEGIN TRANSACTION;" ); } } - inline bool Database::beginTransaction( bool bRollback ) { - if( m_transaction.m_bValid ) { - return false; + inline bool Database::shouldInsideUseTransaction() const{ + if( m_nOutTransactionNum <= 0 ) { + return true; } else { - m_transaction = detail::DatabaseTransaction( con, bRollback ); - return true; + return false; } } - inline bool Database::shouldInsideUseTransaction() const { - if( m_transaction.m_bValid ) { - return false; - } - else { - return true; + inline void Database::endTransaction() { + --m_nOutTransactionNum; + if( m_nOutTransactionNum == 0 ) { + Database::dbExecQuery( this->con, "COMMIT TRANSACTION;" ); } + + if( m_nOutTransactionNum < 0 ) + m_nOutTransactionNum = 0; } - inline bool Database::endTransaction() { - if( m_transaction.m_bValid ) { - m_transaction.~DatabaseTransaction(); - return true ; - } - else { - return false; - } + inline DatabaseTransaction Database::transactionGuard( bool bRollback ) { + return DatabaseTransaction(this, bRollback ); } template @@ -113,7 +115,7 @@ template void Database::dbUpdate(bean_key key, C& bean, bool bUseTransaction ) { if( bUseTransaction ) { - try { + try { /** 这里是不是不对,应该执行事务后失败后才回滚吧*/ dbExecQuery( key.con, "ROLLBACK TRANSACTION;" ); } catch( ... ) {} @@ -163,12 +165,21 @@ bean_ptr Database::copyBean(const C& c) return manageBean( new C(c) ); } + +template +void Database::copyBeanToDB( const C& c ) +{ + sqlid_t id = allocId( Database::getClassName() ); + bean_key key( con, id ); + dbUpdate( key, const_cast< C& >(c), shouldInsideUseTransaction() ); +} + template inline bean_ptr Database::manageBean(C* ptr) { sqlid_t id=allocId( Database::getClassName() ); bean_key key(con,id); - dbUpdate(key,*ptr, !m_transaction.m_bValid); + dbUpdate(key,*ptr, shouldInsideUseTransaction() ); return Registry::createBeanPtr(this, key,ptr); } diff --git a/sample.cpp b/sample.cpp index defa14a..28f52ce 100644 --- a/sample.cpp +++ b/sample.cpp @@ -8,18 +8,38 @@ using namespace std; #define TEST_OLD_DB 0 +class Id { + friend class hiberlite::access; + + template + void hibernate( Archive & ar ) + { + ar & HIBERLITE_NVP( m_id ); + //ar & HIBERLITE_NVP( bio2 ); + } +public: + string m_id; +}; + +HIBERLITE_EXPORT_CLASS( Id ) + class Person{ friend class hiberlite::access; template void hibernate(Archive & ar) { + ar & HIBERLITE_NVP( id ); ar & HIBERLITE_NVP( name ); - //ar & HIBERLITE_NVP( wname ); + ar & HIBERLITE_NVP( wname ); ar & HIBERLITE_NVP(age); ar & HIBERLITE_NVP( bio ); //ar & HIBERLITE_NVP( bio2 ); } public: + Person() {} + ~Person() {} + + Id id; string name; wstring wname; double age; @@ -68,9 +88,9 @@ void createDB() https://blog.csdn.net/majiakun1/article/details/46607163 加了外部使用事务的接口,能快一点 */ -void createDBmuch() +void createDBMuch() { - hiberlite::Database db( "sample.db" ); + hiberlite::Database db( "sample.db", hiberlite::EDBSynchronous_Normal ); //register bean classes db.registerBeanClass(); @@ -86,13 +106,15 @@ void createDBmuch() const char* names[5] = { "Stanley Marsh", "Kyle Broflovski", "Eric Theodore Cartman", "Kenneth McCormick", "Leopold Stotch" }; + auto t = db.transactionGuard(); db.beginTransaction(false); - for( int j = 0; j < 100; ++j ) { + for( int j = 0; j < 500; ++j ) { for( unsigned int i = 0; i < 5; i++ ) { Person x; x.name = names[i % 5] ; - x.name += " " + std::to_string(j); + x.name += " " + std::to_string( j ); + x.id.m_id = x.name; x.wname = hiberlite::utf8ToWstring( names[i % 5] ); x.age = 14 + i * 0.1; x.bio.push_back( "Hello" ); @@ -103,7 +125,8 @@ void createDBmuch() x.bio2.push_back( L"wworld" ); x.bio2.push_back( L"!" ); - hiberlite::bean_ptr p = db.copyBean( x ); //create a managed copy of the object + db.copyBeanToDB( x ); + //hiberlite::bean_ptr p = db.copyBean( x ); //create a managed copy of the object } } @@ -112,9 +135,10 @@ void createDBmuch() void printDB() { - hiberlite::Database db("sample.db"); + hiberlite::Database db("sample.db", hiberlite::EDBSynchronous_Normal ); db.registerBeanClass(); + auto t = db.transactionGuard(); cout << string(15,'=')+"\nreading the DB\n"; vector< hiberlite::bean_ptr > v=db.getAllBeans(); @@ -162,12 +186,41 @@ void modifyDB() vector< hiberlite::bean_ptr > v2 = db.getAllBeans(); } + +void modifyDBMuch() +{ + cout << string( 15, '=' ) + "\nmodify the DB\n"; + hiberlite::Database db( "sample.db", hiberlite::EDBSynchronous_Normal ); + db.registerBeanClass(); + + auto t = db.transactionGuard(); + vector< hiberlite::bean_ptr > v = db.getAllBeans(); + + cout << "every record becomes 1 year older.\n\n"; + for( auto i: v ) { + i->age += 1; + } + + cout << "every record becomes 1 year older.\n\n"; + for( auto i : v ) { + i->age += 1; + } + + cout << "every record will be deleted.\n\n"; + for( auto i : v ) { + i.destroy(); + } + + vector< hiberlite::bean_ptr > v2 = db.getAllBeans(); +} + int main() { //createDB(); - createDBmuch(); + createDBMuch(); printDB(); - modifyDB(); + modifyDBMuch(); + //modifyDB(); printDB(); modifyDB(); printDB(); diff --git a/src/Database.cpp b/src/Database.cpp index c501d76..b48a2f7 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -7,9 +7,9 @@ Database::Database() : mx(NULL) { } -Database::Database(std::string fname) : mx(NULL) +Database::Database(std::string fname, EDBSynchronous sync) : mx(NULL) { - open(fname); + open(fname, sync ); } Database::~Database() @@ -17,7 +17,7 @@ Database::~Database() close(); } -void Database::open(std::string fname) +void Database::open(std::string fname, EDBSynchronous sync ) { sqlite3* db=NULL; @@ -28,6 +28,22 @@ void Database::open(std::string fname) throw database_error( std::string("database error: ")+sqlite3_errmsg(db) ); con=shared_connection(new autoclosed_con(db)); + switch( sync ) + { + case hiberlite::EDBSynchronous_Default: + break; + case hiberlite::EDBSynchronous_Full: sqlite3_exec( db, "PRAGMA synchronous = full; ", 0, 0, 0 ); + break; + case hiberlite::EDBSynchronous_Normal: + sqlite3_exec( db, "PRAGMA synchronous = normal; ", 0, 0, 0 ); + break; + case hiberlite::EDBSynchronous_Off: + sqlite3_exec( db, "PRAGMA synchronous = OFF; ", 0, 0, 0 ); + break; + default: + break; + } + }catch(std::runtime_error e){ if(db) sqlite3_close(db); From b389a7bb9c59d8b6796872bd3877c3333f3722e1 Mon Sep 17 00:00:00 2001 From: "haifu.tian" Date: Sun, 10 Apr 2022 23:12:59 +0800 Subject: [PATCH 7/7] [dev] add save Policy --- include/Database.h | 6 +++--- include/Database_tmpl_impl.hpp | 8 ++++---- include/Registry.h | 4 ++-- include/Registry_impl.hpp | 13 ++++++++----- include/bean_ptr.h | 12 +++++++++++- include/bean_ptr_impl.hpp | 3 ++- include/common.h | 6 ++++++ sample.cpp | 22 +++++++++++++++------- 8 files changed, 51 insertions(+), 23 deletions(-) diff --git a/include/Database.h b/include/Database.h index d81b052..e41815c 100644 --- a/include/Database.h +++ b/include/Database.h @@ -105,13 +105,13 @@ class Database : noncopyable inline DatabaseTransaction transactionGuard( bool bRollback = true ); template - bean_ptr loadBean(sqlid_t objId); + bean_ptr loadBean(sqlid_t objId, ESavePolicy save = ESavePolicy_Default); template - std::vector getBeanIds(); + std::vector getBeanIds( ); template - std::vector< bean_ptr > getAllBeans(); + std::vector< bean_ptr > getAllBeans( ESavePolicy save = ESavePolicy_Default ); template bean_ptr copyBean(const C& c); diff --git a/include/Database_tmpl_impl.hpp b/include/Database_tmpl_impl.hpp index 0ef46aa..51832d7 100644 --- a/include/Database_tmpl_impl.hpp +++ b/include/Database_tmpl_impl.hpp @@ -184,9 +184,9 @@ inline bean_ptr Database::manageBean(C* ptr) } template -inline bean_ptr Database::loadBean(sqlid_t id) +inline bean_ptr Database::loadBean(sqlid_t id, ESavePolicy save ) { - return Registry::get( this, bean_key(con,id) ); + return Registry::get( this, bean_key(con,id), save ); } template @@ -196,14 +196,14 @@ std::vector Database::getBeanIds() } template -std::vector< bean_ptr > Database::getAllBeans() +std::vector< bean_ptr > Database::getAllBeans( ESavePolicy save ) { std::vector ids=getBeanIds(); size_t N=ids.size(); std::vector< bean_ptr > ans; ans.reserve(N); for(size_t i=0;i(ids[i]) ); + ans.push_back( loadBean(ids[i], save) ); return ans; } diff --git a/include/Registry.h b/include/Registry.h index 8935bff..e96951b 100644 --- a/include/Registry.h +++ b/include/Registry.h @@ -32,10 +32,10 @@ class Registry{ static void notifyRBDies(rb_pair* r); public: - static bean_ptr get( Database* pDb, const bean_key key); + static bean_ptr get( Database* pDb, const bean_key key, ESavePolicy save = ESavePolicy_Default ); static bool has(const bean_key key); - static bean_ptr createBeanPtr( Database* pDb, bean_key key, C* obj); + static bean_ptr createBeanPtr( Database* pDb, bean_key key, C* obj, ESavePolicy save = ESavePolicy_Default ); }; template diff --git a/include/Registry_impl.hpp b/include/Registry_impl.hpp index fe79de3..606f0eb 100644 --- a/include/Registry_impl.hpp +++ b/include/Registry_impl.hpp @@ -18,12 +18,13 @@ bean_key rb_pair::get_key() } template -bean_ptr Registry::createBeanPtr( Database* pDb, bean_key key, C* obj) +bean_ptr Registry::createBeanPtr( Database* pDb, bean_key key, C* obj, ESavePolicy save ) { if(key.id==Database::NULL_ID) return bean_ptr( pDb, key,NULL); real_bean* rb=new real_bean( pDb, key,obj); + rb->setSavePolicy( save ); rb_pair* para=new rb_pair(rb); if( rbpairs.find(key)!=rbpairs.end() ) @@ -36,14 +37,16 @@ bean_ptr Registry::createBeanPtr( Database* pDb, bean_key key, C* obj) } template -bean_ptr Registry::get( Database* pDb, const bean_key key) +bean_ptr Registry::get( Database* pDb, const bean_key key, ESavePolicy save ) { typename std::map* >::iterator it; it=rbpairs.find(key); if(it==rbpairs.end()) - return createBeanPtr( pDb, key,NULL); - else - return bean_ptr( pDb, key, it->second); + return createBeanPtr( pDb, key,NULL, save ); + else { + it->second->getRes()->setSavePolicy( save ); + return bean_ptr( pDb, key, it->second ); + } } template diff --git a/include/bean_ptr.h b/include/bean_ptr.h index 83f1a31..aa184e2 100644 --- a/include/bean_ptr.h +++ b/include/bean_ptr.h @@ -22,6 +22,10 @@ class real_bean : noncopyable { bean_key get_key() const { return key; } + inline ESavePolicy getSavePolicy() const { + return m_eSavePolicy; + } + inline void setSavePolicy( ESavePolicy save ) { m_eSavePolicy = save; } void save(); void destroy(); @@ -39,7 +43,7 @@ class real_bean : noncopyable { bean_key key; C* obj; bool forgotten; - + ESavePolicy m_eSavePolicy = ESavePolicy_Default; private: friend class Registry; real_bean( Database* pDb, const bean_key _key, C* _obj); //only Registry can create the real_bean @@ -67,6 +71,12 @@ class bean_ptr : public shared_res< real_bean > bean_ptr(const bean_ptr& other); bean_ptr& operator=(const bean_ptr& other); + inline ESavePolicy getSavePolicy() const { + return shared_res< real_bean >::get_object()->getSavePolicy(); + } + inline void setSavePolicy( ESavePolicy save ) { + return shared_res< real_bean >::get_object()->setSavePolicy( save ); + } void save(); void destroy(); diff --git a/include/bean_ptr_impl.hpp b/include/bean_ptr_impl.hpp index 4a4327c..eedd7c2 100644 --- a/include/bean_ptr_impl.hpp +++ b/include/bean_ptr_impl.hpp @@ -8,7 +8,8 @@ real_bean::real_bean( Database* pDb, const bean_key _key, C* _obj) template real_bean::~real_bean() { - save(); + if( getSavePolicy() == ESavePolicy_ExitSave ) + save(); delete obj; } diff --git a/include/common.h b/include/common.h index f8c03fc..61337ca 100644 --- a/include/common.h +++ b/include/common.h @@ -17,6 +17,12 @@ namespace hiberlite{ +enum ESavePolicy { + ESavePolicy_Default, + ESavePolicy_ExitSave = ESavePolicy_Default, + ESavePolicy_ExitNoSave, +}; + class noncopyable { protected: diff --git a/sample.cpp b/sample.cpp index 28f52ce..a15dd5f 100644 --- a/sample.cpp +++ b/sample.cpp @@ -141,7 +141,7 @@ void printDB() auto t = db.transactionGuard(); cout << string(15,'=')+"\nreading the DB\n"; - vector< hiberlite::bean_ptr > v=db.getAllBeans(); + vector< hiberlite::bean_ptr > v=db.getAllBeans( hiberlite::ESavePolicy_ExitNoSave ); cout << "found " << v.size() << " persons in the database:\n"; @@ -206,12 +206,20 @@ void modifyDBMuch() i->age += 1; } - cout << "every record will be deleted.\n\n"; - for( auto i : v ) { - i.destroy(); + if( v.size() > 0 ) { + cout << v[0]->name << " will be deleted.\n"; + v[0].destroy(); + + if( v[0] ) { + cout << "error: v[0] should be null!"; + } } + //cout << "every record will be deleted.\n\n"; + //for( auto i : v ) { + // i.destroy(); + //} - vector< hiberlite::bean_ptr > v2 = db.getAllBeans(); + vector< hiberlite::bean_ptr > v2 = db.getAllBeans( hiberlite::ESavePolicy_ExitNoSave ); } int main() @@ -222,7 +230,7 @@ int main() modifyDBMuch(); //modifyDB(); printDB(); - modifyDB(); - printDB(); + //modifyDB(); + //printDB(); return 0; }