Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the SQL generator able to only output specific kinds of data #102

Merged
merged 1 commit into from Mar 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Modyllic/Generator/PlainSQL.php
Expand Up @@ -15,6 +15,7 @@ function ignore_index( $index ) {
}

function create_sqlmeta() {}
function drop_sqlmeta() {}
function insert_meta($kind,$which,array $what) {}
function delete_meta($kind,$which) {}
function update_meta($kind,$which,$what) {}
Expand Down
168 changes: 128 additions & 40 deletions Modyllic/Generator/SQL.php
Expand Up @@ -11,59 +11,123 @@

class Modyllic_Generator_SQL {

static function schema_types() {
return array('database','sqlmeta','tables','views','routines','events','triggers');
}

static function validate_schema_types(array $what) {
$diff = array_diff($what, self::schema_types());
if ( count($diff) ) {
throw new Exception("Unknown kind of SQL schema element: ".implode(", ",$diff));
}
}

// ALTER

function alter_sql( Modyllic_Diff $diff, $delim=";;", $sep=TRUE ) {
$this->alter( $diff );
function alter_sql( Modyllic_Diff $diff, $delim=";;", $sep=TRUE, array $what = null ) {
if ( ! isset($what) ) {
$what = self::schema_types();
}
self::validate_schema_types($what);
$this->alter( $diff, $what );
return $this->sql_document( $delim, $sep );
}

function alter( Modyllic_Diff $diff ) {
function alter( Modyllic_Diff $diff, array $what ) {
if ( ! $diff->changeset->has_changes() ) {
$this->cmd("-- No changes detected.");
return $this;
}
$this->alter_database( $diff->changeset->schema );

if ( $diff->changeset->create_sqlmeta ) {
if ( in_array('database',$what) ) {
$this->alter_database( $diff->changeset->schema );
}
if ( in_array('sqlmeta',$what) and $diff->changeset->create_sqlmeta ) {
$this->create_sqlmeta();
}

$this->drop_triggers( $diff->changeset->remove['triggers'] );
$this->drop_events( $diff->changeset->remove['events'] );
$this->drop_routines( $diff->changeset->remove['routines'] );
$this->drop_views( $diff->changeset->remove['views'] );
$this->drop_tables( $diff->changeset->remove['tables'] );
if ( in_array('triggers',$what) ) {
$this->drop_triggers( $diff->changeset->remove['triggers'] );
}
if ( in_array('events',$what) ) {
$this->drop_events( $diff->changeset->remove['events'] );
}
if ( in_array('routines',$what) ) {
$this->drop_routines( $diff->changeset->remove['routines'] );
}
if ( in_array('views',$what) ) {
$this->drop_views( $diff->changeset->remove['views'] );
}
if ( in_array('tables',$what) ) {
$this->drop_tables( $diff->changeset->remove['tables'] );
}

$this->create_tables( $diff->changeset->add['tables'], $diff->changeset->schema );
$this->create_views( $diff->changeset->add['views'] );
$this->create_routines( $diff->changeset->add['routines'] );
$this->create_events( $diff->changeset->add['events'] );
$this->create_triggers( $diff->changeset->add['triggers'] );
if ( in_array('tables',$what) ) {
$this->create_tables( $diff->changeset->add['tables'], $diff->changeset->schema );
}
if ( in_array('views',$what) ) {
$this->create_views( $diff->changeset->add['views'] );
}
if ( in_array('routines',$what) ) {
$this->create_routines( $diff->changeset->add['routines'] );
}
if ( in_array('events',$what) ) {
$this->create_events( $diff->changeset->add['events'] );
}
if ( in_array('triggers',$what) ) {
$this->create_triggers( $diff->changeset->add['triggers'] );
}

$this->alter_tables( $diff->changeset->update['tables'] );
$this->alter_views( $diff->changeset->update['views'] );
$this->alter_routines( $diff->changeset->update['routines'] );
$this->alter_events( $diff->changeset->update['events'] );
$this->alter_triggers( $diff->changeset->update['triggers'] );
if ( in_array('tables',$what) ) {
$this->alter_tables( $diff->changeset->update['tables'] );
}
if ( in_array('views',$what) ) {
$this->alter_views( $diff->changeset->update['views'] );
}
if ( in_array('routines',$what) ) {
$this->alter_routines( $diff->changeset->update['routines'] );
}
if ( in_array('events',$what) ) {
$this->alter_events( $diff->changeset->update['events'] );
}
if ( in_array('triggers',$what) ) {
$this->alter_triggers( $diff->changeset->update['triggers'] );
}
return $this;
}

// CREATE

function create_sql( Modyllic_Schema $schema, $delim=";;", $sep=TRUE ) {
$this->create( $schema );
function create_sql( Modyllic_Schema $schema, $delim=";;", $sep=TRUE, array $what=null ) {
if ( ! isset($what) ) {
$what = self::schema_types();
}
self::validate_schema_types($what);
$this->create( $schema, $what );
return $this->sql_document( $delim, $sep );
}

function create( Modyllic_Schema $schema, $delim=";;", $sep=TRUE ) {
$this->create_database( $schema );
$this->create_sqlmeta();
$this->create_tables( $schema->tables, $schema );
$this->create_views( $schema->views );
$this->create_routines( $schema->routines );
$this->create_events( $schema->events );
$this->create_triggers( $schema->triggers );
function create( Modyllic_Schema $schema, array $what ) {
if ( in_array('database',$what) ) {
$this->create_database( $schema );
}
if ( in_array('sqlmeta',$what) ) {
$this->create_sqlmeta();
}
if ( in_array('tables',$what) ) {
$this->create_tables( $schema->tables, $schema );
}
if ( in_array('views',$what) ) {
$this->create_views( $schema->views );
}
if ( in_array('routines',$what) ) {
$this->create_routines( $schema->routines );
}
if ( in_array('events',$what) ) {
$this->create_events( $schema->events );
}
if ( in_array('triggers',$what) ) {
$this->create_triggers( $schema->triggers );
}
return $this;
}

Expand All @@ -82,24 +146,48 @@ function create_sqlmeta() {
$this->extend(") ENGINE=MyISAM");
$this->end_cmd();
}


// DROP

function drop_sql( Modyllic_Schema $schema, $delim=";", $sep=FALSE ) {
$this->drop($schema);
function drop_sql( Modyllic_Schema $schema, $delim=";", $sep=FALSE, array $what = null ) {
if ( ! isset($what) ) {
$what = self::schema_types();
}
self::validate_schema_types($what);
$this->drop($schema,$what);
return $this->sql_document( $delim, $sep );
}

function drop( Modyllic_Schema $schema ) {
$this->drop_triggers( $schema->triggers );
$this->drop_events( $schema->events );
$this->drop_routines( $schema->routines );
$this->drop_views( $schema->views );
$this->drop_tables( $schema->tables );
$this->drop_database( $schema );
function drop( Modyllic_Schema $schema, array $what ) {
if ( in_array('triggers',$what) ) {
$this->drop_triggers( $schema->triggers );
}
if ( in_array('events',$what) ) {
$this->drop_events( $schema->events );
}
if ( in_array('routines',$what) ) {
$this->drop_routines( $schema->routines );
}
if ( in_array('views',$what) ) {
$this->drop_views( $schema->views );
}
if ( in_array('tables',$what) ) {
$this->drop_tables( $schema->tables );
}
if ( in_array('sqlmeta',$what) and $schema->sqlmeta_exists ) {
$this->drop_sqlmeta();
}
if ( in_array('database',$what) ) {
$this->drop_database( $schema );
}
return $this;
}

function drop_sqlmeta() {
$this->cmd('DROP TABLE SQLMETA');
}

// DATABASE

function create_database($schema) {
Expand Down
20 changes: 18 additions & 2 deletions scripts/sqldiff
Expand Up @@ -10,6 +10,7 @@
require_once "Modyllic/Commandline.php";
require_once "Modyllic/Diff.php";
require_once "Modyllic/Generator.php";
require_once "Modyllic/Generator/SQL.php";

$args = Modyllic_Commandline::getArgs(array(
'description' => 'Generate a PHP helper class for the stored procs in the schema',
Expand All @@ -29,12 +30,27 @@ $args = Modyllic_Commandline::getArgs(array(
'long_name' => '--dialect',
'description' => 'dialect to output in',
'action' => 'Dialect',
'default' => 'Modyllic_Generator_NativeSQL' ) ),
'default' => 'Modyllic_Generator_NativeSQL',
),
'only' => array(
'long_name' => '--only',
'description' => 'comma separated list of kinds of schema objects to process: database, sqlmeta, tables, views, routines, events, triggers',
'action' => 'StoreString',
'default' => implode(",",Modyllic_Generator_SQL::schema_types())
) ),
'arguments' => array(
'fromschema' => array('optional'=>true),
'toschema' => array('optional'=>true)
)));

try {
$only = preg_split('/\s*,\s*/',$args->options['only']);
Modyllic_Generator_SQL::validate_schema_types($only);
}
catch (Exception $e) {
Modyllic_Commandline::displayError($e->getMessage());
}

if ( (isset($args->options['fromschema']) or isset($args->options['toschema'])) and isset($args->args['fromschema']) ) {
Modyllic_Commandline::displayError( "You can't specify a schema both positionally and via options" );
}
Expand Down Expand Up @@ -74,4 +90,4 @@ if ( ! $diff->changeset->has_changes() ) {
$class = Modyllic_Generator::dialectToClass( $args->options['dialect'] );
$gen = new $class();

print $gen->alter_sql($diff);
print $gen->alter_sql($diff, ';;', true, $only);
20 changes: 18 additions & 2 deletions scripts/sqldrop
Expand Up @@ -9,6 +9,7 @@

require_once "Modyllic/Commandline.php";
require_once "Modyllic/Generator.php";
require_once "Modyllic/Generator/SQL.php";

$args = Modyllic_Commandline::getArgs(array(
'description' => 'Display drop statements for a schema',
Expand All @@ -18,7 +19,14 @@ $args = Modyllic_Commandline::getArgs(array(
'long_name' => '--dialect',
'description' => 'dialect to output in',
'action' => 'Dialect',
'default' => 'Modyllic_Generator_NativeSQL' ) ),
'default' => 'Modyllic_Generator_NativeSQL',
),
'only' => array(
'long_name' => '--only',
'description' => 'comma separated list of kinds of schema objects to process: database, sqlmeta, tables, views, routines, events, triggers',
'action' => 'StoreString',
'default' => implode(",",Modyllic_Generator_SQL::schema_types())
) ),
'arguments' => array(
'spec' => array('multiple'=>true) )));

Expand All @@ -27,4 +35,12 @@ $schema = Modyllic_Commandline::schema($args->args['spec']);
$class = Modyllic_Generator::dialectToClass( $args->options['dialect'] );
$gen = new $class();

print $gen->drop_sql( $schema );
try {
$only = preg_split('/\s*,\s*/',$args->options['only']);
Modyllic_Generator_SQL::validate_schema_types($only);
}
catch (Exception $e) {
Modyllic_Commandline::displayError($e->getMessage());
}

print $gen->drop_sql( $schema, ';;', true, $only );
20 changes: 18 additions & 2 deletions scripts/sqldump
Expand Up @@ -9,6 +9,7 @@

require_once "Modyllic/Commandline.php";
require_once "Modyllic/Generator.php";
require_once "Modyllic/Generator/SQL.php";

$args = Modyllic_Commandline::getArgs(array(
'description' => 'Load one or more schema and print them out',
Expand All @@ -18,7 +19,14 @@ $args = Modyllic_Commandline::getArgs(array(
'long_name' => '--dialect',
'description' => 'dialect to output in',
'action' => 'Dialect',
'default' => 'Modyllic_Generator_NativeSQL' ) ),
'default' => 'Modyllic_Generator_NativeSQL',
),
'only' => array(
'long_name' => '--only',
'description' => 'comma separated list of kinds of schema objects to process: database, sqlmeta, tables, views, routines, events, triggers',
'action' => 'StoreString',
'default' => implode(",",Modyllic_Generator_SQL::schema_types())
) ),
'arguments' => array(
'spec' => array('multiple'=>true) )));

Expand All @@ -27,4 +35,12 @@ $schema = Modyllic_Commandline::schema($args->args['spec']);
$class = Modyllic_Generator::dialectToClass( $args->options['dialect'] );
$gen = new $class();

print $gen->create_sql( $schema );
try {
$only = preg_split('/\s*,\s*/',$args->options['only']);
Modyllic_Generator_SQL::validate_schema_types($only);
}
catch (Exception $e) {
Modyllic_Commandline::displayError($e->getMessage());
}

print $gen->create_sql( $schema, ';;', true, $only );
2 changes: 1 addition & 1 deletion test/generator/Triggers.t
Expand Up @@ -78,7 +78,7 @@ require_ok("Modyllic/Diff.php");
$diff = new Modyllic_Diff($schema1,$schema2);

$gen = new Modyllic_Generator_SQL();
$sql = $gen->alter( $diff )->sql_commands();
$sql = $gen->alter( $diff, array('triggers') )->sql_commands();

is( count($sql), 2, "Diff requires two SQL commands" );

Expand Down