File: //homepages/oneclick/WordPress/6.9/601/scripts/core/database.php
<?php
namespace Core;
use \PDO;
class Database
{
private $connection;
public function __construct($host, $username, $passwd, $dbname)
{
if (strpos($host, '.sock', $offset = 0) > 0) {
$splitHost = explode(':', $host);
$port = null;
$socket = $splitHost[1];
$dsn = 'mysql:unix_socket=' . $socket . ';dbname=' . $dbname;
} else {
$port = 3306;
$socket = null;
$dsn = 'mysql:host=' . $host . ';port=' . $port . ';dbname=' . $dbname;
}
$this->connection = new PDO($dsn, $username, $passwd);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function import($schema)
{
$inString = false;
$inComment = false;
$start = 0;
$len = strlen($schema);
$backslash = false;
for ($i = 0; $i < $len; $i++) {
if (($schema[$i] == '#' || ($schema[$i] == '-' && $schema[$i + 1] == '-')) && !$inString) {
$inComment = true;
continue;
}
if ($schema[$i] == "\n" && $inComment) {
$inComment = false;
$start = $i + 1;
continue;
}
if ($inComment) {
continue;
}
if ($schema[$i] == ';' && !$inString) {
$statement = substr($schema, $start, $i - $start);
if (!preg_match("/^[ \n\r\t]*$/", $statement)) {
$this->connection->query($statement);
}
$start = $i + 1;
}
if ($inString) {
if ($schema[$i - 1] == '\\') {
$backslash = !$backslash;
} else {
$backslash = false;
}
}
if ($schema[$i] == $inString && !$backslash) {
$inString = false;
} elseif (($schema[$i] == '"' || $schema[$i] == "'") && !$inString && ($i > 0 && $schema[$i - 1] != '\\')) {
$inString = $schema[$i];
}
}
if (!$inComment) {
$statement = substr($schema, $start);
if (!preg_match("/^[ \n\r\t]*$/", $statement)) {
$this->connection->query($statement);
}
}
}
public function drop($prefix)
{
$query = "SHOW TABLES LIKE \"$prefix%\"";
$queryResult = $this->connection->query($query);
$deleteQuery = "DROP TABLE `{$queryResult->fetchColumn()}`";
while ($row = $queryResult->fetchColumn()) {
$deleteQuery .= ", $row";
}
$this->connection->query($deleteQuery);
}
public function prepareSql($query, $parameters = '')
{
return $this->connection->prepare($query)->execute($parameters);
}
public function querySql($query)
{
$this->connection->query($query);
}
public function select($query)
{
$query .= ' LIMIT 1;';
return $this->connection->query($query)->fetchAll(PDO::FETCH_COLUMN, 0)[0];
}
/**
* get all tables which are ending with options
*
*
* @return array
*/
public function selectOptionsTableNames()
{
$query = "SHOW TABLES LIKE '%options'";
$result = $this->connection->query($query)->fetchAll(PDO::FETCH_COLUMN, 0);
return $result;
}
public function selectFromRaw($query)
{
return $this->connection->query($query)->fetch(PDO::FETCH_COLUMN, 0);
}
public function rowCount($table)
{
return $this->connection->query("SELECT COUNT(*) FROM `$table`")->fetchColumn();
}
}