Date: Tuesday, December 13, 2005 @ 14:57:34
Author: gilles
Path: /cvsroot/carob/carob/contrib/CPP
Added: Makefile (1.1) read_example.cpp (1.1)
(new files)
Added C++ example to show how to do a simple read request (select * from ...)
with demo-raidb1
------------------+
Makefile | 42 ++++++++++++++++++++++
read_example.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 143 insertions(+)
Index: carob/contrib/CPP/Makefile
diff -u /dev/null carob/contrib/CPP/Makefile:1.1
--- /dev/null Tue Dec 13 14:57:34 2005
+++ carob/contrib/CPP/Makefile Tue Dec 13 14:57:34 2005
@@ -0,0 +1,42 @@
+#
+# Sequoia: Database clustering technology.
+# Copyright (C) 2005 Emic Networks
+# Contact: [EMAIL PROTECTED]
+#
+# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Initial developer(s): Gilles Rayrat
+# Contributor(s):
+#
+
+# Makefile for the Carob C++ API example
+
+# Put an absolute path in here to be able to run from any directory
+CAROB_ROOT = ../..
+
+LIB_CAROB = carob
+
+INCDIR = ${CAROB_ROOT}/include
+
+# for GCC
+CXXFLAGS = -g3 -Wall -I${INCDIR}
+LDFLAGS = -Wl,-rpath,${CAROB_ROOT} -L${CAROB_ROOT}
-l${LIB_CAROB} -ldl -rdynamic
+
+EXE = read_example
+OBJS = read_example.o
+
+${EXE}: ${OBJS}
+ ${CXX} ${LDFLAGS} -o $@ $^
+
+clean:
+ ${RM} ${TESTOBJS} ${EXE}
Index: carob/contrib/CPP/read_example.cpp
diff -u /dev/null carob/contrib/CPP/read_example.cpp:1.1
--- /dev/null Tue Dec 13 14:57:34 2005
+++ carob/contrib/CPP/read_example.cpp Tue Dec 13 14:57:34 2005
@@ -0,0 +1,101 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005 Emic Networks
+ * Contact: [EMAIL PROTECTED]
+ *
+ * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Initial developer(s): Gilles Rayrat
+ * Contributor(s):
+ */
+
+/*
+ * Here is a simple example of retrieving data with carob.
+ * A controller must be running on the local host. This example reads data from
+ * the base created with sequoia's demo/demo-raidb1.sh script.
+ * Compilation: use makefile or:
+ * g++ -I../../include -L../.. -lcarob read_example.cpp -o read_example
+ */
+#include <iostream>
+
+#include "Connection.hpp"
+#include "ConnectionPool.hpp"
+#include "DriverResultSet.hpp"
+#include "ResultSetMetaData.hpp"
+#include "Statement.hpp"
+
+using namespace CarobNS;
+using std::wcout;
+using std::endl;
+
+int main(int argc, char *argv[])
+{
+
+ // 1- Connecting:
+ Connection* connectionPtr = NULL; //The connection we will use
+
+ // 1.1 First step: get a reference to the connection pool
+ ConnectionPool& connectionPool = ConnectionPool::getInstance();
+
+ // 1.2 2nd step: fill in the parameters of the connection
+ ConnectionParameters connectionPrms(L"localhost",
+ 25322,
+ L"myDB",
+ L"user",
+ L"",
+ DEBUG_LEVEL_DEBUG);
+ try
+ {
+ // 1.3 3rd step: connect to controller
+ connectionPtr = connectionPool.connectToController(connectionPrms);
+
+ // Now, we are connected to the controller
+
+ // 2- Executing the request
+ // 2.1 The easier way to create a request is to use a statement
+ Statement* statementPtr = connectionPtr->createStatement();
+ // statementPtr->setFetchSize(1);
+ // 2.2 Execute the request and get a pointer to the result
+ DriverResultSet* drsPtr = statementPtr->executeQuery(
+ L"SELECT * FROM address");
+ // 3- Getting the results of the request
+ // 3.1 Let's display 50 rows using metadata and toString() method
+ ResultSetMetaData rsmd(drsPtr);
+ // 3.2 Iterate through the rows
+ for (int i=0; i<50; i++)
+ {
+ // 3.3 Retrieve the next row
+ drsPtr->next();
+ // 3.4 Iterate through the columns
+ for (int j=0; j<rsmd.getColumnCount(); j++)
+ wcout<<drsPtr->getString(j+1)<<L"\t";
+ wcout<<endl;
+ }
+ // 4- Clean-up: Don't forget this step !
+ delete statementPtr;
+ }
+ // 5- Deal with errors
+ catch (CarobException ce)
+ {
+ wcout<<L"Error occured: "<<ce.description()<<endl;
+ }
+ // we could detail:
+ // catch (AuthenticationException ae)
+ // catch (BackendException be)
+ // catch (SocketIOException sioe)
+ // etc.
+ catch (...) //always a good idea
+ {
+ wcout<<L"An unknow error occured"<<endl;
+ }
+}
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits