| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_IncrementalBackup_Database_MysqlConnection implements MWP_IncrementalBackup_Database_ConnectionInterface |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var resource |
| 16 |
*/ |
| 17 |
private $connection; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var |
| 21 |
*/ |
| 22 |
private $configuration; |
| 23 |
|
| 24 |
public function __construct(MWP_IncrementalBackup_Database_Configuration $configuration) |
| 25 |
{ |
| 26 |
$this->configuration = $configuration; |
| 27 |
|
| 28 |
if (!extension_loaded('mysql')) { |
| 29 |
throw new MWP_IncrementalBackup_Database_Exception_ConnectionException("Mysql extension is not loaded."); |
| 30 |
} |
| 31 |
|
| 32 |
if ($configuration->isSocket()) { |
| 33 |
/** @handled function */ |
| 34 |
$this->connection = @mysql_connect(':'.$configuration->getSocketPath(), $configuration->getUsername(), $configuration->getPassword()); |
| 35 |
} else { |
| 36 |
$host = $configuration->getHost(); |
| 37 |
if ($configuration->getPort() !== null) { |
| 38 |
$host .= ':'.$configuration->getPort(); |
| 39 |
} |
| 40 |
/** @handled function */ |
| 41 |
$this->connection = @mysql_connect($host, $configuration->getUsername(), $configuration->getPassword()); |
| 42 |
} |
| 43 |
|
| 44 |
if (!is_resource($this->connection)) { |
| 45 |
throw new MWP_IncrementalBackup_Database_Exception_ConnectionException(mysql_error(), mysql_errno()); |
| 46 |
} |
| 47 |
/** @handled function */ |
| 48 |
@mysql_set_charset($configuration->getCharset(), $this->connection); |
| 49 |
/** @handled function */ |
| 50 |
mysql_select_db($configuration->getDatabase(), $this->connection); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* {@inheritdoc} |
| 55 |
*/ |
| 56 |
public function query($query, $useResult = false) |
| 57 |
{ |
| 58 |
if ($useResult) { |
| 59 |
/** @handled function */ |
| 60 |
$result = mysql_unbuffered_query($query, $this->connection); |
| 61 |
} else { |
| 62 |
/** @handled function */ |
| 63 |
$result = mysql_query($query, $this->connection); |
| 64 |
} |
| 65 |
|
| 66 |
if ($result === false) { |
| 67 |
throw new MWP_IncrementalBackup_Database_Exception_ConnectionException(mysql_error($this->connection), mysql_errno($this->connection)); |
| 68 |
} |
| 69 |
|
| 70 |
return new MWP_IncrementalBackup_Database_MysqlStatement($result); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @param mixed $value any primitive value |
| 75 |
* |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
public function quote($value) |
| 79 |
{ |
| 80 |
return "'".mysql_real_escape_string($value, $this->connection)."'"; |
| 81 |
} |
| 82 |
} |
| 83 |
|