Last active
August 29, 2017 18:39
-
-
Save codeperl/ecd47d4a6f890076d38e3e51f42ad0ad to your computer and use it in GitHub Desktop.
Likhlam bose bose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Mysql; | |
/** | |
* Class MysqlConnector. | |
* Wrapper class of mysqli. | |
*/ | |
class MySqlConnector { | |
private $connection; | |
public function __construct($host, $user, $password, $port=null, $sock=null) { | |
if(!$this->connection) { | |
$this->connection = $this->createConnection($host, $user, $password, $port, $sock); | |
} | |
} | |
private function createConnection($host, $user, $password, $port=null, $sock=null) { | |
$mysqli = new mysqli($host, $user, $password, $port, $sock); | |
if ($mysqli->connect_error) { | |
throw new \Exception("Connect Error ({$mysqli->connect_errno}) {$mysqli->connect_error}"); | |
} | |
return $mysqli; | |
} | |
public function getConnection() { | |
return $this->connection; | |
} | |
public function close($connection) { | |
try { | |
$connection->close(); | |
} catch(\Exception $e) { | |
die($e->getMessage()); | |
} | |
} | |
} | |
/** | |
* Class DatabaseSelector. | |
* Select a database by pre-existing mysqli instance. | |
*/ | |
class DatabaseSelector { | |
public function __construct(\mysqli $connection, $database) { | |
try { | |
$connection->select_db($database); | |
} catch(\Exception $e) { | |
die($e->getMessage()); | |
} | |
return $connection; | |
} | |
} | |
/** | |
* Class QueryManager. | |
* Atom of query with pre-existing mysqli instance. | |
*/ | |
class QueryManager { | |
private $connection; | |
public function __construct(\mysqli $connection) { | |
$this->connection = $connection; | |
} | |
public function query($query) { | |
return $this->connection->query($query); | |
} | |
} | |
/** | |
* Class ArrayResult. | |
* Formatting of result which is Resource objects and | |
* user need to use that Resource object at work! | |
*/ | |
class ArrayResult { | |
public function __construct($resultObject) { | |
$results = []; | |
try { | |
while($result) { | |
$results[] = $result->fetch_object(); | |
} | |
return $results; | |
} catch(\Exception $e) { | |
die($e->getMessage()); | |
} | |
} | |
} | |
/** | |
* Class MyCustomResultManager. | |
* For customized format of result. | |
*/ | |
class MyCustomResultManager { | |
public function getTotal($results) { | |
if($results[0]) { | |
return $results[0]->total; | |
} | |
throw new \Exception("Error!"); | |
} | |
} | |
$mysqlConnector = new MySqlConnector(); | |
$connection = new DatabaseSelector($mysqlConnector->getConnection(), 'test'); | |
$queryManager = new QueryManager($connection); | |
$users = new ArrayResult($queryManager->query("SELECT * FROM user;")); | |
$results = new ArrayResult($queryManager->query("SELECT COUNT(id) AS total FROM user;")); | |
$customResultManager = new MyCustomResultManager(); | |
$total = $customResultManager->getTotal($results); | |
$mysqlConnection->close($connection); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment