Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

DDL support for Zend\Db #4311

Closed
wants to merge 6 commits into from
Closed

DDL support for Zend\Db #4311

wants to merge 6 commits into from

Conversation

ralphschindler
Copy link
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`

@@ -0,0 +1,175 @@
<?php

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ...

@tca3
Copy link
Contributor

tca3 commented Apr 25, 2013

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
Copy link
Member Author

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

}

$newSpec = str_replace('...', implode(', ', $newSpecParts), $this->specification);
//var_dump($newSpec, $newSpecTypes);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to remove that :).

Ralph Schindler added 3 commits April 29, 2013 15:48
@ghost ghost assigned weierophinney May 1, 2013
weierophinney added a commit that referenced this pull request May 1, 2013
weierophinney added a commit that referenced this pull request May 1, 2013
- 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
weierophinney added a commit that referenced this pull request May 1, 2013
@weierophinney
Copy link
Member

Merged to develop for 2.2.0.

@ramunasd
Copy link

ramunasd commented Jul 3, 2013

What about simple non-unique column/s index?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants