Browse Source

Added ability to set tables a temporary in the schema collection

Jose Lorenzo Rodriguez 12 years ago
parent
commit
67be765896

+ 2 - 1
src/Database/Schema/MysqlSchema.php

@@ -232,7 +232,8 @@ class MysqlSchema extends BaseSchema {
  */
 	public function createTableSql(Table $table, $columns, $constraints, $indexes) {
 		$content = implode(",\n", array_merge($columns, $constraints, $indexes));
-		$content = sprintf("CREATE TABLE `%s` (\n%s\n)", $table->name(), $content);
+		$temporary = $table->temporary() ? ' TEMPORARY ' : ' ';
+		$content = sprintf("CREATE%sTABLE `%s` (\n%s\n)", $temporary, $table->name(), $content)
 		$options = $table->options();
 		if (isset($options['engine'])) {
 			$content .= sprintf(' ENGINE=%s', $options['engine']);

+ 2 - 1
src/Database/Schema/PostgresSchema.php

@@ -419,8 +419,9 @@ class PostgresSchema extends BaseSchema {
 		$content = array_merge($columns, $constraints);
 		$content = implode(",\n", array_filter($content));
 		$tableName = $this->_driver->quoteIdentifier($table->name());
+		$temporary = $table->temporary() ? ' TEMPORARY ' : ' ';
 		$out = [];
-		$out[] = sprintf("CREATE TABLE %s (\n%s\n)", $tableName, $content);
+		$out[] = sprintf("CREATE%sTABLE %s (\n%s\n)", $temporary, $tableName, $content);
 		foreach ($indexes as $index) {
 			$out[] = $index;
 		}

+ 2 - 1
src/Database/Schema/SqliteSchema.php

@@ -342,7 +342,8 @@ class SqliteSchema extends BaseSchema {
 	public function createTableSql(Table $table, $columns, $constraints, $indexes) {
 		$lines = array_merge($columns, $constraints);
 		$content = implode(",\n", array_filter($lines));
-		$table = sprintf("CREATE TABLE \"%s\" (\n%s\n)", $table->name(), $content);
+		$temporary = $table->temporary() ? ' TEMPORARY ' : ' ';
+		$table = sprintf("CREATE%sTABLE \"%s\" (\n%s\n)", $temporary, $table->name(), $content);
 		$out = [$table];
 		foreach ($indexes as $index) {
 			$out[] = $index;

+ 21 - 0
src/Database/Schema/Table.php

@@ -66,6 +66,13 @@ class Table {
 	protected $_options = [];
 
 /**
+ * Whether or not the table is temporary
+ *
+ * @var boolean
+ */
+	protected $_temporary = false;
+
+/**
  * The valid keys that can be used in a column
  * definition.
  *
@@ -499,6 +506,20 @@ class Table {
 	}
 
 /**
+ * Get/Set whether the table is temporrary in the database
+ *
+ * @param boolean|null $set whether or not the table is to be temporary
+ * @return this|boolean Either the table instance, the current temporary setting
+ */
+	public function temporary($set = null) {
+		if ($set == null) {
+			return $this->_temporary;
+		}
+		$this->_temporary = (bool)$set;
+		return $this;
+	}
+
+/**
  * Generate the SQL to create the Table.
  *
  * Uses the connection to access the schema dialect