test-empty.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #include "soci.h"
9 #include "soci-empty.h"
10 #include <iostream>
11 #include <string>
12 #include <cassert>
13 #include <cstdlib>
14 #include <ctime>
15 
16 using namespace soci;
17 
18 std::string connectString;
20 
21 
22 // NOTE:
23 // This file is supposed to serve two purposes:
24 // 1. To be a starting point for implementing new tests (for new backends).
25 // 2. To exercise (at least some of) the syntax and try the SOCI library
26 // against different compilers, even in those environments where there
27 // is no database. SOCI uses advanced template techniques which are known
28 // to cause problems on different versions of popular compilers, and this
29 // test is handy to verify that the code is accepted by as many compilers
30 // as possible.
31 //
32 // Both of these purposes mean that the actual code here is meaningless
33 // from the database-development point of view. For new tests, you may wish
34 // to remove this code and keep only the general structure of this file.
35 
36 struct Person
37 {
38  int id;
39  std::string firstName;
40  std::string lastName;
41 };
42 
43 namespace soci
44 {
45  template<> struct type_conversion<Person>
46  {
47  typedef values base_type;
48  static void from_base(values & /* r */, indicator /* ind */,
49  Person & /* p */)
50  {
51  }
52  };
53 }
54 
55 void test1()
56 {
57  {
58  session sql(backEnd, connectString);
59 
60  sql << "Do what I want.";
61  sql << "Do what I want " << 123 << " times.";
62 
63  std::string query = "some query";
64  sql << query;
65 
66  int i = 7;
67  sql << "insert", use(i);
68  sql << "select", into(i);
69 
70 #if defined (__LP64__) || ( __WORDSIZE == 64 )
71  long int li = 9;
72  sql << "insert", use(li);
73  sql << "select", into(li);
74 #endif
75 
76  long long ll = 11;
77  sql << "insert", use(ll);
78  sql << "select", into(ll);
79 
80  indicator ind = i_ok;
81  sql << "insert", use(i, ind);
82  sql << "select", into(i, ind);
83 
84  std::vector<int> numbers(100);
85  sql << "insert", use(numbers);
86  sql << "select", into(numbers);
87 
88  std::vector<indicator> inds(100);
89  sql << "insert", use(numbers, inds);
90  sql << "select", into(numbers, inds);
91 
92  {
93  statement st = (sql.prepare << "select", into(i));
94  st.execute();
95  st.fetch();
96  }
97  {
98  statement st = (sql.prepare << "select", into(i, ind));
99  }
100  {
101  statement st = (sql.prepare << "select", into(numbers));
102  }
103  {
104  statement st = (sql.prepare << "select", into(numbers, inds));
105  }
106  {
107  statement st = (sql.prepare << "insert", use(i));
108  }
109  {
110  statement st = (sql.prepare << "insert", use(i, ind));
111  }
112  {
113  statement st = (sql.prepare << "insert", use(numbers));
114  }
115  {
116  statement st = (sql.prepare << "insert", use(numbers, inds));
117  }
118  {
119  Person p;
120  sql << "select person", into(p);
121  }
122 
123  }
124 
125  std::cout << "test 1 passed" << std::endl;
126 }
127 
128 
129 int main(int argc, char** argv)
130 {
131 
132 #ifdef _MSC_VER
133  // Redirect errors, unrecoverable problems, and assert() failures to STDERR,
134  // instead of debug message window.
135  // This hack is required to run asser()-driven tests by Buildbot.
136  // NOTE: Comment this 2 lines for debugging with Visual C++ debugger to catch assertions inside.
137  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
138  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
139 #endif //_MSC_VER
140 
141  if (argc == 2)
142  {
143  connectString = argv[1];
144  }
145  else
146  {
147  std::cout << "usage: " << argv[0]
148  << " connectstring\n"
149  << "example: " << argv[0]
150  << " \'connect_string_for_empty_backend\'\n";
151  std::exit(1);
152  }
153 
154  try
155  {
156  test1();
157  // test2();
158  // ...
159 
160  std::cout << "\nOK, all tests passed.\n\n";
161 
162  return EXIT_SUCCESS;
163  }
164  catch (std::exception const & e)
165  {
166  std::cout << e.what() << '\n';
167  }
168 
169  return EXIT_FAILURE;
170 }
details::into_container< T, details::no_indicator > into(T &t)
Definition: into.h:51
backend_factory const & backEnd
Definition: test-empty.cpp:19
std::string lastName
Definition: test-empty.cpp:40
std::string firstName
Definition: test-empty.cpp:39
std::string connectString
Definition: test-empty.cpp:18
int main(int argc, char **argv)
Definition: test-empty.cpp:129
void test1()
Definition: test-empty.cpp:55
int id
Definition: test-empty.cpp:38
static void from_base(values &, indicator, Person &)
Definition: test-empty.cpp:48
details::prepare_type prepare
Definition: session.h:69
details::use_container< T, details::no_indicator > use(T &t, const std::string &name=std::string())
Definition: use.h:43
SOCI_EMPTY_DECL backend_factory const * factory_empty()
bool execute(bool withDataExchange=false)
Definition: statement.h:208


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41