Example #1
0
int main()
{
	int rc = 0;

	// set default password if none specified in environment
	setenv("ISC_USER", "sysdba", 0);
	setenv("ISC_PASSWORD", "masterkey", 0);

	// With ThrowStatusWrapper passed as status interface FbException will be thrown on error
	ThrowStatusWrapper status(master->getStatus());

	// Declare pointers to required interfaces
	IProvider* prov = master->getDispatcher();
	IAttachment* att = NULL;
	ITransaction* tra = NULL;
	IBlob* blob = NULL;

	try
	{
		// create database
		att = prov->createDatabase(&status, "blob_07.fdb", 0, NULL);
		tra = att->startTransaction(&status, 0, NULL);

		// create table
		att->execute(&status, tra, 0, "create table blobs_table (b blob sub_type text)", SAMPLES_DIALECT,
			NULL, NULL, NULL, NULL);
		tra->commitRetaining(&status);

		// Message for data exchange
		FB_MESSAGE(Msg, ThrowStatusWrapper,
			(FB_BLOB, b)
		) message(&status, master);
		message.clear();

		// create blob
		blob = att->createBlob(&status, tra, &message->b, 0, NULL);

		// populate blob with data
		for (const char** seg = testData; *seg; ++seg)
			blob->putSegment(&status, strlen(*seg), *seg);
		blob->close(&status);
		blob = NULL;

		// insert blob into the table
		att->execute(&status, tra, 0, "insert into blobs_table(b) values(?)", SAMPLES_DIALECT,
			message.getMetadata(), message.getData(), NULL, NULL);
		tra->commitRetaining(&status);
		printf("Test blob inserted into blobs_table\n...\n");

		// Read blob from table
		message.clear();
		att->execute(&status, tra, 0, "select first(1) b from blobs_table", SAMPLES_DIALECT,
			NULL, NULL, message.getMetadata(), message.getData());
		blob = att->openBlob(&status, tra, &message->b, 0, NULL);

		// Read segments from blob
		// Use very small segment buffer to show read of incomplete segment
		printf("Read inserted blob from blobs_table\n...\n");
		int bufOver = 0;
		for(bool eof = false; !eof; )
		{
			const char* lineFeed = "\n";
			char buf[32];
			unsigned l = 0;
			switch (blob->getSegment(&status, sizeof(buf) - 1, buf, &l))
			{
				case IStatus::RESULT_OK:
					break;
				case IStatus::RESULT_SEGMENT:
					lineFeed = "";
					bufOver++;
					break;
				default:
					eof = true;
					continue;
			}
			buf[l] = 0;
			printf("%s%s", buf, lineFeed);
		}
		printf("\nSegment not fit in buffer counter = %d\n\n", bufOver);

		// cleanup
		blob->close(&status);
		blob = NULL;
		tra->commit(&status);
		tra = NULL;

		// uncomment next line to play with errors during drop database
		// printf("Attach with any client to blob_07.fdb to prevent it being dropped and press enter"); getchar();

		// drop database
		drop(&att);
	}
	catch (const FbException& error)
	{
		// handle error
		rc = 1;
		errPrint(error.getStatus());

		if (att)
			drop(&att);
	}

	// release interfaces after error caught
	if (blob)
		blob->release();
	if (tra)
		tra->release();
	if (att)
		att->release();

	status.dispose();
	prov->release();

	return rc;
}
Example #2
0
int main()
{
	int rc = 0;

	setenv("ISC_USER", "sysdba", 0);
	setenv("ISC_PASSWORD", "masterkey", 0);

	ThrowStatusWrapper status(master->getStatus());
	IProvider* prov = master->getDispatcher();

	IAttachment* att = NULL;
	ITransaction* tra = NULL;
	IResultSet* rs = NULL;

	const char* dbName = "employee";

	try
	{
		att = prov->attachDatabase(&status, dbName, 0, NULL);
		tra = att->startTransaction(&status, 0, NULL);

		// Comment some tables
		att->execute(&status, tra, 0, "comment on table employee is 'Employees'",
			SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
		att->execute(&status, tra, 0, "comment on table customer is 'Customers'",
			SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
		att->execute(&status, tra, 0, "comment on table country is 'Countries and national currencies'",
			SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
		tra->commitRetaining(&status);

		// Print tables list
		FB_MESSAGE(Input, ThrowStatusWrapper,
			(FB_INTEGER, systemFlag)
		) input(&status, master);

		FB_MESSAGE(Output, ThrowStatusWrapper,
			(FB_SMALLINT, relationId)
			(FB_VARCHAR(31), relationName)
			(FB_VARCHAR(100), description)
		) output(&status, master);

		input.clear();
		input->systemFlag = 0;

		rs = att->openCursor(&status, tra, 0,
			"select rdb$relation_id, rdb$relation_name, rdb$description"
			"  from rdb$relations"
			"  where rdb$system_flag = ?"
			"  order by rdb$relation_id",
			SAMPLES_DIALECT, input.getMetadata(), input.getData(), output.getMetadata(), NULL, 0);

		printf("  ID Name/comment\n");
		while (rs->fetchNext(&status, output.getData()) == IStatus::RESULT_OK)
		{
			unsigned lRelName = output->relationNameNull ? 0 : output->relationName.length;
			unsigned lDesc = output->descriptionNull ? 0 : output->description.length;
			printf("%4d %*.*s%c%*.*s\n", output->relationId,
				lRelName, lRelName, output->relationName.str,
				lDesc ? '/' : ' ',
				lDesc, lDesc, output->description.str);
		}

		rs->close(&status);
		rs = NULL;

		tra->commit(&status);
		tra = NULL;

		att->detach(&status);
		att = NULL;
	}
	catch (const FbException& error)
	{
		// handle error
		rc = 1;

		char buf[256];
		master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
		fprintf(stderr, "%s\n", buf);
	}

	// release interfaces after error caught
	if (rs)
		rs->release();
	if (tra)
		tra->release();
	if (att)
		att->release();

	// generic cleanup
	prov->release();
	status.dispose();
}
Example #3
0
int main()
{
	int rc = 0;

	// set default password if none specified in environment
	setenv("ISC_USER", "sysdba", 0);
	setenv("ISC_PASSWORD", "masterkey", 0);

	// 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;

	// IAttachment and ITransaction contain methods to work with database attachment
	// and transactions
	IAttachment* att = NULL;
	ITransaction* tra = 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
		// 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 empty database
		att = prov->createDatabase(&status, "fbtests.fdb", dpb - dpbBuf, dpbBuf);
		printf("Database fbtests.fdb created\n");

		// detach from database
		att->detach(&status);
		att = NULL;

		// attach it once again
		att = prov->attachDatabase(&status, "fbtests.fdb", 0, NULL);
		printf("Re-attached database fbtests.fdb\n");

		// start transaction
		tra = att->startTransaction(&status, 0, NULL);

		// create table
		att->execute(&status, tra, 0, "create table dates_table (d1 date)", 3,
			NULL, NULL, NULL, NULL);	// Input parameters and output data not used

		// commit transaction retaining
		tra->commitRetaining(&status);
		printf("Table dates_table created\n");

		// insert a record into dates_table
		att->execute(&status, tra, 0, "insert into dates_table values (CURRENT_DATE)", 3,
			NULL, NULL, NULL, NULL);	// Input parameters and output data not used

		// commit transaction (will close interface)
		tra->commit(&status);
		tra = NULL;

		printf("Record inserted into dates_table\n");

		// detach from database (will close interface)
		att->detach(&status);
		att = NULL;
	}
	catch (const FbException& error)
	{
		// handle error
		rc = 1;
		isc_print_status(error.getStatus()->getErrors());
	}

	// release interfaces after error caught
	if (tra)
		tra->release();
	if (att)
		att->release();
	if (prov)
		prov->release();
	if (st)
		st->dispose();

	return rc;
}