示例#1
0
	/**
	 * Performs the actual optimization. In this case the optimizer first
	 * scans the value. If the value if of type 'datetime' and the column
	 * is not it tries to make the column datetime. If the column is 'datetime'
	 * and the value 'datetime' it blocks further optimization. If the value
	 * is NOT 'datetime' then it immediately returns true, thus allowing further
	 * optimization.
	 *
	 * @return boolean $yesNo advance to next optimizer
	 */
	public function optimize() {
		if (!$this->matchesDateTime($this->value)) return true;
		
		$type = $this->writer->scanType($this->value);
		
		$fields = $this->writer->getColumns($this->table);
		
		if (!in_array($this->column,array_keys($fields))) return false;
		
		$typeInField = $this->writer->code($fields[$this->column]);
		
		if ($typeInField!="datetime") {
			if ($this->matchesDateTime($this->value)) {
				
				$cnt = (int) $this->adapter->getCell("select count(*) as n from {$this->table} where
						  {$this->column} regexp '[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]'
						  OR {$this->column} IS NULL");
				$total = (int) $this->adapter->getCell("SELECT count(*) FROM ".$this->writer->noKW($this->table));
				
				if ($total===$cnt) { 
					$this->adapter->exec("ALTER TABLE ".$this->writer->noKW($this->table)." change ".$this->writer->noKW($this->column)." ".$this->writer->noKW($this->column)." datetime ");
				}
				
				return false;
			}
			
			return true;
		}
		else {
			
			return false; 
		}
	}