getTableName 取得 Table 名稱
public static getTableName ( ) : string | ||
return | string |
/** * create table on db * * @param Pix_Table $table * @access public * @return void */ public function createTable($table) { $sql = "CREATE TABLE " . $this->column_quote($table->getTableName()); $types = array('bigint', 'tinyint', 'int', 'varchar', 'char', 'text', 'float', 'double', 'binary'); foreach ($table->_columns as $name => $column) { $s = $this->column_quote($name) . ' '; $db_type = in_array($column['type'], $types) ? $column['type'] : 'text'; $s .= strtoupper($db_type); if (in_array($db_type, array('varchar', 'char', 'binary'))) { if (!$column['size']) { throw new Exception('you should set the option `size`'); } $s .= '(' . $column['size'] . ')'; } $s .= ' '; if ($column['unsigned']) { $s .= 'UNSIGNED '; } if (isset($column['not-null']) and !$column['not-null']) { $s .= 'NULL '; } else { $s .= 'NOT NULL '; } if (isset($column['default'])) { $s .= 'DEFAULT ' . $this->quoteWithColumn($table, $column['default'], $name) . ' '; } if ($column['auto_increment']) { $s .= 'IDENTITY (1, 1) '; } $column_sql[] = $s; } $s = 'PRIMARY KEY '; $index_columns = array(); foreach (is_array($table->_primary) ? $table->_primary : array($table->_primary) as $pk) { $index_columns[] = $this->column_quote($pk); } $s .= '(' . implode(', ', $index_columns) . ")\n"; $column_sql[] = $s; foreach ($table->getIndexes() as $name => $options) { if ('unique' == $options['type']) { $s = 'UNIQUE KEY ' . $this->column_quote($name) . ' '; } else { $s = 'KEY ' . $this->column_quote($name); } $columns = $options['columns']; $index_columns = array(); foreach ($columns as $column_name) { $index_columns[] = $this->column_quote($column_name); } $s .= '(' . implode(', ', $index_columns) . ') '; $column_sql[] = $s; } $sql .= " (\n" . implode(", \n", $column_sql) . ") \n"; return $this->query($sql, $table); }
/** * dropTable 從資料庫內移除 $table 這個 Table * * @param Pix_Table $table * @access public * @return void */ public function dropTable($table) { if (!Pix_Setting::get('Table:DropTableEnable')) { throw new Pix_Table_Exception("要 DROP TABLE 前請加上 Pix_Setting::set('Table:DropTableEnable', true);"); } $sql = "DROP TABLE " . $this->column_quote($table->getTableName()); return $this->query($sql, $table); }
/** * createTable 將 $table 建立進資料庫內 * * @param Pix_Table $table * @access public * @return void */ public function createTable($table) { $sql = "CREATE TABLE \"" . $table->getTableName() . '"'; $types = array('bigint', 'tinyint', 'int', 'varchar', 'char', 'text', 'float', 'double', 'binary'); $primarys = is_array($table->_primary) ? $table->_primary : array($table->_primary); $pk_isseted = false; foreach ($table->_columns as $name => $column) { $s = $this->column_quote($name) . ' '; $db_type = in_array($column['type'], $types) ? $column['type'] : 'text'; if ($column['unsigned'] and !$column['auto_increment']) { $s .= 'UNSIGNED '; } if ($column['auto_increment']) { $s .= 'SERIAL'; } elseif ('int' == $db_type) { $s .= 'INTEGER'; } elseif ('binary' == $db_type) { $s .= 'BYTEA'; } else { $s .= strtoupper($db_type); } if (in_array($db_type, array('varchar', 'char'))) { if (!$column['size']) { throw new Exception('you should set the option `size`'); } $s .= '(' . $column['size'] . ')'; } $s .= ' '; if ($column['auto_increment']) { if ($primarys[0] != $name or count($primarys) > 1) { throw new Exception('SQLITE 的 AUTOINCREMENT 一定要是唯一的 Primary Key'); } $s .= ' PRIMARY KEY '; $pk_isseted = true; } if (isset($column['default'])) { $s .= 'DEFAULT ' . $this->quoteWithColumn($table, $column['default'], $name) . ' '; } $column_sql[] = $s; } if (!$pk_isseted) { $s = 'PRIMARY KEY '; $index_columns = array(); foreach (is_array($table->_primary) ? $table->_primary : array($table->_primary) as $pk) { $index_columns[] = $this->column_quote($pk); } $s .= '(' . implode(', ', $index_columns) . ")\n"; $column_sql[] = $s; } $sql .= " (\n" . implode(", \n", $column_sql) . ") \n"; // CREATE TABLE $this->query($sql); foreach ($table->getIndexes() as $name => $options) { if ('unique' == $options['type']) { $s = 'CREATE UNIQUE INDEX '; } else { $s = 'CREATE INDEX '; } $columns = $options['columns']; $s .= $this->column_quote($table->getTableName() . '_' . $name) . ' ON ' . $this->column_quote($table->getTableName()); $index_columns = array(); foreach ($columns as $column_name) { $index_columns[] = $this->column_quote($column_name); } $s .= '(' . implode(', ', $index_columns) . ') '; $this->query($s); } }
/** * insertOne 從 db 上增加一筆資料 * * @param Pix_Table $table * @param array|string $keys_values * @access public * @return void */ public function insertOne($table, $keys_values) { if (!is_array($keys_values)) { $keys_values = array(); } $sql = 'INSERT INTO ' . $this->column_quote($table->getTableName()); $keys = $values = array(); foreach ($keys_values as $key => $value) { if (is_null($value)) { continue; } $keys[] = $this->column_quote($key); $values[] = $this->quoteWithColumn($table, $value, $key); } if ($keys) { $sql .= '(' . implode(', ', $keys) . ') VALUES (' . implode(', ', $values) . ')'; } else { $sql .= ' DEFAULT VALUES '; } $this->query($sql); return $this->getLastInsertId($table); }
/** * fetch 取得符合 $search 條件的資料 * * @param Pix_Table $table * @param Pix_Table_Search $search * @param string|array $select_columns * @access public * @return void */ public function fetch($table, $search, $select_columns = '*') { $db = $this->_getDb(); $condictions = $search->getSearchCondictions(); if (count($condictions) == 0) { // 完全沒有條件就是 scan table $options = array(); $options['TableName'] = $table->getTableName(); // 加上指定 column if ('*' != $select_columns) { $options['AttributesToGet'] = $select_columns; } $response = $db->scan($options); } elseif (count($condictions) == 1) { // 只給一個條件的話只能是 HashKey $primary_keys = $table->getPrimaryColumns(); // 只能是 map if ('map' != $condictions[0][0]) { throw new Pix_Table_Exception("不支援的 search 條件"); } // 只能是 Primary Key 的第一個 if ($primary_keys[0] != $condictions[0][1]) { throw new Pix_Table_Exception("不支援的 search 條件"); } $options = array(); $options['TableName'] = $table->getTableName(); $options['HashKeyValue'] = array($this->_getColumnType($table, $primary_keys[0]) => $condictions[0][2]); // 加上 Limit if (!is_null($limit = $search->limit())) { $options['Limit'] = $limit; } // 加上 after or before if ($row = $search->after()) { $options['RangeKeyCondition'] = array('ComparisonOperator' => AmazonDynamoDB::CONDITION_GREATER_THAN, 'AttributeValueList' => array(array($this->_getColumnType($table, $primary_keys[1]) => $row->{$primary_keys[1]}))); } elseif ($row = $search->before()) { $options['RangeKeyCondition'] = array('ComparisonOperator' => AmazonDynamoDB::CONDITION_LESS_THAN, 'AttributeValueList' => array(array($this->_getColumnType($table, $primary_keys[1]) => $row->{$primary_keys[1]}))); $options['ScanIndexForward'] = false; } // 加上指定 column if ('*' != $select_columns) { $options['AttributesToGet'] = $select_columns; } $response = $db->query($options); } else { throw new Pix_Table_Exception("不支援的 search 條件"); } if (200 != $response->status) { throw new Pix_Table_Exception("AmazonDynamoDB: " . $get_response->body->Message); } $ret = array(); foreach ($response->body->Items[0] as $item) { $row = array(); foreach ($table->_columns as $name => $col) { if ($item->{$name}) { $row[$name] = strval($item->{$name}->S); } } $ret[] = $row; } return $ret; }