Date: Thursday, December 22, 2005 @ 19:55:20
  Author: marc
    Path: /cvsroot/carob/odbsequoia/src

   Added: GNUmakefile (1.1) connect.cpp (1.1) connect.hpp (1.1)
          dummymain.cpp (1.1) env.cpp (1.1) env.hpp (1.1) odbc.ini (1.1)
          setup.hpp (1.1) simpletest.c (1.1) stmt.cpp (1.1)

First prototype.


---------------+
 GNUmakefile   |   52 +++++++++++++++++++++
 connect.cpp   |  121 ++++++++++++++++++++++++++++++++++++++++++++++++++
 connect.hpp   |   51 +++++++++++++++++++++
 dummymain.cpp |    5 ++
 env.cpp       |  135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 env.hpp       |   49 ++++++++++++++++++++
 odbc.ini      |   25 ++++++++++
 setup.hpp     |   42 +++++++++++++++++
 simpletest.c  |   47 +++++++++++++++++++
 stmt.cpp      |   80 +++++++++++++++++++++++++++++++++
 10 files changed, 607 insertions(+)


Index: odbsequoia/src/GNUmakefile
diff -u /dev/null odbsequoia/src/GNUmakefile:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/GNUmakefile  Thu Dec 22 19:55:20 2005
@@ -0,0 +1,52 @@
+# Sequoia: Database clustering technology 
+# Copyright 2005-2006 Continuent, Inc. 
+
+# 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.
+
+# Author: Marc Herbert
+
+
+CAROB_PATH=../../carob
+
+# need to prefix by "lib" in case we want to shortcut the DM
+ROOTNAME=odbsequoia
+
+LIBSHORT=lib${ROOTNAME}.so
+LIB=${LIBSHORT}.1
+
+LDFLAGS=-lodbc
+
+CAROB=carob
+
+
+OBJS=env.o connect.o stmt.o
+CXXFLAGS=-Wall -g3 -I${CAROB_PATH}/include
+
+
+a.out: simpletest.c ${LIBSHORT}
+       c++ -Wall -g3 simpletest.c ${LDFLAGS} -Wl,-rpath -Wl,. -o $@
+
+# libodbsequoia targets
+${LIBSHORT}: ${LIB}
+       - ln -s $^ $@
+
+# libcarob.a needs -lpthread
+${LIB}:        ${OBJS} ${CAROB_PATH}/lib${CAROB}.a
+       c++ ${CXXFLAGS} -shared $^ -lpthread -lodbcinst -o $@
+
+clean:
+       rm -f ${LIB} ${LIBSHORT} a.out ${OBJS}
+
+# useful to detect linking issues *before* the driver manager gets them
+linktest: ${LIBSHORT}
+       g++ dummymain.cpp ${LIBSHORT} -lodbc -lodbcinst $@
Index: odbsequoia/src/connect.cpp
diff -u /dev/null odbsequoia/src/connect.cpp:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/connect.cpp  Thu Dec 22 19:55:20 2005
@@ -0,0 +1,121 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005-2006 Continuent, Inc.
+ * 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): Marc Herbert
+ * Contributor(s): 
+ */
+
+
+#include <cstdlib> // for atoi()
+
+#include <sql.h>
+#include <sqlucode.h>
+
+#include <odbcinst.h>
+
+#include "Connection.hpp"
+
+#include "setup.hpp"
+
+#include "connect.hpp"
+
+
+#define MAX_VALUE_LEN 100
+
+
+using namespace ODBSeqNS;
+using namespace CarobNS;
+
+
+SQLRETURN
+SQLConnectW(SQLHDBC conn_hdle,
+            SQLWCHAR *wdsn, SQLSMALLINT dsnlen,
+            SQLWCHAR *user, SQLSMALLINT userlen,
+            SQLWCHAR *pass, SQLSMALLINT passlen)
+{
+    SQLRETURN  ret = SQL_SUCCESS;
+    ODBCConnection * conn = (ODBCConnection *) conn_hdle;
+
+    char temp[MAX_VALUE_LEN];
+
+    // Get a narrow, C-style DSN
+    // Waiting for SQLGetPrivateProfileStringW() ?
+    std::string dsn = toString(fromSQLW(wdsn, dsnlen));
+
+#if 0 // looks like LPCSTR is const after all. Are we sure about this?
+    dsnlen = dsn.length(); // fixing NTS
+    char *cdsn = new char[dsnlen+1];
+    dsn.copy(cdsn, dsnlen);
+    cdsn[dsnlen] = 0;
+#else
+    LPCSTR cdsn = dsn.c_str();
+#endif
+
+    // Slurp DSN information from odbc.ini into ConnectionParameters
+    if (!SQLGetPrivateProfileString(cdsn, SERVER_INI, "localhost",
+       temp, MAX_VALUE_LEN, ODBC_INI))
+        return SQL_ERROR; // TODO: diags
+    std::wstring serverhost = fromString(std::string(temp));
+
+    if (!SQLGetPrivateProfileString(cdsn, PORT_INI, "25322",
+       temp, MAX_VALUE_LEN, ODBC_INI))
+        return SQL_ERROR; // TODO: diags
+    inetport_t port = atoi(temp);
+
+    if (!SQLGetPrivateProfileString(cdsn, DATABASE_INI, "",
+       temp, MAX_VALUE_LEN, ODBC_INI))
+        return SQL_ERROR; // TODO: diags
+    std::wstring vdbname = fromString(std::string(temp));
+
+#if 0    
+    delete[] cdsn;
+#endif
+
+    try
+    {
+        CarobNS::ConnectionPool *connectionPoolPtr = 
&CarobNS::ConnectionPool::getInstance();
+        CarobNS::ConnectionParameters
+            connectionPrms(serverhost, port, vdbname,
+                           fromSQLW(user, userlen), fromSQLW(pass, passlen),
+                           DEBUG_LEVEL_DEBUG);
+        conn->carob_conn = 
connectionPoolPtr->connectToController(connectionPrms);
+    }
+    catch (CarobNS::CarobException& ce)
+    {
+        ret = SQL_ERROR;
+    }
+    return ret;
+}
+
+
+SQLRETURN ODBSeqNS::alloc_connect(SQLHANDLE input_handle, SQLHANDLE * 
output_handle)
+{
+    ODBCConnection *newconn = new ODBCConnection;
+    newconn->env = (ODBCEnv *) input_handle;
+    *output_handle = newconn;
+    return SQL_SUCCESS;
+}
+
+
+
+/*
+ * Local Variables:
+ * c-file-style: "bsd"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Index: odbsequoia/src/connect.hpp
diff -u /dev/null odbsequoia/src/connect.hpp:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/connect.hpp  Thu Dec 22 19:55:20 2005
@@ -0,0 +1,51 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005-2006 Continuent, Inc.
+ * 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): Marc Herbert
+ * Contributor(s): 
+ */
+
+#include "ConnectionPool.hpp"
+#include "Connection.hpp"
+
+#include "env.hpp"
+
+namespace ODBSeqNS {
+
+typedef struct conn_ {
+    ODBCEnv *env;
+    CarobNS::Connection* carob_conn;
+    // TODO: rely on ConnectionParameters instead
+    std::wstring serverhost;
+    std::wstring vdbname;
+} ODBCConnection;
+
+
+
+SQLRETURN alloc_connect(SQLHANDLE input_handle, SQLHANDLE * output_handle);
+SQLRETURN alloc_statement(SQLHANDLE input_handle, SQLHANDLE * output_handle);
+
+
+} // namespace ODBSeqNS 
+
+/*
+ * Local Variables:
+ * c-file-style: "bsd"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Index: odbsequoia/src/dummymain.cpp
diff -u /dev/null odbsequoia/src/dummymain.cpp:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/dummymain.cpp        Thu Dec 22 19:55:20 2005
@@ -0,0 +1,5 @@
+
+int main()
+{
+   return 1;  
+}
Index: odbsequoia/src/env.cpp
diff -u /dev/null odbsequoia/src/env.cpp:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/env.cpp      Thu Dec 22 19:55:20 2005
@@ -0,0 +1,135 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005-2006 Continuent, Inc.
+ * 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): Marc Herbert
+ * Contributor(s): 
+ */
+
+
+#include <iostream>
+
+#include <sql.h>
+// unicode: 'W' (wchar) and 'A' protos
+#include <sqlucode.h>
+
+#include "connect.hpp"
+
+
+using namespace ODBSeqNS;
+using namespace CarobNS;
+
+// should probably move this elsewhere
+namespace {
+int sqlwcstrlen(const SQLWCHAR *instr)
+{
+    int        len = 0;
+    while (instr[len] != 0)
+        len++;
+    return len;
+}
+}
+
+std::wstring
+fromSQLW(const SQLWCHAR * const in, SQLSMALLINT inLen)
+{
+    bool isNTS = (SQL_NTS == inLen);
+
+// this macro belongs to unixodbc. TODO: check about iodbc, windows etc.
+#ifdef SQL_WCHART_CONVERT
+#warning SQLWCHAR == wchar_t: optimized but untested code!
+
+    std::wstring objres;
+    if (isNTS)
+        objres = std::wstring(in);
+    else
+        objres = std::wstring(in, inLen);
+
+#else  // sizeof(SQLWCHAR) != sizeof(wchar_t)
+
+    if (isNTS)
+        inLen = sqlwcstrlen(in);
+    wchar_t *res = new wchar_t[inLen];
+    for (int i=0; i<inLen ; i++)
+        res[i] = in[i]; // FIXME: this is poor man's UCS-4/UTF-16 converter 
indeed!
+
+    std::wstring objres(res, inLen);
+    delete[] res;
+
+#endif
+
+    return objres;
+}
+
+/*  SQLAllocConnect/SQLAllocEnv/SQLAllocStmt mapped to SQLAllocHandle() here 
by DM */
+SQLRETURN              
+SQLAllocHandle(SQLSMALLINT handle_type, SQLHANDLE input_handle,
+               SQLHANDLE * output_handle)
+{
+  SQLRETURN            ret = SQL_SUCCESS;
+
+  switch (handle_type)
+    {
+    case SQL_HANDLE_ENV:
+      *output_handle = new ODBCEnv;
+      break;
+    case SQL_HANDLE_DBC:
+      ret = alloc_connect(input_handle, output_handle);
+      break;
+    case SQL_HANDLE_STMT:
+      ret = alloc_statement(input_handle, output_handle);
+      break;
+    case SQL_HANDLE_DESC:
+      ret = SQL_ERROR;
+      break;
+    default:
+      ret = SQL_ERROR;
+      break;
+    }
+
+  return ret;
+}
+
+
+/*  SQLFreeConnect/SQLFreeEnv/SQLFreeStmt mapped to SQLFreeHandle() here by DM 
*/
+SQLRETURN
+SQLFreeHandle(SQLSMALLINT handle_type, SQLHANDLE handle)
+{
+    SQLRETURN          ret = SQL_SUCCESS;
+    // FIXME: leaking
+    return ret;
+}
+
+
+/* is there a SQLGetEnvAttrW()? */
+SQLRETURN
+SQLSetEnvAttr(SQLHENV env_handle, SQLINTEGER attribute, SQLPOINTER value,
+              SQLINTEGER str_len)
+{
+       SQLRETURN       ret = SQL_SUCCESS;
+        // FIXME: ignored
+
+       return ret;
+}
+
+
+/*
+ * Local Variables:
+ * c-file-style: "bsd"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Index: odbsequoia/src/env.hpp
diff -u /dev/null odbsequoia/src/env.hpp:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/env.hpp      Thu Dec 22 19:55:20 2005
@@ -0,0 +1,49 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005-2006 Continuent, Inc.
+ * 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): Marc Herbert
+ * Contributor(s): 
+ */
+
+
+// #ifndef ODBSEQ_ENV
+// #define ODBSEQ_ENV
+
+#include <string>
+
+#include <sql.h>
+
+namespace ODBSeqNS {
+
+typedef struct env_ {
+    // empty for now
+} ODBCEnv;
+
+}
+
+std::wstring fromSQLW(const SQLWCHAR *in, SQLSMALLINT inLen);
+
+// #endif // include only once
+
+/*
+ * Local Variables:
+ * c-file-style: "bsd"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
+
Index: odbsequoia/src/odbc.ini
diff -u /dev/null odbsequoia/src/odbc.ini:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/odbc.ini     Thu Dec 22 19:55:20 2005
@@ -0,0 +1,25 @@
+[directPG]
+Description            = PostgreSQL db
+Driver         = PostgreSQL
+Database               = db
+Servername             = localhost
+UserName               = postgres
+Password               = 
+Port           = 5432
+Protocol               = 6.4
+ReadOnly               = Yes
+RowVersioning          = No
+ShowSystemTables               = No
+ShowOidColumn          = No
+FakeOidIndex           = No
+ConnSettings           = 
+
+[seqsource]
+Description = Test Sequoia
+Driver = Sequoia
+Database               = myDB
+Servername             = localhost
+UserName               = user
+Password               = 
+Port           = 25322
+
Index: odbsequoia/src/setup.hpp
diff -u /dev/null odbsequoia/src/setup.hpp:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/setup.hpp    Thu Dec 22 19:55:20 2005
@@ -0,0 +1,42 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005-2006 Continuent, Inc.
+ * 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): Marc Herbert
+ * Contributor(s): 
+ */
+
+/* ODBC initialization files */
+#ifndef WIN32
+#define ODBC_INI "odbc.ini"
+#define ODBCINST_INI "odbcinst.ini"
+#else
+#define ODBC_INI "ODBC.INI"
+#define ODBCINST_INI "ODBCINST.INI"
+#endif
+
+
+#define SERVER_INI "Servername"
+#define DATABASE_INI "Database"
+#define PORT_INI "Port"
+
+/*
+ * Local Variables:
+ * c-file-style: "bsd"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Index: odbsequoia/src/simpletest.c
diff -u /dev/null odbsequoia/src/simpletest.c:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/simpletest.c Thu Dec 22 19:55:20 2005
@@ -0,0 +1,47 @@
+
+#include <sql.h>
+#include <sqlucode.h>
+
+// !log_info.log_flag && !ODBCSharedTraceFlag
+
+
+#define SEQTEST 1
+
+int main()
+{
+  SQLHENV henv;
+  SQLHDBC hdbc;
+  SQLHSTMT hstmt;
+
+
+  if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv) != SQL_SUCCESS)
+    return 1;
+
+
+  if (SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0) 
!= SQL_SUCCESS)
+    return 2;
+
+
+  if (SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc) != SQL_SUCCESS)
+    return 3;
+
+#if SEQTEST // sequoia
+  if (SQLConnect(hdbc, (SQLCHAR *) "seqsource", SQL_NTS,
+                (SQLCHAR *) "user", SQL_NTS,
+                (SQLCHAR *) "", SQL_NTS) != SQL_SUCCESS)
+#else // postgres
+  if (SQLConnect(hdbc, (SQLCHAR *) "directPG", SQL_NTS,
+                (SQLCHAR *) "postgres", SQL_NTS,
+                (SQLCHAR *) "", SQL_NTS) != SQL_SUCCESS)
+#endif
+    return 4;
+
+  if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) != SQL_SUCCESS)
+    return 5;
+  
+  if (SQLExecDirect(hstmt, (SQLCHAR *) "UPDATE PRODUCT set cost=3.1422 where 
ID=1",
+                   SQL_NTS) != SQL_SUCCESS)
+    return 6;
+
+  return 0;
+}
Index: odbsequoia/src/stmt.cpp
diff -u /dev/null odbsequoia/src/stmt.cpp:1.1
--- /dev/null   Thu Dec 22 19:55:20 2005
+++ odbsequoia/src/stmt.cpp     Thu Dec 22 19:55:20 2005
@@ -0,0 +1,80 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005-2006 Continuent, Inc.
+ * 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): Marc Herbert
+ * Contributor(s): 
+ */
+
+#include <sql.h>
+// wchar and 'A' stuff
+#include <sqlucode.h>
+
+#include "Statement.hpp"
+
+#include "connect.hpp"
+
+
+namespace ODBSeqNS {
+
+typedef struct odbsequoia_stmt_ {
+    // a ref here instead?
+    ODBCConnection *conn;
+    CarobNS::Statement* carob_stmt;
+} ODBCStatement;
+
+}
+
+
+using namespace ODBSeqNS;
+using namespace CarobNS;
+
+SQLRETURN      
+SQLExecDirectW(SQLHSTMT StatementHandle,
+               SQLWCHAR *StatementText, SQLINTEGER TextLength)
+{
+    SQLRETURN  ret = SQL_SUCCESS;
+    ODBCStatement * stmt = (ODBCStatement *)StatementHandle;
+
+
+    try
+    {    
+        std::wstring wstext = fromSQLW(StatementText, TextLength);
+        stmt->carob_stmt->executeUpdate(wstext); 
+    }
+    catch (const CarobException& ce)
+    {
+        ret = SQL_ERROR;
+    }
+    return ret;
+}
+
+SQLRETURN ODBSeqNS::alloc_statement(SQLHANDLE InputHandle, SQLHANDLE * 
OutputHandle)
+{
+    ODBCStatement *newstmt = new ODBCStatement;
+    newstmt->conn = (ODBCConnection *) InputHandle;
+    newstmt->carob_stmt = newstmt->conn->carob_conn->createStatement();
+    *OutputHandle = newstmt;
+    return SQL_SUCCESS;
+}
+
+/*
+ * Local Variables:
+ * c-file-style: "bsd"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to