63 "SDBAPI test program");
75 string server = args[
"s"].AsString();
85 cout <<
"Creating SelectSample table...";
86 sql =
"if exists( select * from sysobjects \
87 where name = 'SelectSample' \
90 drop table SelectSample \
94 query.RequireRowCount(0);
97 sql =
"create table SelectSample (\
98 int_val int not null, \
99 fl_val real not null, \
100 date_val smalldatetime not null, \
101 str_val varchar(255) not null, \
102 text_val text not null)";
105 query.RequireRowCount(0);
108 sql =
"insert SelectSample values (1, 2.5, '11/05/2005', 'Test string1', 'TextBlobTextBlobTextBlobTextBlobTextBlob') \
109 insert SelectSample values (2, 3.3, '11/06/2005', 'Test string2', 'TextBlobTextBlobTextBlobTextBlobTextBlob') \
110 insert SelectSample values (3, 4.4, '11/07/2005', 'Test string3', 'TextBlobTextBlobTextBlobTextBlobTextBlob') \
111 insert SelectSample values (4, 5.5, '11/08/2005', 'Test string4', 'TextBlobTextBlobTextBlobTextBlobTextBlob') \
112 insert SelectSample values (5, 6.6, '11/09/2005', 'Test string5', 'TextBlobTextBlobTextBlobTextBlobTextBlob')";
115 query.RequireRowCount(0);
118 sql =
"select int_val, fl_val, date_val, str_val from SelectSample";
119 cout << endl <<
"Testing simple select..." << endl <<
sql << endl;
123 query.RequireRowCount(5);
125 bool show_names =
true;
126 for (
const auto& row :
query) {
128 for (
unsigned i = 1;
i <= row.GetTotalColumns(); ++
i) {
129 cout << row.GetColumnName(
i) <<
" ";
135 for(
unsigned i = 1;
i <= row.GetTotalColumns(); ++
i) {
136 cout << row[
i].AsString() <<
"|";
140 cout << row[1].AsInt4() <<
"|"
141 << row[2].AsFloat() <<
"|"
142 << row[
"date_val"].AsString() <<
"|"
143 << row[4].AsString() <<
"|"
149 cout <<
"Rows : " <<
query.GetRowCount() << endl;
152 cout <<
"Exception: " << e.
what() << endl;
156 cout << endl <<
"Creating BulkSample table..." << endl;
157 sql =
"if exists( select * from sysobjects \
158 where name = 'BulkSample' \
161 drop table BulkSample \
165 query.RequireRowCount(0);
168 sql =
"create table BulkSample (\
171 mode tinyint not null, \
172 date datetime not null)";
175 query.RequireRowCount(0);
180 cout <<
"Initializing BulkSample table..." << endl;
186 for (
int i = 0;
i < 10; ++
i) {
197 sql =
"if exists( select * from sysobjects \
198 where name = 'SampleProc' \
201 drop proc SampleProc \
205 query.RequireRowCount(0);
210 sql =
"create procedure SampleProc \
216 select int_val, fl_val, date_val from SelectSample \
217 where int_val < @id and fl_val <= @f \
219 select 2121, 'Parameter @id:', @id, 'Parameter @f:', @f, 'Parameter @o:', @o \
220 print 'Print test output' \
225 sql =
"create procedure SampleProc \
231 select int_val, fl_val, date_val from SelectSample \
232 where int_val < @id and fl_val <= @f \
234 select 2121, 'Parameter @id:', @id, 'Parameter @f:', @f, 'Parameter @o:', @o \
235 print 'Print test output' \
236 raiserror('Raise Error test output', 1, 1) \
242 query.RequireRowCount(0);
248 cout << endl <<
"Calling stored procedure..." << endl;
250 query.SetParameter(
"@id", 5);
251 query.SetParameter(
"@f",
f);
253 query.ExecuteSP(
"SampleProc");
256 for (
const auto& row:
query.SingleSet()) {
257 if (row[1].AsInt4() == 2121) {
258 cout << row[2].AsString() <<
" "
259 << row[3].AsString() <<
" "
260 << row[4].AsString() <<
" "
261 << row[5].AsString() <<
" "
262 << row[6].AsString() <<
" "
263 << row[7].AsString() <<
" "
267 cout << row[1].AsInt4() <<
"|"
268 << row[2].AsFloat() <<
"|"
269 << row[
"date_val"].AsString() <<
"|"
274 cout <<
"Output param: "
275 <<
query.GetParameter(
"@o").AsInt4()
277 cout <<
"Status : " <<
query.GetStatus() << endl;
280 cout << endl <<
"Reconnecting..." << endl;
288 cout << endl <<
"Creating BlobSample table..." << endl;
289 sql =
"if exists( select * from sysobjects \
290 where name = 'BlobSample' \
293 drop table BlobSample \
297 query.RequireRowCount(0);
300 sql =
"create table BlobSample (\
302 blob2 text null, blob text null, unique (id))";
305 query.RequireRowCount(0);
312 cout <<
"Initializing BlobSample table..." << endl;
318 for (
int i = 0;
i < COUNT; ++
i ) {
325 cout <<
"Checking BLOB size..." << endl;
326 query.SetSql(
"select 'Written blob size' as size, datalength(blob) \
327 from BlobSample where id = 1");
329 query.RequireRowCount(1);
331 for (
const auto& row:
query.SingleSet()) {
332 cout << row[1].AsString() <<
": "
333 << row[2].AsInt4() << endl;
339 cout <<
"Rowcount test..." << endl;
340 sql =
"update BlobSample set blob ='deleted'";
343 query.RequireRowCount(0);
345 cout <<
"Rows updated: " <<
query.GetRowCount() << endl;
348 cout <<
"Deleting BlobSample table..." << endl;
349 sql =
"drop table BlobSample";
352 query.RequireRowCount(0);
354 cout <<
"Done." << endl;
356 catch(out_of_range) {
357 cout <<
"Exception: Out of range" << endl;
360 catch(exception& e) {
361 cout <<
"Exception: " << e.what() << endl;
Object used to perform bulk-inserting operations to database.
void Complete()
Complete bulk insert.
void Bind(int col, ESDB_Type type)
Bind column for bulk insert.
Database connection object.
void Close(void)
Close database object.
void Connect(void)
Explicitly (re)connect to the database server.
CQuery NewQuery(void)
Get new CQuery object for this database.
CBulkInsert NewBulkInsert(const string &table_name, int autoflush)
Get new CBulkInsert object.
Object used to execute queries and stored procedures on the database server and retrieve result sets.
Convenience class to initialize database connection parameters from URL-like strings and/or applicati...
CSDB_ConnectionParam & Set(EParam param, const string &value, TSetFlags flags=0)
Set one of the "essential" database connection parameters, unless overridden in a configuration file.
@ eService
Named service, interfaces-file alias, or raw server name, per http://ncbi.github.io/cxx-toolkit/pages...
Exception class used throughout the API.
virtual void Exit()
Cleanup on application exit.
virtual void Init()
Initialize the application.
virtual int Run()
Run the application.
CArgDescriptions * argList
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
void SetUsageContext(const string &usage_name, const string &usage_description, bool usage_sort_args=false, SIZE_TYPE usage_width=78)
Set extra info to be used by PrintUsage().
void AddDefaultKey(const string &name, const string &synopsis, const string &comment, EType type, const string &default_value, TFlags flags=0, const string &env_var=kEmptyStr, const char *display_value=nullptr)
Add description for optional key with default value.
@ eString
An arbitrary string.
virtual const char * what(void) const noexcept
Standard report (includes full backlog).
uint8_t Uint1
1-byte (8-bit) unsigned integer
static const SAutoMax kMax_Auto
Generic stand-in for type-specific kMax_* constants from ncbi_limits.h, useful in any context with ex...
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
@ eCurrent
Use current time. See also CCurrentTime.
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines command line argument related classes.
Defines unified interface to application:
double f(double x_, const double &y_)
CBulkInsert & EndRow(CBulkInsert &bi)
Manipulator ending row in the bulk-insert object.
@ eSDB_Text
Like Transact-SQL TEXT.
@ eSDB_DateTime
Like Transact-SQL DATETIME.
@ eSDB_Int4
Like Transact-SQL INT (signed 32-bit integer)
@ eSDB_Byte
Like Transact-SQL BYTE (unsigned 8-bit integer)
@ eSP_InOut
Parameter can be returned from stored procedure.
int NcbiSys_main(int argc, ncbi::TXChar *argv[])