summaryrefslogtreecommitdiff
path: root/it_dbi.class
diff options
context:
space:
mode:
authorNathan Gass2008-07-16 12:40:48 +0000
committerNathan Gass2008-07-16 12:40:48 +0000
commit0b486afe33e43c9f0eee690b63753683930fe1c0 (patch)
tree5d9a5106d30bec2fdbb8f77114ee0b76346ff2f1 /it_dbi.class
parentf1a90f3fb6effcb1d3a5e59e9c138fa005c3cf98 (diff)
downloaditools-0b486afe33e43c9f0eee690b63753683930fe1c0.tar.gz
itools-0b486afe33e43c9f0eee690b63753683930fe1c0.tar.bz2
itools-0b486afe33e43c9f0eee690b63753683930fe1c0.zip
added optional array for query(), use it internally to pass db info along, don't save dbid in p/_p
Diffstat (limited to 'it_dbi.class')
-rw-r--r--it_dbi.class46
1 files changed, 23 insertions, 23 deletions
diff --git a/it_dbi.class b/it_dbi.class
index 914a162..6c26a3a 100644
--- a/it_dbi.class
+++ b/it_dbi.class
@@ -66,7 +66,6 @@ function it_dbi($p = array(), $query = null)
$p += (array)$GLOBALS['it_dbi_defaultconfig'] + array('db' => $GLOBALS['ULTRADB']) + $this->_defaultconfig;
unset($this->_defaultconfig); # to shorten ED() output
- $p['dbid'] = "{$p['user']}@{$p['server']}:{$p['db']}";
$this->_p = $p;
if ($p['table']) # Standard use: create a table object
@@ -108,9 +107,8 @@ function createclasses($p = array())
$dbi = $GLOBALS['it_dbi'] ? $GLOBALS['it_dbi'] : new it_dbi($p);
$p += $dbi->_p;
- $p['dbid'] = "{$p['user']}@{$p['server']}:{$p['db']}";
- for ($res = $dbi->query('SHOW TABLES'); $row = mysql_fetch_row($res);)
+ for ($res = $dbi->query('SHOW TABLES', $p); $row = mysql_fetch_row($res);)
{
# Either create class in autoloader or manually just below
if (!class_exists($p['classprefix'] . $row[0]))
@@ -130,12 +128,12 @@ function createclass($p)
# Make sure singleton exists
$dbi = $GLOBALS['it_dbi'] ? $GLOBALS['it_dbi'] : new it_dbi(array('table' => null) + $p);
- $dbid = "{$dbi->_p['user']}@{$dbi->_p['server']}:{$dbi->_p['db']}";
+ $dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
if (!isset($dbi->_tables[$dbid]))
{
$dbi->_tables[$dbid] = array();
- for ($res = $dbi->query('SHOW TABLES'); $row = mysql_fetch_row($res);)
+ for ($res = $dbi->query('SHOW TABLES', $p); $row = mysql_fetch_row($res);)
$dbi->_tables[$dbid][$row[0]] = true;
}
@@ -159,7 +157,7 @@ function createclass($p)
}
}";
- debug("it_dbi::createclass('{$p['table']}'): creating class $classname", 5);
+ debug("it_dbi::createclass('{$p['table']}'): creating class $classname, dbid=$dbid", 5);
eval($code);
}
}
@@ -172,8 +170,9 @@ function createclass($p)
function _connect($p = array())
{
$p += $this->_p;
+ $dbid = "{$p['user']}@{$p['server']}:{$p['db']}";
- if ($p['reconnect'] || !($this->_link = $GLOBALS['it_dbi']->_state[$p['dbid']]['link']))
+ if ($p['reconnect'] || !($this->_link = $GLOBALS['it_dbi']->_state[$dbid]['link']))
{
# Force new link if same server/user was seen before (mysql ignores selected db)
if ($GLOBALS['it_dbi']->_connected["{$p['server']}/{$p['user']}"]++)
@@ -195,7 +194,7 @@ function _connect($p = array())
if (!(@mysql_select_db($p['db'], $this->_link)))
$this->_fatal("_connect(): can't select database \"{$p['db']}\"");
- $GLOBALS['it_dbi']->_state[$p['dbid']]['link'] = $this->_link;
+ $GLOBALS['it_dbi']->_state[$dbid]['link'] = $this->_link;
}
}
@@ -380,31 +379,31 @@ function clear($pp = true)
* @param $query complete SQL query string
* @return raw MySQL result. May die if query fails and safety is big enough
*/
-function query($query)
+function query($query, $p = array())
{
+ $p += $this->_p;
$start = gettimeofday();
- if ($this->_p['server_update'] && !preg_match('/^(EXPLAIN|SELECT|SHOW) /i', $query))
+ if ($p['server_update'] && !preg_match('/^(EXPLAIN|SELECT|SHOW) /i', $query))
{
- debug("switching to update server \"{$this->_p['server_update']}\"", 5);
- $this->_p['server'] = $this->_p['server_update'];
- $this->_p['dbid'] = "{$this->_p['user']}@{$this->_p['server']}:{$this->_p['db']}";
+ debug("switching to update server \"{$p['server_update']}\"", 5);
+ $this->_p['server'] = $p['server_update'];
unset($this->_p['server_update'], $this->_link);
}
- $this->_connect(); # must be called after update server switching code
+ $this->_connect($p); # must be called after update server switching code
- debug($this->_p['dbid'] . '.' . get_class($this) . "::query(\"$query\")", 4);
+ debug("{$p['user']}@{$p['server']}:{$p['db']}" . '.' . get_class($this) . "::query(\"$query\")", 4);
- if (!($result = mysql_query($query, $this->_link)) && $this->_p['safety'])
+ if (!($result = mysql_query($query, $this->_link)) && $p['safety'])
{
$errno = mysql_errno($this->_link);
- if (($this->_p['safety'] < 2) && ($errno == 1062)) # Duplicate entry
+ if (($p['safety'] < 2) && ($errno == 1062)) # Duplicate entry
return false;
if ($errno == 2006) # mysql server has gone away: retry
{
- it::log('sqllog', "it_dbi(): reconnecting mysql_connect {$this->_p['server']}, {$this->_p['db']}");
+ it::log('sqllog', "it_dbi(): reconnecting mysql_connect {$p['server']}, {$p['db']}");
$this->_connect(array('reconnect' => true));
$result = mysql_query($query, $this->_link);
}
@@ -645,21 +644,22 @@ function delete($query = null)
function _get_field_info()
{
$result = array();
+ $dbid = "{$this->_p['user']}@{$this->_p['server']}:{$this->_p['db']}";
- if (!($this->_fields = $GLOBALS['it_dbi']->_state[$this->_p['dbid']]['fields'][$this->_p['table']]))
+ if (!($this->_fields = $GLOBALS['it_dbi']->_state[$dbid]['fields'][$this->_p['table']]))
{
- debug("it_dbi(): no fields for {$this->_p['dbid']}.{$this->_p['table']}, calculating.", 5);
+ debug("it_dbi(): no fields for {$dbid}.{$this->_p['table']}, calculating.", 5);
for ($res = $this->query('SHOW COLUMNS FROM ' . $this->_p['table']); $field = mysql_fetch_assoc($res);)
{
$this->_fields[$field['Field']] = $field + array('Length' => preg_match('/date|time/', $field['Type']) ? 20 : intval(it::match('\d+', $field['Type'])));
$this->_isint[$field['Field']] = $field['Type'] == "int(11)";
}
- $GLOBALS['it_dbi']->_state[$this->_p['dbid']]['fields'][$this->_p['table']] = $this->_fields;
- $GLOBALS['it_dbi']->_state[$this->_p['dbid']]['isint'][$this->_p['table']] = $this->_isint;
+ $GLOBALS['it_dbi']->_state[$dbid]['fields'][$this->_p['table']] = $this->_fields;
+ $GLOBALS['it_dbi']->_state[$dbid]['isint'][$this->_p['table']] = $this->_isint;
}
else # Existing _fields, copy other info too
- $this->_isint = $GLOBALS['it_dbi']->_state[$this->_p['dbid']]['isint'][$this->_p['table']];
+ $this->_isint = $GLOBALS['it_dbi']->_state[$dbid]['isint'][$this->_p['table']];
foreach($this->_fields as $field)
{