Menu

[r1815]: / cppdb / trunk / test / test_basic.cpp  Maximize  Restore  History

Download this file

134 lines (126 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// Distributed under:
//
// the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// or (at your opinion) under:
//
// The MIT License
// (See accompanying file MIT.txt or a copy at
// http://www.opensource.org/licenses/mit-license.php)
//
///////////////////////////////////////////////////////////////////////////////
#include <cppdb/frontend.h>
#include <iostream>
#include <sstream>
#define TEST(x) do { if(x) break; std::ostringstream ss; ss<<"Failed in " << __LINE__ <<' '<< #x; throw std::runtime_error(ss.str()); } while(0)
int main(int argc,char **argv)
{
std::string cs = "sqlite3:db=db.db";
if(argc >= 2) {
cs = argv[1];
}
try {
cppdb::session sql(cs);
try {
sql << "DROP TABLE test" << cppdb::exec;
}
catch(cppdb::cppdb_error const &e){}
if(sql.engine() == "sqlite3") {
sql<< "create table test ( id integer primary key autoincrement not null, "
"n integer, f real , t timestamp ,name text )" << cppdb::exec;
}
else if(sql.engine() == "mysql") {
sql<< "create table test ( id integer primary key auto_increment not null, "
"n integer, f real , t timestamp ,name text )" << cppdb::exec;
}
else if(sql.engine() == "postgresql" ) {
sql<< "create table test ( id serial primary key not null "
",n integer, f double precision , t timestamp ,name text )" << cppdb::exec;
}
else if(sql.engine() == "mssql" ) {
sql<< "create table test ( id integer identity(1,1) primary key not null "
",n integer, f double precision , t datetime ,name text )" << cppdb::exec;
}
else {
std::cerr << "Unknown engine: " << sql.engine() << std::endl;
return 1;
}
std::tm t;
time_t tt;
tt=time(NULL);
t = *localtime(&tt);
std::cout<<asctime(&t);
std::string torig=asctime(&t);
int n;
long long rowid;
{
cppdb::statement stat = sql<<"insert into test(n,f,t,name) values(?,?,?,?)"
<<10<<3.1415926565<<t
<<"Hello \'World\'";
stat.exec();
rowid = stat.sequence_last("test_id_seq");
TEST(rowid==1);
TEST(stat.affected()==1);
std::cout<<"ID: "<<rowid<<", Affected rows "<<stat.affected()<<std::endl;
}
{
cppdb::statement stat = sql<<"insert into test(n,f,t,name) values(?,?,?,?)"
<< cppdb::use(10,cppdb::not_null_value)
<< cppdb::use(3.1415926565,cppdb::null_value)
<< cppdb::use(t,cppdb::not_null_value)
<< cppdb::use("Hello \'World\'",cppdb::not_null_value)
<< cppdb::exec;
rowid = stat.sequence_last("test_id_seq");
std::cout<<"ID: "<<rowid<<", Affected rows "<<stat.affected()<<std::endl;
TEST(rowid==2);
TEST(stat.affected()==1);
}
cppdb::result res;
if(sql.engine()=="mssql")
res = sql<<"select top 10 id,n,f,t,name from test";
else
res = sql<<"select id,n,f,t,name from test limit 10";
n=0;
while(res.next()){
double f=-1;
int id=-1,k=-1;
std::tm atime=std::tm();
std::string name="nonset";
cppdb::null_tag_type tag;
res >> id >> k >> cppdb::into(f,tag) >> atime >> name;
std::cout <<id << ' '<<k <<' '<<f<<' '<<name<<' '<<asctime(&atime)<<std::endl;
TEST(id==n+1);
TEST(k==10);
TEST(n==0 ? f==3.1415926565: f==-1);
TEST(n==0 ? tag==cppdb::not_null_value : tag==cppdb::null_value);
TEST(asctime(&atime) == torig);
TEST(name=="Hello 'World'");
n++;
}
TEST(n==2);
res = sql << "SELECT n FROM test WHERE id=?" << 1 << cppdb::row;
TEST(!res.empty());
int val;
res >> val;
TEST(val == 10);
cppdb::statement stat = sql<<"delete from test where 1<>0" << cppdb::exec;
std::cout<<"Deleted "<<stat.affected()<<" rows\n";
TEST(stat.affected()==2);
TEST(!stat.empty());
stat.clear();
TEST(stat.empty());
TEST(cppdb::statement().empty());
}
catch(std::exception const &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
std::cout << "Ok" << std::endl;
return 0;
}
MongoDB Logo MongoDB