public function compile(Database $db) { // Start with the basic syntax. $sql = 'CREATE TABLE ' . $db->quote_table($this->_table['name']); // You are allowed to create a table without any columns. Dont ask me why. if (count($this->_table['columns']) > 0) { // Get ready for the column data. $sql .= ' ('; // Compile the columns in the normal way. foreach ($this->_table['columns'] as $name => $data) { $sql .= Database_Query_Builder::compile_column($data, $db) . ','; } // Compile constraints in a normal way foreach ($this->_table['constraints'] as $name => $data) { $sql .= Database_Query_Builder::compile_constraint($data, $db) . ','; } // Seperate the columns with commars, and add the table constraints at the end. $sql = rtrim($sql, ',') . ') '; } // Process table options foreach ($this->_table['options'] as $key => $option) { $sql .= Database_Query_Builder::compile_statement(array($key => $option)) . ' '; } // Remove the trailing space. return rtrim($sql, ' ') . ';'; }
/** * Compiles a column array into SQL syntax. * * @param Database The active database instance. * @param array The column array. * @return string The SQL syntax. */ public static function compile_column(array $column, Database $db) { // Start with the column name $sql = $db->quote_identifier($column['name']) . ' '; // Compile the datatype $sql .= self::compile_method($column['datatype'], array($db, 'quote')) . ' '; // Compile the column constraints foreach ($column['constraints'] as $name => $data) { // Use the compile statement method to compile the statement $sql .= Database_Query_Builder::compile_statement(array($name => $data), ' ') . ' '; } // Remove the trailing space $sql = rtrim($sql, ' '); // Return the SQL as is. return $sql; }