/** * 轉換成 SQL 語法 * * @return string */ public function __toString() { // COLUMN $sql = 'SELECT'; if ($this->_parts['DISTINCT']) { $sql .= ' DISTINCT'; } if (1 === count($this->_parts['COLUMN']) && '*' === $this->_parts['COLUMN'][0]) { $columns = $this->_parts['COLUMN']; } else { $columns = array(); foreach ($this->_parts['COLUMN'] as $col => $alias) { if (is_int($col)) { $columns[] = $this->_db->quoteIdentifier($alias); } else { $columns[] = $col . ' AS ' . $this->_db->quoteIdentifier($alias); } } } $sql .= ' ' . join(', ', $columns); // TABLE $sql .= ' FROM'; $sql .= ' ' . $this->_db->quoteIdentifier($this->_parts['TABLE']); // WHERE if (!empty($this->_parts['WHERE'])) { $sql .= ' WHERE '; $sql .= implode(' ', $this->_parts['WHERE']); } // GROUP BY if (!empty($this->_parts['GROUP'])) { $sql .= ' GROUP BY '; $sql .= join(', ', array_map(array($this->_db, 'quoteIdentifier'), $this->_parts['GROUP'])); } // HAVING if (!empty($this->_parts['HAVING'])) { $sql .= ' HAVING ' . implode(' ', $this->_parts['HAVING']); } // ORDER BY if (!empty($this->_parts['ORDER'])) { $sql .= ' ORDER BY '; $orders = array(); foreach ($this->_parts['ORDER'] as $column => $dir) { $orders[] = $this->_db->quoteIdentifier($column) . ' ' . $dir; } $sql .= join(', ', $orders); } // LIMIT if (!empty($this->_parts['LIMIT_COUNT']) && empty($this->_parts['LIMIT_OFFSET'])) { $sql .= ' LIMIT ' . (int) $this->_parts['LIMIT_COUNT']; } elseif (!empty($this->_parts['LIMIT_COUNT'])) { $sql .= ' LIMIT ' . (int) $this->_parts['LIMIT_OFFSET'] . ', ' . (int) $this->_parts['LIMIT_COUNT']; } return $sql; }
public function setUp() { $options = array('dbname' => 'goez_test', 'host' => '127.0.0.1', 'username' => 'www', 'password' => '123456'); $this->_db = \Goez\Db::factory('mysql', $options); $this->_insertName = 'insert' . time(); }
/** * @expectedException Exception */ public function testFactoryFail() { $db = \Goez\Db::factory(null); // Exception }