Skip to content

Instantly share code, notes, and snippets.

@kjdev
Created February 6, 2014 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjdev/8836991 to your computer and use it in GitHub Desktop.
Save kjdev/8836991 to your computer and use it in GitHub Desktop.
<?php
class db
{
private $_dsn = '';
private $_username = null;
private $_password = null;
private $_pdo = null;
public function __construct($cli)
{
if (preg_match('/^mysql /', $cli)) {
$args = explode(' ', $cli);
$shortopts = ['D', 'h', 'p', 'P', 'u'];
$longopts = ['database', 'host', 'password', 'port', 'user',
'default-character-set'];
$params = $this->_parseOpts($args, $shortopts, $longopts);
if (!isset($params['database']) && isset($args[0])) {
$params['database'] = $args[0];
}
if (!isset($params['database'])) {
throw new Exception('MySQL: required database');
}
$this->_dsn = 'mysql:dbname=' . $params['database'];
if (isset($params['host'])) {
$this->_dsn .= ';host=' . $params['host'];
}
if (isset($params['port'])) {
$this->_dsn .= ';port=' . $params['port'];
}
if (isset($params['default-character-set'])) {
$this->_dsn .= ';charset=' . $params['default-character-set'];
}
if (isset($params['user'])) {
$this->_username = $params['user'];
}
if (isset($params['password'])) {
$this->_password = $params['password'];
}
} else if (preg_match('/^psql /', $cli)) {
$args = explode(' ', $cli);
$shortopts = ['d', 'h', 'p', 'U', 'W'];
$longopts = ['dbname', 'host', 'port', 'username', 'password'];
$params = $this->_parseOpts($args, $shortopts, $longopts);
if (!isset($params['dbname']) && isset($args[0])) {
$params['dbname'] = $args[0];
}
if (!isset($params['username']) && isset($args[1])) {
$params['username'] = $args[1];
}
if (!isset($params['dbname'])) {
throw new Exception('PostgreSQL: required dbname');
}
$this->_dsn = 'pgsql:dbname=' . $params['dbname'];
if (isset($params['host'])) {
$this->_dsn .= ';host=' . $params['host'];
}
if (isset($params['port'])) {
$this->_dsn .= ';port=' . $params['port'];
}
if (isset($params['username'])) {
$this->_username = $params['username'];
}
if (isset($params['password'])) {
$this->_password = $params['password'];
}
} else {
throw new Exception('invalid command');
}
}
private function _parseOpts(&$args, $shortopts, $longopts)
{
$params = [];
$count = count($args);
for ($i = 0; $i < $count; $i++) {
if (strncmp($args[$i], '--', 2) == 0) {
$key = substr($args[$i], 2);
if (($n = array_search($key, $longopts)) === false) {
throw new Exception("invalid parameter: ${key}");
}
} else if (strncmp($args[$i], '-', 1) == 0) {
$key = substr($args[$i], 1);
if (($n = array_search($key, $shortopts)) === false ||
!isset($longopts[$n])) {
throw new Exception("invalid short parameter: $key");
}
} else {
continue;
}
$val = $args[++$i];
$params[$longopts[$n]] = $val;
unset($args[$i]);
unset($args[$i-1]);
}
array_shift($args);
return $params;
}
private function _connect()
{
if ($this->_pdo === null) {
$this->_pdo = new PDO($this->_dsn,
$this->_username, $this->_password);
}
return $this->_pdo;
}
public function execute($sql)
{
if (empty($sql) || !is_string($sql)) {
throw new Exception('invalid SQL string');
}
$stmt = $this->_connect()->prepare($sql);
$args = func_get_args();
array_shift($args);
$stmt->execute($args);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment