From 718400528c91e39ed27900a4d702a5a284d41a9b Mon Sep 17 00:00:00 2001 From: Rait Liiv Date: Tue, 2 Apr 2013 14:37:01 +0300 Subject: [PATCH 1/8] Added visual studio solution and projects for datamapper. Individual projects for sqlite, test, dbc-cpp and datamapper. No precompiled header - std includes directly included in main.cpp --- test/src/main.cpp | 4 + visual studio/Datamapper-cpp.sln | 45 +++ visual studio/Datamapper-cpp.vcxproj | 85 ++++++ visual studio/Datamapper-cpp.vcxproj.filters | 22 ++ visual studio/dbc-cpp.vcproj | 273 +++++++++++++++++++ visual studio/dbc-cpp.vcxproj | 116 ++++++++ visual studio/dbc-cpp.vcxproj.filters | 101 +++++++ visual studio/sqlite.vcproj | 173 ++++++++++++ visual studio/sqlite.vcxproj | 85 ++++++ visual studio/sqlite.vcxproj.filters | 26 ++ visual studio/test.vcproj | 209 ++++++++++++++ visual studio/test.vcxproj | 103 +++++++ visual studio/test.vcxproj.filters | 32 +++ 13 files changed, 1274 insertions(+) create mode 100644 visual studio/Datamapper-cpp.sln create mode 100644 visual studio/Datamapper-cpp.vcxproj create mode 100644 visual studio/Datamapper-cpp.vcxproj.filters create mode 100644 visual studio/dbc-cpp.vcproj create mode 100644 visual studio/dbc-cpp.vcxproj create mode 100644 visual studio/dbc-cpp.vcxproj.filters create mode 100644 visual studio/sqlite.vcproj create mode 100644 visual studio/sqlite.vcxproj create mode 100644 visual studio/sqlite.vcxproj.filters create mode 100644 visual studio/test.vcproj create mode 100644 visual studio/test.vcxproj create mode 100644 visual studio/test.vcxproj.filters diff --git a/test/src/main.cpp b/test/src/main.cpp index 63cc656..0136c38 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -10,6 +10,10 @@ #include #include +#include +#include +#include +#include /* Include * and call dm::sql::TraceSqlToStderr(); diff --git a/visual studio/Datamapper-cpp.sln b/visual studio/Datamapper-cpp.sln new file mode 100644 index 0000000..dc41f44 --- /dev/null +++ b/visual studio/Datamapper-cpp.sln @@ -0,0 +1,45 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Datamapper-cpp", "Datamapper-cpp.vcxproj", "{C1F847C0-1983-47CE-AD05-F0A10A27CCA3}" + ProjectSection(ProjectDependencies) = postProject + {0C7330B9-3C7C-49FC-AF63-1494C787B64A} = {0C7330B9-3C7C-49FC-AF63-1494C787B64A} + {901486B9-789B-40EE-B748-A95E4F9CC624} = {901486B9-789B-40EE-B748-A95E4F9CC624} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test.vcxproj", "{901486B9-789B-40EE-B748-A95E4F9CC624}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sqlite", "sqlite.vcxproj", "{4CD61C40-F2DC-42EF-9680-AA9325FF879B}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dbc-cpp", "dbc-cpp.vcxproj", "{0C7330B9-3C7C-49FC-AF63-1494C787B64A}" + ProjectSection(ProjectDependencies) = postProject + {4CD61C40-F2DC-42EF-9680-AA9325FF879B} = {4CD61C40-F2DC-42EF-9680-AA9325FF879B} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C1F847C0-1983-47CE-AD05-F0A10A27CCA3}.Debug|Win32.ActiveCfg = Debug|Win32 + {C1F847C0-1983-47CE-AD05-F0A10A27CCA3}.Debug|Win32.Build.0 = Debug|Win32 + {C1F847C0-1983-47CE-AD05-F0A10A27CCA3}.Release|Win32.ActiveCfg = Release|Win32 + {C1F847C0-1983-47CE-AD05-F0A10A27CCA3}.Release|Win32.Build.0 = Release|Win32 + {901486B9-789B-40EE-B748-A95E4F9CC624}.Debug|Win32.ActiveCfg = Debug|Win32 + {901486B9-789B-40EE-B748-A95E4F9CC624}.Debug|Win32.Build.0 = Debug|Win32 + {901486B9-789B-40EE-B748-A95E4F9CC624}.Release|Win32.ActiveCfg = Release|Win32 + {901486B9-789B-40EE-B748-A95E4F9CC624}.Release|Win32.Build.0 = Release|Win32 + {4CD61C40-F2DC-42EF-9680-AA9325FF879B}.Debug|Win32.ActiveCfg = Debug|Win32 + {4CD61C40-F2DC-42EF-9680-AA9325FF879B}.Debug|Win32.Build.0 = Debug|Win32 + {4CD61C40-F2DC-42EF-9680-AA9325FF879B}.Release|Win32.ActiveCfg = Release|Win32 + {4CD61C40-F2DC-42EF-9680-AA9325FF879B}.Release|Win32.Build.0 = Release|Win32 + {0C7330B9-3C7C-49FC-AF63-1494C787B64A}.Debug|Win32.ActiveCfg = Debug|Win32 + {0C7330B9-3C7C-49FC-AF63-1494C787B64A}.Debug|Win32.Build.0 = Debug|Win32 + {0C7330B9-3C7C-49FC-AF63-1494C787B64A}.Release|Win32.ActiveCfg = Release|Win32 + {0C7330B9-3C7C-49FC-AF63-1494C787B64A}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/visual studio/Datamapper-cpp.vcxproj b/visual studio/Datamapper-cpp.vcxproj new file mode 100644 index 0000000..773166a --- /dev/null +++ b/visual studio/Datamapper-cpp.vcxproj @@ -0,0 +1,85 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + + + + {C1F847C0-1983-47CE-AD05-F0A10A27CCA3} + Win32Proj + Datamappercpp + + + + Application + true + v110 + Unicode + + + Application + false + v110 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(ProjectDir)..\test\testcpp\include;$(ProjectDir)..\include;$(ProjectDir)..\lib\dbccpp\include + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/visual studio/Datamapper-cpp.vcxproj.filters b/visual studio/Datamapper-cpp.vcxproj.filters new file mode 100644 index 0000000..17e6a1b --- /dev/null +++ b/visual studio/Datamapper-cpp.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/visual studio/dbc-cpp.vcproj b/visual studio/dbc-cpp.vcproj new file mode 100644 index 0000000..96abe02 --- /dev/null +++ b/visual studio/dbc-cpp.vcproj @@ -0,0 +1,273 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/visual studio/dbc-cpp.vcxproj b/visual studio/dbc-cpp.vcxproj new file mode 100644 index 0000000..81ac2f6 --- /dev/null +++ b/visual studio/dbc-cpp.vcxproj @@ -0,0 +1,116 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {0C7330B9-3C7C-49FC-AF63-1494C787B64A} + dbccpp + Win32Proj + + + + StaticLibrary + v110 + Unicode + true + + + StaticLibrary + v110 + Unicode + + + + + + + + + + + + + <_ProjectFileVersion>11.0.51106.1 + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + + + + Disabled + $(ProjectDir)..\lib\dbccpp\include;$(ProjectDir)..\lib\sqlite-amalgamation;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + Level4 + true + EditAndContinue + + + + + MaxSpeed + true + $(ProjectDir)\..\boost;$(ProjectDir)\..\sqlite-amalgamation;$(ProjectDir)\..\..\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + Level4 + true + ProgramDatabase + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/visual studio/dbc-cpp.vcxproj.filters b/visual studio/dbc-cpp.vcxproj.filters new file mode 100644 index 0000000..cfd6728 --- /dev/null +++ b/visual studio/dbc-cpp.vcxproj.filters @@ -0,0 +1,101 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + + \ No newline at end of file diff --git a/visual studio/sqlite.vcproj b/visual studio/sqlite.vcproj new file mode 100644 index 0000000..6f346a0 --- /dev/null +++ b/visual studio/sqlite.vcproj @@ -0,0 +1,173 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/visual studio/sqlite.vcxproj b/visual studio/sqlite.vcxproj new file mode 100644 index 0000000..b11259b --- /dev/null +++ b/visual studio/sqlite.vcxproj @@ -0,0 +1,85 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + + + + + + + + {4CD61C40-F2DC-42EF-9680-AA9325FF879B} + sqlite + Win32Proj + + + + StaticLibrary + v110 + Unicode + true + + + StaticLibrary + v110 + Unicode + + + + + + + + + + + + + <_ProjectFileVersion>11.0.51106.1 + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + + + + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + Level3 + EditAndContinue + + + + + MaxSpeed + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + Level3 + ProgramDatabase + + + + + + \ No newline at end of file diff --git a/visual studio/sqlite.vcxproj.filters b/visual studio/sqlite.vcxproj.filters new file mode 100644 index 0000000..711f7af --- /dev/null +++ b/visual studio/sqlite.vcxproj.filters @@ -0,0 +1,26 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/visual studio/test.vcproj b/visual studio/test.vcproj new file mode 100644 index 0000000..b06d52e --- /dev/null +++ b/visual studio/test.vcproj @@ -0,0 +1,209 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/visual studio/test.vcxproj b/visual studio/test.vcxproj new file mode 100644 index 0000000..0546ba3 --- /dev/null +++ b/visual studio/test.vcxproj @@ -0,0 +1,103 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {901486B9-789B-40EE-B748-A95E4F9CC624} + test + Win32Proj + + + + Application + v110 + Unicode + true + + + Application + v110 + Unicode + + + + + + + + + + + + + <_ProjectFileVersion>11.0.51106.1 + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + true + + + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + false + + + + Disabled + $(ProjectDir)..\test\testcpp\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + Level3 + EditAndContinue + + + true + Console + MachineX86 + + + + + MaxSpeed + true + $(ProjectDir)\..\..\test\testcpp\include;$(ProjectDir)\..\..\include; $(ProjectDir)\..\boost;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + Level3 + ProgramDatabase + + + true + Console + true + true + MachineX86 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/visual studio/test.vcxproj.filters b/visual studio/test.vcxproj.filters new file mode 100644 index 0000000..b3a188a --- /dev/null +++ b/visual studio/test.vcxproj.filters @@ -0,0 +1,32 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file From 7dc7d0abf443d765331c2c7318063e6fa164261c Mon Sep 17 00:00:00 2001 From: Rait Liiv Date: Tue, 2 Apr 2013 14:38:13 +0300 Subject: [PATCH 2/8] git ignoring .suo .sdf and .opensdf files. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 409bec0..aadcee6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ obj/ bin/ datamappercpp-test test.sqlite +*.suo +*.sdf +*.opensdf From 82782367468be47518b44d346f6cf34ef1e06488 Mon Sep 17 00:00:00 2001 From: Rait Liiv Date: Wed, 3 Apr 2013 11:41:31 +0300 Subject: [PATCH 3/8] Changes to projects. test.vcxproj is now static library, compiling only testcpp.cpp. Include fixes to reference only one utilcpp directory under root/include. --- visual studio/Datamapper-cpp.vcxproj | 4 ++++ visual studio/dbc-cpp.vcxproj | 10 +++++++++- visual studio/test.vcxproj | 4 ++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/visual studio/Datamapper-cpp.vcxproj b/visual studio/Datamapper-cpp.vcxproj index 773166a..96e34fd 100644 --- a/visual studio/Datamapper-cpp.vcxproj +++ b/visual studio/Datamapper-cpp.vcxproj @@ -44,6 +44,7 @@ true + $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);$(BOOST_INCLUDE); false @@ -56,10 +57,13 @@ Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) $(ProjectDir)..\test\testcpp\include;$(ProjectDir)..\include;$(ProjectDir)..\lib\dbccpp\include + false Console true + dbc-cpp.lib;test.lib;%(AdditionalDependencies) + $(OutDir) diff --git a/visual studio/dbc-cpp.vcxproj b/visual studio/dbc-cpp.vcxproj index 81ac2f6..ad97983 100644 --- a/visual studio/dbc-cpp.vcxproj +++ b/visual studio/dbc-cpp.vcxproj @@ -43,6 +43,7 @@ $(SolutionDir)$(Configuration)\ $(Configuration)\ + $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);$(BOOST_INCLUDE); $(SolutionDir)$(Configuration)\ @@ -51,7 +52,7 @@ Disabled - $(ProjectDir)..\lib\dbccpp\include;$(ProjectDir)..\lib\sqlite-amalgamation;%(AdditionalIncludeDirectories) + $(ProjectDir)..\lib\dbccpp\include;$(ProjectDir)..\lib\sqlite-amalgamation;$(ProjectDir)..\include;%(AdditionalIncludeDirectories) WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) true EnableFastChecks @@ -60,7 +61,14 @@ Level4 true EditAndContinue + false + + sqlite.lib + + + $(OutDir) + diff --git a/visual studio/test.vcxproj b/visual studio/test.vcxproj index 0546ba3..6f8caf9 100644 --- a/visual studio/test.vcxproj +++ b/visual studio/test.vcxproj @@ -23,7 +23,7 @@ true - Application + StaticLibrary v110 Unicode @@ -53,7 +53,7 @@ Disabled - $(ProjectDir)..\test\testcpp\include;%(AdditionalIncludeDirectories) + $(ProjectDir)..\test\testcpp\include;$(ProjectDir)..\include;%(AdditionalIncludeDirectories) WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true EnableFastChecks From e3855cac6103fd50d33d682f6866610c1209fc1b Mon Sep 17 00:00:00 2001 From: Rait Liiv Date: Wed, 3 Apr 2013 11:41:54 +0300 Subject: [PATCH 4/8] ignoring Debug folders. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index aadcee6..c12c851 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ test.sqlite *.suo *.sdf *.opensdf +Debug From 77f960458f7bd3a254f1d8bfa0aa8142caabb71b Mon Sep 17 00:00:00 2001 From: Rait Liiv Date: Wed, 3 Apr 2013 11:43:41 +0300 Subject: [PATCH 5/8] small fixes. Unreferenced variable from exception catch. std::streamoff instead of long. --- include/datamappercpp/sql/Repository.h | 2 +- .../sql/detail/SqlStatementBuilder.h | 4 ++-- test/src/main.cpp | 19 ++++++++++--------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/datamappercpp/sql/Repository.h b/include/datamappercpp/sql/Repository.h index e61e600..0b0f4ab 100644 --- a/include/datamappercpp/sql/Repository.h +++ b/include/datamappercpp/sql/Repository.h @@ -319,7 +319,7 @@ class Repository // TODO: entity.id << *result; entity.id = (*result)[0]; } - catch (const dbc::NoResultsError& e) + catch (const dbc::NoResultsError&) { std::ostringstream msg; msg << "No " << Mapping::getLabel() << " exists for query '" diff --git a/include/datamappercpp/sql/detail/SqlStatementBuilder.h b/include/datamappercpp/sql/detail/SqlStatementBuilder.h index eb42cbd..b8b144c 100644 --- a/include/datamappercpp/sql/detail/SqlStatementBuilder.h +++ b/include/datamappercpp/sql/detail/SqlStatementBuilder.h @@ -32,7 +32,7 @@ class SqlStatementBuilder Mapping::accept(fieldBuilder, _dummy_entity); // replace last comma with ')' - long pos = sql.tellp(); + std::streamoff pos = sql.tellp(); sql.seekp(pos - 1); sql << ")"; @@ -78,7 +78,7 @@ class SqlStatementBuilder UpdateStatementFieldBuilder fieldBuilder(sql); Mapping::accept(fieldBuilder, _dummy_entity); - long pos = sql.tellp(); + std::streamoff pos = sql.tellp(); sql.seekp(pos - 1); // remove last comma sql << " WHERE id=?"; diff --git a/test/src/main.cpp b/test/src/main.cpp index 0136c38..ce894d8 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -1,19 +1,20 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + #include #include - #include -#include - #include #include -#include -#include -#include -#include -#include -#include /* Include * and call dm::sql::TraceSqlToStderr(); From 29b2d06a101061120ef00e4fbb058fb0ac11f2c9 Mon Sep 17 00:00:00 2001 From: Rait Liiv Date: Fri, 5 Apr 2013 12:46:42 +0300 Subject: [PATCH 6/8] Added __int64 and wstring support. Added more repository interfaces. Minor fixes. Added datamapper files to datamapper project. Todo: move main.cpp back to test project and make test project as runnable and datamapper as library. --- include/datamappercpp/Field.h | 6 + include/datamappercpp/sql/Repository.h | 126 +++++++++++++++++- .../sql/detail/SqlStatementBuilder.h | 53 +++++++- .../detail/StatementBuilderFieldVisitors.h | 1 + visual studio/Datamapper-cpp.vcxproj | 9 ++ visual studio/Datamapper-cpp.vcxproj.filters | 23 ++++ 6 files changed, 210 insertions(+), 8 deletions(-) diff --git a/include/datamappercpp/Field.h b/include/datamappercpp/Field.h index 722d90d..69ec837 100644 --- a/include/datamappercpp/Field.h +++ b/include/datamappercpp/Field.h @@ -44,6 +44,9 @@ struct Field template <> std::string Field::getType() const { return "INT"; } +template <> +std::string Field<__int64>::getType() const { return "INT"; } + // TODO: add CHECK( in { 0, 1 } ) template <> std::string Field::getType() const { return "INT"; } @@ -57,6 +60,9 @@ std::string Field::getType() const { return "TEXT"; } template <> std::string Field::getType() const { return "TEXT"; } +template <> +std::string Field::getType() const { return "TEXT"; } + } #endif /* DATAMAPPERCPP_FIELD_H */ diff --git a/include/datamappercpp/sql/Repository.h b/include/datamappercpp/sql/Repository.h index 0b0f4ab..cc4c5c3 100644 --- a/include/datamappercpp/sql/Repository.h +++ b/include/datamappercpp/sql/Repository.h @@ -1,3 +1,4 @@ +#pragma once #include #include #include @@ -17,6 +18,7 @@ namespace stdutil = std; } #else + #include #include namespace dm { @@ -24,6 +26,7 @@ } #endif + namespace dm { namespace sql { @@ -70,6 +73,7 @@ class ObjectFieldBinder unsigned int _counter; }; + /** * Repository transfers domain entities and their collections to and from * the database. @@ -82,6 +86,7 @@ class Repository { public: typedef std::vector Entities; + typedef std::vector > EntitiesPtr; typedef SqlStatementBuilder EntitySqlBuilder; static void CreateTable(bool enableTransaction = true) @@ -159,7 +164,7 @@ class Repository entity.id = -1; } - static void Delete(int id, + static void Delete(__int64 id, bool enableTransaction = true, bool checkOneDeleted = true) { @@ -181,6 +186,21 @@ class Repository transaction.commit(); } + template + static void DeleteByField(std::string field, + const Value &value, + bool enableTransaction = true) + { + Statement statement = PrepareStatement(EntitySqlBuilder::DeleteByFieldStatement(field)); + *statement << value; + + Transaction transaction(enableTransaction); + + statement->executeUpdate(); + + transaction.commit(); + } + static void DeleteAll(bool enableTransaction = true) { Transaction transaction(enableTransaction); @@ -190,7 +210,7 @@ class Repository transaction.commit(); } - static Entity Get(int id) + static Entity Get(__int64 id) { // note that SELECTs are outside transactions and // mixing them with ongoing transactions may cause problems @@ -222,6 +242,13 @@ class Repository return GetByQueryImpl(statement, allowMany, -1); } + static Entity GetByQuery(const std::wstring& sql, + bool allowMany = false) + { + return GetByQuery(static_cast(toMBStr(sql)), allowMany); + //toMBStr is included from SqlStatementBuilder.h + } + static Entity GetByQuery(Statement& statement, bool allowMany = false) { @@ -236,6 +263,24 @@ class Repository return GetManyByQuery(_getAllEntitiesStatement); } + static EntitiesPtr GetAllPtr() + { + prepareStatement(_getAllEntitiesStatement, + &EntitySqlBuilder::SelectAllStatement); + + return GetManyByQueryPtr(_getAllEntitiesStatement); + } + + template + static EntitiesPtr GetManyByFieldPtr(const std::string& fieldname, Value value) + { + Statement statement = PrepareStatement( + EntitySqlBuilder::SelectByFieldStatement(fieldname)); + *statement << value; + + return GetManyByQueryPtr(statement); + } + template static Entities GetManyByField(const std::string& fieldname, Value value) { @@ -246,12 +291,63 @@ class Repository return GetManyByQuery(statement); } + template + static EntitiesPtr GetManyByFieldPtr(const std::string& fieldname1, Value1 value1, + const std::string& fieldname2, Value2 value2) + { + Statement statement = PrepareStatement( + EntitySqlBuilder::SelectByFieldStatement(fieldname1, fieldname2)); + *statement << value1 << value2; + + return GetManyByQueryPtr(statement); + } + + template + static Entities GetManyByField(const std::string& fieldname1, Value1 value1, + const std::string& fieldname2, Value2 value2) + { + Statement statement = PrepareStatement( + EntitySqlBuilder::SelectByFieldStatement(fieldname1, fieldname2)); + *statement << value1 << value2; + + return GetManyByQuery(statement); + } + + template + static EntitiesPtr GetManyByFieldPtr(const std::string& fieldname1, Value1 value1, + const std::string& fieldname2, Value2 value2, const std::string& fieldname3, Value3 value3) + { + Statement statement = PrepareStatement( + EntitySqlBuilder::SelectByFieldStatement(fieldname1, fieldname2, fieldname3)); + *statement << value1 << value2 << value3; + + return GetManyByQueryPtr(statement); + } + + template + static Entities GetManyByField(const std::string& fieldname1, Value1 value1, + const std::string& fieldname2, Value2 value2, const std::string& fieldname3, Value3 value3) + { + Statement statement = PrepareStatement( + EntitySqlBuilder::SelectByFieldStatement(fieldname1, fieldname2, fieldname3)); + *statement << value1 << value2 << value3; + + return GetManyByQuery(statement); + } + static Entities GetManyByQuery(const std::string& sql) { Statement statement = PrepareStatement(sql); return GetManyByQuery(statement); } + static Entities GetManyByQuery(const std::wstring& sql) + { + Statement statement = PrepareStatement(toMBStr(sql)); + //toMBStr is included from SqlStatementBuilder.h + return GetManyByQuery(statement); + } + static Entities GetManyByQuery(Statement& statement) { Entities entities; @@ -271,6 +367,30 @@ class Repository return entities; } + static EntitiesPtr GetManyByQueryPtr(Statement& statement) + { + EntitiesPtr entities; + dbc::ResultSet::ptr result(statement->executeQuery()); + + while (result->next()) + { + boost::shared_ptr entity( new Entity ); + entity->id = (*result)[0]; + + ObjectFieldBinder fieldbinder(*result); + Mapping::accept(fieldbinder, *entity); + + entities.push_back(entity); + } + + return entities; + } + + static dbc::ResultSet::ptr ExecuteQuery(std::string sql) + { + Statement statement; + } + static void ResetStatements() { // Free the resources associated with prepared statments. @@ -307,7 +427,7 @@ class Repository } inline static Entity GetByQueryImpl(Statement& statement, - bool allowMany, int id) + bool allowMany, __int64 id) { Entity entity; diff --git a/include/datamappercpp/sql/detail/SqlStatementBuilder.h b/include/datamappercpp/sql/detail/SqlStatementBuilder.h index b8b144c..eb220f2 100644 --- a/include/datamappercpp/sql/detail/SqlStatementBuilder.h +++ b/include/datamappercpp/sql/detail/SqlStatementBuilder.h @@ -4,14 +4,24 @@ #include #include -#include -#include - -#include #include +#include // FIXME: ostringstream::exceptions(s.badbit | s.failbit); +namespace +{ +#include + std::string toMBStr(std::wstring const& str) + { + std::string mbstr( str.size(), 0 ); + #pragma warning(disable:4996)//We are perfectly aware the wcstombs is not "safe" operation as the destination size is not given - fortunately our destination has dynamic size. + wcstombs(&mbstr[0], &str[0], str.size()); + #pragma warning(default:4996) + return mbstr; + } +} + namespace dm { namespace sql { @@ -78,7 +88,7 @@ class SqlStatementBuilder UpdateStatementFieldBuilder fieldBuilder(sql); Mapping::accept(fieldBuilder, _dummy_entity); - std::streamoff pos = sql.tellp(); + std::streamoff pos = sql.tellp(); sql.seekp(pos - 1); // remove last comma sql << " WHERE id=?"; @@ -105,6 +115,16 @@ class SqlStatementBuilder return sql.str(); } + static std::string DeleteByFieldStatement(std::string &field) + { + std::ostringstream sql; + + sql << "DELETE FROM " << Mapping::getLabel() + << " WHERE " << field << "=?"; + + return sql.str(); + } + static std::string SelectAllStatement() { std::ostringstream sql; @@ -129,6 +149,29 @@ class SqlStatementBuilder return sql.str(); } + // TODO: implement SelectByFieldStatement as variadic arguments template for C++11. + static std::string SelectByFieldStatement(const std::string field1, const std::string field2) + { + std::ostringstream sql; + + sql << "SELECT * FROM " << Mapping::getLabel() + << " WHERE " << field1 << "=? AND " + << field2 << "=?"; + + return sql.str(); + } + + static std::string SelectByFieldStatement(const std::string field1, const std::string field2, const std::string field3) + { + std::ostringstream sql; + + sql << "SELECT * FROM " << Mapping::getLabel() + << " WHERE " << field1 << "=? AND " + << field2 << "=? AND " << field3 << "=?"; + + return sql.str(); + } + private: // disable instantiation to assure the class is only used via it's static // functions diff --git a/include/datamappercpp/sql/detail/StatementBuilderFieldVisitors.h b/include/datamappercpp/sql/detail/StatementBuilderFieldVisitors.h index 59748de..545948f 100644 --- a/include/datamappercpp/sql/detail/StatementBuilderFieldVisitors.h +++ b/include/datamappercpp/sql/detail/StatementBuilderFieldVisitors.h @@ -1,3 +1,4 @@ +#pragma once #include #include diff --git a/visual studio/Datamapper-cpp.vcxproj b/visual studio/Datamapper-cpp.vcxproj index 96e34fd..ad9df38 100644 --- a/visual studio/Datamapper-cpp.vcxproj +++ b/visual studio/Datamapper-cpp.vcxproj @@ -13,6 +13,15 @@ + + + + + + + + + {C1F847C0-1983-47CE-AD05-F0A10A27CCA3} Win32Proj diff --git a/visual studio/Datamapper-cpp.vcxproj.filters b/visual studio/Datamapper-cpp.vcxproj.filters index 17e6a1b..1c04083 100644 --- a/visual studio/Datamapper-cpp.vcxproj.filters +++ b/visual studio/Datamapper-cpp.vcxproj.filters @@ -19,4 +19,27 @@ Source Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + \ No newline at end of file From 0280afd8c5ba6ab203a0955b21e6f0bc57171eb7 Mon Sep 17 00:00:00 2001 From: Rait Liiv Date: Fri, 5 Apr 2013 17:07:15 +0300 Subject: [PATCH 7/8] Modified tests to test wstring and __int64 usage. Decided not to move the main.cpp back to test project. --- test/src/main.cpp | 71 +++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/test/src/main.cpp b/test/src/main.cpp index ce894d8..1e989f9 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -25,31 +25,36 @@ struct Person { typedef std::vector list; - int id; + __int64 id; std::string name; - int age; + std::wstring wname; + __int64 age; double height; Person() : - id(-1), name(), age(0), height(0.0) + id(-1), name(), wname(), age(0), height(0.0) { } - Person(int i, const std::string& n, int a, double h) : - id(i), name(n), age(a), height(h) + Person(__int64 i, const std::string& n, const std::wstring& wn, __int64 a, double h) : + id(i), name(n), wname(wn), age(a), height(h) { } bool operator==(const Person& rhs) const { - return id == rhs.id && name == rhs.name + return id == rhs.id && name == rhs.name && wname == rhs.wname && age == rhs.age && height == rhs.height; } - operator std::string() const + operator std::wstring() const { - std::ostringstream out; + std::wstring nameWstring; + #pragma warning(disable:4996)//We are perfectly aware the mbstowcs is not "safe" operation as the destination size is not given - fortunately our destination has dynamic size. + mbstowcs(&nameWstring[0], &name[0], name.length()); + #pragma warning(default:4996) + std::wostringstream out; out << "Person: {" << id << "," - << name << "," + << nameWstring << "," << age << "," << height << "}"; return out.str(); @@ -58,7 +63,7 @@ struct Person }; void print_person(const Person& p) -{ std::cout << static_cast(p) << std::endl; } +{ std::wcout << static_cast(p) << std::endl; } class PersonMapping { @@ -71,7 +76,8 @@ class PersonMapping { // Note that field order is important v.visitField(dm::Field("name", "UNIQUE NOT NULL"), p.name); - v.visitField(dm::Field("age"), p.age); + v.visitField(dm::Field("wname"), p.wname); + v.visitField(dm::Field<__int64>("age"), p.age); v.visitField(dm::Field("height"), p.height); } @@ -93,6 +99,8 @@ class TestDataMapperCpp : public Test::Suite TestDataMapperCpp() { dm::sql::ConnectDatabase("test.sqlite"); + dm::sql::ExecuteStatement(std::string("DROP TABLE IF EXISTS ") + + PersonMapping::getLabel()); } ~TestDataMapperCpp() @@ -121,6 +129,7 @@ class TestDataMapperCpp : public Test::Suite "CREATE TABLE IF NOT EXISTS person" "(id INTEGER PRIMARY KEY AUTOINCREMENT," "name TEXT UNIQUE NOT NULL," + "wname TEXT," "age INT," "height REAL);" "CREATE INDEX IF NOT EXISTS person_name_idx ON person (name)"); @@ -128,12 +137,12 @@ class TestDataMapperCpp : public Test::Suite Test::assertEqual( "Insert statement is correct", PersonSql::InsertStatement(), - "INSERT INTO person (name,age,height) VALUES (?,?,?)"); + "INSERT INTO person (name,wname,age,height) VALUES (?,?,?,?)"); Test::assertEqual( "Update statement is correct", PersonSql::UpdateStatement(), - "UPDATE person SET name=?,age=?,height=? WHERE id=?"); + "UPDATE person SET name=?,wname=?,age=?,height=? WHERE id=?"); Test::assertEqual( "Select by ID statement is correct", @@ -150,7 +159,7 @@ class TestDataMapperCpp : public Test::Suite { PersonRepository::CreateTable(); - Person p(-1, "Ervin", 38, 1.80); + Person p(-1, "Ervin", L"ŽŸ®", 8446744073709551615, 1.80); PersonRepository::Save(p); Test::assertTrue( "Object ID is correctly set during saving", @@ -158,8 +167,8 @@ class TestDataMapperCpp : public Test::Suite Person::list ps; - ps.push_back(Person(-1, "Marvin", 24, 1.65)); - ps.push_back(Person(-1, "Steve", 32, 2.10)); + ps.push_back(Person(-1, "Marvin", L"¥¥¥¥", 24, 1.65)); + ps.push_back(Person(-1, "Steve", L"Ž¥¥", 32, 2.10)); PersonRepository::Save(ps); Test::assertTrue( @@ -171,16 +180,16 @@ class TestDataMapperCpp : public Test::Suite { Person p = PersonRepository::Get(1); Test::assertTrue("Get objects by ID works", - p.name == "Ervin" && p.age == 38 && p.height == 1.80); + p.name == "Ervin" && p.wname == L"ŽŸ®" && p.age == 8446744073709551615 && p.height == 1.80); p = PersonRepository::GetByField("name", "Marvin"); Test::assertTrue("Get objects by field works", - p.name == "Marvin" && p.age == 24 && p.height == 1.65); + p.name == "Marvin" && p.wname == L"¥¥¥¥" && p.age == 24 && p.height == 1.65); p = PersonRepository::GetByQuery("SELECT * FROM person " "WHERE name LIKE '%eve'"); Test::assertTrue("Get objects by query works", - p.name == "Steve" && p.age == 32 && p.height == 2.10); + p.name == "Steve" && p.wname == L"Ž¥¥" && p.age == 32 && p.height == 2.10); dm::sql::Statement statement = dm::sql::PrepareStatement( "SELECT * FROM person WHERE name LIKE '%vin'"); @@ -188,7 +197,15 @@ class TestDataMapperCpp : public Test::Suite Test::assertTrue( "More than one result causes the first one to be returned " "if allow_many is true", - p.name == "Ervin" && p.age == 38 && p.height == 1.80); + p.name == "Ervin" && p.wname == L"ŽŸ®" && p.age == 8446744073709551615 && p.height == 1.80); + + statement = dm::sql::PrepareStatement( + "SELECT * FROM person WHERE wname LIKE ? AND age = ?"); + *statement << L"%Ÿ®" << 8446744073709551615; + p = PersonRepository::GetByQuery(statement); + Test::assertTrue( + "Statement operator << works for wstring and __int64", + p.name == "Ervin" && p.wname == L"ŽŸ®" && p.age == 8446744073709551615 && p.height == 1.80); Test::assertThrows( @@ -207,9 +224,9 @@ class TestDataMapperCpp : public Test::Suite void testMultipleObjectLoading() { Person::list expected; - expected.push_back(Person(1, "Ervin", 38, 1.80)); - expected.push_back(Person(2, "Marvin", 24, 1.65)); - expected.push_back(Person(3, "Steve", 32, 2.10)); + expected.push_back(Person(1, "Ervin", L"ŽŸ®", 8446744073709551615, 1.80)); + expected.push_back(Person(2, "Marvin", L"¥¥¥¥", 24, 1.65)); + expected.push_back(Person(3, "Steve", L"Ž¥¥", 32, 2.10)); Person::list ps = PersonRepository::GetAll(); @@ -226,8 +243,8 @@ class TestDataMapperCpp : public Test::Suite ps, expected); expected.clear(); - expected.push_back(Person(1, "Ervin", 38, 1.80)); - expected.push_back(Person(2, "Marvin", 32, 1.65)); + expected.push_back(Person(1, "Ervin", L"ŽŸ®", 8446744073709551615, 1.80)); + expected.push_back(Person(2, "Marvin", L"¥¥¥¥", 32, 1.65)); ps = PersonRepository::GetManyByQuery("SELECT * FROM person " "WHERE name LIKE '%vin'"); @@ -257,7 +274,7 @@ class TestDataMapperCpp : public Test::Suite { PersonRepository::Delete(1); // Ervin is gone - Person marvin(2, "Marvin", 24, 1.65); + Person marvin(2, "Marvin", L"¥¥¥¥", 24, 1.65); PersonRepository::Delete(marvin); // Marvin is gone too Test::assertTrue( From 59ca0358c6fe80c504c13651a11a3983a51b647e Mon Sep 17 00:00:00 2001 From: Rait Liiv Date: Mon, 8 Apr 2013 11:32:13 +0300 Subject: [PATCH 8/8] Small changes to GetManyByFieldPtr and added a test for it. --- include/datamappercpp/sql/Repository.h | 15 ++++---- test/src/main.cpp | 49 +++++++++++++++----------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/include/datamappercpp/sql/Repository.h b/include/datamappercpp/sql/Repository.h index cc4c5c3..d8e4bad 100644 --- a/include/datamappercpp/sql/Repository.h +++ b/include/datamappercpp/sql/Repository.h @@ -19,6 +19,7 @@ } #else #include + #include #include namespace dm { @@ -86,7 +87,7 @@ class Repository { public: typedef std::vector Entities; - typedef std::vector > EntitiesPtr; + typedef std::vector > EntitiesPtr; typedef SqlStatementBuilder EntitySqlBuilder; static void CreateTable(bool enableTransaction = true) @@ -367,14 +368,14 @@ class Repository return entities; } - static EntitiesPtr GetManyByQueryPtr(Statement& statement) + static EntitiesPtr GetManyByQueryPtr(Statement& statement) { EntitiesPtr entities; dbc::ResultSet::ptr result(statement->executeQuery()); while (result->next()) { - boost::shared_ptr entity( new Entity ); + stdutil::shared_ptr entity(stdutil::make_shared()); entity->id = (*result)[0]; ObjectFieldBinder fieldbinder(*result); @@ -386,10 +387,10 @@ class Repository return entities; } - static dbc::ResultSet::ptr ExecuteQuery(std::string sql) - { - Statement statement; - } + static dbc::ResultSet::ptr ExecuteQuery(std::string sql) + { + Statement statement; + } static void ResetStatements() { diff --git a/test/src/main.cpp b/test/src/main.cpp index 1e989f9..26a914c 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -24,10 +24,11 @@ struct Person { typedef std::vector list; + typedef std::vector> listPtr; __int64 id; std::string name; - std::wstring wname; + std::wstring wname; __int64 age; double height; @@ -41,16 +42,16 @@ struct Person bool operator==(const Person& rhs) const { - return id == rhs.id && name == rhs.name && wname == rhs.wname - && age == rhs.age && height == rhs.height; + return id == rhs.id && name == rhs.name && wname == rhs.wname + && age == rhs.age && height == rhs.height; } operator std::wstring() const { - std::wstring nameWstring; - #pragma warning(disable:4996)//We are perfectly aware the mbstowcs is not "safe" operation as the destination size is not given - fortunately our destination has dynamic size. - mbstowcs(&nameWstring[0], &name[0], name.length()); - #pragma warning(default:4996) + std::wstring nameWstring; + #pragma warning(disable:4996)//We are perfectly aware the mbstowcs is not "safe" operation as the destination size is not given - fortunately our destination has dynamic size. + mbstowcs(&nameWstring[0], &name[0], name.length()); + #pragma warning(default:4996) std::wostringstream out; out << "Person: {" << id << "," @@ -76,7 +77,7 @@ class PersonMapping { // Note that field order is important v.visitField(dm::Field("name", "UNIQUE NOT NULL"), p.name); - v.visitField(dm::Field("wname"), p.wname); + v.visitField(dm::Field("wname"), p.wname); v.visitField(dm::Field<__int64>("age"), p.age); v.visitField(dm::Field("height"), p.height); } @@ -99,8 +100,8 @@ class TestDataMapperCpp : public Test::Suite TestDataMapperCpp() { dm::sql::ConnectDatabase("test.sqlite"); - dm::sql::ExecuteStatement(std::string("DROP TABLE IF EXISTS ") - + PersonMapping::getLabel()); + dm::sql::ExecuteStatement(std::string("DROP TABLE IF EXISTS ") + + PersonMapping::getLabel()); } ~TestDataMapperCpp() @@ -124,15 +125,15 @@ class TestDataMapperCpp : public Test::Suite typedef dm::sql::SqlStatementBuilder PersonSql; Test::assertEqual( - "Create table statement is correct", - PersonSql::CreateTableStatement(), - "CREATE TABLE IF NOT EXISTS person" - "(id INTEGER PRIMARY KEY AUTOINCREMENT," - "name TEXT UNIQUE NOT NULL," - "wname TEXT," - "age INT," - "height REAL);" - "CREATE INDEX IF NOT EXISTS person_name_idx ON person (name)"); + "Create table statement is correct", + PersonSql::CreateTableStatement(), + "CREATE TABLE IF NOT EXISTS person" + "(id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT UNIQUE NOT NULL," + "wname TEXT," + "age INT," + "height REAL);" + "CREATE INDEX IF NOT EXISTS person_name_idx ON person (name)"); Test::assertEqual( "Insert statement is correct", @@ -199,9 +200,9 @@ class TestDataMapperCpp : public Test::Suite "if allow_many is true", p.name == "Ervin" && p.wname == L"ŽŸ®" && p.age == 8446744073709551615 && p.height == 1.80); - statement = dm::sql::PrepareStatement( + statement = dm::sql::PrepareStatement( "SELECT * FROM person WHERE wname LIKE ? AND age = ?"); - *statement << L"%Ÿ®" << 8446744073709551615; + *statement << L"%Ÿ®" << 8446744073709551615; p = PersonRepository::GetByQuery(statement); Test::assertTrue( "Statement operator << works for wstring and __int64", @@ -242,6 +243,12 @@ class TestDataMapperCpp : public Test::Suite Test::assertEqual("Get many objects by field works", ps, expected); + Person::listPtr listPtrExpected; + listPtrExpected.push_back(boost::shared_ptr(new Person(1, "Ervin", L"ŽŸ®", 8446744073709551615, 1.80))); + Person::listPtr listPtr = PersonRepository::GetManyByFieldPtr("age", 8446744073709551615); + Test::assertEqual("Get many object pointers by field works", + *listPtr[0], *listPtrExpected[0]); + expected.clear(); expected.push_back(Person(1, "Ervin", L"ŽŸ®", 8446744073709551615, 1.80)); expected.push_back(Person(2, "Marvin", L"¥¥¥¥", 32, 1.65));