mirror of
https://github.com/dg/dibi.git
synced 2025-08-10 08:04:32 +02:00
- DibiFluent autodetects modifiers for associative arrays (after where, orderBy, ...)
- json_encode in PHP < 5.2
This commit is contained in:
@@ -38,6 +38,16 @@ class DibiFluent extends DibiObject
|
|||||||
'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT', '%end'),
|
'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT', '%end'),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
public static $modifiers = array(
|
||||||
|
'VALUES' => '%l',
|
||||||
|
'SET' => '%a',
|
||||||
|
'WHERE' => '%and',
|
||||||
|
'HAVING' => '%and',
|
||||||
|
'ORDER BY' => '%by',
|
||||||
|
'GROUP BY' => '%by',
|
||||||
|
);
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
public static $separators = array(
|
public static $separators = array(
|
||||||
'SELECT' => ',',
|
'SELECT' => ',',
|
||||||
@@ -88,7 +98,7 @@ class DibiFluent extends DibiObject
|
|||||||
*/
|
*/
|
||||||
public function __call($clause, $args)
|
public function __call($clause, $args)
|
||||||
{
|
{
|
||||||
$clause = self::_clause($clause);
|
$clause = self::_formatClause($clause);
|
||||||
|
|
||||||
// lazy initialization
|
// lazy initialization
|
||||||
if ($this->command === NULL) {
|
if ($this->command === NULL) {
|
||||||
@@ -104,11 +114,14 @@ class DibiFluent extends DibiObject
|
|||||||
if (count($args) === 1) {
|
if (count($args) === 1) {
|
||||||
$arg = $args[0];
|
$arg = $args[0];
|
||||||
// TODO: really ignore TRUE?
|
// TODO: really ignore TRUE?
|
||||||
if ($arg === TRUE) {
|
if ($arg === TRUE) { // flag
|
||||||
$args = array();
|
$args = array();
|
||||||
|
|
||||||
} elseif (is_string($arg) && preg_match('#^[a-z][a-z0-9_.]*$#i', $arg)) {
|
} elseif (is_string($arg) && preg_match('#^[a-z][a-z0-9_.]*$#i', $arg)) { // identifier
|
||||||
$args = array('%n', $arg);
|
$args = array('%n', $arg);
|
||||||
|
|
||||||
|
} elseif (is_array($arg) && is_string(key($arg))) { // associative array
|
||||||
|
$args = array(isset(self::$modifiers[$clause]) ? self::$modifiers[$clause] : '%a', $arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +171,7 @@ class DibiFluent extends DibiObject
|
|||||||
*/
|
*/
|
||||||
public function clause($clause, $remove = FALSE)
|
public function clause($clause, $remove = FALSE)
|
||||||
{
|
{
|
||||||
$this->cursor = & $this->clauses[self::_clause($clause)];
|
$this->cursor = & $this->clauses[self::_formatClause($clause)];
|
||||||
|
|
||||||
if ($remove) {
|
if ($remove) {
|
||||||
$this->cursor = NULL;
|
$this->cursor = NULL;
|
||||||
@@ -320,7 +333,7 @@ class DibiFluent extends DibiObject
|
|||||||
$data = $this->clauses;
|
$data = $this->clauses;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$clause = self::_clause($clause);
|
$clause = self::_formatClause($clause);
|
||||||
if (array_key_exists($clause, $this->clauses)) {
|
if (array_key_exists($clause, $this->clauses)) {
|
||||||
$data = array($clause => $this->clauses[$clause]);
|
$data = array($clause => $this->clauses[$clause]);
|
||||||
} else {
|
} else {
|
||||||
@@ -350,7 +363,7 @@ class DibiFluent extends DibiObject
|
|||||||
* @param string
|
* @param string
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private static function _clause($s)
|
private static function _formatClause($s)
|
||||||
{
|
{
|
||||||
if ($s === 'order' || $s === 'group') {
|
if ($s === 'order' || $s === 'group') {
|
||||||
$s .= 'By';
|
$s .= 'By';
|
||||||
|
@@ -118,7 +118,7 @@ class DibiProfiler extends DibiObject implements IDibiProfiler
|
|||||||
$caption = 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)';
|
$caption = 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)';
|
||||||
|
|
||||||
$payload['FirePHP.Firebug.Console'][] = array('TABLE', array($caption, self::$table));
|
$payload['FirePHP.Firebug.Console'][] = array('TABLE', array($caption, self::$table));
|
||||||
$payload = json_encode($payload);
|
$payload = function_exists('json_encode') ? json_encode($payload) : self::json_encode($payload);
|
||||||
foreach (str_split($payload, 4998) as $num => $s) {
|
foreach (str_split($payload, 4998) as $num => $s) {
|
||||||
header('X-FirePHP-Data-' . str_pad(++$num, 12, '0', STR_PAD_LEFT) . ': ' . $s);
|
header('X-FirePHP-Data-' . str_pad(++$num, 12, '0', STR_PAD_LEFT) . ': ' . $s);
|
||||||
}
|
}
|
||||||
@@ -179,4 +179,39 @@ class DibiProfiler extends DibiObject implements IDibiProfiler
|
|||||||
fclose($handle);
|
fclose($handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static function json_encode($val)
|
||||||
|
{
|
||||||
|
// indexed array
|
||||||
|
if (is_array($val) && (!$val
|
||||||
|
|| array_keys($val) === range(0, count($val) - 1))) {
|
||||||
|
return '[' . implode(',', array_map(array(__CLASS__, 'json_encode'), $val)) . ']';
|
||||||
|
}
|
||||||
|
|
||||||
|
// associative array
|
||||||
|
if (is_array($val) || is_object($val)) {
|
||||||
|
$tmp = array();
|
||||||
|
foreach ($val as $k => $v) {
|
||||||
|
$tmp[] = self::json_encode((string) $k) . ':' . self::json_encode($v);
|
||||||
|
}
|
||||||
|
return '{' . implode(',', $tmp) . '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_string($val)) {
|
||||||
|
$val = str_replace(array("\\", "\x00"), array("\\\\", "\\u0000"), $val); // due to bug #40915
|
||||||
|
return '"' . addcslashes($val, "\x8\x9\xA\xC\xD/\"") . '"';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_int($val) || is_float($val)) {
|
||||||
|
return rtrim(rtrim(number_format($val, 5, '.', ''), '0'), '.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_bool($val)) {
|
||||||
|
return $val ? 'true' : 'false';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'null';
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user