DDL support for Zend\Db #4311

Closed
wants to merge 6 commits into
from

Projects

None yet

7 participants

@ralphschindler
Member

This is initial support for DDL commands in Zend\Db\Sql via an object-oriented abstraction interface. There are many things that would be supported long term.

At current, this component is capable of creating, altering and dropping tables, with columns, constraints and indexes. It has MySQL specialization support. Below is a sample script and its output:


use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Ddl;

$ct = new Ddl\CreateTable();
$ct->setTable('bar');
$ct->addColumn(new Ddl\Column\Integer('id', 12, true, null, ['auto_increment' => true, 'comment' => 'Some comment']));
$ct->addColumn(new Ddl\Column\Varchar('name', 255));
$ct->addColumn(new Ddl\Column\Char('foo', 20));
$ct->addConstraint(new Ddl\Constraint\PrimaryKey('id'));
$ct->addConstraint(new Ddl\Constraint\UniqueKey(['name', 'foo'], 'my_unique_key'));

$sql = new Sql($adapter);
echo $sql->getSqlStringForSqlObject($ct);

echo PHP_EOL . PHP_EOL;

$at = new Ddl\AlterTable('bar');
$at->changeColumn('name', new Ddl\Column\Varchar('new_name', 50));
$at->addColumn(new Ddl\Column\Varchar('another', 255));
$at->addColumn(new Ddl\Column\Varchar('other_id', 255));
$at->dropColumn('foo');
$at->addConstraint(new Ddl\Constraint\ForeignKey('my_fk', 'other_id', 'other_table', 'id', 'CASCADE', 'CASCADE'));
$at->dropConstraint('my_index');
echo $sql->getSqlStringForSqlObject($at);

echo PHP_EOL . PHP_EOL;

$dt = new Ddl\DropTable('bar');
echo $sql->getSqlStringForSqlObject($dt);

echo PHP_EOL . PHP_EOL;

Which results in

CREATE TABLE `bar` (
    `id` INTEGER AUTO_INCREMENT COMMENT 'Some comment',
    `name` VARCHAR(255) NOT NULL,
    `foo` CHAR(20) NOT NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT UNIQUE KEY `my_unique_key` (`name`, `foo`)
)

