From 6bc3bf2d8222426fdbfcc98069d32f78ad01c2b4 Mon Sep 17 00:00:00 2001 From: Chip Turner Date: Thu, 18 Jul 2013 15:22:41 -0700 Subject: [PATCH] Change interface to ext_mysql C++ code to allow transporting underlying conns This allows for connections to be created from existing mysql connections as well as extracting the underlying connection from it. --- hphp/runtime/ext/ext_mysql.cpp | 26 +++++++++++++++++--------- hphp/runtime/ext/ext_mysql.h | 8 +++++++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/hphp/runtime/ext/ext_mysql.cpp b/hphp/runtime/ext/ext_mysql.cpp index 08d1b2020..e4d524f2b 100644 --- a/hphp/runtime/ext/ext_mysql.cpp +++ b/hphp/runtime/ext/ext_mysql.cpp @@ -201,23 +201,27 @@ void MySQL::SetDefaultConn(MySQL *conn) { /////////////////////////////////////////////////////////////////////////////// // class MySQL -static MYSQL *create_new_conn() { - MYSQL *ret = mysql_init(NULL); - mysql_options(ret, MYSQL_OPT_LOCAL_INFILE, 0); +static MYSQL *configure_conn(MYSQL* conn) { + mysql_options(conn, MYSQL_OPT_LOCAL_INFILE, 0); if (RuntimeOption::MySQLConnectTimeout) { - MySQLUtil::set_mysql_timeout(ret, MySQLUtil::ConnectTimeout, + MySQLUtil::set_mysql_timeout(conn, MySQLUtil::ConnectTimeout, RuntimeOption::MySQLConnectTimeout); } int readTimeout = s_mysql_data->readTimeout; if (readTimeout) { - MySQLUtil::set_mysql_timeout(ret, MySQLUtil::ReadTimeout, readTimeout); - MySQLUtil::set_mysql_timeout(ret, MySQLUtil::WriteTimeout, readTimeout); + MySQLUtil::set_mysql_timeout(conn, MySQLUtil::ReadTimeout, readTimeout); + MySQLUtil::set_mysql_timeout(conn, MySQLUtil::WriteTimeout, readTimeout); } - return ret; + return conn; +} + +static MYSQL *create_new_conn() { + return configure_conn(mysql_init(nullptr)); } MySQL::MySQL(const char *host, int port, const char *username, - const char *password, const char *database) + const char *password, const char *database, + MYSQL* raw_connection) : m_port(port), m_last_error_set(false), m_last_errno(0), m_xaction_count(0), m_multi_query(false) { if (host) m_host = host; @@ -225,7 +229,11 @@ MySQL::MySQL(const char *host, int port, const char *username, if (password) m_password = password; if (database) m_database = database; - m_conn = create_new_conn(); + if (raw_connection) { + m_conn = configure_conn(raw_connection); + } else { + m_conn = create_new_conn(); + } } MySQL::~MySQL() { diff --git a/hphp/runtime/ext/ext_mysql.h b/hphp/runtime/ext/ext_mysql.h index 531cd64d1..46fa668d4 100644 --- a/hphp/runtime/ext/ext_mysql.h +++ b/hphp/runtime/ext/ext_mysql.h @@ -92,7 +92,8 @@ private: public: MySQL(const char *host, int port, const char *username, - const char *password, const char *database); + const char *password, const char *database, + MYSQL* raw_connection = nullptr); ~MySQL(); void setLastError(const char *func); void close(); @@ -114,6 +115,11 @@ public: int connect_timeout); MYSQL *get() { return m_conn;} + MYSQL *eject_mysql() { + auto ret = m_conn; + m_conn = nullptr; + return ret; + } private: MYSQL *m_conn;