| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]> |
| 4 | // |
| 5 | // Distributed under: |
| 6 | // |
| 7 | // the Boost Software License, Version 1.0. |
| 8 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | // |
| 11 | // or (at your opinion) under: |
| 12 | // |
| 13 | // The MIT License |
| 14 | // (See accompanying file MIT.txt or a copy at |
| 15 | // http://www.opensource.org/licenses/mit-license.php) |
| 16 | // |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | #include <cppdb/frontend.h> |
| 19 | #include <cppdb/connection_specific.h> |
| 20 | #include <iostream> |
| 21 | #include <sstream> |
| 22 | |
| 23 | #define TEST(x) do { if(x) break; std::ostringstream ss; ss<<"Failed in " << __LINE__ <<' '<< #x; throw std::runtime_error(ss.str()); } while(0) |
| 24 | |
| 25 | class my_specific_a : public cppdb::connection_specific_data { |
| 26 | public: |
| 27 | int val; |
| 28 | my_specific_a(int v=0) : val(v) {} |
| 29 | }; |
| 30 | |
| 31 | class my_specific_b : public cppdb::connection_specific_data { |
| 32 | public: |
| 33 | int val; |
| 34 | my_specific_b(int v=0) : val(v) {} |
| 35 | }; |
| 36 | |
| 37 | int call_counter = 0; |
| 38 | |
| 39 | void caller(cppdb::session &) |
| 40 | { |
| 41 | call_counter++; |
| 42 | } |
| 43 | |
| 44 | struct functor_caller { |
| 45 | void operator()(cppdb::session &) const |
| 46 | { |
| 47 | call_counter++; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | int main(int argc,char **argv) |
| 53 | { |
| 54 | std::cout << "Testing CppDB version `" << cppdb::version_string() |
| 55 | << "' " << cppdb::version_number() << std::endl; |
| 56 | std::string cs = "sqlite3:db=db.db"; |
| 57 | if(argc >= 2) { |
| 58 | cs = argv[1]; |
| 59 | } |
| 60 | try { |
| 61 | cppdb::session sql(cs); |
| 62 | |
| 63 | try { |
| 64 | sql << "DROP TABLE test" << cppdb::exec; |
| 65 | } |
| 66 | catch(cppdb::cppdb_error const &e){} |
| 67 | if(sql.engine() == "sqlite3") { |
| 68 | sql<< "create table test ( id integer primary key autoincrement not null, " |
| 69 | "n integer, f real , t timestamp ,name text )" << cppdb::exec; |
| 70 | } |
| 71 | else if(sql.engine() == "mysql") { |
| 72 | sql<< "create table test ( id integer primary key auto_increment not null, " |
| 73 | "n integer, f real , t timestamp ,name text )" << cppdb::exec; |
| 74 | } |
| 75 | else if(sql.engine() == "postgresql" ) { |
| 76 | sql<< "create table test ( id serial primary key not null " |
| 77 | ",n integer, f double precision , t timestamp ,name text )" << cppdb::exec; |
| 78 | } |
| 79 | else if(sql.engine() == "mssql" ) { |
| 80 | sql<< "create table test ( id integer identity(1,1) primary key not null " |
| 81 | ",n integer, f double precision , t datetime ,name text )" << cppdb::exec; |
| 82 | } |
| 83 | else { |
| 84 | std::cerr << "Unknown engine: " << sql.engine() << std::endl; |
| 85 | return 1; |
| 86 | } |
| 87 | std::tm t; |
| 88 | std::time_t tt; |
| 89 | tt=std::time(NULL); |
| 90 | t = *std::localtime(&tt); |
| 91 | std::cout<<asctime(&t); |
| 92 | std::string torig=asctime(&t); |
| 93 | int n; |
| 94 | long long rowid; |
| 95 | { |
| 96 | cppdb::statement stat = sql<<"insert into test(n,f,t,name) values(?,?,?,?)" |
| 97 | <<10<<3.1415926565<<t |
| 98 | <<"Hello \'World\'"; |
| 99 | stat.exec(); |
| 100 | rowid = stat.sequence_last("test_id_seq"); |
| 101 | TEST(rowid==1); |
| 102 | TEST(stat.affected()==1); |
| 103 | std::cout<<"ID: "<<rowid<<", Affected rows "<<stat.affected()<<std::endl; |
| 104 | } |
| 105 | { |
| 106 | cppdb::statement stat = sql<<"insert into test(n,f,t,name) values(?,?,?,?)" |
| 107 | << cppdb::use(10,cppdb::not_null_value) |
| 108 | << cppdb::use(3.1415926565,cppdb::null_value) |
| 109 | << cppdb::use(t,cppdb::not_null_value) |
| 110 | << cppdb::use("Hello \'World\'",cppdb::not_null_value) |
| 111 | << cppdb::exec; |
| 112 | rowid = stat.sequence_last("test_id_seq"); |
| 113 | std::cout<<"ID: "<<rowid<<", Affected rows "<<stat.affected()<<std::endl; |
| 114 | TEST(rowid==2); |
| 115 | TEST(stat.affected()==1); |
| 116 | } |
| 117 | |
| 118 | cppdb::result res; |
| 119 | if(sql.engine()=="mssql") |
| 120 | res = sql<<"select top 10 id,n,f,t,name from test"; |
| 121 | else |
| 122 | res = sql<<"select id,n,f,t,name from test limit 10"; |
| 123 | n=0; |
| 124 | while(res.next()){ |
| 125 | double f=-1; |
| 126 | int id=-1,k=-1; |
| 127 | std::tm atime=std::tm(); |
| 128 | std::string name="nonset"; |
| 129 | cppdb::null_tag_type tag; |
| 130 | res >> id >> k >> cppdb::into(f,tag) >> atime >> name; |
| 131 | std::cout <<id << ' '<<k <<' '<<f<<' '<<name<<' '<<asctime(&atime)<<std::endl; |
| 132 | TEST(id==n+1); |
| 133 | TEST(k==10); |
| 134 | TEST(n==0 ? f==3.1415926565: f==-1); |
| 135 | TEST(n==0 ? tag==cppdb::not_null_value : tag==cppdb::null_value); |
| 136 | TEST(asctime(&atime) == torig); |
| 137 | TEST(name=="Hello 'World'"); |
| 138 | n++; |
| 139 | } |
| 140 | TEST(n==2); |
| 141 | |
| 142 | res = sql << "SELECT n FROM test WHERE id=?" << 1 << cppdb::row; |
| 143 | TEST(!res.empty()); |
| 144 | int val; |
| 145 | res >> val; |
| 146 | TEST(val == 10); |
| 147 | res.clear(); |
| 148 | |
| 149 | cppdb::statement stat = sql<<"delete from test where 1<>0" << cppdb::exec; |
| 150 | std::cout<<"Deleted "<<stat.affected()<<" rows\n"; |
| 151 | TEST(stat.affected()==2); |
| 152 | TEST(!stat.empty()); |
| 153 | stat.clear(); |
| 154 | TEST(stat.empty()); |
| 155 | TEST(cppdb::statement().empty()); |
| 156 | |
| 157 | sql.reset_specific(new my_specific_a(10)); |
| 158 | TEST(sql.get_specific<my_specific_b>()==0); |
| 159 | TEST(sql.get_specific<my_specific_a>()!=0); |
| 160 | TEST(sql.get_specific<my_specific_a>()->val==10); |
| 161 | sql.reset_specific(new my_specific_b(20)); |
| 162 | TEST(sql.get_specific<my_specific_b>()!=0); |
| 163 | TEST(sql.get_specific<my_specific_a>()!=0); |
| 164 | TEST(sql.get_specific<my_specific_a>()->val==10); |
| 165 | TEST(sql.get_specific<my_specific_b>()->val==20); |
| 166 | sql.reset_specific<my_specific_b>(); |
| 167 | TEST(sql.get_specific<my_specific_b>()==0); |
| 168 | TEST(sql.get_specific<my_specific_a>()!=0); |
| 169 | my_specific_a *p = sql.release_specific<my_specific_a>(); |
| 170 | TEST(p!=0); |
| 171 | delete p; |
| 172 | TEST(sql.get_specific<my_specific_a>()==0); |
| 173 | TEST(sql.release_specific<my_specific_a>() == 0); |
| 174 | |
| 175 | TEST(call_counter == 0); |
| 176 | TEST(sql.once_called()==false); |
| 177 | sql.once(&caller); |
| 178 | TEST(sql.once_called()==true); |
| 179 | TEST(call_counter == 1); |
| 180 | sql.once(&caller); |
| 181 | TEST(call_counter == 1); |
| 182 | sql.close(); |
| 183 | bool have_pool = cs.find("@pool_size")!=std::string::npos; |
| 184 | sql.open(cs); |
| 185 | TEST(sql.once_called()==have_pool); |
| 186 | sql.once(functor_caller()); |
| 187 | TEST(have_pool ? call_counter == 1 : call_counter == 2); |
| 188 | } |
| 189 | catch(std::exception const &e) { |
| 190 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 191 | return 1; |
| 192 | } |
| 193 | std::cout << "Ok" << std::endl; |
| 194 | return 0; |
| 195 | } |