En un proyecto para un blog personal estoy usando una clase de Diego Romero, el enlace: http://driverop.com.ar/php_html/dbutil.
Ahora, sabiendo bien que la extensión por defecto de PHP para MySQL (mysql_*) va a ser eliminada, quiero migrar a PDO preferiblemente.
Mi pregunta es: ¿cómo podría convertir dicha clase toda a PDO?
P.D.: tengo los siguientes avances:
Código PHP:
<?php
namespace titssphpclasses;
abstract class DB_Abstract {
protected $_conn = null;
protected $_err = array(
'is' => false,
'msg' => '',
'num' => 0
);
protected $_row = array(
'num' => 0,
'aff' => 0
);
protected $_result = null;
public function __construct() {
$this->connect(
DB_HOST,
DB_USER,
DB_PASS,
DB_NAME,
DB_PORT
);
}
abstract protected function checkError();
abstract protected function connect($host, $user, $pass, $name, $port/* = null*/);
abstract protected function disconnect();
abstract protected function isConnected();
abstract protected function query($sql, array $data);
abstract protected function insert($table, array $data);
abstract protected function select($table, array $fields, $where/* = null*/);
abstract protected function update($table, array $data, $where/* = null*/);
abstract protected function delete($table, $where);
abstract protected function first();
abstract protected function next();
abstract protected function last();
abstract protected function seek($pos);
abstract protected function seekBy($table, $field, $value, $order/* = null*/);
abstract protected function setUTF8($utf8/* = true*/);
}
?>
Código PHP:
<?php
namespace titssphpclasses;
class DB_PDO extends DB_Abstract {
private function checkError(PDOStatement $stm = null) {
$pdo = $this->_conn;
if (!is_null($stm)) {
$pdo = $stm;
}
$info = $pdo->errorInfo();
$this->_err['num'] = is_null($info[1]) ? 0 : $info[1];
$this->_err['is'] = $this->_err['num'] != 0;
$this->_err['msg'] = $info[2];
return $this->_err['is'];
}
public function connect($host, $user, $pass, $name, $port = null) {
$dsn = "mysql:host=$host;dbname=$name";
if (is_int($port) && $port > 0) {
$dsn .= ";port=$port";
}
try {
$this->_conn = new PDO($dsn, $user, $pass, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_AUTOCOMMIT => false
));
} catch (PDOException $e) {
$this->_err['is'] = true;
$this->_err['num'] = $e->getCode();
$this->_err['msg'] = explode('] ', $e->getMessage())[2];
}
return $this->checkError();
}
public function disconnect() {
$this->_conn = null;
}
public function isConnected() {
return is_a($this->_conn, 'PDO');
}
public function query($sql, array $data) {
$this->_row['num'] = 0;
$stm = $this->_conn->prepare($sql);
foreach ($data as $key => $value) {
$stm->bindParam(':' . $key, $value);
}
$stm->execute();
if (!$this->checkError($stm)) {
}
}
public function insert($table, array $data) {
}
public function select($table, array $fields, $where) {
}
public function update($table, array $data, $where) {
}
public function delete($table, $where) {
}
public function first() {
}
public function next() {
}
public function last() {
}
public function seek($pos) {
}
public function seekBy($table, $field, $value, $order = null) {
}
public function setUTF8($utf8 = true) {
$this->_result = $this->_conn->query('SET NAMES ' . ($utf8 ? 'utf8' : 'latin1'));
return $this->checkError();
}
}
?>
P.D. 2: El backslash se autoelimina al escribir el código.