1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 22:26:43 +02:00

- DibiFluent autodetects modifiers for associative arrays (after where, orderBy, ...)

- json_encode in PHP < 5.2
This commit is contained in:
David Grudl
2008-10-25 00:26:56 +00:00
parent 32dd3969a3
commit 56628144b4
2 changed files with 55 additions and 7 deletions

View File

@@ -38,6 +38,16 @@ class DibiFluent extends DibiObject
'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 */
public static $separators = array(
'SELECT' => ',',
@@ -88,7 +98,7 @@ class DibiFluent extends DibiObject
*/
public function __call($clause, $args)
{
$clause = self::_clause($clause);
$clause = self::_formatClause($clause);
// lazy initialization
if ($this->command === NULL) {
@@ -104,11 +114,14 @@ class DibiFluent extends DibiObject
if (count($args) === 1) {
$arg = $args[0];
// TODO: really ignore TRUE?
if ($arg === TRUE) {
if ($arg === TRUE) { // flag
$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);
} 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)
{
$this->cursor = & $this->clauses[self::_clause($clause)];
$this->cursor = & $this->clauses[self::_formatClause($clause)];
if ($remove) {
$this->cursor = NULL;
@@ -320,7 +333,7 @@ class DibiFluent extends DibiObject
$data = $this->clauses;
} else {
$clause = self::_clause($clause);
$clause = self::_formatClause($clause);
if (array_key_exists($clause, $this->clauses)) {
$data = array($clause => $this->clauses[$clause]);
} else {
@@ -350,7 +363,7 @@ class DibiFluent extends DibiObject
* @param string
* @return string
*/
private static function _clause($s)
private static function _formatClause($s)
{
if ($s === 'order' || $s === 'group') {
$s .= 'By';

View File

@@ -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)';
$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) {
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);
}
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';
}
}