8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-22 18:43:02 +01:00

Update samples - use IUtil and IXpbBuilder

Avoid plain isc_ calls
This commit is contained in:
alexpeshkoff 2015-11-05 17:15:10 +00:00
parent 6d3f0d9bf8
commit f001ede048
4 changed files with 60 additions and 60 deletions

View File

@ -11,6 +11,8 @@
* IProvider - main interface to access DB / service
* IAttachment - database attachment interface
* ITransaction - transaction interface
* IUtil - helper calls here and there
* IXpbBuilder - build various parameters blocks
*
* Run something like this to build: c++ 01.create.cpp -lfbclient
*
@ -51,38 +53,34 @@ int main()
// Declare pointers to required interfaces
// IStatus is used to return wide error description to user
IStatus* st = NULL;
// IProvider is needed to start to work with database (or service)
IProvider* prov = NULL;
// Status vector, main dispatcher and utility interfaces are returned by IMaster functions
// No error return may happen - these functions always succeed
IStatus* st = master->getStatus();
IProvider* prov = master->getDispatcher();
IUtil* utl = master->getUtilInterface();
// IAttachment and ITransaction contain methods to work with database attachment
// and transactions
IAttachment* att = NULL;
ITransaction* tra = NULL;
// IXpbBuilder is used to access various parameters blocks used in API
IXpbBuilder* dpb = NULL;
try
{
// status vector and main dispatcher are returned by calls to IMaster functions
// no error return may happen - these functions always succeed
st = master->getStatus();
prov = master->getDispatcher();
// status wrapper - will be used later in all calls where status interface is needed
// Status wrapper - will be used later in all calls where status interface is needed
// With ThrowStatusWrapper passed as status interface FbException will be thrown on error
ThrowStatusWrapper status(st);
// create DPB (to be replaced with IPBWriter)
unsigned char dpbBuf[32];
unsigned char *dpb = dpbBuf;
*dpb++ = isc_dpb_version1;
*dpb++ = isc_dpb_page_size;
*dpb++ = 2;
*dpb++ = (8 * 1024) & 0xFF;
*dpb++ = (8 * 1024) >> 8;
// create DPB - use non-default page size 4Kb
dpb = utl->getXpbBuilder(&status, IXpbBuilder::DPB, NULL, 0);
dpb->insertInt(&status, isc_dpb_page_size, 4 * 1024);
// create empty database
att = prov->createDatabase(&status, "fbtests.fdb", dpb - dpbBuf, dpbBuf);
att = prov->createDatabase(&status, "fbtests.fdb", dpb->getBufferLength(&status),
dpb->getBuffer(&status));
printf("Database fbtests.fdb created\n");
// detach from database
@ -122,7 +120,10 @@ int main()
{
// handle error
rc = 1;
isc_print_status(error.getStatus()->getErrors());
char buf[256];
utl->formatStatus(buf, sizeof(buf), error.getStatus());
fprintf(stderr, "%s\n", buf);
}
// release interfaces after error caught
@ -130,10 +131,11 @@ int main()
tra->release();
if (att)
att->release();
if (prov)
prov->release();
if (st)
st->dispose();
if (dpb)
dpb->dispose();
st->dispose();
prov->release();
return rc;
}

View File

@ -55,9 +55,11 @@ int main()
setenv("ISC_USER", "sysdba", 0);
setenv("ISC_PASSWORD", "masterkey", 0);
// status vector and main dispatcher
ThrowStatusWrapper status(master->getStatus());
IProvider* prov = master->getDispatcher();
// declare pointers to required interfaces
IStatus* st = NULL;
IProvider* prov = NULL;
IAttachment* att = NULL;
ITransaction* tra = NULL;
@ -75,11 +77,6 @@ int main()
try
{
// status vector and main dispatcher
st = master->getStatus();
ThrowStatusWrapper status(st);
prov = master->getDispatcher();
// attach employee db
att = prov->attachDatabase(&status, "employee", 0, NULL);
@ -134,10 +131,9 @@ int main()
catch (const FbException& error)
{
// Handle exception raised during statement execution
int sqlcode = isc_sqlcode(error.getStatus()->getErrors());
// Don't save the update, if the new budget exceeds the limit.
if (sqlcode == -625)
if (error.getStatus()->getErrors()[1] == isc_not_valid)
{
// Don't save the update, if the new budget exceeds the limit.
printf("\tExceeded budget limit -- not updated.\n");
tra->rollbackRetaining(&status);
@ -171,7 +167,10 @@ int main()
{
// handle error
rc = 1;
isc_print_status(error.getStatus()->getErrors());
char buf[256];
master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
fprintf(stderr, "%s\n", buf);
}
// release interfaces after error caught
@ -185,10 +184,9 @@ int main()
tra->release();
if (att)
att->release();
if (prov)
prov->release();
if (st)
st->dispose();
prov->release();
status.dispose();
return rc;
}
@ -202,11 +200,11 @@ int get_input (char *dept_no, double *percent)
{
if (Dept_data[Input_ptr] == 0)
return 0;
strcpy(dept_no, Dept_data[Input_ptr]);
if ((*percent = Percent_data[Input_ptr++]) == 0)
return 0;
return 1;
}

View File

@ -45,9 +45,11 @@ int main()
setenv("ISC_USER", "sysdba", 0);
setenv("ISC_PASSWORD", "masterkey", 0);
// status vector and main dispatcher
ThrowStatusWrapper status(master->getStatus());
IProvider* prov = master->getDispatcher();
// declare pointers to required interfaces
IStatus* st = NULL;
IProvider* prov = NULL;
IAttachment* att = NULL;
ITransaction* tra = NULL;
IStatement* stmt = NULL;
@ -59,11 +61,6 @@ int main()
try
{
// status vector and main dispatcher
st = master->getStatus();
ThrowStatusWrapper status(st);
prov = master->getDispatcher();
// attach employee db
att = prov->attachDatabase(&status, "employee", 0, NULL);
@ -176,7 +173,10 @@ int main()
{
// handle error
rc = 1;
isc_print_status(error.getStatus()->getErrors());
char buf[256];
master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
fprintf(stderr, "%s\n", buf);
}
// release interfaces after error caught
@ -192,10 +192,9 @@ int main()
tra->release();
if (att)
att->release();
if (prov)
prov->release();
if (st)
st->dispose();
prov->release();
status.dispose();
return rc;
}

View File

@ -53,8 +53,7 @@ int main()
setenv("ISC_USER", "sysdba", 0);
setenv("ISC_PASSWORD", "masterkey", 0);
IStatus* st = master->getStatus();
ThrowStatusWrapper status(st);
ThrowStatusWrapper status(master->getStatus());
IProvider* prov = master->getDispatcher();
IAttachment* att = NULL;
@ -153,7 +152,10 @@ int main()
// handle error
fflush(stdout);
rc = 1;
isc_print_status(error.getStatus()->getErrors());
char buf[256];
master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
fprintf(stderr, "%s\n", buf);
}
if (meta)
@ -164,10 +166,9 @@ int main()
tra->release();
if (att)
att->release();
if (prov)
prov->release();
if (st)
st->dispose();
prov->release();
status.dispose();
return rc;
}