Skip to content

Commit

Permalink
Modyllic_Table -> Modyllic_Schema_Table
Browse files Browse the repository at this point in the history
  • Loading branch information
iarna committed Apr 24, 2012
1 parent 0e54a96 commit 789554e
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 166 deletions.
4 changes: 2 additions & 2 deletions Modyllic/Diff.php
Expand Up @@ -393,15 +393,15 @@ function __construct() {

/**
* Note that a table was added
* @param Modyllic_Table $table
* @param Modyllic_Schema_Table $table
*/
function add_table( $table ) {
$this->add['tables'][$table->name] = $table;
}

/**
* Note that a table was removed
* @param Modyllic_Table $table
* @param Modyllic_Schema_Table $table
*/
function remove_table( $table ) {
$this->remove['tables'][$table->name] = $table;
Expand Down
2 changes: 1 addition & 1 deletion Modyllic/Generator/MySQL.php
Expand Up @@ -396,7 +396,7 @@ function drop_triggers( $triggers ) {
// TABLE

function table_meta($table) {
if ( $table->static != Modyllic_Table::STATIC_DEFAULT ) {
if ( $table->static != Modyllic_Schema_Table::STATIC_DEFAULT ) {
return array( "static"=>$table->static );
}
else {
Expand Down
2 changes: 1 addition & 1 deletion Modyllic/Parser.php
Expand Up @@ -803,7 +803,7 @@ function cmd_CREATE_TABLE() {
}
$this->get_symbol('(');

$this->ctx = $this->schema->add_table( new Modyllic_Table($table) );
$this->ctx = $this->schema->add_table( new Modyllic_Schema_Table($table) );
$this->ctx->charset = $this->schema->charset;
$this->ctx->collate = $this->schema->collate;
$this->ctx->docs = $this->cmddocs;
Expand Down
163 changes: 2 additions & 161 deletions Modyllic/Schema.php
Expand Up @@ -11,9 +11,7 @@

// Components
require_once "Modyllic/Schema/View.php";
require_once "Modyllic/Schema/Column.php";
require_once "Modyllic/Schema/Index.php";
require_once "Modyllic/Schema/Index/Foreign.php";
require_once "Modyllic/Schema/Table.php";

/**
* A base class for various schema objects. Handles generic things like
Expand Down Expand Up @@ -95,7 +93,7 @@ function merge( $schema ) {
}

/**
* @param Modyllic_Table $table
* @param Modyllic_Schema_Table $table
*/
function add_table( $table ) {
$this->tables[$table->name] = $table;
Expand Down Expand Up @@ -221,163 +219,6 @@ function equal_to( $other ) {
}
}

/**
* A collection of columns, indexes and other information comprising a table
*/
class Modyllic_Table extends Modyllic_Diffable {
public $name;
public $columns = array();
public $indexes = array();
const STATIC_DEFAULT = false;
public $static = self::STATIC_DEFAULT;
public $data = array();
public $last_column;
public $last_index;
public $engine = 'InnoDB';
public $charset = 'utf8';
public $collate = 'utf8_general_ci';
public $docs = "";
/**
* @param string $name
*/
function __construct($name) {
$this->name = $name;
}


/**
* @param Modyllic_Schema_Column $column
*/
function add_column($column) {
if ( isset($this->last_column) ) {
$column->after = $this->last_column->name;
}
$this->last_column = $column;
$this->columns[$column->name] = $column;
return $column;
}
/**
* @param Modyllic_Schema_Index $index
*/
function add_index($index) {
$name = $index->get_name();
if ( isset($this->indexes[$name]) ) {
throw new Exception("In table ".$this->name."- duplicate key name ".$name);
}
$this->indexes[$name] = $index;
$this->last_index = $index;
foreach ($index->columns as $cname=>$value) {
if ( ! isset($this->columns[$cname]) ) {
throw new Exception("In table ".$this->name.", index $name, can't index unknown column $cname");
}
}
// If this is a primary key and has only one column then we'll flag that column as a primary key
if ($index->primary and count($index->columns) == 1) {
$name = current( array_keys($index->columns) );
$len = current( array_values($index->columns) );
// And if there's no length limiter on the column...
if ( $len === false ) {
$this->columns[$name]->is_primary = true;
}
}
return $index;
}

/**
* @param string $prefix Get's an index name
* @returns string
*/
function gen_index_name( $prefix, $always_num=false ) {
$num = 1;
$name = $prefix . ($always_num? "_$num": "");
while ( isset($this->indexes[$name]) or isset($this->indexes['~'.$name]) ) {
$name = $prefix . "_" . ++$num;
}
return $name;
}

/**
* @param Modyllic_Table $other
* @returns bool True if $other is equivalent to $this
*/
function equal_to( $other ) {
if ( $this->name != $other->name ) { return false; }
if ( $this->engine != $other->engine ) { return false; }
if ( $this->charset != $other->charset ) { return false; }
if ( $this->static != $other->static ) { return false; }
if ( count($this->columns) != count($other->columns) ) { return false; }
if ( count($this->indexes) != count($other->indexes) ) { return false; }
foreach ( $this->columns as $key=>&$value ) {
if ( ! $value->equal_to( $other->columns[$key] ) ) { return false; }
}
foreach ( $this->indexes as $key=>&$value ) {
if ( ! $value->equal_to( $other->indexes[$key] ) ) { return false; }
}
return true;
}

/**
* Clears the data associated with this table. Also initializes it,
* allowing data to be inserted into it.
*/
function clear_data() {
$this->data = array();
$this->static = true;
}

/**
* Add a row of data to this table
* @throws Exception when data is not yet initialized.
*/
function add_row( $row ) {
if ( ! $this->static and $this->name != "SQLMETA" ) {
throw new Exception("Cannot add data to ".$this->name.
", not initialized for schema supplied data-- call TRUNCATE first.");
}
foreach ($row as $col_name=>&$value) {
if ( ! isset($this->columns[$col_name]) ) {
throw "INSERT references $col_name in ".$this->name." but $col_name doesn't exist";
}
$col = $this->columns[$col_name];
$norm_value = $col->type->normalize($value);
$row[$col_name] = $norm_value;
}
$this->data[] = $row;
}

/**
* Get the primary key. If there is no primary key then we require all
* columns to uniquely identify a row.
* @returns array
*/
function primary_key() {
foreach ($this->indexes as &$index) {
if ( $index->primary) {
return $index->columns;
break;
}
}
$pk = array();
foreach ($this->columns as $name=>&$col) {
$pk[$name] = false;
}
return $pk;
}

/**
* For a given row, return the key/value pairs needed to match it, based
* on the primary key for this table.
* @returns array
*/
function match_row($row) {
$where = array();
foreach ($this->primary_key() as $key=>$len) {
$where[$key] = @$row[$key];
}
return $where;
}

}

class Modyllic_CodeBody extends Modyllic_Diffable {
public $body = "BEGIN\nEND";
Expand Down
169 changes: 169 additions & 0 deletions Modyllic/Schema/Table.php
@@ -0,0 +1,169 @@
<?php
/**
* Copyright © 2012 Online Buddies, Inc. - All Rights Reserved
*
* @package Modyllic
* @author bturner@online-buddies.com
*/

require_once "Modyllic/Schema/Column.php";
require_once "Modyllic/Schema/Index.php";
require_once "Modyllic/Schema/Index/Foreign.php";

/**
* A collection of columns, indexes and other information comprising a table
*/
class Modyllic_Schema_Table extends Modyllic_Diffable {
public $name;
public $columns = array();
public $indexes = array();
const STATIC_DEFAULT = false;
public $static = self::STATIC_DEFAULT;
public $data = array();
public $last_column;
public $last_index;
public $engine = 'InnoDB';
public $charset = 'utf8';
public $collate = 'utf8_general_ci';
public $docs = "";
/**
* @param string $name
*/
function __construct($name) {
$this->name = $name;
}


/**
* @param Modyllic_Schema_Column $column
*/
function add_column($column) {
if ( isset($this->last_column) ) {
$column->after = $this->last_column->name;
}
$this->last_column = $column;
$this->columns[$column->name] = $column;
return $column;
}
/**
* @param Modyllic_Schema_Index $index
*/
function add_index($index) {
$name = $index->get_name();
if ( isset($this->indexes[$name]) ) {
throw new Exception("In table ".$this->name."- duplicate key name ".$name);
}
$this->indexes[$name] = $index;
$this->last_index = $index;
foreach ($index->columns as $cname=>$value) {
if ( ! isset($this->columns[$cname]) ) {
throw new Exception("In table ".$this->name.", index $name, can't index unknown column $cname");
}
}
// If this is a primary key and has only one column then we'll flag that column as a primary key
if ($index->primary and count($index->columns) == 1) {
$name = current( array_keys($index->columns) );
$len = current( array_values($index->columns) );
// And if there's no length limiter on the column...
if ( $len === false ) {
$this->columns[$name]->is_primary = true;
}
}
return $index;
}

/**
* @param string $prefix Get's an index name
* @returns string
*/
function gen_index_name( $prefix, $always_num=false ) {
$num = 1;
$name = $prefix . ($always_num? "_$num": "");
while ( isset($this->indexes[$name]) or isset($this->indexes['~'.$name]) ) {
$name = $prefix . "_" . ++$num;
}
return $name;
}

/**
* @param Modyllic_Schema_Table $other
* @returns bool True if $other is equivalent to $this
*/
function equal_to( $other ) {
if ( $this->name != $other->name ) { return false; }
if ( $this->engine != $other->engine ) { return false; }
if ( $this->charset != $other->charset ) { return false; }
if ( $this->static != $other->static ) { return false; }
if ( count($this->columns) != count($other->columns) ) { return false; }
if ( count($this->indexes) != count($other->indexes) ) { return false; }
foreach ( $this->columns as $key=>&$value ) {
if ( ! $value->equal_to( $other->columns[$key] ) ) { return false; }
}
foreach ( $this->indexes as $key=>&$value ) {
if ( ! $value->equal_to( $other->indexes[$key] ) ) { return false; }
}
return true;
}

/**
* Clears the data associated with this table. Also initializes it,
* allowing data to be inserted into it.
*/
function clear_data() {
$this->data = array();
$this->static = true;
}

/**
* Add a row of data to this table
* @throws Exception when data is not yet initialized.
*/
function add_row( $row ) {
if ( ! $this->static and $this->name != "SQLMETA" ) {
throw new Exception("Cannot add data to ".$this->name.
", not initialized for schema supplied data-- call TRUNCATE first.");
}
foreach ($row as $col_name=>&$value) {
if ( ! isset($this->columns[$col_name]) ) {
throw "INSERT references $col_name in ".$this->name." but $col_name doesn't exist";
}
$col = $this->columns[$col_name];
$norm_value = $col->type->normalize($value);
$row[$col_name] = $norm_value;
}
$this->data[] = $row;
}

/**
* Get the primary key. If there is no primary key then we require all
* columns to uniquely identify a row.
* @returns array
*/
function primary_key() {
foreach ($this->indexes as &$index) {
if ( $index->primary) {
return $index->columns;
break;
}
}
$pk = array();
foreach ($this->columns as $name=>&$col) {
$pk[$name] = false;
}
return $pk;
}

/**
* For a given row, return the key/value pairs needed to match it, based
* on the primary key for this table.
* @returns array
*/
function match_row($row) {
$where = array();
foreach ($this->primary_key() as $key=>$len) {
$where[$key] = @$row[$key];
}
return $where;
}

}
2 changes: 1 addition & 1 deletion test/General_Functional.t
Expand Up @@ -30,7 +30,7 @@ is( count($schema->tables), 2, "Parsed two table" );
is( count($schema->routines), 0, "Parsed no routines" );

$test_table = $schema->tables['test'];
ok( $test_table instanceOf Modyllic_Table, "We got an Modyllic_Schema_Table object for the table test" );
ok( $test_table instanceOf Modyllic_Schema_Table, "We got an Modyllic_Schema_Table object for the table test" );
is( $test_table->name, "test", "The name attribute got set" );
is( $test_table->engine, "InnoDB", "The default engine got set" );
is( $test_table->charset, "utf8", "The default charset got set" );
Expand Down

0 comments on commit 789554e

Please sign in to comment.