From db00b5002de2cca34f8cb4862d5b1dbd983caed6 Mon Sep 17 00:00:00 2001 From: Nimetu Date: Wed, 29 Jan 2014 14:06:48 +0200 Subject: [PATCH 1/2] Add mysqli database class to shard admin tool --HG-- branch : shard-admin-tool-mysqli-class --- code/ryzom/tools/server/admin/common.php | 4 +- .../tools/server/admin/functions_mysqli.php | 295 ++++++++++++++++++ 2 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 code/ryzom/tools/server/admin/functions_mysqli.php diff --git a/code/ryzom/tools/server/admin/common.php b/code/ryzom/tools/server/admin/common.php index 644ebba32..caf90a812 100644 --- a/code/ryzom/tools/server/admin/common.php +++ b/code/ryzom/tools/server/admin/common.php @@ -16,7 +16,7 @@ //assert_options(ASSERT_QUIET_EVAL, 1); //assert_options(ASSERT_CALLBACK, 'nt_common_assert'); - require_once(NELTOOL_SYSTEMBASE .'functions_mysql.php'); + require_once(NELTOOL_SYSTEMBASE .'functions_mysqli.php'); require_once(NELTOOL_SYSTEMBASE .'smarty/Smarty.class.php'); require_once(NELTOOL_SYSTEMBASE. 'functions_tool_administration.php'); require_once(NELTOOL_SYSTEMBASE. 'nel/admin_modules_itf.php'); @@ -166,4 +166,4 @@ nt_common_add_debug('-- Common init. complete.'); } -?> \ No newline at end of file +?> diff --git a/code/ryzom/tools/server/admin/functions_mysqli.php b/code/ryzom/tools/server/admin/functions_mysqli.php new file mode 100644 index 000000000..f4df4a486 --- /dev/null +++ b/code/ryzom/tools/server/admin/functions_mysqli.php @@ -0,0 +1,295 @@ +persistency = $persistency; + $this->user = $sqluser; + $this->password = $sqlpassword; + $this->server = $sqlserver; + $this->dbname = $database; + + if($this->persistency) + { + $this->server = 'p:'.$this->server; + } + + $this->db_connect_id = mysqli_connect($this->server, $this->user, $this->password); + if($this->db_connect_id) + { + if($database != "") + { + $this->dbname = $database; + $dbselect = mysqli_select_db($this->db_connect_id, $this->dbname); + if(!$dbselect) + { + mysqli_close($this->db_connect_id); + $this->db_connect_id = $dbselect; + } + } + return $this->db_connect_id; + } + else + { + echo "Connection to mySQL failed!"; + exit; + } + } + + // + // Other base methods + // + function sql_close() + { + if($this->db_connect_id) + { + if($this->query_result) + { + @mysqli_free_result($this->query_result); + } + $result = mysqli_close($this->db_connect_id); + return $result; + } + else + { + return false; + } + } + + // + // Base query method + // + function sql_query($query = "", $transaction = FALSE) + { + // Remove any pre-existing queries + unset($this->query_result); + if($query != "") + { + nt_common_add_debug($query); + $this->num_queries++; + $this->query_result = mysqli_query($this->db_connect_id, $query); + if (!($this->db_connect_id instanceof mysqli)) { + var_dump($this->db_connect_id); + var_dump($query, $this); + } + } + if($this->query_result) + { + return $this->query_result; + } + else + { + return ( $transaction == 'END_TRANSACTION' ) ? true : false; + } + } + + function sql_select_db($dbname) + { + if($this->db_connect_id) + { + $result = mysqli_select_db($this->db_connect_id, $dbname); + return $result; + } + return false; + } + function sql_reselect_db() + { + if($this->db_connect_id) + { + $result = mysqli_select_db($this->db_connect_id, $this->dbname); + return $result; + } + return false; + } + // + // Other query methods + // + function sql_numrows($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = mysqli_num_rows($query_id); + return $result; + } + else + { + return false; + } + } + function sql_affectedrows() + { + if($this->db_connect_id) + { + $result = mysqli_affected_rows($this->db_connect_id); + return $result; + } + else + { + return false; + } + } + function sql_numfields($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + $result = mysqli_num_fields($query_id); + return $result; + } + else + { + return false; + } + } + // function sql_fieldname($query_id = 0){} + // function sql_fieldtype($offset, $query_id = 0){} + function sql_fetchrow($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + return mysqli_fetch_array($query_id); + } + else + { + return false; + } + } + function sql_fetchrowset($query_id = 0) + { + if(!$query_id) + { + $query_id = $this->query_result; + } + if($query_id) + { + while($row = mysqli_fetch_array($query_id)) + { + $result[] = $row; + } + return $result; + } + else + { + return false; + } + } + // function sql_fetchfield($field, $rownum = -1, $query_id = 0){} + // function sql_rowseek($rownum, $query_id = 0){} + function sql_nextid(){ + if($this->db_connect_id) + { + $result = mysqli_insert_id($this->db_connect_id); + return $result; + } + else + { + return false; + } + } + function sql_freeresult($query_id = 0){ + if(!$query_id) + { + $query_id = $this->query_result; + } + + if ( $query_id ) + { + @mysqli_free_result($query_id); + + return true; + } + else + { + return false; + } + } + function sql_error($query_id = 0) + { + $result["message"] = mysqli_error($this->db_connect_id); + $result["code"] = mysqli_errno($this->db_connect_id); + + return $result; + } + +} // class sql_db + +class sql_db_string extends sql_db +{ + // + // Constructor ($connstring format : mysql://user:password@host/dbname) + // + function sql_db_string($connstring, $persistency = true) + { + $ret = false; + if ($connstring != '') + { + if (preg_match("#^mysqli?://([^:]+)(?::([^@]*))?@([^\\/]+)/([^/]+)[/]?$#", $connstring, $params)) + { + $sqlserver = $params[3]; + $sqluser = $params[1]; + $sqlpassword = $params[2]; + $database = $params[4]; + + $ret = $this->sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency); + } + } + + return $ret; + } +} // class sql_db_string + + +} // if ... define + From cd4ba6e579be2c621ed4b046687723ce87f6bb41 Mon Sep 17 00:00:00 2001 From: Nimetu Date: Wed, 29 Jan 2014 14:11:08 +0200 Subject: [PATCH 2/2] Removed debug output --HG-- branch : shard-admin-tool-mysqli-class --- code/ryzom/tools/server/admin/functions_mysqli.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/code/ryzom/tools/server/admin/functions_mysqli.php b/code/ryzom/tools/server/admin/functions_mysqli.php index f4df4a486..da455eb79 100644 --- a/code/ryzom/tools/server/admin/functions_mysqli.php +++ b/code/ryzom/tools/server/admin/functions_mysqli.php @@ -109,10 +109,6 @@ class sql_db nt_common_add_debug($query); $this->num_queries++; $this->query_result = mysqli_query($this->db_connect_id, $query); - if (!($this->db_connect_id instanceof mysqli)) { - var_dump($this->db_connect_id); - var_dump($query, $this); - } } if($this->query_result) {