1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

- added configuration option 'resultClass'

- updated class Object
This commit is contained in:
David Grudl
2008-08-28 02:02:21 +00:00
parent 119d5a1995
commit 360f8799cf
3 changed files with 38 additions and 17 deletions

View File

@@ -224,6 +224,7 @@ abstract class Object
} }
// property getter support // property getter support
$name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
$m = 'get' . $name; $m = 'get' . $name;
if (self::hasAccessor($class, $m)) { if (self::hasAccessor($class, $m)) {
// ampersands: // ampersands:
@@ -231,10 +232,16 @@ abstract class Object
// - doesn't call &$this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value'; // - doesn't call &$this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
$val = $this->$m(); $val = $this->$m();
return $val; return $val;
} else {
throw new /*::*/MemberAccessException("Cannot read an undeclared property $class::\$$name.");
} }
$m = 'is' . $name;
if (self::hasAccessor($class, $m)) {
$val = $this->$m();
return $val;
}
$name = func_get_arg(0);
throw new /*::*/MemberAccessException("Cannot read an undeclared property $class::\$$name.");
} }
@@ -256,18 +263,21 @@ abstract class Object
} }
// property setter support // property setter support
if (self::hasAccessor($class, 'get' . $name)) { $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
if (self::hasAccessor($class, 'get' . $name) || self::hasAccessor($class, 'is' . $name)) {
$m = 'set' . $name; $m = 'set' . $name;
if (self::hasAccessor($class, $m)) { if (self::hasAccessor($class, $m)) {
$this->$m($value); $this->$m($value);
return;
} else { } else {
$name = func_get_arg(0);
throw new /*::*/MemberAccessException("Cannot assign to a read-only property $class::\$$name."); throw new /*::*/MemberAccessException("Cannot assign to a read-only property $class::\$$name.");
} }
} else {
throw new /*::*/MemberAccessException("Cannot assign to an undeclared property $class::\$$name.");
} }
$name = func_get_arg(0);
throw new /*::*/MemberAccessException("Cannot assign to an undeclared property $class::\$$name.");
} }
@@ -280,6 +290,7 @@ abstract class Object
*/ */
public function __isset($name) public function __isset($name)
{ {
$name[0] = $name[0] & "\xDF";
return $name !== '' && self::hasAccessor(get_class($this), 'get' . $name); return $name !== '' && self::hasAccessor(get_class($this), 'get' . $name);
} }
@@ -318,8 +329,6 @@ abstract class Object
// (works good since 5.0.4) // (works good since 5.0.4)
$cache[$c] = array_flip(get_class_methods($c)); $cache[$c] = array_flip(get_class_methods($c));
} }
// case-sensitive checking, capitalize the fourth character
$m[3] = $m[3] & "\xDF";
return isset($cache[$c][$m]); return isset($cache[$c][$m]);
} }

View File

@@ -53,6 +53,12 @@ class DibiConnection extends /*Nette::*/Object
*/ */
private $inTxn = FALSE; private $inTxn = FALSE;
/**
* Result set encapsulation.
* @var string
*/
private $resultClass = 'DibiResult';
/** /**
@@ -103,12 +109,18 @@ class DibiConnection extends /*Nette::*/Object
unset($config['result:objects']); unset($config['result:objects']);
} }
if (isset($config['resultObjects'])) { if (isset($config['resultObjects'])) { // normalize
// normalize
$val = $config['resultObjects']; $val = $config['resultObjects'];
$config['resultObjects'] = is_string($val) && !is_numeric($val) ? $val : (bool) $val; $config['resultObjects'] = is_string($val) && !is_numeric($val) ? $val : (bool) $val;
} }
if (isset($config['resultClass'])) {
if (strcasecmp($config['resultClass'], 'DibiResult') && !is_subclass_of($config['resultClass'], 'DibiResult')) {
throw new InvalidArgumentException("Class '$config[resultClass]' is not DibiResult descendant.");
}
$this->resultClass = $config['resultClass'];
}
$config['name'] = $name; $config['name'] = $name;
$this->config = $config; $this->config = $config;
$this->driver = new $class; $this->driver = new $class;
@@ -294,7 +306,7 @@ class DibiConnection extends /*Nette::*/Object
dibi::notify($this, 'beforeQuery', $sql); dibi::notify($this, 'beforeQuery', $sql);
if ($res = $this->driver->query($sql)) { // intentionally = if ($res = $this->driver->query($sql)) { // intentionally =
$res = new DibiResult($res, $this->config); $res = new $this->resultClass($res, $this->config);
} }
$time += microtime(TRUE); $time += microtime(TRUE);

View File

@@ -21,7 +21,7 @@
/** /**
* dibi result-set abstract class. * dibi result-set.
* *
* <code> * <code>
* $result = dibi::query('SELECT * FROM [table]'); * $result = dibi::query('SELECT * FROM [table]');
@@ -288,7 +288,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
* *
* @return mixed value on success, FALSE if no next record * @return mixed value on success, FALSE if no next record
*/ */
final function fetchSingle() final public function fetchSingle()
{ {
$row = $this->getDriver()->fetch(TRUE); $row = $this->getDriver()->fetch(TRUE);
if (!is_array($row)) return FALSE; if (!is_array($row)) return FALSE;
@@ -315,7 +315,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
* @param bool simplify one-column result set? * @param bool simplify one-column result set?
* @return array * @return array
*/ */
final function fetchAll($offset = NULL, $limit = NULL, $simplify = TRUE) final public function fetchAll($offset = NULL, $limit = NULL, $simplify = TRUE)
{ {
$limit = $limit === NULL ? -1 : (int) $limit; $limit = $limit === NULL ? -1 : (int) $limit;
$this->seek((int) $offset); $this->seek((int) $offset);
@@ -354,7 +354,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
* @return array * @return array
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
final function fetchAssoc($assoc) final public function fetchAssoc($assoc)
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetch(FALSE); $row = $this->fetch(FALSE);
@@ -437,7 +437,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
* @return array * @return array
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
final function fetchPairs($key = NULL, $value = NULL) final public function fetchPairs($key = NULL, $value = NULL)
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetch(FALSE); $row = $this->fetch(FALSE);