static function load($file, $schema) { # A preparsed schemafile will have a .sqlc extension $file_bits = explode(".", $file); array_pop($file_bits); $sqlc_file = implode(".", $file_bits) . ".sqlc"; $sqlc = @stat($sqlc_file); $sql = @stat($file); if (!$sql) { throw new Modyllic_Loader_Exception("{$file}: File not found."); } else { if (!$sqlc or $sqlc[7] == 0 or $sqlc[9] < $sql[9]) { if (($data = @file_get_contents($file)) === false) { throw new Modyllic_Loader_Exception("Error opening {$file}"); } $parser = new Modyllic_Parser(); $parser->partial($schema, $data, $file, ";"); } else { if (($data = @file_get_contents($sqlc_file)) === false) { throw new Modyllic_Loader_Exception("Error opening {$sqlc_file}"); } $subschema = unserialize($data); if ($subschema === FALSE) { throw new Modyllic_Loader_Exception("Error unserializing {$sqlc_file}"); } $schema->merge($subschema); } } }
/** * @returns Modyllic_Schema */ static function load(PDO $dbh, $dbname, $schema) { $dbh->exec("USE information_schema"); $dbschema = self::selectrow($dbh, "SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM SCHEMATA WHERE SCHEMA_NAME=?", array($dbname)); if (!$dbschema) { throw new Exception("Database {$dbname} does not exist"); } $parser = new Modyllic_Parser(); if ($schema->name_is_default) { $schema->set_name($dbschema['SCHEMA_NAME']); $schema->charset = $dbschema['DEFAULT_CHARACTER_SET_NAME']; $schema->collate = $dbschema['DEFAULT_COLLATION_NAME']; } $table_sth = self::query($dbh, "SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA=? AND TABLE_TYPE='BASE TABLE'", array($dbname)); $tables = array(); while ($table_row = $table_sth->fetch(PDO::FETCH_ASSOC)) { Modyllic_Status::$source_count++; $table = self::selectrow($dbh, "SHOW CREATE TABLE " . Modyllic_SQL::quote_ident($dbname) . "." . Modyllic_SQL::quote_ident($table_row['TABLE_NAME'])); $tables[$table_row['TABLE_NAME']] = $table['Create Table']; } $routine_sth = self::query($dbh, "SELECT ROUTINE_TYPE, ROUTINE_NAME FROM ROUTINES WHERE ROUTINE_SCHEMA=?", array($dbname)); $routines = array(); while ($routine = $routine_sth->fetch(PDO::FETCH_ASSOC)) { Modyllic_Status::$source_count++; if ($routine['ROUTINE_TYPE'] == 'PROCEDURE') { $proc = self::selectrow($dbh, "SHOW CREATE PROCEDURE " . Modyllic_SQL::quote_ident($dbname) . "." . Modyllic_SQL::quote_ident($routine['ROUTINE_NAME'])); $routines[$routine['ROUTINE_NAME']] = $proc['Create Procedure']; } else { if ($routine['ROUTINE_TYPE'] == 'FUNCTION') { $func = self::selectrow($dbh, "SHOW CREATE FUNCTION " . Modyllic_SQL::quote_ident($dbname) . "." . Modyllic_SQL::quote_ident($routine['ROUTINE_NAME'])); $routines[$routine['ROUTINE_NAME']] = $func['Create Function']; } else { throw new Exception("Unknown routine type " . $routine['ROUTINE_TYPE'] . " for " . $routine['ROUTINE_NAME']); } } } foreach ($tables as $table_name => $table_sql) { Modyllic_Status::$source_name = "{$dbname}." . $table_name; $parser->partial($schema, $table_sql, "{$dbname}.{$table_name}"); Modyllic_Status::$source_index++; } ksort($schema->tables); foreach ($routines as $routine_name => $routine_sql) { Modyllic_Status::$source_name = "{$dbname}.{$routine_name}"; $parser->partial($schema, $routine_sql, "{$dbname}.{$routine_name}"); Modyllic_Status::$source_index++; } ksort($schema->routines); if (isset($schema->tables['SQLMETA'])) { $table = $schema->tables['SQLMETA']; $meta_sth = self::query($dbh, "SELECT kind,which,value FROM " . Modyllic_SQL::quote_ident($dbname) . ".SQLMETA"); while ($meta = $meta_sth->fetch(PDO::FETCH_ASSOC)) { $table->add_row($meta); } } $schema->load_sqlmeta(); // Look for data to load... foreach ($schema->tables as $name => $table) { if ($table->static) { $data_sth = self::query($dbh, "SELECT * FROM " . Modyllic_SQL::quote_ident($dbname) . "." . Modyllic_SQL::quote_ident($name)); while ($data_row = $data_sth->fetch(PDO::FETCH_ASSOC)) { $table->add_row($data_row); } } } }