Ejemplo n.º 1
0
 /**
  * Prepares the environment before running a test.
  */
 protected function setUp()
 {
     parent::setUp();
     // TODO Auto-generated Oops_Sql_SelectorTest::setUp()
     $this->Oops_Sql_Selector = new Oops_Sql_Selector('testDatabase.testTable', 'testPrimaryKey', array('testField1', 'testField2', 'testField3'));
     Oops_Sql::setLink(mysql_connect());
 }
Ejemplo n.º 2
0
 /**
  * @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);
     }
 }
Ejemplo n.º 3
0
 public function getValue()
 {
     require_once 'Oops/Sql.php';
     $r = Oops_Sql::Query("SELECT {$this}");
     list($value) = mysql_fetch_row($r);
     return $value;
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
 /**
  * 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);
 }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
 public static function quoteValue($v)
 {
     if (is_null($v)) {
         return 'NULL';
     }
     /**
      * Check if value is a MySQL user variable
      */
     switch (gettype($v)) {
         case 'boolean':
             return $v ? 1 : 0;
         case 'integer':
         case 'double':
             return $v;
         case 'object':
             switch (get_class($v)) {
                 case 'Oops_Sql_Variable':
                 case 'Oops_Sql_Expression':
                     return (string) $v;
                 case 'Oops_Sql_Value_String':
                     break;
             }
             // no break here
         // no break here
         default:
             return "'" . Oops_Sql::Escape((string) $v) . "'";
     }
 }
Ejemplo n.º 8
0
 /**
  * Same as ToIntArray but escapes all strings with mysql_real_escape_string
  *
  * @static
  */
 public static function ToEscapedArray(&$a, $keepillegal = false)
 {
     Oops_Utils::ToArray($a);
     if (count($a)) {
         foreach ($a as $k => $v) {
             $v1 = Oops_Sql::Escape($v);
             if ($v != $v1) {
                 if (!$keepillegal) {
                     unset($a[$k]);
                 } else {
                     $a[$k] = $v1;
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
 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'];
 }
Ejemplo n.º 10
0
 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");
 }
Ejemplo n.º 11
0
 /**
  * Get requested value, modified to the requested type
  *
  * @param string $key
  *        	request key
  * @param string $type
  *        	required value type
  * @param mixed $default
  *        	default value
  * @return mixed
  * @tutorial Oops/Oops/Controller.cls#handling_request
  */
 function Request($key, $type = null, $default = null)
 {
     if (!strlen($key)) {
         return false;
     }
     if (is_null($value = $this->_request->get($key))) {
         return $default;
     }
     if (is_null($type)) {
         return $value;
     }
     switch (strtolower($type)) {
         case 'bool':
         case 'boolean':
             return (bool) $value;
         case 'int':
         case 'integer':
             return (int) $value;
         case 'float':
         case 'double':
         case 'numeric':
         case 'decimal':
             return (double) $value;
         case 'array':
             require_once 'Oops/Utils.php';
             Oops_Utils::ToArray($value);
             return $value;
         case 'arrayint':
             require_once 'Oops/Utils.php';
             Oops_Utils::ToIntArray($value);
             return $value;
         case 'arraysql':
             require_once 'Oops/Utils.php';
             Oops_Utils::ToIntArray($value);
             return $value;
         case 'sql':
             require_once 'Oops/Sql.php';
             return Oops_Sql::Escape($value);
         case 'words':
             return preg_replace('/[^\\s\\w]/', '', $value);
         case 'trimmedwords':
             return trim(preg_replace('/[^\\s\\w]/', '', $value));
         default:
             return $value;
     }
 }
Ejemplo n.º 12
0
 public static function Escape($s)
 {
     if (!isset(self::$_link)) {
         Oops_Sql::Connect();
     }
     return mysql_real_escape_string($s, self::$_link);
 }