#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <iostream>
#ifdef WIN32
#include <windows.h>
#include <tchar.h>
#endif
#include <SQLAPI.h>
#include <samisc.h>
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <assert.h>
#include <chrono>
#include <vector>
using namespace std;
static FILE *pFile = NULL;
size_t nTotalBound;
size_t FromFileWriter( SAPieceType_t &ePieceType,
void *pBuf, size_t nLen, void *pAddlData)
{
if(ePieceType == SA_FirstPiece)
{
const char *sFilename = (const char *)pAddlData;
pFile = fopen(sFilename, "rb");
if(!pFile)
SAException::throwUserException(-1,
_TSA("Can not open file '%s'"),
(const SAChar*)SAString(sFilename));
nTotalBound = 0;
}
size_t nRead = fread(pBuf, 1, nLen, pFile);
nTotalBound += nRead;
// show progress
printf("%d bytes of file bound\n", nTotalBound);
if(feof(pFile))
{
if( ePieceType == SA_FirstPiece )
ePieceType = SA_OnePiece;
else
ePieceType = SA_LastPiece;
fclose(pFile);
pFile = NULL;
}
return nRead;
}
SAString ReadWholeFile(const char *sFilename)
{
SAString s;
char sBuf[1024];
FILE *pFile = fopen(sFilename, "rb");
if(!pFile)
SAException::throwUserException(-1,
_TSA("Error opening file '%s'\n"),
(const SAChar*)SAString(sFilename));
do
{
size_t nRead = fread(sBuf, 1, sizeof(sBuf), pFile);
s += SAString((const void*)sBuf, nRead);
}
while(!feof(pFile));
fclose(pFile);
return s;
}
SAString ReadWholeTextFile(const SAChar *szFilename)
{
SAString s;
char szBuf[32*1024];
FILE *pFile = _tfopen(szFilename, _TSA("rb"));
if( ! pFile )
SAException::throwUserException(-1,
_TSA("Error opening file '%s'\n"),
(const SAChar*)SAString(szFilename));
do
{
size_t nRead = fread(szBuf, 1, sizeof(szBuf), pFile);
s += SAString(szBuf, nRead);
}
while(!feof(pFile));
fclose(pFile);
return s;
}
void WriteWholeFile(const char *sFilename, const SAString& data)
{
FILE *pFile = fopen(sFilename, "wb");
size_t n, written = 0, len = data.GetBinaryLength();
const void* pData = (const void*)data;
sa_printf(_TSA("PRGLEN: %d\n"), len);
if(!pFile)
SAException::throwUserException(-1,
_TSA("Error opening file '%s'\n"),
(const SAChar*)SAString(sFilename));
while( len > written ) {
n = fwrite((const char*)pData+written, 1, sa_min(1024,len-written), pFile);
if( n <= 0 )
break;
written += n;
}
fclose(pFile);
}
size_t nTotalRead;
void IntoFileReader(
SAPieceType_t ePieceType,
void *pBuf,
size_t nLen,
size_t nBlobSize,
void *pAddlData)
{
const char *sFilename = (const char *)pAddlData;
if(ePieceType == SA_FirstPiece || ePieceType == SA_OnePiece)
{
nTotalRead = 0;
pFile = fopen(sFilename, "wb");
if(!pFile)
SAException::throwUserException(-1,
_TSA("Can not open file '%s' for writing"),
(const SAChar*)SAString(sFilename));
}
fwrite(pBuf, 1, nLen, pFile);
nTotalRead += nLen;
if(ePieceType == SA_LastPiece || ePieceType == SA_OnePiece)
{
fclose(pFile);
pFile = NULL;
printf("%s : %d bytes of %d read\n",
sFilename, nTotalRead, nBlobSize);
}
}
void Ora_Fetch_Cursor(SACommand& cmd, int stage)
{
for(int s = 1; s < stage; ++s) printf(" ");
printf("START CURSOR #%d\n", stage);
try
{
if( cmd.isResultSet() )
{
//cmd.setOption(SACMD_PREFETCH_ROWS) = _TSA("5");
while( cmd.FetchNext() )
{
for( int i = 1; i <= cmd.FieldCount(); ++i )
{
SAField& f = cmd.Field(i);
if( SA_dtCursor == f.FieldType() )
{
SACommand* cur = f.asCursor();
if( NULL == cur)
{
for(int s = 1; s < stage; ++s) printf(" ");
printf("NULL result set\n");
}
else
Ora_Fetch_Cursor(*cur, stage + 1);
}
else
{
for(int s = 1; s < stage; ++s) printf(" ");
printf("VAL #%d: %s\n",
i, f.asString().GetMultiByteChars());
}
}
}
}
else
{
for(int s = 1; s < stage; ++s) printf(" ");
printf("Empty result set\n");
}
}
catch(SAException &x)
{
for(int s = 1; s < stage; ++s) printf(" ");
printf("ERROR: %s\n", x.ErrText().GetMultiByteChars());
}
for(int s = 1; s < stage; ++s) printf(" ");
printf("END CURSOR #%d\n", stage);
}
/*
create function func1
return sys_refcursor
is
v_cur sys_refcursor;
begin
open v_cur for
select
1 a
, cursor(select level l from dual connect by level < 5) c
from dual
connect by level < 5;
return v_cur;
end;
create procedure proc1 (p_cur out sys_refcursor)
is
begin
open p_cur for
select level l from dual connect by level < 50;
end;
/
*/
int Oracle_RefCur()
{
SAConnection con;
//con.setOption(_TSA("UseAPI")) = _TSA("OCI7");
try {
con.Connect(_TSA("ora102"),
_TSA("scott"), _TSA("tiger"), SA_Oracle_Client);
SACommand cmd(&con, _TSA("select func1() from t1"));
//SACommand cmd(&con, _TSA("proc1"));
while( true )
{
//cmd.setOption(SACMD_PREFETCH_ROWS) = _TSA("5");
cmd.Execute();
Ora_Fetch_Cursor(cmd, 1);
/*
SACommand* pCur = cmd.Param(_TSA("p_routes")).asCursor();
if( NULL != pCur )
Ora_Fetch_Cursor(*pCur, 1);
*/
}
}
catch(SAException &x)
{
printf("ERROR: %s\n", x.ErrText().GetMultiByteChars());
}
return 0;
}
/*
create table t1(f1 integer not null, f2 clob);
insert into t1(f1,f2) values(1,NULL);
create or replace procedure test_sp(po_file OUT CLOB) IS
BEGIN
select f2 into po_file from t1 where f1=1;
END;
*/
int FB2_LastInsert()
{
/*
CREATE TABLE T1
(
F1 INTEGER NOT NULL,
F2 VARCHAR(20) NOT NULL
);
CREATE GENERATOR GEN_T1_F1;
SET GENERATOR GEN_T1_F1 TO 0;
SET TERM !! ;
CREATE TRIGGER T1_BI FOR T1
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.F1 IS NULL) THEN
NEW.F1 = GEN_ID(GEN_T1_F1, 1);
END!!
SET TERM ; !!
*/
SAConnection con;
SACommand cmd(&con,
_TSA("INSERT INTO t1 (f2) values ('val x') returning f1"));
try
{
con.Connect(_TSA("test"), _TSA("sysdba"), _TSA("masterkey"),
SA_InterBase_Client);
cmd.Execute();
if( cmd.FetchNext() )
printf("F1 value: %d\n", cmd.Field(1).asLong());
}
catch(SAException &x)
{
// SAConnection::Rollback()
// can also throw an exception
// (if a network error for example),
// we will be ready
try
{
// on error rollback changes
con.Rollback();
}
catch(SAException &)
{
}
// print error message
printf("%s\n", x.ErrText().GetMultiByteChars());
}
return 0;
}
void test_scroll(SAConnection &con)
{
SACommand cmd(&con);
//cmd.setOption(SACMD_PREFETCH_ROWS) = _TSA("5"); // wronf value!!!???
cmd.setOption(SACMD_PREFETCH_ROWS) = _TSA("3");
cmd.setOption(SACMD_SCROLLABLE) = _TSA("1");
cmd.setCommandText(_TSA("select f1 from t1"));
cmd.Execute();
int ch;
printf("ENTER n,p,f,l or q to quit.\n");
do {
ch = _getch();
bool bResult = false;
try
{
switch(ch)
{
case 'n':
bResult = cmd.FetchNext();
break;
评论6
最新资源