SOCI Logo Get SOCI at SourceForge.net. Fast, secure and Free Open Source software downloads

test-empty.cpp

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
00003 // Distributed under the Boost Software License, Version 1.0.
00004 // (See accompanying file LICENSE_1_0.txt or copy at
00005 // http://www.boost.org/LICENSE_1_0.txt)
00006 //
00007 
00008 //
00009 #if defined(SOCI_HEADERS_BURIED)
00010 #       include <soci/core/soci.h>
00011 #       include <soci/backends/empty/soci-empty.h>
00012 #else
00013 #       include <soci.h>
00014 #       include <soci-empty.h>
00015 #endif
00016 //
00017 #include <iostream>
00018 #include <string>
00019 #include <cassert>
00020 #include <cstdlib>
00021 #include <ctime>
00022 
00023 using namespace soci;
00024 
00025 std::string connectString;
00026 backend_factory const &backEnd = empty;
00027 
00028 
00029 // NOTE:
00030 // This file is supposed to serve two purposes:
00031 // 1. To be a starting point for implementing new tests (for new backends).
00032 // 2. To exercise (at least some of) the syntax and try the SOCI library
00033 //    against different compilers, even in those environments where there
00034 //    is no database. SOCI uses advanced template techniques which are known
00035 //    to cause problems on different versions of popular compilers, and this
00036 //    test is handy to verify that the code is accepted by as many compilers
00037 //    as possible.
00038 //
00039 // Both of these purposes mean that the actual code here is meaningless
00040 // from the database-development point of view. For new tests, you may wish
00041 // to remove this code and keep only the general structure of this file.
00042 
00043 struct Person
00044 {
00045     int id;
00046     std::string firstName;
00047     std::string lastName;
00048 };
00049 
00050 namespace soci
00051 {
00052     template<> struct type_conversion<Person>
00053     {
00054         typedef values base_type;
00055         static void from_base(values & /* r */, indicator /* ind */,
00056             Person & /* p */)
00057         {
00058         }
00059     };
00060 }
00061 
00062 void test1()
00063 {
00064     {
00065         session sql(backEnd, connectString);
00066 
00067         sql << "Do what I want.";
00068         sql << "Do what I want " << 123 << " times.";
00069 
00070         std::string query = "some query";
00071         sql << query;
00072 
00073         int i = 7;
00074         sql << "insert", use(i);
00075         sql << "select", into(i);
00076 
00077         indicator ind = i_ok;
00078         sql << "insert", use(i, ind);
00079         sql << "select", into(i, ind);
00080 
00081         std::vector<int> numbers(100);
00082         sql << "insert", use(numbers);
00083         sql << "select", into(numbers);
00084 
00085         std::vector<indicator> inds(100);
00086         sql << "insert", use(numbers, inds);
00087         sql << "select", into(numbers, inds);
00088 
00089         {
00090             statement st = (sql.prepare << "select", into(i));
00091             st.execute();
00092             st.fetch();
00093         }
00094         {
00095             statement st = (sql.prepare << "select", into(i, ind));
00096         }
00097         {
00098             statement st = (sql.prepare << "select", into(numbers));
00099         }
00100         {
00101             statement st = (sql.prepare << "select", into(numbers, inds));
00102         }
00103         {
00104             statement st = (sql.prepare << "insert", use(i));
00105         }
00106         {
00107             statement st = (sql.prepare << "insert", use(i, ind));
00108         }
00109         {
00110             statement st = (sql.prepare << "insert", use(numbers));
00111         }
00112         {
00113             statement st = (sql.prepare << "insert", use(numbers, inds));
00114         }
00115         {
00116             Person p;
00117             sql << "select person", into(p);
00118         }
00119 
00120     }
00121 
00122     std::cout << "test 1 passed" << std::endl;
00123 }
00124 
00125 
00126 int main(int argc, char** argv)
00127 {
00128 
00129 #ifdef _MSC_VER
00130     // Redirect errors, unrecoverable problems, and assert() failures to STDERR,
00131     // instead of debug message window.
00132     // This hack is required to run asser()-driven tests by Buildbot.
00133     // NOTE: Comment this 2 lines for debugging with Visual C++ debugger to catch assertions inside.
00134     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
00135     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
00136 #endif //_MSC_VER
00137 
00138     if (argc == 2)
00139     {
00140         connectString = argv[1];
00141     }
00142     else
00143     {
00144         std::cout << "usage: " << argv[0]
00145             << " connectstring\n"
00146             << "example: " << argv[0]
00147             << " \'connect_string_for_empty_backend\'\n";
00148         std::exit(1);
00149     }
00150 
00151     try
00152     {
00153         test1();
00154         // test2();
00155         // ...
00156 
00157         std::cout << "\nOK, all tests passed.\n\n";
00158     }
00159     catch (std::exception const & e)
00160     {
00161         std::cout << e.what() << '\n';
00162     }
00163 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
SourceForge Logo

Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1