Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit d5622163f8b3c644fee5c5f88af9fa917a574e66 1 parent e4a872a
@iarna iarna authored
View
1  Modyllic/Generator/PlainSQL.php
@@ -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) {}
View
168 Modyllic/Generator/SQL.php
@@ -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;
}
@@ -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) {
View
20 scripts/sqldiff
@@ -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',
@@ -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" );
}
@@ -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);
View
20 scripts/sqldrop
@@ -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',
@@ -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) )));
@@ -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 );
View
20 scripts/sqldump
@@ -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',
@@ -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) )));
@@ -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 );
View
2  test/generator/Triggers.t
@@ -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" );
Please sign in to comment.
Something went wrong with that request. Please try again.