summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Gass2020-08-31 12:01:50 +0200
committerNathan Gass2020-08-31 12:01:50 +0200
commitee6ddf4597115d922e24376e675990fb56ec9b26 (patch)
tree6ec90ac1d3ea1792c6da0d00e59c51143cdd789e
parentb3ac83aff7c63644199a134c8e976f972990c187 (diff)
downloaditools-ee6ddf4597115d922e24376e675990fb56ec9b26.tar.gz
itools-ee6ddf4597115d922e24376e675990fb56ec9b26.tar.bz2
itools-ee6ddf4597115d922e24376e675990fb56ec9b26.zip
use late static binding to enable subclassing
-rw-r--r--it_dbi.class57
1 files changed, 30 insertions, 27 deletions
diff --git a/it_dbi.class b/it_dbi.class
index eb5b10e..bf75a98 100644
--- a/it_dbi.class
+++ b/it_dbi.class
@@ -21,6 +21,8 @@
class it_dbi
{
+ static $singleton;
+
var $_found_rows; # public: number of found rows if CALC_FOUND_ROWS was set
var $_data; # semi-public: current result as assoc array
@@ -69,7 +71,7 @@ function __construct($p = array(), $query = null)
$p['db'] = strtr(it::match('/www/([^/]*)', $p['home']), '.-', '__');
# If the global singleton defaults are for this db/server/server_update then use them.
- $dp = (array)$GLOBALS['it_dbi']->_p;
+ $dp = (array)static::$singleton->_p;
if ((!isset($p['db']) || ($p['db'] == $dp['db'])) && (!isset($p['server']) || ($p['server'] == $dp['server'])) && (!isset($p['server_update']) || ($p['server_update'] == $dp['server_update'])))
$p += $dp;
@@ -81,8 +83,8 @@ function __construct($p = array(), $query = null)
if ($p['table']) # Standard use: create a table object
{
- if (!isset($GLOBALS['it_dbi']))
- new it_dbi;
+ if (!isset(static::$singleton))
+ new static;
if ($p['getfieldinfo'])
$this->_p += $this->_get_field_info(); # Get $this->_fields and p[keyfield, autoincrement, randomid]
@@ -98,7 +100,7 @@ function __construct($p = array(), $query = null)
$this->read($query);
}
else
- $GLOBALS['it_dbi'] =& $this;
+ static::$singleton =& $this;
}
/**
@@ -115,26 +117,26 @@ function __construct($p = array(), $query = null)
static function createclasses($p = array())
{
# Make sure singleton exists
- $dbi = $GLOBALS['it_dbi'] ? $GLOBALS['it_dbi'] : new it_dbi($p);
+ $dbi = static::$singleton ?: new static($p);
$p += $dbi->_p;
$dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
- $state = it_dbi::_state_get($dbid);
+ $state = static::_state_get($dbid);
if (!$tables = $state['tables'])
{
$tables = $dbi->tables($p);
- $state = it_dbi::_state_get($dbid); # State could have been modified by $db->tables() call
+ $state = static::_state_get($dbid); # State could have been modified by $db->tables() call
$state['tables'] = $tables;
- it_dbi::_state_put($dbid, $state);
+ static::_state_put($dbid, $state);
}
foreach ($tables as $table)
{
# Either create class in autoloader or manually just below
if (!class_exists($p['classprefix'] . $table))
- it_dbi::createclass(array('table' => $table) + $p);
+ static::createclass(array('table' => $table) + $p);
}
}
@@ -149,22 +151,22 @@ static function createclass($p)
$p = array('table' => $p);
# Make sure singleton exists
- $dbi = $GLOBALS['it_dbi'] ? $GLOBALS['it_dbi'] : new it_dbi(array('table' => null) + $p);
+ $dbi = static::$singleton ?: new static(['table' => null] + $p);
$p += $dbi->_p; # FIXME: (has to be checked for side effects!)
$dbid = $dbi->_dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
if (!isset($dbi->_tables[$dbid]))
{
- $state = it_dbi::_state_get($dbid);
+ $state = static::_state_get($dbid);
$dbi->_tables[$dbid] = array();
if (!($tables = $state['tables']))
{
$tables = $dbi->tables($p);
- $state = it_dbi::_state_get($dbid); # State could have been modified by query above
+ $state = static::_state_get($dbid); # State could have been modified by query above
$state['tables'] = $tables;
- it_dbi::_state_put($dbid, $state);
+ static::_state_put($dbid, $state);
}
foreach ($tables as $table)
@@ -178,7 +180,8 @@ static function createclass($p)
if (substr($classname, 0, 4) != 'PMA_') # It is designed behaviour that an error is generated if this class already exists!
{
$interface = function_exists("interface_exists") && interface_exists("Iterator", false) ? "implements Iterator" : "";
- $code = "class $classname extends it_dbi $interface
+ $parentname = get_called_class();
+ $code = "class $classname extends $parentname $interface
{
function __construct(/* \$query ... */)
{
@@ -206,12 +209,12 @@ function _connect($p = array())
{
$p += $this->_p;
$dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
- $state = it_dbi::_state_get($dbid);
+ $state = static::_state_get($dbid);
if ($p['reconnect'] || !($this->_link = $state['link']))
{
# Force new link if same server/user was seen before (mysql ignores selected db)
- if ($GLOBALS['it_dbi']->_connected["{$p['server']}/{$p['user']}"]++)
+ if (static::$singleton->_connected["{$p['server']}/{$p['user']}"]++)
list($this->_link, $error) = $this->_connect_db($p);
else
list($this->_link, $error) = $this->_connect_db($p);
@@ -229,7 +232,7 @@ function _connect($p = array())
# NOTE: This overwrites old state but that is on purpose. New link means we should refetch all info about connection
$state['link'] = $this->_link;
- it_dbi::_state_put($dbid, $state, false); # Store only locally as link is not shared anyway
+ static::_state_put($dbid, $state, false); # Store only locally as link is not shared anyway
}
}
@@ -555,7 +558,7 @@ function query($query, $p = array())
{
# Purge cache for schema changes (after modifying table)
$dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
- it_dbi::_state_purgeshared($dbid);
+ static::_state_purgeshared($dbid);
}
if ($writing && $this->_p['throttle_writes'])
@@ -890,7 +893,7 @@ function _get_field_info()
{
$result = array();
$dbid = "{$this->_p['user']}@{$this->_p['server']}:{$this->_p['db']}";
- $state = it_dbi::_state_get($dbid);
+ $state = static::_state_get($dbid);
if (!($this->_fields = $state['fields'][$this->_p['table']]))
{
@@ -906,11 +909,11 @@ function _get_field_info()
foreach (preg_grep('/_' . $this->_p['localized_defaultlanguage'] . '$/', array_keys((array)$this->_fields)) as $field)
$this->_localizedfields[substr($field, 0, -1 - strlen($this->_p['localized_defaultlanguage']))] = true;
- $state = it_dbi::_state_get($dbid); # State could have been modified by query above
+ $state = static::_state_get($dbid); # State could have been modified by query above
$state['fields'][$this->_p['table']] = $this->_fields;
$state['convertfunc'][$this->_p['table']] = $this->_convertfunc;
$state['localizedfields'][$this->_p['table']] = $this->_localizedfields;
- it_dbi::_state_put($dbid, $state);
+ static::_state_put($dbid, $state);
}
else # Existing _fields, copy other info too
{
@@ -928,14 +931,14 @@ function _get_field_info()
}
}
- unset($GLOBALS['it_dbi']->_p['table'], $GLOBALS['it_dbi']->_p['keyfield']); # Remove cruft
+ unset(static::$singleton->_p['table'], static::$singleton->_p['keyfield']); # Remove cruft
return $result;
}
static function _state_get($dbid)
{
- if (!($result = $GLOBALS['it_dbi']->_state[$dbid]))
- $result = $GLOBALS['it_dbi']->_state[$dbid] = (array)it_cache::get("dbi:$dbid");
+ if (!($result = static::$singleton->_state[$dbid]))
+ $result = static::$singleton->_state[$dbid] = (array)it_cache::get(get_called_class() . ":$dbid");
#var_dump("get", $dbid, $result);
return $result;
@@ -944,15 +947,15 @@ static function _state_get($dbid)
static function _state_put($dbid, $state, $shared = true)
{
#var_dump("put", $dbid, $state);
- $GLOBALS['it_dbi']->_state[$dbid] = $state;
+ static::$singleton->_state[$dbid] = $state;
if ($shared)
- it_cache::put("dbi:$dbid", array('link' => null) + (array)$state); # link is not transferable
+ it_cache::put(get_called_class() . ":$dbid", array('link' => null) + (array)$state); # link is not transferable
}
static function _state_purgeshared($dbid = null)
{
#var_dump("purgeshared", $dbid);
- it_cache::put("dbi:" . ($dbid ? $dbid : $GLOBALS['it_dbi']->_dbid), array()); # Nuke shared cache
+ it_cache::put(get_called_class(). ":" . ($dbid ? $dbid : static::$singleton->_dbid), array()); # Nuke shared cache
}
#