Date: Monday, December 5, 2005 @ 17:31:35
Author: zsolt
Path: /cvsroot/carob/libmysequoia
Added: include/Utils.hpp (1.1) src/Utils.cpp (1.1)
Modified: .cdtproject (1.2 -> 1.3) include/CarobMySQL.hpp (1.1 -> 1.2)
src/CarobMySQL.cpp (1.2 -> 1.3) src/MySQLAPI.cpp (1.1 -> 1.2)
- library initialization/destruction
- initial connection handling
------------------------+
.cdtproject | 9 +-
include/CarobMySQL.hpp | 36 +++++++++++
include/Utils.hpp | 32 ++++++++++
src/CarobMySQL.cpp | 142 +++++++++++++++++++++++++++++++++++++++++++++--
src/MySQLAPI.cpp | 53 +++++++++++++++--
src/Utils.cpp | 40 +++++++++++++
6 files changed, 294 insertions(+), 18 deletions(-)
Index: libmysequoia/.cdtproject
diff -u libmysequoia/.cdtproject:1.2 libmysequoia/.cdtproject:1.3
--- libmysequoia/.cdtproject:1.2 Mon Dec 5 14:32:52 2005
+++ libmysequoia/.cdtproject Mon Dec 5 17:31:35 2005
@@ -3,7 +3,7 @@
<cdtproject id="org.eclipse.cdt.make.core.make">
<extension id="org.eclipse.cdt.core.ELF"
point="org.eclipse.cdt.core.BinaryParser"/>
-<extension id="org.eclipse.cdt.core.nullindexer"
point="org.eclipse.cdt.core.CIndexer"/>
+<extension id="org.eclipse.cdt.core.domsourceindexer"
point="org.eclipse.cdt.core.CIndexer"/>
<data>
<item id="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true"
selectedProfileId="org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile"/>
@@ -49,6 +49,7 @@
</profile>
</item>
<item id="org.eclipse.cdt.core.pathentry">
+<pathentry include="include" kind="inc" path="" system="true"/>
<pathentry kind="src" path=""/>
<pathentry kind="out" path=""/>
<pathentry kind="con"
path="org.eclipse.cdt.make.core.DISCOVERED_SCANNER_INFO"/>
@@ -57,21 +58,21 @@
<buildTargets>
<target name="all" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder">
<buildCommand>make</buildCommand>
-<buildArguments></buildArguments>
+<buildArguments/>
<buildTarget>all</buildTarget>
<stopOnError>false</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
</target>
<target name="clean" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder">
<buildCommand>make</buildCommand>
-<buildArguments></buildArguments>
+<buildArguments/>
<buildTarget>clean</buildTarget>
<stopOnError>false</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
</target>
<target name="test" path="" targetID="org.eclipse.cdt.make.MakeTargetBuilder">
<buildCommand>make</buildCommand>
-<buildArguments></buildArguments>
+<buildArguments/>
<buildTarget>test</buildTarget>
<stopOnError>false</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
Index: libmysequoia/include/CarobMySQL.hpp
diff -u libmysequoia/include/CarobMySQL.hpp:1.1
libmysequoia/include/CarobMySQL.hpp:1.2
--- libmysequoia/include/CarobMySQL.hpp:1.1 Fri Dec 2 13:58:39 2005
+++ libmysequoia/include/CarobMySQL.hpp Mon Dec 5 17:31:35 2005
@@ -1,4 +1,4 @@
-/**
+/*
* Sequoia: Database clustering technology.
* Copyright (C) 2005 Emic Networks
* Contact: [EMAIL PROTECTED]
@@ -53,6 +53,9 @@
* the database name with select_db the real connect will happen. This is
needed
* because sequoia does not support a connect without a virtual database
name.
* @param host name of the host to connect
+ * @param user name of the user which is used in connection
+ * @param passwd password of the user used in connect
+ * @param db name of the virtual database
* @param port number of the port, if 0 use the default 25322
* @param unix_socket if this parameter is not null, return an error
* @param clientflag connection specific flags
@@ -191,6 +194,37 @@
Statement *stmtPtr;
//holds the result set for 'live' type of results
MYSQL_RES *liveResultPtr;
+
+ /**
+ * Allocate and fill the MYSQL structure
+ * @param host name of the host to connect
+ * @param user name of the user which is used in connection
+ * @param passwd password of the user used in connect
+ * @param db name of the virtual database
+ * @param port number of the port
+ */
+ void set_connect_info(const char *host, const char *user,
+ const char *passwd, const char *db, unsigned int port);
+
+ /**
+ * Delete and free the actual connection and it's associated resources
+ */
+ void delete_connection();
+
+ /**
+ * Set the current mysql error to the specified one. The mysql error message
will be filled from the error list.
+ * @param errcode mysql error code
+ * @param sqlstate ansi sql specific error message
+ */
+ void set_error(int errcode, const char *sqlstate);
+
+ /**
+ * Set the current mysql error to the specified one
+ * @param errcode mysql error code
+ * @param errmsg detailed mysql error message
+ * @param sqlstate ansi sql specific error message
+ */
+ void set_error(int errcode, const char *errmsg, const char *sqlstate);
};
#endif /* _CAROBMYSQL_HPP */
Index: libmysequoia/include/Utils.hpp
diff -u /dev/null libmysequoia/include/Utils.hpp:1.1
--- /dev/null Mon Dec 5 17:31:35 2005
+++ libmysequoia/include/Utils.hpp Mon Dec 5 17:31:35 2005
@@ -0,0 +1,32 @@
+/*
+ * 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): Zsolt Simon, Csaba Simon
+ * Contributor(s):
+ */
+
+#ifndef _UTILS_HPP
+#define _UTILS_HPP
+
+#include <string.h>
+
+char *cstrdup(const char *src);
+char *cstrdup(const char *src, int len);
+
+#define FREE_AND_NULL(p) if (p) {delete p; p=0;}
+
+#endif /*_UTILS_HPP*/
Index: libmysequoia/src/CarobMySQL.cpp
diff -u libmysequoia/src/CarobMySQL.cpp:1.2 libmysequoia/src/CarobMySQL.cpp:1.3
--- libmysequoia/src/CarobMySQL.cpp:1.2 Fri Dec 2 14:05:13 2005
+++ libmysequoia/src/CarobMySQL.cpp Mon Dec 5 17:31:35 2005
@@ -1,4 +1,4 @@
-/**
+/*
* Sequoia: Database clustering technology.
* Copyright (C) 2005 Emic Networks
* Contact: [EMAIL PROTECTED]
@@ -20,13 +20,33 @@
*/
#include <CarobMySQL.hpp>
+#include <Utils.hpp>
-CarobMYSQL::CarobMYSQL ()
+CarobMYSQL::CarobMYSQL (): connectionPtr(NULL), stmtPtr(NULL),
liveResultPtr(NULL)
{
+ //TODO handle special case when not enough memory
+ mysqlPtr = new CMYSQL();
+ memset(&mysqlPtr->my, 0, sizeof(MYSQL));
+ mysqlPtr->carob = this;
+ //TODO maybe a real server version string ??
+ mysqlPtr->my.server_version = "50015";
+
+ connectionPool = &ConnectionPool::getInstance();
+
+ /*
+ * initialize live results pointer. This will be used to store the result set
+ * when mysql_use_result() function is used
+ */
+
+// liveResultPtr = new MYSQL_RES;
+// memset(liveResultSet, 0, sizeof(MYSQL_RES));
}
CarobMYSQL::~CarobMYSQL ()
{
+ if (mysqlPtr) delete mysqlPtr;
+
+ delete_connection();
}
bool
@@ -34,19 +54,82 @@
const char *passwd, const char *db, unsigned int port,
const char *unix_socket, unsigned long clientflag)
{
- return 0;
+ //TODO handle special case when a live query is ongoing, in that case return
CR_COMMANDS_OUT_OF_SYNC
+
+ if (unix_socket)
+ {
+ //TODO error handling
+ //set_error(CR_NOT_IMPLEMENTED, unknown_sqlstate);
+ return false;
+ }
+
+ /* Must supply at least a user name, or connection to sequoia will fail */
+ if (!user || !*user)
+ {
+ //TODO error handling
+ //set_error(CR_NULL_POINTER, unknown_sqlstate);
+ return false;
+ }
+
+ if (!host || !*host)
+ host = "localhost";
+
+ /* Check for delayed connection. If db name not given, the connection can't
happen, because sequoia doesn't support it.
+ * The real connection will happen after the user call the command
mysql_select_db or mysql_real_connect */
+ if (!db || !*db)
+ {
+ delete_connection();
+ set_connect_info(host, user, passwd, 0, port ? port : 25322);
+
+ return true;
+ }
+
+ try
+ {
+ ConnectionParameters connectionParameters(fromString(host), port ? port :
25322,
+ fromString(db), fromString(user), fromString(passwd), DEBUG_LEVEL_DEBUG);
+
+ Connection *newConnectionPtr =
connectionPool->connectToController(connectionParameters);
+ if (newConnectionPtr)
+ {
+ //TODO handle not enough memory
+ delete_connection();
+ set_connect_info(host, user, passwd, 0, port ? port : 25322);
+
+ connectionPtr = newConnectionPtr;
+ stmtPtr = connectionPtr->createStatement();
+ }
+
+ return true;
+ }
+ catch (CarobException &e)
+ {
+ //set_error(0, toString(e.description).c_str(),
toString(e.getSQLState).c_str());
+ //TODO error handling
+ return 0;
+ }
}
bool
CarobMYSQL::select_db (const char *db)
{
- return 0;
+ /* Connect only if a user, password and host was already specified,
otherwise raise an error */
+ if (mysqlPtr->my.user && mysqlPtr->my.passwd && mysqlPtr->my.host)
+ return connect(mysqlPtr->my.host, mysqlPtr->my.user, mysqlPtr->my.passwd,
db, mysqlPtr->my.port,
+ 0, mysqlPtr->my.client_flag);
+ else
+ {
+ //set_error(CR_NULL_POINTER, unknown_sqlstate);
+ //TODO error handling
+
+ return false;
+ }
}
bool
CarobMYSQL::change_user (const char *user, const char *passwd, const char *db)
{
- return 0;
+ return connect(mysqlPtr->my.host, user, passwd, db, mysqlPtr->my.port, 0,
mysqlPtr->my.client_flag);
}
bool
@@ -123,5 +206,52 @@
MYSQL *
CarobMYSQL::getMYSQL ()
{
- return 0;
+ return (MYSQL *)mysqlPtr;
+}
+
+void
+CarobMYSQL::set_connect_info(const char *host, const char *user,
+ const char *passwd, const char *db, unsigned int port)
+{
+ mysqlPtr->my.host = cstrdup(host);
+ mysqlPtr->my.user = cstrdup(user);
+ mysqlPtr->my.passwd = cstrdup(passwd);
+ mysqlPtr->my.db = cstrdup(db);
+ mysqlPtr->my.port = port;
+
+ //Warning buffer overflow error ...
+ char buf[101];
+ sprintf(buf, "%s via TCP/IP", host);
+ mysqlPtr->my.host_info = cstrdup(buf);
+}
+
+void
+CarobMYSQL::delete_connection()
+{
+ FREE_AND_NULL(mysqlPtr->my.host);
+ FREE_AND_NULL(mysqlPtr->my.user);
+ FREE_AND_NULL(mysqlPtr->my.passwd);
+ FREE_AND_NULL(mysqlPtr->my.db);
+ FREE_AND_NULL(mysqlPtr->my.host_info);
+
+ FREE_AND_NULL(connectionPtr);
+ FREE_AND_NULL(stmtPtr);
+ FREE_AND_NULL(liveResultPtr);
+}
+
+void
+CarobMYSQL::set_error(int errcode, const char *sqlstate)
+{
+// set_error(errcode, ER(errcode), sqlstate);
+ //TODO find the real error message
+ set_error(errcode, "", sqlstate);
+}
+
+void
+CarobMYSQL::set_error(int errcode, const char *errmsg, const char *sqlstate)
+{
+ mysqlPtr->my.net.last_errno = errcode;
+ //strcpy(net->last_error, ER(errcode));
+ strcpy(mysqlPtr->my.net.last_error, errmsg);
+ strcpy(mysqlPtr->my.net.sqlstate, sqlstate);
}
Index: libmysequoia/src/MySQLAPI.cpp
diff -u libmysequoia/src/MySQLAPI.cpp:1.1 libmysequoia/src/MySQLAPI.cpp:1.2
--- libmysequoia/src/MySQLAPI.cpp:1.1 Fri Dec 2 13:54:20 2005
+++ libmysequoia/src/MySQLAPI.cpp Mon Dec 5 17:31:35 2005
@@ -1,4 +1,4 @@
-/**
+/*
* Sequoia: Database clustering technology.
* Copyright (C) 2005 Emic Networks
* Contact: [EMAIL PROTECTED]
@@ -23,23 +23,37 @@
/* Library initialization, shutdown */
+/* Gets or initializes a MYSQL structure. */
MYSQL *STDCALL
mysql_init (MYSQL * mysql)
{
- return 0;
+ if (!mysql)
+ {
+ CarobMYSQL *cmysql = new CarobMYSQL();
+ if (cmysql)
+ mysql = cmysql->getMYSQL();
+ else
+ return 0;
+ }
+ return mysql;
}
+/* Closes a server connection. */
void STDCALL
-mysql_close (MYSQL * sock)
+mysql_close (MYSQL * mysql)
{
+ if (mysql)
+ delete ((CMYSQL *)mysql)->carob;
}
+/* Initialize the MYSQL client library. */
int STDCALL
mysql_server_init (int argc, char **argv, char **groups)
{
return 0;
}
+/* Finalize the MYSQL client library. */
void STDCALL
mysql_server_end (void)
{
@@ -47,34 +61,59 @@
/* Connect functions */
+/* Connects to a MySQL server. This function is deprecated; use
mysql_real_connect() instead. */
MYSQL *STDCALL
mysql_connect (MYSQL * mysql, const char *host, const char *user,
const char *passwd)
{
- return 0;
+ if (mysql)
+ {
+ return ((CMYSQL *)mysql)->carob->connect(host, user, passwd, 0, 0, 0, 0) ?
mysql : 0;
+ }
+ else
+ return 0;
}
+/* Connects to a MySQL server. */
MYSQL *STDCALL
mysql_real_connect (MYSQL * mysql, const char *host, const char *user,
const char *passwd, const char *db, unsigned int port,
const char *unix_socket, unsigned long clientflag)
{
- return 0;
+ if (mysql)
+ {
+ return ((CMYSQL *)mysql)->carob->connect(host, user, passwd, db, port,
unix_socket, clientflag) ? mysql : 0;
+ }
+ else
+ return 0;
}
+/* Selects a database. */
int STDCALL
mysql_select_db (MYSQL * mysql, const char *db)
{
- return 0;
+ if (mysql)
+ {
+ return ((CMYSQL *)mysql)->carob->select_db(db) ? 0 : 1;
+ }
+ else
+ return 0;
}
+/* Changes user and database on an open connection. */
my_bool STDCALL
mysql_change_user (MYSQL * mysql, const char *user, const char *passwd,
const char *db)
{
- return 0;
+ if (mysql)
+ {
+ return !((CMYSQL *)mysql)->carob->change_user(user, passwd, db);
+ }
+ else
+ return 0;
}
+/* Sets connect options for mysql_connect(). */
int STDCALL
mysql_options (MYSQL * mysql, enum mysql_option option, const char *arg)
{
Index: libmysequoia/src/Utils.cpp
diff -u /dev/null libmysequoia/src/Utils.cpp:1.1
--- /dev/null Mon Dec 5 17:31:35 2005
+++ libmysequoia/src/Utils.cpp Mon Dec 5 17:31:35 2005
@@ -0,0 +1,40 @@
+/*
+ * 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): Zsolt Simon, Csaba Simon
+ * Contributor(s):
+ */
+
+#include <Utils.hpp>
+
+char *cstrdup(const char *src)
+{
+ return src ? cstrdup(src, strlen(src)) : 0;
+}
+
+char *cstrdup(const char *src, int len)
+{
+ if (src)
+ {
+ char *result = new char[len+1];
+ memcpy(result, src, len);
+ result[len] = 0;
+ return result;
+ }
+ else
+ return 0;
+}
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits