1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 04:10:38 +02:00

Removed PDO from e_db_mysql

e_db_mysql has divorced e_db_pdo. They are now independently functioning implementations of e_db.
This commit is contained in:
Nick Liu
2020-01-19 12:52:22 +01:00
parent 72d3f07410
commit c78976750c

View File

@@ -67,7 +67,7 @@ class e_db_mysql implements e_db
protected $mySQLport = 3306;
public $mySQLPrefix;
/** @var PDO */
/** @var resource */
protected $mySQLaccess;
public $mySQLresult;
public $mySQLrows;
@@ -92,9 +92,6 @@ class e_db_mysql implements e_db
public $total_results = false; // Total number of results
private $pdo = false; // using PDO or not.
private $pdoBind= false;
/** @var e107_db_debug */
private $dbg;
@@ -106,15 +103,6 @@ class e_db_mysql implements e_db
*/
public function __construct()
{
global $pref, $db_defaultPrefix;
if((PHP_MAJOR_VERSION > 6) || !function_exists('mysql_connect') || (defined('e_PDO') && e_PDO === true))
{
$this->pdo = true;
}
e107::getSingleton('e107_traffic')->BumpWho('Create db object', 1);
$this->mySQLPrefix = MPREFIX; // Set the default prefix - may be overridden
@@ -149,7 +137,7 @@ class e_db_mysql implements e_db
function getPDO()
{
return $this->pdo;
return false;
}
function debugMode($bool)
@@ -196,29 +184,7 @@ class e_db_mysql implements e_db
$this->mySQLerror = false;
if($this->pdo)
{
if(strpos($mySQLserver,':')!==false && substr_count($mySQLserver, ':')===1)
{
list($this->mySQLserver,$this->mySQLport) = explode(':',$mySQLserver,2);
}
try
{
$this->mySQLaccess = new PDO("mysql:host=".$this->mySQLserver."; port=".$this->mySQLport, $this->mySQLuser, $this->mySQLpassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $ex)
{
$this->mySQLlastErrText = $ex->getMessage();
$this->mySQLlastErrNum = $ex->getCode();
return 'e1';
}
}
elseif(defined("USE_PERSISTANT_DB") && USE_PERSISTANT_DB == TRUE)
if(defined("USE_PERSISTANT_DB") && USE_PERSISTANT_DB == TRUE)
{
// No persistent link parameter permitted
if ( ! $this->mySQLaccess = @mysql_pconnect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword))
@@ -236,24 +202,18 @@ class e_db_mysql implements e_db
}
}
$this->mySqlServerInfo = ($this->pdo) ? $this->mySQLaccess->query('select version()')->fetchColumn() : mysql_get_server_info(); // We always need this for db_Set_Charset() - so make generally available
$this->mySqlServerInfo = mysql_get_server_info(); // We always need this for db_Set_Charset() - so make generally available
// Set utf8 connection?
//@TODO: simplify when yet undiscovered side-effects will be fixed
$this->db_Set_Charset();
$this->setSQLMode();
// if ($this->pdo!== true && !@mysql_select_db($this->mySQLdefaultdb, $this->mySQLaccess))
if (!$this->database($this->mySQLdefaultdb))
{
return 'e2';
}
if($this->pdo == true)
{
// $this->mySQLaccess->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// $this->mySQLaccess->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
}
$this->dbError('dbConnect/SelectDB');
// Save the connection resource
@@ -295,48 +255,18 @@ class e_db_mysql implements e_db
list($this->mySQLserver,$this->mySQLport) = explode(':',$mySQLserver,2);
}
// if($this->mySQLserver === 'localhost') // problematic.
if (!$this->mySQLaccess = @mysql_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink))
{
// $this->mySQLserver = '127.0.0.1'; // faster by almost 1 second.
$this->mySQLlastErrText = mysql_error();
return false;
}
if($this->pdo) // PDO
{
try
{
$this->mySQLaccess = new PDO("mysql:host=".$this->mySQLserver."; port=".$this->mySQLport, $this->mySQLuser, $this->mySQLpassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $ex)
{
$this->mySQLlastErrText = $ex->getMessage();
$this->mySQLLastErrNum = $ex->getCode();
$this->dbg->log($this->mySQLlastErrText); // Useful for Debug.
return false;
}
// $this->mySqlServerInfo = $this->mySQLaccess->getAttribute(PDO::ATTR_SERVER_INFO);
$this->mySqlServerInfo = $this->mySQLaccess->query('select version()')->fetchColumn();
}
else // Legacy.
{
if (!$this->mySQLaccess = @mysql_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink))
{
$this->mySQLlastErrText = mysql_error();
return false;
}
$this->mySqlServerInfo = mysql_get_server_info();
}
$this->mySqlServerInfo = mysql_get_server_info();
$this->db_Set_Charset();
$this->setSQLMode();
//if ($db_ConnectionID == NULL){
$db_ConnectionID = $this->mySQLaccess;
// }
$db_ConnectionID = $this->mySQLaccess;
return true;
}
@@ -372,25 +302,6 @@ class e_db_mysql implements e_db
return true;
}
if($this->pdo)
{
try
{
$this->mySQLaccess->query("use `".$database."`");
// $this->mySQLaccess->select_db($database); $dbh->query("use newdatabase");
}
catch (PDOException $e)
{
$this->mySQLlastErrText = $e->getMessage();
$this->mySQLlastErrNum = $e->getCode();
return false;
}
return true;
}
if (!@mysql_select_db($database, $this->mySQLaccess))
{
return false;
@@ -463,10 +374,8 @@ class e_db_mysql implements e_db
*
* If a SELECT query includes SQL_CALC_FOUND_ROWS, the value of FOUND_ROWS() is retrieved and stored in $this->total_results
* @param string|array $query
* @param string $query['PREPARE'] PDO Format query.
*@param array $query['BIND'] eg. array['my_field'] = array('value'=>'whatever', 'type'=>'str');
* @param object $rli
* @return boolean|PDOStatement | resource - as mysql_query() function.
* @return boolean|resource - as mysql_query() function.
* FALSE indicates an error
* For SELECT, SHOW, DESCRIBE, EXPLAIN and others returning a result set, returns a resource
* TRUE indicates success in other cases
@@ -496,69 +405,9 @@ class e_db_mysql implements e_db
$b = microtime();
if($this->pdo)
{
if(is_array($query) && !empty($query['PREPARE']) && !empty($query['BIND']))
{
/** @var PDOStatement $prep */
$prep = $this->mySQLaccess->prepare($query['PREPARE']);
foreach($query['BIND'] as $k=>$v)
{
$prep->bindValue(':'.$k, $v['value'],$v['type']);
}
try
{
$prep->execute();
$sQryRes = $prep->rowCount();
}
catch(PDOException $ex)
{
$sQryRes = false;
$this->mySQLlastErrText = $ex->getMessage();
$this->mySQLlastErrNum = $ex->getCode();
}
}
else
{
try
{
if(preg_match('#^(CREATE TABLE|DROP TABLE|ALTER TABLE|RENAME TABLE|CREATE DATABASE|CREATE INDEX)#',$query, $matches))
{
/** @var PDO $rli */
$sQryRes = is_null($rli) ? $this->mySQLaccess->exec($query) : $rli->exec($query);
if($sQryRes !==false)
{
$sQryRes = true; // match with non-PDO results.
}
}
else
{
/** @var PDO $rli */
$sQryRes = is_null($rli) ? $this->mySQLaccess->query($query) : $rli->query($query);
}
}
catch(PDOException $ex)
{
$sQryRes = false;
$this->mySQLlastErrText = $ex->getMessage();
$this->mySQLlastErrNum = $ex->getCode();
}
}
}
else
{
$sQryRes = is_null($rli) ? @mysql_query($query,$this->mySQLaccess) : @mysql_query($query, $rli);
$this->mySQLlastErrNum = mysql_errno();
$this->mySQLlastErrText = mysql_error();
}
$sQryRes = is_null($rli) ? @mysql_query($query, $this->mySQLaccess) : @mysql_query($query, $rli);
$this->mySQLlastErrNum = mysql_errno();
$this->mySQLlastErrText = mysql_error();
$e = microtime();
@@ -580,17 +429,9 @@ class e_db_mysql implements e_db
if (!is_array($query) && (strpos($query,'EXPLAIN') !==0) && (strpos($query,'SQL_CALC_FOUND_ROWS') !== false) && (strpos($query,'SELECT') !== false))
{
if($this->pdo)
{
$rc = $this->mySQLaccess->query('SELECT FOUND_ROWS();')->fetch(PDO::FETCH_COLUMN);
$this->total_results = intval($rc);
}
else /* @XXX Subject of Removal. */
{
$fr = mysql_query('SELECT FOUND_ROWS()', $this->mySQLaccess);
$rc = mysql_fetch_array($fr);
$this->total_results = (int) $rc['FOUND_ROWS()'];
}
$fr = mysql_query('SELECT FOUND_ROWS()', $this->mySQLaccess);
$rc = mysql_fetch_array($fr);
$this->total_results = (int)$rc['FOUND_ROWS()'];
}
@@ -608,35 +449,7 @@ class e_db_mysql implements e_db
if(is_object($db_debug))
{
$buglink = is_null($rli) ? $this->mySQLaccess : $rli;
if($this->pdo == true)
{
if(is_array($query))
{
$query = "PREPARE: ".$query['PREPARE']."<br />BIND:".print_a($query['BIND'],true); // ,true);
}
if(isset($ex) && is_object($ex))
{
$query = $ex->getMessage();
$query .= print_a($ex->getTrace(),true);
}
}
if($this->pdo == true && $buglink instanceof PDO)
{
$db_debug->Mark_Query($query, 'PDO', $sQryRes, $aTrace, $mytime, $pTable);
}
else
{
$db_debug->Mark_Query($query, $buglink, $sQryRes, $aTrace, $mytime, $pTable);
}
}
else
{
// echo "what happened to db_debug??!!<br />";
$db_debug->Mark_Query($query, $buglink, $sQryRes, $aTrace, $mytime, $pTable);
}
}
@@ -944,13 +757,10 @@ class e_db_mysql implements e_db
$fieldTypes = $this->_getTypes($arg);
$keyList= '`'.implode('`,`', array_keys($arg['data'])).'`';
$tmp = array();
$bind = array();
foreach($arg['data'] as $fk => $fv)
{
$tmp[] = ($this->pdo == true) ? ':'.$fk : $this->_getFieldValue($fk, $fv, $fieldTypes);
$fieldType = isset($fieldTypes[$fk]) ? $fieldTypes[$fk] : null;
$bind[$fk] = array('value'=>$this->_getPDOValue($fieldType,$fv), 'type'=> $this->_getPDOType($fieldType,$this->_getPDOValue($fieldType,$fv)));
$tmp[] = $this->_getFieldValue($fk, $fv, $fieldTypes);
}
$valList= implode(', ', $tmp);
@@ -975,17 +785,6 @@ class e_db_mysql implements e_db
{
$query = "REPLACE INTO ".$this->mySQLPrefix."{$table} ({$keyList}) VALUES ({$valList})";
}
if($this->pdo == true)
{
$query = array(
'PREPARE' => $query,
'BIND' => $bind,
);
}
}
else
{
@@ -1000,10 +799,7 @@ class e_db_mysql implements e_db
{
$result = false; // ie. there was an error.
if($this->pdo !== true)
{
$this->mySQLresult = mysql_affected_rows($this->mySQLaccess);
}
$this->mySQLresult = mysql_affected_rows($this->mySQLaccess);
if($this->mySQLresult === 1 ) // insert.
{
@@ -1030,15 +826,13 @@ class e_db_mysql implements e_db
{
if(true === $REPLACE)
{
$tmp = ($this->pdo) ? $this->mySQLresult : mysql_affected_rows($this->mySQLaccess);
$tmp = mysql_affected_rows($this->mySQLaccess);
$this->dbError('db_Replace');
// $tmp == -1 (error), $tmp == 0 (not modified), $tmp == 1 (added), greater (replaced)
if ($tmp == -1) { return false; } // mysql_affected_rows error
return $tmp;
}
// $tmp = ($this->pdo) ? $this->mySQLaccess->lastInsertId() : mysql_insert_id($this->mySQLaccess);
$tmp = $this->lastInsertId();
$this->dbError('db_Insert');
@@ -1054,7 +848,7 @@ class e_db_mysql implements e_db
public function lastInsertId()
{
$tmp = ($this->pdo) ? (int) $this->mySQLaccess->lastInsertId() : mysql_insert_id($this->mySQLaccess);
$tmp = mysql_insert_id($this->mySQLaccess);
return ($tmp) ? $tmp : true; // return true even if table doesn't have auto-increment.
}
@@ -1068,38 +862,24 @@ class e_db_mysql implements e_db
return $this->total_results;
}
/**
* @param resource $result
* @return false|int
*/
public function rowCount($result=null)
{
if($this->pdo)
if (!is_resource($result))
{
if(!$this->mySQLresult)
{
return -1;
}
$result = $this->mySQLresult;
}
/** @var PDOStatement $resource */
$resource = $this->mySQLresult;
if ($this->pdo)
if (is_resource($result))
{
$this->mySQLrows = $resource->rowCount();
}
elseif (is_resource($this->mySQLresult))
{
$this->mySQLrows = mysql_num_rows($this->mySQLresult);
$this->mySQLrows = mysql_num_rows($result);
}
$this->dbError('db_Rows');
return $this->mySQLrows;
}
/**
* insert() alias
* @deprecated
@@ -1141,10 +921,8 @@ class e_db_mysql implements e_db
private function _prepareUpdateArg($tableName, $arg)
{
$this->pdoBind = array();
if (is_array($arg)) // Remove the need for a separate db_UpdateArray() function.
{
if(!isset($arg['_FIELD_TYPES']) && !isset($arg['data']))
{
//Convert data if not using 'new' format
@@ -1172,23 +950,11 @@ class e_db_mysql implements e_db
$new_data = '';
//$this->pdoBind = array(); // moved up to the beginning of the method to make sure it is initialized properly
foreach ($arg['data'] as $fn => $fv)
{
$new_data .= ($new_data ? ', ' : '');
$ftype = isset($fieldTypes[$fn]) ? $fieldTypes[$fn] : 'str';
$new_data .= ($this->pdo == true && $ftype !='cmd') ? "`{$fn}`= :". $fn : "`{$fn}`=".$this->_getFieldValue($fn, $fv, $fieldTypes);
if($fv === '_NULL_')
{
$ftype = 'null';
}
if($ftype != 'cmd')
{
$this->pdoBind[$fn] = array('value'=>$this->_getPDOValue($ftype,$fv), 'type'=> $this->_getPDOType($ftype,$this->_getPDOValue($ftype,$fv)));
}
$new_data .= "`{$fn}`=".$this->_getFieldValue($fn, $fv, $fieldTypes);
}
$arg = $new_data .(isset($arg['WHERE']) ? ' WHERE '. $arg['WHERE'] : '');
@@ -1229,33 +995,12 @@ class e_db_mysql implements e_db
$query = 'UPDATE '.$this->mySQLPrefix.$table.' SET '.$arg;
if($this->pdo == true && !empty($this->pdoBind))
{
$query = array(
'PREPARE' => $query,
'BIND' => $this->pdoBind,
);
}
$result = $this->mySQLresult = $this->db_Query($query, NULL, 'db_Update', $debug, $log_type, $log_remark);
if ($result !==false)
{
$result = mysql_affected_rows($this->mySQLaccess);
if($this->pdo == true)
{
if(is_object($result))
{
// make sure to return the number of records affected, instead of an object
$result = $this->rowCount();
}
}
else
{
$result = mysql_affected_rows($this->mySQLaccess);
}
// $result = ($this->pdo) ? $result : mysql_affected_rows($this->mySQLaccess);
$this->dbError('db_Update');
if ($result === -1) { return false; } // Error return from mysql_affected_rows
return $result;
@@ -1364,114 +1109,6 @@ class e_db_mysql implements e_db
}
}
/**
* Return a value for use in PDO bindValue() - based on field-type.
* @param $type
* @param $fieldValue
* @return int|string
*/
private function _getPDOValue($type, $fieldValue)
{
if(is_string($fieldValue) && ($fieldValue === '_NULL_'))
{
$type = 'null';
}
switch($type)
{
case "int":
case "integer":
return (int) $fieldValue;
break;
case 'float':
// fix - convert localized float numbers
// $larr = localeconv();
// $search = array($larr['decimal_point'], $larr['mon_decimal_point'], $larr['thousands_sep'], $larr['mon_thousands_sep'], $larr['currency_symbol'], $larr['int_curr_symbol']);
// $replace = array('.', '.', '', '', '', '');
// return str_replace($search, $replace, floatval($fieldValue));
return e107::getParser()->toNumber($fieldValue);
break;
case 'null':
return (
is_string($fieldValue) && (
($fieldValue !== '_NULL_') && ($fieldValue !== '')
)
) ? $fieldValue : null;
break;
case 'array':
if(is_array($fieldValue))
{
return e107::serialize($fieldValue);
}
return $fieldValue;
break;
case 'todb': // using as default causes serious BC issues.
if($fieldValue == '') { return ''; }
return e107::getParser()->toDB($fieldValue);
break;
case 'cmd':
case 'safestr':
case 'str':
case 'string':
case 'escape':
default:
return $fieldValue;
break;
}
}
/**
* Convert FIELD_TYPE to PDO compatible Field-Type
* @param $type
* @return int
*/
private function _getPDOType($type, $value = null)
{
switch($type)
{
case "int":
case "integer":
return PDO::PARAM_INT;
break;
case 'null':
return ($value === null) ? PDO::PARAM_NULL : PDO::PARAM_STR;
break;
case 'cmd':
case 'safestr':
case 'str':
case 'string':
case 'escape':
case 'array':
case 'todb':
case 'float':
return PDO::PARAM_STR;
break;
}
// e107::getMessage()->addDebug("MySQL Missing Field-Type: ".$type);
return PDO::PARAM_STR;
}
/**
* @DEPRECATED
Similar to db_Update(), but splits the variables and the 'WHERE' clause.
@@ -1501,7 +1138,7 @@ class e_db_mysql implements e_db
}
if ($result = $this->mySQLresult = $this->db_Query('UPDATE '.$this->mySQLPrefix.$table.' SET '.$new_data.$vars.' '.$arg, NULL, 'db_UpdateArray', $debug, $log_type, $log_remark))
{
$result = ($this->pdo) ? $this->mySQLresult->rowCount() : mysql_affected_rows($this->mySQLaccess);
$result = mysql_affected_rows($this->mySQLaccess);
if ($result == -1) return FALSE; // Error return from mysql_affected_rows
return $result;
}
@@ -1538,59 +1175,33 @@ class e_db_mysql implements e_db
*/
function fetch($type = null)
{
if(defined('MYSQL_ASSOC'))
{
switch ($type)
{
case 'both':
case 3: // MYSQL_BOTH:
$type = ($this->pdo) ? PDO::FETCH_BOTH: MYSQL_BOTH; // 3
$type = MYSQL_BOTH; // 3
break;
case 'num':
case 2; // MYSQL_NUM: // 2
$type = ($this->pdo) ? PDO::FETCH_NUM : MYSQL_NUM;
$type = MYSQL_NUM;
break;
default:
case 'assoc':
case 1; //: // 1
$type = ($this->pdo) ? PDO::FETCH_ASSOC : MYSQL_ASSOC;
$type = MYSQL_ASSOC;
break;
}
}
else
{
if($this->pdo) // convert type to PDO.
{
switch ($type)
{
case 'both': // 3
$type = PDO::FETCH_BOTH;
break;
case 'num': // 2
$type = PDO::FETCH_NUM;
break;
case 'assoc': // 1
default:
$type = PDO::FETCH_ASSOC;
break;
}
}
}
$b = microtime();
if($this->mySQLresult)
{
/** @var PDOStatement $resource */
$resource = $this->mySQLresult;
$row = ($this->pdo) ? $resource->fetch($type) : @mysql_fetch_array($this->mySQLresult,$type);
$row = @mysql_fetch_array($this->mySQLresult, $type);
e107::getSingleton('e107_traffic')->Bump('db_Fetch', $b);
if ($row)
{
@@ -1635,7 +1246,7 @@ class e_db_mysql implements e_db
$query=$table;
if ($this->mySQLresult = $this->db_Query($query, NULL, 'db_Count', $debug, $log_type, $log_remark))
{
$rows = $this->mySQLrows = ($this->pdo) ? $this->mySQLresult->fetch(PDO::FETCH_ASSOC) : @mysql_fetch_array($this->mySQLresult);
$rows = $this->mySQLrows = @mysql_fetch_array($this->mySQLresult);
$this->dbError('db_Count');
return (int) $rows['COUNT(*)'];
}
@@ -1655,7 +1266,7 @@ class e_db_mysql implements e_db
$query='SELECT COUNT'.$fields.' FROM '.$this->mySQLPrefix.$table.' '.$arg;
if ($this->mySQLresult = $this->db_Query($query, NULL, 'db_Count', $debug, $log_type, $log_remark))
{
$rows = $this->mySQLrows = ($this->pdo) ? $this->mySQLresult->fetch(PDO::FETCH_NUM) : @mysql_fetch_array($this->mySQLresult);
$rows = $this->mySQLrows = @mysql_fetch_array($this->mySQLresult);
$this->dbError('db_Count');
return (int) $rows[0];
}
@@ -1730,7 +1341,7 @@ class e_db_mysql implements e_db
if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table, NULL, 'db_Delete', $debug, $log_type, $log_remark))
{
// return the number of records deleted instead of an object
$this->mySQLrows = ($this->pdo) ? $this->mySQLresult->rowCount() : mysql_affected_rows($this->mySQLaccess);
$this->mySQLrows = mysql_affected_rows($this->mySQLaccess);
$this->dbError('db_Delete');
return $this->mySQLrows;
}
@@ -1744,7 +1355,7 @@ class e_db_mysql implements e_db
{
if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table.' WHERE '.$arg, NULL, 'db_Delete', $debug, $log_type, $log_remark))
{
$this->mySQLrows = ($this->pdo) ? $this->mySQLresult->rowCount() : mysql_affected_rows($this->mySQLaccess);
$this->mySQLrows = mysql_affected_rows($this->mySQLaccess);
$this->dbError('db_Delete');
return $this->mySQLrows;
}
@@ -1829,9 +1440,7 @@ class e_db_mysql implements e_db
{ // Successful query which may return a row count (because it operated on a number of rows without returning a result set)
if(preg_match('#^(DELETE|INSERT|REPLACE|UPDATE)#',$query, $matches))
{ // Need to check mysql_affected_rows() - to return number of rows actually updated
/** @var PDOStatement $resource */
$resource = $this->mySQLresult;
$tmp = ($this->pdo) ? $resource->rowCount() : mysql_affected_rows($this->mySQLaccess);
$tmp = mysql_affected_rows($this->mySQLaccess);
$this->dbError('db_Select_gen');
return $tmp;
}
@@ -2301,24 +1910,11 @@ class e_db_mysql implements e_db
return $this->field($table,$fieldid,$key, $retinfo);
}
function columnCount()
{
if($this->pdo)
{
/** @var PDOStatement $resource */
$resource = $this->mySQLresult;
return $resource->columnCount();
}
else
{
return mysql_num_fields($this->mySQLresult);
}
return mysql_num_fields($this->mySQLresult);
}
/**
* Determines if a plugin field (and key) exist. OR if fieldid is numeric - return the field name in that position.
*
@@ -2444,7 +2040,6 @@ class e_db_mysql implements e_db
*/
function escape($data, $strip = true)
{
if ($strip)
{
$data = strip_if_magic($data);
@@ -2452,12 +2047,6 @@ class e_db_mysql implements e_db
$this->provide_mySQLaccess();
if($this->pdo)
{
return $data;
// return $this->mySQLaccess->quote($data);
}
return mysql_real_escape_string($data,$this->mySQLaccess);
}
@@ -2779,141 +2368,10 @@ class e_db_mysql implements e_db
*/
function backup($table='*', $file='', $options=null)
{
if($this->pdo === false)
{
$this->mysqlLastErrText = "PDO is required to use the mysql backup() method";
return false;
}
// $dbtable = $this->mySQLdefaultdb;
$fileName = ($table =='*') ? str_replace(" ","_",SITENAME) : $table;
$fileName = preg_replace('/[^\w]/i',"",$fileName);
$backupFile = ($file) ? e_BACKUP.$file : e_BACKUP.strtolower($fileName)."_".$this->mySQLPrefix.date("Y-m-d-H-i-s").".sql";
if($table === '*')
{
$nolog = vartrue($options['nologs']) ? 'nologs' : 'all';
$tableList = $this->tables($nolog);
}
else
{
$tableList = explode(",",$table);
}
if(!empty($options['gzip']))
{
$backupFile .= '.gz';
}
include_once(dirname(__FILE__) . '/Ifsnop/Mysqldump/Mysqldump.php');
$config = e107::getMySQLConfig();
$dumpSettings = array(
'compress' => !empty($options['gzip']) ? Ifsnop\Mysqldump\Mysqldump::GZIP : Ifsnop\Mysqldump\Mysqldump::NONE,
'include-tables' => array(),
'no-data' => false,
'add-drop-table' => !empty($options['droptable']) ? true : false,
'single-transaction' => true,
'lock-tables' => true,
'add-locks' => true,
'extended-insert' => true,
'disable-foreign-keys-check' => true,
'skip-triggers' => false,
'add-drop-trigger' => true,
'databases' => false,
'add-drop-database' => false,
'hex-blob' => true,
'reset-auto-increment' => false,
);
foreach($tableList as $tab)
{
$dumpSettings['include-tables'][] = $config['mySQLprefix'].trim($tab);
}
try {
$dump = new Ifsnop\Mysqldump\Mysqldump('mysql:host='.$config['mySQLserver'].';dbname='.$config['mySQLdefaultdb'], $config['mySQLuser'], $config['mySQLpassword'], $dumpSettings);
$dump->start($backupFile);
return $backupFile;
}
catch (\Exception $e)
{
$this->mysqlLastErrText = 'mysqldump-php error: ' .$e->getMessage();
return false;
}
/*
$header = "-- e107 Database Backup File \n";
$header .= "-- Host: ".$_SERVER['SERVER_NAME']."\n";
$header .= "-- Generation Time: ".date('r')."\n";
$header .= "-- Encoding: UTF-8\n\n\n";
file_put_contents($backupFile,$header, FILE_APPEND);
foreach ($tableList as $table)
{
unset($text);
$text = "";
$text .= vartrue($options['droptable']) ? "DROP TABLE IF EXISTS `".$this->mySQLPrefix.$table."`;\n" : "";
$this->gen("SHOW CREATE TABLE `".$this->mySQLPrefix.$table."`");
$row2 = $this->fetch();
$text .= $row2['Create Table'];
$text .= ";\n\n";
file_put_contents($backupFile,$text,FILE_APPEND);
// echo $text;
// ob_end_clean(); // prevents memory exhaustian on large databases but breaks layout. .
$count = $this->gen("SELECT * FROM `#".$table."`");
$data_array = "";
//TODO After so many rows (50,000?), make a new files to avoid overly large inserts.
while($row = $this->fetch())
{
$fields = array_keys($row);
$text = "\nINSERT INTO `".$this->mySQLPrefix.$table."` (`".implode("` ,`",$fields)."`) VALUES \n";
file_put_contents($backupFile,$text,FILE_APPEND);
$d = array();
foreach($fields as $val)
{
$d[] = is_numeric($row[$val]) ? $row[$val] : "'".$this->escape($row[$val])."'";
}
$data_array = "(".implode(", ",$d).");\n";
file_put_contents($backupFile, $data_array, FILE_APPEND); // Do this here to save memory.
}
$text = "\n\n\n";
file_put_contents($backupFile, $text, FILE_APPEND);
unset($fields);
}
return $backupFile;
// file_put_contents('memory.log', 'memory used in line ' . __LINE__ . ' is: ' . memory_get_usage() . PHP_EOL, FILE_APPEND);
*/
$this->mysqlLastErrText = "PDO is required to use the mysql backup() method";
return false;
}
/**
* @return string relating to error (empty string if no error)
* @param string $from
@@ -2922,23 +2380,6 @@ class e_db_mysql implements e_db
*/
function dbError($from)
{
// $this->mySQLaccess->getMessage();
if($this->pdo)
{
$this->mySQLerror = true;
if($this->mySQLlastErrNum === 0)
{
return null;
}
return $from." :: ".$this->mySQLlastErrText;
}
$this->mySQLlastErrNum = mysql_errno();
$this->mySQLlastErrText = '';
if ($this->mySQLlastErrNum == 0)
@@ -3011,7 +2452,7 @@ class e_db_mysql implements e_db
{
if ( ! $debug)
{
($this->pdo) ? $this->db_Query("SET NAMES `$charset`") : @mysql_query("SET NAMES `$charset`");
@mysql_query("SET NAMES `$charset`");
}
else
{
@@ -3254,7 +2695,7 @@ class e_db_mysql implements e_db
/**
* @deprecated 2.1.9 Used only to provide $mySQLaccess to other instances of e_db_mysql scattered around
* @return PDO
* @return resource
*/
public function get_mySQLaccess()
{