From 1c548a533cf6d3fff80c1a61e87c69e2e8631aff Mon Sep 17 00:00:00 2001 From: Alexander Peshkov Date: Fri, 18 Mar 2016 18:29:25 +0300 Subject: [PATCH] Updated samples, added sample 6 --- examples/interfaces/01.create.cpp | 4 +- examples/interfaces/02.update.cpp | 2 +- examples/interfaces/03.select.cpp | 2 +- examples/interfaces/04.print_table.cpp | 2 +- examples/interfaces/05.user_metadata.cpp | 3 +- examples/interfaces/06.fb_message.cpp | 128 +++++++++++++++++++++++ examples/interfaces/ifaceExamples.h | 2 + 7 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 examples/interfaces/06.fb_message.cpp diff --git a/examples/interfaces/01.create.cpp b/examples/interfaces/01.create.cpp index 7ad0f16c4b..0d46327d67 100644 --- a/examples/interfaces/01.create.cpp +++ b/examples/interfaces/01.create.cpp @@ -95,7 +95,7 @@ int main() tra = att->startTransaction(&status, 0, NULL); // create table - att->execute(&status, tra, 0, "create table dates_table (d1 date)", 3, + att->execute(&status, tra, 0, "create table dates_table (d1 date)", SAMPLES_DIALECT, NULL, NULL, NULL, NULL); // Input parameters and output data not used // commit transaction retaining @@ -103,7 +103,7 @@ int main() 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, + att->execute(&status, tra, 0, "insert into dates_table values (CURRENT_DATE)", SAMPLES_DIALECT, NULL, NULL, NULL, NULL); // Input parameters and output data not used // commit transaction (will close interface) diff --git a/examples/interfaces/02.update.cpp b/examples/interfaces/02.update.cpp index cc1a842a8d..78b6ba5dc5 100644 --- a/examples/interfaces/02.update.cpp +++ b/examples/interfaces/02.update.cpp @@ -84,7 +84,7 @@ int main() tra = att->startTransaction(&status, 0, NULL); // prepare statement - stmt = att->prepare(&status, tra, 0, updstr, 3, 0); + stmt = att->prepare(&status, tra, 0, updstr, SAMPLES_DIALECT, 0); // build metadata // IMaster creates empty new metadata in builder diff --git a/examples/interfaces/03.select.cpp b/examples/interfaces/03.select.cpp index 71039df5b6..a3277e078e 100644 --- a/examples/interfaces/03.select.cpp +++ b/examples/interfaces/03.select.cpp @@ -70,7 +70,7 @@ int main() // prepare statement stmt = att->prepare(&status, tra, 0, "select last_name, first_name, phone_ext from phone_list " "where location = 'Monterey' order by last_name, first_name", - 3, IStatement::PREPARE_PREFETCH_METADATA); + SAMPLES_DIALECT, IStatement::PREPARE_PREFETCH_METADATA); // get list of columns meta = stmt->getOutputMetadata(&status); diff --git a/examples/interfaces/04.print_table.cpp b/examples/interfaces/04.print_table.cpp index 5ef6b8b2d4..b5d7521657 100644 --- a/examples/interfaces/04.print_table.cpp +++ b/examples/interfaces/04.print_table.cpp @@ -78,7 +78,7 @@ int main() "or RDB$VIEW_SOURCE is not null"; // Do not use IStatement - just ask attachment to open cursor - curs = att->openCursor(&status, tra, 0, sql, 3, NULL, NULL, NULL, NULL, 0); + curs = att->openCursor(&status, tra, 0, sql, SAMPLES_DIALECT, NULL, NULL, NULL, NULL, 0); meta = curs->getMetadata(&status); unsigned cols = meta->getCount(&status); diff --git a/examples/interfaces/05.user_metadata.cpp b/examples/interfaces/05.user_metadata.cpp index 335041ef2d..30b76440ee 100644 --- a/examples/interfaces/05.user_metadata.cpp +++ b/examples/interfaces/05.user_metadata.cpp @@ -222,7 +222,8 @@ int main() tra = att->startTransaction(&status, 0, NULL); // open cursor - curs = att->openCursor(&status, tra, 0, "select current_user from rdb$database", 3, NULL, NULL, meta, NULL, 0); + curs = att->openCursor(&status, tra, 0, "select current_user from rdb$database", + SAMPLES_DIALECT, NULL, NULL, meta, NULL, 0); // fetch record from cursor and print it curs->fetchNext(&status, buffer); diff --git a/examples/interfaces/06.fb_message.cpp b/examples/interfaces/06.fb_message.cpp new file mode 100644 index 0000000000..239d794322 --- /dev/null +++ b/examples/interfaces/06.fb_message.cpp @@ -0,0 +1,128 @@ +/* + * PROGRAM: Object oriented API samples. + * MODULE: 06.fb_message.cpp + * DESCRIPTION: A sample of using static messages. + * Prints user-defined tables with comments. + * + * Example for the following macro: + * + * FB_MESSAGE - defines static messages + * C++ specific sample! + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Initial Developer of the Original Code is Adriano dos Santos Fernandes. + * Portions created by the Initial Developer are Copyright (C) 2011 the Initial Developer. + * All Rights Reserved. + * + * Contributor(s): + * Alexander Peshkov + * + */ + +#include "ifaceExamples.h" +#include + +static IMaster* master = fb_get_master_interface(); + +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(); +} diff --git a/examples/interfaces/ifaceExamples.h b/examples/interfaces/ifaceExamples.h index 9bc8eecf94..40b54787a4 100644 --- a/examples/interfaces/ifaceExamples.h +++ b/examples/interfaces/ifaceExamples.h @@ -47,3 +47,5 @@ typedef int FbSampleAtomic; #endif using namespace Firebird; + +#define SAMPLES_DIALECT SQL_DIALECT_V6