Instantly share code, notes, and snippets.

@kijin /ezpdo.php
Last active Jul 4, 2018

Embed
What would you like to do?
Easy PDO wrapper class
<?php
/**
* Easy PDO for PHP 5.x
* Version 1.2
*
* Copyright (c) 2009-2014 Kijin Sung <kijin@kijinsung.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class ezPDO extends PDO
{
// Constructor override.
public function __construct($dsn, $user = null, $pass = null, $options = array())
{
// Workaround for the MySQL charset bug in PHP 5.3.6 and below.
if (preg_match('/^mysql:.*charset=([a-z0-9._-]+)/i', $dsn, $matches))
{
$options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $matches[1];
}
// Workaround for PDO's inconsistent warning/exception behavior.
if (!array_key_exists(PDO::ATTR_ERRMODE, $options))
{
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
}
// Workaround for prepared statement emulation "feature".
if (!array_key_exists(PDO::ATTR_EMULATE_PREPARES, $options))
{
$options[PDO::ATTR_EMULATE_PREPARES] = false;
}
// Call the parent class's constructor.
parent::__construct($dsn, $user, $pass, $options);
}
// Query method override.
public function query($querystring)
{
// Retrieve all parameters except the querystring.
$parameters = func_get_args();
array_shift($parameters);
// Handle the case of a single array containing all parameters.
if (count($parameters) == 1 && is_array($parameters[0]))
{
$parameters = $parameters[0];
}
// If there are any parameters, use a prepared statement.
if (count($parameters))
{
$statement = $this->prepare($querystring);
$statement->execute($parameters);
}
else
{
$statement = parent::query($querystring);
}
// Return the PDOStatement object.
return $statement;
}
// Query and fetch a single value.
public function queryFetchSingle($querystring)
{
$parameters = func_get_args();
$statement = call_user_func_array(array($this, 'query'), $parameters);
return $statement->fetchColumn();
}
// Query and fetch all results as associative arrays.
public function queryFetchAll($querystring)
{
$parameters = func_get_args();
$statement = call_user_func_array(array($this, 'query'), $parameters);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
// Query and fetch all results as objects.
public function queryFetchAllObjects($querystring)
{
$parameters = func_get_args();
$statement = call_user_func_array(array($this, 'query'), $parameters);
return $statement->fetchAll(PDO::FETCH_OBJ);
}
// Process a string to be used in a LIKE query.
public function likeify($str, $include_percent_chars = true)
{
$str = str_replace(array('\\', '%', '_'), array('\\\\', '\\%', '\\_'), $str);
return $include_percent_chars ? "%$str%" : $str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment