* get rid of these annoying _ in member and method names
* introduces XDBFormat interface in order to allow objects to provide
their own formatting when used as a query argument.
* s/XorgDB/XDB/
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
/** class for describing flags
*/
/** class for describing flags
*/
+class PlFlagSet implements XDBFormat
{
/** string that holds the PlFlagSet */
private $values = array();
{
/** string that holds the PlFlagSet */
private $values = array();
+
+
+ /** format for XDB
+ */
+ public function format()
+ {
+ return XDB::escape($this->flags());
+ }
}
// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
}
// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
**************************************************************************/
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
**************************************************************************/
-class XDBException extends PlException {
- public function __construct($query, $error) {
- if (strpos($query, 'INSERT') === false && strpos($query, 'UPDATE') === false
- && strpos($query, 'REPLACE') === false && strpos($query, 'DELETE') === false) {
- $text = 'Erreur lors de l\'interrogation de la base de données';
- } else {
- $text = 'Erreur lors de l\'écriture dans la base de données';
- }
- parent::__construct($text, $query . "\n" . $error);
- }
-}
-
class XDB
{
private static $mysqli = null;
class XDB
{
private static $mysqli = null;
- public static function _prepare($args)
+ public static function prepare($args)
{
global $globals;
$query = array_map(Array('XDB', 'escape'), $args);
{
global $globals;
$query = array_map(Array('XDB', 'escape'), $args);
return call_user_func_array('sprintf', $query);
}
return call_user_func_array('sprintf', $query);
}
- public static function _reformatQuery($query)
+ public static function reformatQuery($query)
{
$query = preg_split("/\n\\s*/", trim($query));
$length = 0;
{
$query = preg_split("/\n\\s*/", trim($query));
$length = 0;
- public static function _query($query)
+ public static function run($query)
- PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($query));
+ PlBacktrace::$bt['MySQL']->start(XDB::reformatQuery($query));
}
$res = XDB::$mysqli->query($query);
}
$res = XDB::$mysqli->query($query);
- throw new XDBException(XDB::_reformatQuery($query), XDB::$mysqli->error);
+ throw new XDBException(XDB::reformatQuery($query), XDB::$mysqli->error);
}
return $res;
}
private static function queryv($query)
{
}
return $res;
}
private static function queryv($query)
{
- return new XOrgDBResult(self::_prepare($query));
+ return new XDBResult(self::prepare($query));
}
public static function query()
}
public static function query()
public static function format()
{
public static function format()
{
- return self::_prepare(func_get_args());
+ return self::prepare(func_get_args());
}
// Produce the SQL statement for setting/unsetting a flag
}
// Produce the SQL statement for setting/unsetting a flag
const WILDCARD_SUFFIX = 0x02;
const WILDCARD_CONTAINS = 0x03; // WILDCARD_PREFIX | WILDCARD_SUFFIX
const WILDCARD_SUFFIX = 0x02;
const WILDCARD_CONTAINS = 0x03; // WILDCARD_PREFIX | WILDCARD_SUFFIX
+ // Produce a valid XDB argument that get formatted as a wildcard
+ // according to the given mode.
+ //
+ // Example:
+ // XDB::query("SELECT * FROM table WHERE field {?}", XDB::wildcard($text, WILDCARD_EXACT));
+ public static function wildcard($mode, $value)
+ {
+ return new XDBWildcard($value, $mode);
+ }
+
// Returns the SQL statement for a wildcard search.
public static function formatWildcards($mode, $text)
{
// Returns the SQL statement for a wildcard search.
public static function formatWildcards($mode, $text)
{
- if ($mode == self::WILDCARD_EXACT) {
- return XDB::format(' = {?}', $text);
- } else {
- $text = str_replace(array('%', '_'), array('\%', '\_'), $text);
- if ($mode & self::WILDCARD_PREFIX) {
- $text = $text . '%';
- }
- if ($mode & self::WILDCARD_SUFFIX) {
- $text = '%' . $text;
- }
- return XDB::format(" LIKE {?}", $text);
- }
+ return XDB::wildcard($mode, $text)->format();
}
// Returns a FIELD(blah, 3, 1, 2) for use in an order with custom orders
}
// Returns a FIELD(blah, 3, 1, 2) for use in an order with custom orders
if ($globals->mode != 'rw' && !strpos($args[0], 'logger')) {
return;
}
if ($globals->mode != 'rw' && !strpos($args[0], 'logger')) {
return;
}
- return self::_query(XDB::_prepare($args));
+ return self::run(XDB::prepare($args));
}
public static function iterator()
{
}
public static function iterator()
{
- return new XOrgDBIterator(self::_prepare(func_get_args()));
+ return new XDBIterator(self::prepare(func_get_args()));
}
public static function iterRow()
{
}
public static function iterRow()
{
- return new XOrgDBIterator(self::_prepare(func_get_args()), MYSQL_NUM);
+ return new XDBIterator(self::prepare(func_get_args()), MYSQL_NUM);
}
private static function findQuery($params, $default = array())
}
private static function findQuery($params, $default = array())
}
/** Fetch all rows returned by the given query.
}
/** Fetch all rows returned by the given query.
- * This functions can take 2 optional arguments (cf XOrgDBResult::fetchAllRow()).
+ * This functions can take 2 optional arguments (cf XDBResult::fetchAllRow()).
* Optional arguments are given *before* the query.
*/
public static function fetchAllRow()
* Optional arguments are given *before* the query.
*/
public static function fetchAllRow()
}
/** Fetch all rows returned by the given query.
}
/** Fetch all rows returned by the given query.
- * This functions can take 2 optional arguments (cf XOrgDBResult::fetchAllAssoc()).
+ * This functions can take 2 optional arguments (cf XDBResult::fetchAllAssoc()).
* Optional arguments are given *before* the query.
*/
public static function fetchAllAssoc()
* Optional arguments are given *before* the query.
*/
public static function fetchAllAssoc()
}
/** Fetch a column from the result of the given query.
}
/** Fetch a column from the result of the given query.
- * This functions can take 1 optional arguments (cf XOrgDBResult::fetchColumn()).
+ * This functions can take 1 optional arguments (cf XDBResult::fetchColumn()).
* Optional arguments are given *before* the query.
*/
public static function fetchColumn()
* Optional arguments are given *before* the query.
*/
public static function fetchColumn()
return 'NULL';
case 'object':
return 'NULL';
case 'object':
- if ($var instanceof PlFlagSet) {
- return "'" . addslashes($var->flags()) . "'";
+ if ($var instanceof XDBFormat) {
+ return $var->format();
} else {
return "'".addslashes(serialize($var))."'";
}
} else {
return "'".addslashes(serialize($var))."'";
}
+class XDBException extends PlException
+ public function __construct($query, $error)
+ {
+ if (strpos($query, 'INSERT') === false && strpos($query, 'UPDATE') === false
+ && strpos($query, 'REPLACE') === false && strpos($query, 'DELETE') === false) {
+ $text = 'Erreur lors de l\'interrogation de la base de données';
+ } else {
+ $text = 'Erreur lors de l\'écriture dans la base de données';
+ }
+ parent::__construct($text, $query . "\n" . $error);
+ }
+}
+
+interface XDBFormat
+{
+ public function format();
+}
+class XDBWildcard implements XDBFormat
+{
+ private $value;
+ private $mode;
+
+ public function __construct($value, $mode)
+ {
+ $this->value = $value;
+ $this->mode = $mode;
+ }
+
+ public function format()
+ {
+ if ($this->mode == XDB::WILDCARD_EXACT) {
+ return XDB::format(' = {?}', $this->value);
+ } else {
+ $text = str_replace(array('%', '_'), array('\%', '\_'), $this->value);
+ if ($this->mode & XDB::WILDCARD_PREFIX) {
+ $text = $text . '%';
+ }
+ if ($this->mode & XDB::WILDCARD_SUFFIX) {
+ $text = '%' . $text;
+ }
+ return XDB::format(" LIKE {?}", $text);
+ }
+ }
+}
+
+
+class XDBResult
+{
+ private $res;
public function __construct($query)
{
public function __construct($query)
{
- $this->_res = XDB::_query($query);
+ $this->res = XDB::run($query);
}
public function free()
{
}
public function free()
{
- if ($this->_res) {
- $this->_res->free();
+ if ($this->res) {
+ $this->res->free();
- protected function _fetchRow()
+ protected function fetchRow()
- return $this->_res ? $this->_res->fetch_row() : null;
+ return $this->res ? $this->res->fetch_row() : null;
- protected function _fetchAssoc()
+ protected function fetchAssoc()
- return $this->_res ? $this->_res->fetch_assoc() : null;
+ return $this->res ? $this->res->fetch_assoc() : null;
}
public function fetchAllRow($id = false, $keep_array = false)
{
$result = Array();
}
public function fetchAllRow($id = false, $keep_array = false)
{
$result = Array();
- while (($data = $this->_res->fetch_row())) {
+ while (($data = $this->res->fetch_row())) {
if ($id !== false) {
$key = $data[$id];
unset($data[$id]);
if ($id !== false) {
$key = $data[$id];
unset($data[$id]);
public function fetchAllAssoc($id = false, $keep_array = false)
{
$result = Array();
public function fetchAllAssoc($id = false, $keep_array = false)
{
$result = Array();
- while (($data = $this->_res->fetch_assoc())) {
+ while (($data = $this->res->fetch_assoc())) {
if ($id !== false) {
$key = $data[$id];
unset($data[$id]);
if ($id !== false) {
$key = $data[$id];
unset($data[$id]);
public function fetchOneAssoc()
{
public function fetchOneAssoc()
{
- $tmp = $this->_fetchAssoc();
+ $tmp = $this->fetchAssoc();
$this->free();
return $tmp;
}
public function fetchOneRow()
{
$this->free();
return $tmp;
}
public function fetchOneRow()
{
- $tmp = $this->_fetchRow();
+ $tmp = $this->fetchRow();
$this->free();
return $tmp;
}
public function fetchOneCell()
{
$this->free();
return $tmp;
}
public function fetchOneCell()
{
- $tmp = $this->_fetchRow();
+ $tmp = $this->fetchRow();
$this->free();
return $tmp[0];
}
$this->free();
return $tmp[0];
}
{
$res = Array();
if (is_numeric($key)) {
{
$res = Array();
if (is_numeric($key)) {
- while($tmp = $this->_fetchRow()) {
+ while($tmp = $this->fetchRow()) {
$res[] = $tmp[$key];
}
} else {
$res[] = $tmp[$key];
}
} else {
- while($tmp = $this->_fetchAssoc()) {
+ while($tmp = $this->fetchAssoc()) {
public function fetchOneField()
{
public function fetchOneField()
{
- return $this->_res ? $this->_res->fetch_field() : null;
+ return $this->res ? $this->res->fetch_field() : null;
}
public function fetchFields()
}
public function fetchFields()
public function numRows()
{
public function numRows()
{
- return $this->_res ? $this->_res->num_rows : 0;
+ return $this->res ? $this->res->num_rows : 0;
}
public function fieldCount()
{
}
public function fieldCount()
{
- return $this->_res ? $this->_res->field_count : 0;
+ return $this->res ? $this->res->field_count : 0;
-require_once dirname(__FILE__) . '/pliterator.php';
-class XOrgDBIterator extends XOrgDBResult implements PlIterator
+class XDBIterator extends XDBResult implements PlIterator
- private $_result;
- private $_pos;
- private $_total;
- private $_fpos;
- private $_fields;
- private $_mode = MYSQL_ASSOC;
+ private $result;
+ private $pos;
+ private $total;
+ private $fpos;
+ private $fields;
+ private $mode = MYSQL_ASSOC;
public function __construct($query, $mode = MYSQL_ASSOC)
{
parent::__construct($query);
public function __construct($query, $mode = MYSQL_ASSOC)
{
parent::__construct($query);
- $this->_pos = 0;
- $this->_total = $this->numRows();
- $this->_fpost = 0;
- $this->_fields = $this->fieldCount();
- $this->_mode = $mode;
+ $this->pos = 0;
+ $this->total = $this->numRows();
+ $this->fpost = 0;
+ $this->fields = $this->fieldCount();
+ $this->mode = $mode;
}
public function next()
{
}
public function next()
{
- $this->_pos ++;
- if ($this->_pos > $this->_total) {
+ $this->pos ++;
+ if ($this->pos > $this->total) {
$this->free();
unset($this);
return null;
}
$this->free();
unset($this);
return null;
}
- return $this->_mode != MYSQL_ASSOC ? $this->_fetchRow() : $this->_fetchAssoc();
+ return $this->mode != MYSQL_ASSOC ? $this->fetchRow() : $this->fetchAssoc();
}
public function first()
{
}
public function first()
{
- return $this->_pos == 1;
+ return $this->pos == 1;
}
public function last()
{
}
public function last()
{
- return $this->_pos == $this->_total;
+ return $this->pos == $this->total;
}
public function total()
{
}
public function total()
{
}
public function nextField()
{
}
public function nextField()
{
- $this->_fpos++;
- if ($this->_fpos > $this->_fields) {
+ $this->fpos++;
+ if ($this->fpos > $this->fields) {
return null;
}
return $this->fetchOneField();
return null;
}
return $this->fetchOneField();
public function firstField()
{
public function firstField()
{
- return $this->_fpos == 1;
+ return $this->fpos == 1;
}
public function lastField()
{
}
public function lastField()
{
- return $this->_fpos == $this->_fields;
+ return $this->fpos == $this->fields;
}
public function totalFields()
{
}
public function totalFields()
{
function pl_autoload($cls, array $pathes = array())
{
$cls = strtolower($cls);
function pl_autoload($cls, array $pathes = array())
{
$cls = strtolower($cls);
+ if (substr($cls, 0, 3) == 'xdb') {
+ $cls = 'xdb';
+ }
$basepath = dirname(dirname(dirname(__FILE__)));
array_unshift($pathes, 'core/classes', 'classes');
$basepath = dirname(dirname(dirname(__FILE__)));
array_unshift($pathes, 'core/classes', 'classes');