OTL Support

OTL(オラクルTemplateLibrary)は、データベースアクセス用のC++ライブラリです。単一のヘッダファイルで構成されます。OTLの詳細については、次をご覧ください。

OTL認証

EDBOCLコネクタ、バージョン13.1.4.2はOTL4.0で認定されています。OTLがサポートするデータ型を使用し、他のOTL固有の動作のために、OTLベースのアプリを実行する前に、シェルでOTL環境変数を定義する必要があります。OTLの値は重要ではなく、定義するだけです。例、OTLに関連しているシナリオの条件付き実行のためOTL=TRUEをエクスポートことができます。

EDBOCLコネクタは、次のOTL機能で認定されています。

  • otl_connect を使用して、接続、切断、コミット、およびロールバックします。

  • 静的ファンクション otl_cursor::direct_exec を使用した定数SQL文(バインド変数がない場合、SQL文は定数です)。 CREATE TABLECREATE PROCEDURE/FUNCTION などのほとんどのDDLステートメントが含まれます。

  • otl_stream class を使用したバインド変数を持つSQLステートメント。 SELECTUPDATEDELETEINSERT 、および PROCEDURE/FUNCTION 呼び出しのようなほとんどのDMLステートメントが含まれます。

  • otl_datetime を使用した日付/時刻データ型。

  • otl_long_string を使用したRaw/LongRawデータ型。

  • otl_refcur_stream を使用してカーソルを参照します。

接続してログイン

次のコードは、OCLを初期化し、 tnsnames.ora ベースの接続文字列を使用してデータベースに接続する方法を示しています。

otl_connect db;
otl_connect::otl_initialize();

db.rlogon("enterprisedb/edb@EDBX");
if(db.connected)
          cout<<"Connected to Database"<<endl;

CREATETABLE、INSERT、およびSELECT

次のコードは、 otl_cursor::direct_exec を使用してテーブルを作成し、このテーブルに行を挿入する方法を示しています。その後、 otl_stream を使用して、挿入された行を取得できます。

char* createstmt = "create table testtable(c1 VARCHAR2(15), c2 DATE)";
char* insertstmt = "insert into testtable values('test_data123', TO_DATE('2005-12-31 23:59:59','YYYY-MM-DD HH24:MI:SS'))";
char* selectstmt = "select c1, c2 from testtable";

otl_cursor::direct_exec(db, createstmt);    // create table
db.commit();

otl_cursor::direct_exec(db, insertstmt);    //Insert data.

char strData[100];
otl_datetime dtData;
otl_stream otlCur(50, sqlstmnt,db);
while (!otlCur.eof())
{
otlCur >> strData >> dtData;

cout<<"Retrieved Value: "<<data<<endl;
cout<<"Retrieved Value: "<<data.month<<"/"<<data.day<<"/"<<data.year<<" "<<data.hour<<":"<<data.minute<<":"<<data.second<<endl;
}

更新

次のコードは、UPDATEステートメントでバインドパラメータを使用する方法を示しています。

char* updatestmt = "UPDATE testtable SET c1=:c1<char[49]> WHERE c1=:c2<char[49]>";

char whereValue[50] = "test_data123";
char data[50] = "otl test";
otl_stream otlCur(80, updatestmt, db);
otlCur.set_commit(0);
otlCur<<data<<whereValue;

ストアドプロシージャ

次のコードは、 otl_cursor::direct_exec を使用してストアドプロシージャを作成し、 otl_stream を使用してそれを呼び出す方法を示しています。

otl_cursor::direct_exec
(
db,
"CREATE OR REPLACE PROCEDURE my_procOneIntOut "
"  (A IN NUMBER, B OUT NUMBER)"
"IS "
"BEGIN "
"   B := A;"
"END;"
);

otl_stream otlCur(1, "begin my_procOneIntOut(:A<int,in>, :B<int,out>);end;", db);
otlCur.set_commit(0);

int a = 10;
otlCur<<a;

int b;
otlCur>>b;
cout << "B: " << b << endl;

ファンクション

次のコードは、 otl_cursor::direct_exec を使用して関数を作成し、 otl_stream を使用してファンクションを呼び出す方法を示しています。

注釈

この例では、 edb サンプルデータベースの emp テーブルを使用しています。

otl_cursor::direct_exec
(
db,
"CREATE OR REPLACE FUNCTION get_no_int(e_name character varying(10)) "
"RETURNS int AS $$ "
"DECLARE retval int; "


"BEGIN "
            "SELECT empno FROM emp WHERE ename = e_name INTO retval; "
            "RETURN retval; "
    "END; "

    "$$  LANGUAGE plpgsql;"
);

char ename[50] = "SCOTT";
otl_stream otlCur(1,
"begin "
" :rc<int,out> := get_no_int(:c1<char[11],in>);"
"end;"
, db);
otlCur << ename;

int eno;
otlCur >> eno;

cout<<"Retrieved Value: "<<eno<<endl;

REFCURSOR

次のコードは、 OUT パラメーターとして3つのREFCURSORを返し、それを呼び出すプロシージャを使用してパッケージを作成する方法を示しています。

注釈

この例では、 edb サンプルデータベースの emp テーブルを使用しています。

otl_cursor::direct_exec
(
db,
"CREATE OR REPLACE PACKAGE ref_test
IS
TYPE p_cursor IS REF CURSOR;
PROCEDURE getdata(empc OUT p_cursor, salc OUT p_cursor, comc OUT p_cursor);
END ref_test;"
);

otl_cursor::direct_exec
    (
        db,
            "CREATE OR REPLACE PACKAGE BODY ref_test \
             IS \
             PROCEDURE getdata(empc OUT p_cursor, salc OUT p_cursor, comc OUT p_cursor) IS \
             BEGIN \
                    open empc for select empno, ename from EMP; \
                    open salc for select ename, sal from EMP;   \
                    open comc for select ename, comm from EMP;  \
             END; \
             END ref_test;"
    );

otl_stream otlCur(1,
            "BEGIN \
            ref_test.getdata(:cur1<refcur,out[50]>, :cur2<refcur,out[50]>, :cur3<refcur,out[50]>); \
            END;",
          db
         );
otlCur.set_commit(0);

otl_refcur_stream s1; // reference cursor streams for reading rows.
otl_refcur_stream s2; // reference cursor streams for reading rows.
otl_refcur_stream s3; // reference cursor streams for reading rows.

otlCur>>s1;
otlCur>>s2;
otlCur>>s3;

int e_no;
char name[11];
double sal;
double comm;

cout<<"=====> Reading :cur1..."<<endl;
 while(!s1.eof()){ // while not end-of-data
  s1>>e_no>>name;
  cout <<"e_no=" <<e_no <<"\tname: " << name <<endl;
 }

cout<<"=====> Reading :cur2..."<<endl;
 while(!s2.eof()){ // while not end-of-data
  s2>>name>>sal;
  cout <<"name=" <<name <<"\tsalary: " << sal <<endl;
 }

cout<<"=====> Reading :cur3..."<<endl;
 while(!s3.eof()){ // while not end-of-data
  s3>>name>>comm;
  cout <<"name=" <<name <<"\tcommission: " << comm <<endl;
 }

s1.close();
s2.close();
s3.close();