public function createTable($name, array $columns, $primaryKey=null)
    {
        $name = strtolower($name);
        if (strpos($name, 'sqlite_') === 0) {
            throw new \InvalidArgumentException('Created table name must not begin with sqlite_');
        }
        if (empty($columns)) {
            throw new \InvalidArgumentException('No columns specified for new table');
        }
        if ($primaryKey !== null and !is_array($primaryKey)) {
            $primaryKey = array($primaryKey);
        }
        $numAutoIncrement = 0;
        $autoPk = null;
        foreach ($columns as &$column) {
            if (is_array($column)) {
                $column = $this->createColumnFromArray($column);
            }
            if ($column->getAutoIncrement()) {
                $numAutoIncrement++;
                $autoPk = $column->getName();
            }
        }
        unset($column);
        if ($numAutoIncrement > 1) {
            throw new \InvalidArgumentException(
                'More than 1 autoincrement field specified, given: ' . $numAutoIncrement
            );
        }
        if ($autoPk) {
            if ($primaryKey === null) {
                $primaryKey = array($autoPk);
            } elseif (count($primaryKey) != 1 or $autoPk != $primaryKey[0]) {
                throw new \InvalidArgumentException('Invalid primary key: ' . implode(',', $primaryKey));
            }
        }
        if (!$primaryKey) {
            throw new \InvalidArgumentException('Missing primary key for table ' . $name);
        }
        if (in_array($name, $this->getTableNames())) {
            throw new \RuntimeException('Table already exists: ' . $name);
        }

        $colspecs = array();
        foreach ($columns as $column) {
            $colspecs[] = $this->prepareIdentifier($column->getName()) . ' ' . $column->getDefinition();
        }

        $sql = 'CREATE TABLE ' . $this->prepareIdentifier($name) . " (\n";
        $sql .= implode(",\n", $colspecs);
        $sql .= $this->_getTablePkDeclaration($primaryKey, (bool) $numAutoIncrement);
        $sql .= "\n)";

        $this->exec($sql);
        return $this->getTable($name);
    }
public function createTable($name, array $columns, $primaryKey=null)
    {
        $name = strtolower($name);
        if (strpos($name, 'sqlite_') === 0) {
            throw new \InvalidArgumentException('Created table name must not begin with sqlite_');
        }
        if (empty($columns)) {
            throw new \InvalidArgumentException('No columns specified for new table');
        }
        if ($primaryKey !== null and !is_array($primaryKey)) {
            $primaryKey = array($primaryKey);
        }
        $numAutoIncrement = 0;
        $autoPk = null;
        foreach ($columns as &$column) {
            if (is_array($column)) {
                $column = $this->createColumnFromArray($column);
            }
            if ($column->getAutoIncrement()) {
                $numAutoIncrement++;
                $autoPk = $column->getName();
            }
        }
        unset($column);
        if ($numAutoIncrement > 1) {
            throw new \InvalidArgumentException(
                'More than 1 autoincrement field specified, given: ' . $numAutoIncrement
            );
        }
        if ($autoPk) {
            if ($primaryKey === null) {
                $primaryKey = array($autoPk);
            } elseif (count($primaryKey) != 1 or $autoPk != $primaryKey[0]) {
                throw new \InvalidArgumentException('Invalid primary key: ' . implode(',', $primaryKey));
            }
        }
        if (!$primaryKey) {
            throw new \InvalidArgumentException('Missing primary key for table ' . $name);
        }
        if (in_array($name, $this->getTableNames())) {
            throw new \RuntimeException('Table already exists: ' . $name);
        }

        $colspecs = array();
        foreach ($columns as $column) {
            $colspecs[] = $this->prepareIdentifier($column->getName()) . ' ' . $column->getDefinition();
        }

        $sql = 'CREATE TABLE ' . $this->prepareIdentifier($name) . " (\n";
        $sql .= implode(",\n", $colspecs);
        $sql .= $this->_getTablePkDeclaration($primaryKey, (bool) $numAutoIncrement);
        $sql .= "\n)";

        $this->exec($sql);
        return $this->getTable($name);
    }
public function createTable ( $ name , array $ columns , $ primaryKey = null ) { $ name = strtolower ( $ name ) ; if ( strpos ( $ name , 'sqlite_' ) === 0 ) { throw new \ InvalidArgumentException ( 'Created table name must not begin with sqlite_' ) ; } if ( empty ( $ columns ) ) { throw new \ InvalidArgumentException ( 'No columns specified for new table' ) ; } if ( $ primaryKey !== null and ! is_array ( $ primaryKey ) ) { $ primaryKey = array ( $ primaryKey ) ; } $ numAutoIncrement = 0 ; $ autoPk = null ; foreach ( $ columns as & $ column ) { if ( is_array ( $ column ) ) { $ column = $ this -> createColumnFromArray ( $ column ) ; } if ( $ column -> getAutoIncrement ( ) ) { $ numAutoIncrement ++ ; $ autoPk = $ column -> getName ( ) ; } } unset ( $ column ) ; if ( $ numAutoIncrement > 1 ) { throw new \ InvalidArgumentException ( 'More than 1 autoincrement field specified, given: ' . $ numAutoIncrement ) ; } if ( $ autoPk ) { if ( $ primaryKey === null ) { $ primaryKey = array ( $ autoPk ) ; } elseif ( count ( $ primaryKey ) != 1 or $ autoPk != $ primaryKey [ 0 ] ) { throw new \ InvalidArgumentException ( 'Invalid primary key: ' . implode ( ',' , $ primaryKey ) ) ; } } if ( ! $ primaryKey ) { throw new \ InvalidArgumentException ( 'Missing primary key for table ' . $ name ) ; } if ( in_array ( $ name , $ this -> getTableNames ( ) ) ) { throw new \ RuntimeException ( 'Table already exists: ' . $ name ) ; } $ colspecs = array ( ) ; foreach ( $ columns as $ column ) { $ colspecs [ ] = $ this -> prepareIdentifier ( $ column -> getName ( ) ) . ' ' . $ column -> getDefinition ( ) ; } $ sql = 'CREATE TABLE ' . $ this -> prepareIdentifier ( $ name ) . " (\n" ; $ sql .= implode ( ",\n" , $ colspecs ) ; $ sql .= $ this -> _getTablePkDeclaration ( $ primaryKey , ( bool ) $ numAutoIncrement ) ; $ sql .= "\n)" ; $ this -> exec ( $ sql ) ; return $ this -> getTable ( $ name ) ; }
Create a table

Some constraints are checked before the creation is attempted:

- There can be at most 1 autoincrement column.
- If there is an autoincrement column, it must be the primary key.
- Otherwise, $primaryKey must not be empty.
- Table names must not begin with sqlite_. For maximum compatibility,
this constraint is enforced for all DBMS, not only for SQLite.

The $primaryKey value can be omitted for tables with an autoincrement
column because the autoincrement column is automatically made the primary
key.

@param string $name Table name
@param array $columns Array of column objects, created via createColumn(), or associative arrays
@param string|array $primaryKey Primary key (column name or aray of column names)
@return \Nada\Table\AbstractTable A representation of the created table
@throws \InvalidArgumentException if $columns is empty or constraints are violated
@throws \RuntimeException if the table already exists
Create a table