Browse Source

Adding schema generation support to DboMysql

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4311 3807eeeb-6ff5-0310-8944-8be069107fe0
nate 19 years ago
parent
commit
53dca9cccc
2 changed files with 107 additions and 7 deletions
  1. 1 0
      cake/basics.php
  2. 106 7
      cake/libs/model/datasources/dbo/dbo_mysql.php

+ 1 - 0
cake/basics.php

@@ -76,6 +76,7 @@
 				}
 			}
 		}
+		return $loadedModels;
 	}
 /**
  * Loads all plugin models.

+ 106 - 7
cake/libs/model/datasources/dbo/dbo_mysql.php

@@ -180,10 +180,11 @@ class DboMysql extends DboSource {
 			}
 			if (isset($column[0])) {
 				$fields[] = array(
-					'name' => $column[0]['Field'],
-					'type' => $this->column($column[0]['Type']),
-					'null' => $column[0]['Null'],
-					'default' => $column[0]['Default']
+					'name'		=> $column[0]['Field'],
+					'type'		=> $this->column($column[0]['Type']),
+					'null'		=> ($column[0]['Null'] == 'YES' ? true : false),
+					'default'	=> $column[0]['Default'],
+					'length'	=> $this->length($column[0]['Type'])
 				);
 			}
 		}
@@ -355,13 +356,13 @@ class DboMysql extends DboSource {
 		}
 
 		$col = r(')', '', $real);
-		$limit = null;
-		@list($col, $limit) = explode('(', $col);
+		$limit = $this->limit($real);
+		@list($col) = explode('(', $col);
 
 		if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
 			return $col;
 		}
-		if ($col == 'tinyint' && $limit == '1') {
+		if ($col == 'tinyint' && $limit == 1) {
 			return 'boolean';
 		}
 		if (strpos($col, 'int') !== false) {
@@ -388,6 +389,22 @@ class DboMysql extends DboSource {
 		return 'text';
 	}
 /**
+ * Gets the length of a database-native column description, or null if no length
+ *
+ * @param string $real Real database-layer column type (i.e. "varchar(255)")
+ * @return int An integer representing the length of the column
+ */
+	function length($real) {
+		$col = r(array(')', 'unsigned'), '', $real);
+		$limit = null;
+		@list($col, $limit) = explode('(', $col);
+
+		if ($limit != null) {
+			return intval($limit);
+		}
+		return null;
+	}
+/**
  * Enter description here...
  *
  * @param unknown_type $results
@@ -447,6 +464,88 @@ class DboMysql extends DboSource {
 		return mysql_client_encoding($this->connection);
 	}
 /**
+ * Generate a MySQL schema for the given Schema object
+ *
+ * @param object $schema An instance of a subclass of CakeSchema
+ * @param string $table Optional.  If specified only the table name given will be generated.
+ *                      Otherwise, all tables defined in the schema are generated.
+ * @return string
+ */
+	function generateSchema($schema, $table = null) {
+		if (!is_a($schema, 'CakeSchema')) {
+			trigger_error(__('Invalid schema object'), E_USER_WARNING);
+			return null;
+		}
+		$out = '';
+
+		foreach ($schema->tables as $curTable => $columns) {
+			if (empty($table) || $table == $curTable) {
+				$out .= 'CREATE TABLE ' . $this->fullTableName($curTable) . " (\n";
+				$colList = array();
+				$primary = null;
+
+				foreach ($columns as $col) {
+					if (isset($col['key']) && $col['key'] == 'primary') {
+						$primary = $col;
+					}
+					$colList[] = $this->generateColumnSchema($col);
+				}
+				if (empty($primary)) {
+					$primary = array('id', 'integer', 'key' => 'primary');
+					array_unshift($colList, $this->generateColumnSchema($primary));
+				}
+				$colList[] = 'PRIMARY KEY (' . $this->name($primary[0]) . ')';
+				$out .= "\t" . join(",\n\t", $colList) . "\n);\n\n";
+			}
+		}
+		return $out;
+	}
+/**
+ * Generate a MySQL-native column schema string
+ *
+ * @param array $column An array structured like the following: array('name', 'type'[, options]),
+ *                      where options can be 'default', 'length', or 'key'.
+ * @return string
+ */
+	function generateColumnSchema($column) {
+		$name = $type = null;
+		$column = am(array('null' => true), $column);
+		list($name, $type) = $column;
+
+		if (empty($name) || empty($type)) {
+			trigger_error('Column name or type not defined in schema', E_USER_WARNING);
+			return null;
+		}
+		if (!isset($this->columns[$type])) {
+			trigger_error("Column type {$type} does not exist", E_USER_WARNING);
+			return null;
+		}
+		$real = $this->columns[$type];
+		$out = $this->name($name) . ' ' . $real['name'];
+
+		if (isset($real['limit']) || isset($real['length'])) {
+			if (isset($col['length'])) {
+				$length = $col['length'];
+			} elseif (isset($real['length'])) {
+				$length = $real['length'];
+			} else {
+				$length = $real['limit'];
+			}
+			$out .= '(' . $length . ')';
+		}
+
+		if (isset($column['key']) && $column['key'] == 'primary') {
+			$out .= ' NOT NULL AUTO_INCREMENT';
+		} elseif (isset($column['default'])) {
+			$out .= ' DEFAULT ' . $this->value($column['default'], $type);
+		} elseif (isset($column['null']) && $column['null'] == true) {
+			$out .= ' DEFAULT NULL';
+		} elseif (isset($column['default']) && isset($column['null']) && $column['null'] == false) {
+			$out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL';
+		}
+		return $out;
+	}
+/**
  * Enter description here...
  *
  * @param unknown_type $schema