/** * Creates and returns SQL for creation of the table. * * @return created SQL command */ public static function get_creation_sql($node_schema, $node_table) { if ($node_schema->getName() != 'schema') { throw new exception("node_schema object element name is not schema. check stack for offending caller"); } if ($node_table->getName() != 'table') { throw new exception("node_table object element name is not table. check stack for offending caller"); } if (strlen($node_table['inherits']) > 0) { //@TODO: implement compatibility with pgsql table inheritance dbsteward::error("Skipping table '{$node_table['name']}' because MySQL does not support table inheritance"); return "-- Skipping table '{$node_table['name']}' because MySQL does not support table inheritance"; } $table_name = mysql5::get_fully_qualified_table_name($node_schema['name'], $node_table['name']); $sql = "CREATE TABLE {$table_name} (\n"; $cols = array(); foreach ($node_table->column as $column) { $cols[] = mysql5_column::get_full_definition(dbsteward::$new_database, $node_schema, $node_table, $column, false); } $part_sql = static::get_partition_sql($node_schema, $node_table); $sql .= " " . implode(",\n ", $cols) . "\n)"; $opt_sql = mysql5_table::get_table_options_sql(mysql5_table::get_table_options($node_schema, $node_table)); if (!empty($opt_sql)) { $sql .= "\n" . $opt_sql; } if (strlen($node_table['description']) > 0) { $sql .= "\nCOMMENT " . mysql5::quote_string_value($node_table['description']); } if (!empty($part_sql)) { $sql .= "\n" . $part_sql; } $sql .= ';'; // @TODO: implement column statistics // @TODO: table ownership with $node_table['owner'] ? return $sql; }
private static function get_recreate_table_sql($schema, $table) { $fq_name = mysql5::get_fully_qualified_table_name($schema['name'], $table['name']); $fq_tmp_name = mysql5::get_fully_qualified_table_name($schema['name'], $table['name'] . '_DBSTEWARD_MIGRATION'); // utilize MySQL's CREATE TABLE ... SELECT syntax for cleaner recreation // see: http://dev.mysql.com/doc/refman/5.5/en/create-table-select.html $sql = "CREATE TABLE {$fq_tmp_name}"; $opt_sql = mysql5_table::get_table_options_sql(mysql5_table::get_table_options($schema, $table)); if (!empty($opt_sql)) { $sql .= "\n" . $opt_sql; } if (strlen($table['description']) > 0) { $sql .= "\nCOMMENT " . mysql5::quote_string_value($table['description']); } $sql .= "\nSELECT * FROM {$fq_name};\n"; $sql .= "DROP TABLE {$fq_name};\n"; $sql .= "RENAME TABLE {$fq_tmp_name} TO {$fq_name};"; return $sql; }
public function testEmptyName() { $xml = <<<XML <schema name="public" owner="NOBODY"> <table name="test" primaryKey="a" owner="NOBODY"> <tableOption sqlFormat="mysql5" name="" value="5"/> <column name="a" type="int"/> </table> </schema> XML; $schema = new SimpleXMLElement($xml); try { mysql5_table::get_table_options_sql(mysql5_table::get_table_options($schema, $schema->table)); } catch (Exception $ex) { if (strcasecmp($ex->getMessage(), "tableOption of table public.test cannot have an empty name") !== 0) { throw $ex; } return; } $this->fail("Was expecting empty tableOption name exception, got nothing"); }