OTL 샘플

C++ 2012. 4. 6. 11:32



#pragma once



#define OTL_ODBC_MSSQL_2005

#define OTL_STL

#define OTL_UNICODE

#define OTL_UNICODE_EXCEPTION_AND_RLOGON

#include <otl/otlv4.h>


// DB 준비 

/*

-- CREATE TEST INSERT TABLE

CREATE TABLE LogData

(

errormessage VARCHAR(1024)

)


-- CREATE STORED PROCEDURE

CREATE procedure [dbo].[my_proc]

@A int out,

@B varchar(60) out,

@C varchar(60)

as

SET NOCOUNT ON

begin



select @A=@A+1

select @B=@C


return 111

end


-- TEST STORED PROCEDURE

Declare @rc INT

Declare @b VARCHAR(60)


exec @rc = my_proc 1, @b output, 'cccc'

select @rc

select @b

*/


class TestOtl

{

public:

TestOtl(){}

~TestOtl(){}


void insert(otl_connect& db)

{

otl_stream o;

o.open(100, "insert into LogData values(:f1<char[1024]>)", db);

o.set_commit(0); // ROLLBACK // auto commmit off

std::wstring errormsg  = L"ERROR 한글 MESSAGE";

for (int i=0; i<2; ++i) {

o << errormsg.c_str();

}

o.set_flush(); // ROLLBACK // not yet commited

}


void select(otl_connect& db)

{

otl_stream i;

i.open(10, "select * from LogData", db);

wchar_t errormsg[1024] = {0};

while (!i.eof()) {

i >> errormsg;

wcout << L"errormsg : " << errormsg << endl;

}

i.clean();

}


void storedProcedure(otl_connect& db)

{

otl_stream o(1, "{:rc<int,out> = call my_proc( :A<int,inout>, :B<char[31],out>, :C<char[31],in> )}", db);

o.set_commit(0); // set stream auto-commit off since the stream does not generate transaction


o << 1 << L"Test String1"; // assigning :1 = 1, :3 = "Test String1"


int a;

wchar_t b[31] = {0};

int result = -1;

o >> result; // return

o >> a >> b;

wcout << L"A=" << a << L", B="<< b << endl;

wcout << L"result : " << result << endl;

}


void main()

{

int multithread_mode = 1; // 0 : single-thread, 1 : multi-thread

int tret1 = otl_connect::otl_initialize(multithread_mode);


try {

otl_connect db; // connect object;


//config

int timeout = 5; // sec. sql statement timeout

db.set_timeout(timeout);

// @가 들어있는 경우 \\ 처리 필요 : AAA@a -> AAA\\@a

std::string connection_string = "DRIVER={SQL Server};SERVER=192.168.56.101,1433;DATABASE=Test;UID=sa;PWD=AAA\\@1;";

db.rlogon(connection_string.c_str());

db.auto_commit_off();

db.set_transaction_isolation_level(otl_tran_read_committed); // ROLLBACK


while (true) {


insert(db);

db.rollback(); // ROLLBACK

select(db);

storedProcedure(db);

}

db.logoff(); // disconnect

} catch(otl_exception& e) {

wcout << L"error code : " << e.code << L"\n"

<< L"SQLSTATE : " << e.sqlstate << L"\n"

<< L"error msg : " << e.msg << L"\n"

<< L"error statment : " << e.stm_text << L"\n"

<< L"error variable : " << e.var_info << L"\n";

}

}

};