public function getValue() { require_once 'Oops/Sql.php'; $r = Oops_Sql::Query("SELECT {$this}"); list($value) = mysql_fetch_row($r); return $value; }
/** * @param string $pid Process ID * @return array Process Data */ public function get($pid) { if (isset($this->_cached[$pid])) { return $this->_cached[$pid]; } if (preg_match('/[^a-zA-Z0-9_]/', $pid)) { throw new Oops_Process_Exception("Invalid pid", OOPS_PROCESS_EXCEPTION_INVALID_PID); } $r = Oops_Sql::Query("SELECT `class`, `currentState` FROM {$this->_tableProcesses} WHERE pid = '{$pid}'"); switch (mysql_num_rows($r)) { case 0: throw new Oops_Process_Exception("Process not found", OOPS_PROCESS_EXCEPTION_NOT_FOUND); case 1: $ret = mysql_fetch_assoc($r); $ret['variables'] = array(); $r = Oops_Sql::Query("SELECT name, class, id, serialized FROM {$this->_tableProcessData} WHERE pid = '{$pid}'"); while ((list($name, $class, $id, $serialized) = mysql_fetch_row($r)) !== false) { try { $ret['variables'][$name] = $this->_decomposeData($class, $id, $serialized); } catch (Exception $e) { /** * Something was wrong, throw exception? */ // @todo Make it clear with exceptions throw $e; } } $this->_cached[$pid] = $ret; return $ret; default: throw new Oops_Process_Exception("Process storage error", OOPS_PROCESS_EXCEPTION_NOT_FOUND); } }
public function _read($ses_id) { $ses_id = preg_replace('/\\W+/', '', $ses_id); if (!strlen($ses_id)) { return; } $result = Oops_Sql::Query("SELECT ses_value FROM {$this->_tableSessions} WHERE ses_id = '{$ses_id}'", true); list($ses_data) = mysql_fetch_row($result); $this->_savedData = $ses_data; return $ses_data; }
/** * Reads config values from DB and constructs Config object * * @param string $table * @param string|array $keyFields Field name(s) to use as config key. If not given table's primary key will be used * @param string|array $valueFields Field name(s) to use as config value. If not given all fields excluding keys will be used * @param string $keyDelimiter Explode keys by this delimiter and group values for each exploded part */ public function __construct($table, $keyFields = null, $valueFields = null, $keyDelimiter = '.', $allowModifications = false) { $table = Oops_Sql_Common::escapeIdentifiers($table); if (is_null($keyFields)) { $keyFields = array(); $r = Oops_Sql::Query("SHOW COLUMNS FROM {$table}", OOPS_SQL_EXCEPTION); while (($row = mysql_fetch_row($r)) !== false) { if (strtoupper($row[3]) == 'PRI') { $keyFields[] = $row[0]; } } } else { Oops_Utils::ToArray($keyFields); } if (!count($keyFields)) { throw new Exception("No key fields for config"); } if (is_null($valueFields)) { $sql = "SELECT * FROM {$table}"; } else { Oops_Utils::ToArray($valueFields); $select = array_merge($keyFields, $valueFields); foreach ($select as $k => $v) { $select[$k] = Oops_Sql_Common::escapeIdentifiers($v); } $sql = 'SELECT ' . join(',', $select) . " FROM {$table}"; } $r = Oops_Sql::Query($sql); $data = array(); while (($row = mysql_fetch_assoc($r)) !== false) { $keyParts = array(); foreach ($keyFields as $keyField) { $keyParts[] = $row[$keyField]; unset($row[$keyField]); } if (count($row) == 1) { $row = array_pop($row); } $data[join($keyDelimiter, $keyParts)] = $row; } parent::__construct($data, $keyDelimiter, $allowModifications); }
function Analyze($query) { static $connected = false; if (!$connected) { $connected = true; $mysqlLink = Oops_Sql::Connect(); } $this->_trace = false; $this->_worktime = 0; list($t, $m) = explode(' ', microtime()); $start = (double) $t + $m; $r = Oops_Sql::Query($query); if (mysql_errno($mysqlLink)) { $this->_Log($query, 'mysqlerror'); return; } list($t, $m) = explode(' ', microtime()); $end = (double) $t + $m; $this->_worktime = $end - $start; if ($this->maxtime && $this->_worktime > $this->maxtime) { $this->_Log($query, 'querytime'); } if ($this->preg && preg_match($this->preg, $query)) { $this->_Log($query, 'preg'); } if (!preg_match('/\\s*select\\s+/i', $query)) { return $r; } if ($this->temporary || $this->filesort || $this->all || $this->maxrows || $this->registerKeys) { $tableKeys = array(); $rex = Oops_Sql::Query("Explain {$query}"); if (mysql_errno($mysqlLink)) { return $r; } $reasons = array(); while (($row = mysql_fetch_assoc($rex)) !== false) { if ($this->temporary && strpos($row['Extra'], 'temporary') !== false) { $reasons[] = 'temporary'; } if ($this->filesort && strpos($row['Extra'], 'filesort') !== false) { $reasons[] = 'filesort'; } if ($this->all && strtoupper($row['type']) == 'ALL') { $reasons[] = 'ALL'; } if ($this->maxrows && $row['rows'] > $this->maxrows) { $reasons[] = 'manyrows'; } if ($this->registerKeys && $row['table'] && substr($row['table'], 0, 6) != '<union') { $tableid = $row['table']; if (preg_match("/FROM\\s.*([\\w\\`\\.]+)\\s+AS\\s+{$tableid}\\b/siU", $query, $match) || preg_match("/FROM\\s.*([\\w\\`]+\\.\\`?{$tableid}\\b\\`?)/siU", $query, $match)) { $tbl = str_replace('`', '', $match[1]); } else { $tbl = DATABASE_NAME . '.' . $tableid; } if (!strpos($tbl, '.', 1)) { $tbl = DATABASE_NAME . '.' . $tbl; } $tableKeys[$tbl] = $row['key']; if (!strlen($row['key'])) { if (!strlen($row['possible_keys'])) { $reasons[] = 'nopossiblekeys'; } else { $reasons[] = 'nokeys'; } } } } if (sizeof($reasons)) { $reasons = array_unique($reasons); for ($i = 0, $c = sizeof($reasons); $i < $c; $i++) { $this->_Log($query, $reasons[$i]); } } if ($this->registerKeys) { foreach ($tableKeys as $t => $k) { mysql_query("INSERT IGNORE INTO moscow_service.querriesLogKeys (tbl,k) values ('{$t}','{$k}')"); } } } return $r; }
/** * Deletes row or rows from table matching given values * * @param string $table * Table name * @param array $match * Rows match criterias as 'field'=>'value' * @param bool $returnQuery * whenever to return query or run it. Default is false. * @return int Number of affected rows or sql query string */ public static function delete($table, $match, $returnQuery = false) { if (!strlen($table)) { throw new Oops_Sql_Exception("Invalid table name"); } if (!is_array($match)) { throw new Oops_Sql_Exception("Invalid match condition"); } $wheres = array(); foreach ($match as $k => $v) { $wheres[] = self::escapeIdentifiers($k) . " = " . self::quoteValue($v); } $where = join(' AND ', $wheres); $query = "DELETE FROM " . self::escapeIdentifiers($table) . " WHERE {$where}"; if ($returnQuery) { return $query; } Oops_Sql::Query($query, OOPS_SQL_EXCEPTION); return mysql_affected_rows(Oops_Sql::getLink()); }
public function getAutoIncrement() { /** * * @todo use INFORMATION_SCHEMA and database name and table name */ $r = Oops_Sql::Query('SHOW TABLE STATUS LIKE "' . $this->_table . '"'); $row = mysql_fetch_assoc($r); return $row['Auto_increment']; }
public function __install($table) { Oops_Sql::Query("CREATE TABLE IF NOT EXISTS `{$table}` (\r\n\t\t\t`target` char(63),\r\n\t\t\t`source` char(63),\r\n\t\t\tKEY (`target`),\r\n\t\t\tKEY (`source`)\r\n\t\t) DEFAULT CHARSET=UTF8"); }