public function reflectTable(Connection $connection, $tableName) { $columns = array(); $rs = $connection->execute("SHOW COLUMNS FROM {$tableName}"); foreach ($rs as $row) { $options = array(); $name = $row['Field']; if ($row['Type'] == 'tinyint(1)') { $type = Column::BOOLEAN; // TODO : remove this hack !!! } else { preg_match('/^(?P<type>\\w+)(\\((?P<length>\\d+)\\))?$/', $row['Type'], $options); $type = $this->reflectColumnType($options['type']); } $options['nullable'] = $row['Null'] == 'YES' ? true : false; $options['primary_key'] = $row['Key'] == 'PRI' ? true : false; $options['auto_increment'] = preg_match('/auto_increment/i', $row['Extra']) ? true : false; $options['default'] = !empty($row['Default']) ? $row['Default'] : null; $columns[] = new Column($name, $type, $options); } return new Table($tableName, $columns); }
public function reflectTable(Connection $connection, $tableName) { $columns = array(); $rs = $connection->execute("SELECT c.column_name, c.data_type, c.character_maximum_length, c.is_nullable, c.column_default, t.constraint_type" . " FROM information_schema.columns c" . " LEFT JOIN information_schema.key_column_usage k ON c.table_catalog = k.table_catalog" . " AND c.table_name = k.table_name AND c.column_name = k.column_name" . " LEFT JOIN information_schema.table_constraints t ON t.constraint_name = k.constraint_name" . " AND t.constraint_type = 'PRIMARY KEY'" . " WHERE c.table_name = '{$tableName}';"); foreach ($rs as $row) { $options = array(); $name = $row['column_name']; $type = $this->reflectColumnType($row['data_type']); if ($type === Column::STRING) { $options['length'] = $row['character_maximum_length']; } $options['nullable'] = $row['is_nullable'] == 'YES' ? true : false; if (substr($row['column_default'], 0, 7) == 'nextval') { $options['auto_increment'] = true; $options['default'] = null; } else { $options['default'] = !empty($row['Default']) ? $row['Default'] : null; } $options['primary_key'] = $row['constraint_type'] == 'PRIMARY KEY' ? true : false; $columns[] = new Column($name, $type, $options); } return new Table($tableName, $columns); }