ALTER TABLE `bar`
ADD COLUMN `another` VARCHAR(255) NOT NULL,
ADD COLUMN `other_id` VARCHAR(255) NOT NULL,
CHANGE COLUMN `name` `new_name` VARCHAR(50) NOT NULL,
DROP COLUMN `foo`,
ADD CONSTRAINT `my_fk` FOREIGN KEY (`other_id`) REFERENCES `other_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
DROP CONSTRAINT `my_index`

DROP TABLE `bar`
@samsonasik samsonasik commented on the diff Apr 25, 2013
library/Zend/Db/Sql/Ddl/AlterTable.php
@@ -0,0 +1,175 @@
+<?php
+
@samsonasik
samsonasik Apr 25, 2013 Contributor

add

/**
  * Zend Framework (http://framework.zend.com/)
  *
  * @link      http://github.com/zendframework/zf2 for the canonical source repository
  * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  * @license   http://framework.zend.com/license/new-bsd New BSD License
  */

here ...

@samsonasik samsonasik commented on an outdated diff Apr 25, 2013
library/Zend/Db/Sql/Ddl/Column/Blob.php
@@ -0,0 +1,8 @@
+<?php
+/**
+ * Created by JetBrains PhpStorm.
+ * User: ralphschindler
+ * Date: 4/17/13
+ * Time: 2:48 PM
+ * To change this template use File | Settings | File Templates.
@samsonasik
samsonasik Apr 25, 2013 Contributor

remove ide specific doc and add :

add

/**
  * Zend Framework (http://framework.zend.com/)
  *
  * @link      http://github.com/zendframework/zf2 for the canonical source repository
  * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  * @license   http://framework.zend.com/license/new-bsd New BSD License
  */

here ...

@samsonasik samsonasik commented on the diff Apr 25, 2013
library/Zend/Db/Sql/Ddl/Column/Boolean.php
@@ -0,0 +1,28 @@
+<?php
+
@samsonasik
samsonasik Apr 25, 2013 Contributor

add

/**
  * Zend Framework (http://framework.zend.com/)
  *
  * @link      http://github.com/zendframework/zf2 for the canonical source repository
  * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  * @license   http://framework.zend.com/license/new-bsd New BSD License
  */

here ...

@samsonasik samsonasik commented on the diff Apr 25, 2013
library/Zend/Db/Sql/Ddl/Column/Char.php
@@ -0,0 +1,40 @@
+<?php
+
@samsonasik
samsonasik Apr 25, 2013 Contributor

add

/**
  * Zend Framework (http://framework.zend.com/)
  *
  * @link      http://github.com/zendframework/zf2 for the canonical source repository
  * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  * @license   http://framework.zend.com/license/new-bsd New BSD License
  */

here ...

@samsonasik samsonasik commented on the diff Apr 25, 2013
library/Zend/Db/Sql/Ddl/Column/Column.php
@@ -0,0 +1,99 @@
+<?php
+
@samsonasik
samsonasik Apr 25, 2013 Contributor

add

/**
  * Zend Framework (http://framework.zend.com/)
  *
  * @link      http://github.com/zendframework/zf2 for the canonical source repository
  * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  * @license   http://framework.zend.com/license/new-bsd New BSD License
  */

here ...

@samsonasik samsonasik commented on the diff Apr 25, 2013
library/Zend/Db/Sql/Ddl/Column/ColumnInterface.php
@@ -0,0 +1,13 @@
+<?php
+
@samsonasik
samsonasik Apr 25, 2013 Contributor

add

/**
  * Zend Framework (http://framework.zend.com/)
  *
  * @link      http://github.com/zendframework/zf2 for the canonical source repository
  * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  * @license   http://framework.zend.com/license/new-bsd New BSD License
  */

here ...

@samsonasik samsonasik commented on the diff Apr 25, 2013
library/Zend/Db/Sql/Ddl/Column/Date.php
@@ -0,0 +1,37 @@
+<?php
+
@samsonasik
samsonasik Apr 25, 2013 Contributor

add

/**
  * Zend Framework (http://framework.zend.com/)
  *
  * @link      http://github.com/zendframework/zf2 for the canonical source repository
  * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  * @license   http://framework.zend.com/license/new-bsd New BSD License
  */

here ...

@ThomasCantonnet
Contributor

This needs a php-cs-fixer ! :)

Also for instance this:

const TABLE = 'table';
const ADD_COLUMNS = 'addColumns';
const CHANGE_COLUMNS = 'changeColumns';
const DROP_COLUMNS = 'dropColumns';
const ADD_CONSTRAINTS = 'addConstraints';
const DROP_CONSTRAINTS = 'dropConstraints';

Should look better like this:

const TABLE             = 'table';
const ADD_COLUMNS       = 'addColumns';
const CHANGE_COLUMNS    = 'changeColumns';
const DROP_COLUMNS      = 'dropColumns';
const ADD_CONSTRAINTS   = 'addConstraints';
const DROP_CONSTRAINTS  = 'dropConstraints';

I didn't go through all your code though. But there are also many unnecessary \n's and there's also a //var_dump() somewhere.

Looks like a nice feature though, looking forward to using it.

@ralphschindler
Member

It's a work in progress, more things are coming, including unit tests, doc blocks, etc.

@bakura10 bakura10 commented on an outdated diff Apr 27, 2013
library/Zend/Db/Sql/Ddl/Constraint/UniqueKey.php
+
+ $values = array();
+ $values[] = ($this->name) ? $this->name . ' ' : '';
+
+ $newSpecTypes = array(self::TYPE_IDENTIFIER);
+
+ $newSpecParts = array();
+
+
+ for ($i = 0; $i < $colCount; $i++) {
+ $newSpecParts[] = '%' . ($i+2) . '$s';
+ $newSpecTypes[] = self::TYPE_IDENTIFIER;
+ }
+
+ $newSpec = str_replace('...', implode(', ', $newSpecParts), $this->specification);
+//var_dump($newSpec, $newSpecTypes);
@bakura10
bakura10 Apr 27, 2013 Contributor

Don't forget to remove that :).

@weierophinney weierophinney was assigned May 1, 2013
@weierophinney weierophinney added a commit that referenced this pull request May 1, 2013
@weierophinney weierophinney [#4311] CS review
- Made sure file-level docblocks were consistent throughout
- Alphabetized constant and property declarations
- Ensured methods were in public -> protected -> private order
- s/$self/this/ in all @return annotations
- Ensured annotations were available and correct for all public methods
- A few minor formatting changes for readability
ef9de8f
@weierophinney weierophinney added a commit that referenced this pull request May 1, 2013
@weierophinney weierophinney Merge branch 'feature/4311' into develop
Close #4311
5118c86
@weierophinney
Member

Merged to develop for 2.2.0.

@tractorcow tractorcow referenced this pull request in tractorcow/silverstripe-zenddb May 24, 2013
Open

TODO Hook up to Zend DDL interface #1

@ramunasd
ramunasd commented Jul 3, 2013

What about simple non-unique column/s index?

@fyrye
fyrye commented on 4a9a428 Oct 27, 2014

Is the expected behavior of Float to actually create a DECIMAL database field?
This may be perceived as an unexpected result by the community.

Only reason I ask here is that DECIMAL exists in both the FloatTest and Float object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment