function guessDbPrefix(DbSimple_Interface $db, $database = null, $prefix = null) { $res = array(); foreach ($dbs = $db->selectCol("SHOW DATABASES") as $dbname) { try { $tables = $db->selectCol("SHOW TABLES FROM ?# LIKE '%{$this->guessTablePattern}'", $dbname); } catch (Am_Exception_Db $e) { continue; } if (is_array($tables)) { foreach ($tables as $t) { // check fields here $info = $db->select("SHOW COLUMNS FROM `{$dbname}`.{$t}"); $infostr = ""; if (is_array($info)) { foreach ($info as $k => $v) { $infostr .= join(';', $v) . "\n"; } } $wrong = 0; foreach ($this->guessFieldsPattern as $pat) { if (!preg_match('|^' . $pat . '|m', $infostr)) { $wrong++; } } if ($wrong) { continue; } $res[] = $dbname . '.' . substr($t, 0, -strlen($this->guessTablePattern)); } } } return $res; }
public function parseTables(DbSimple_Interface $db) { $prefix = $db->getPrefix(); foreach ($db->selectCol("SHOW TABLES LIKE ?", $prefix . '%') as $tablename) { if (strlen($prefix) && strpos($tablename, $prefix) !== 0) { continue; } // other prefix? $name = substr($tablename, strlen($prefix)); $table = Am_DbSync_Table::createFromDb($name, $db->select("SHOW CREATE TABLE ?#", $tablename)); foreach ($db->select("DESCRIBE ?#", $tablename) as $row) { $table->addField(Am_DbSync_Field::createFromDb($row)); } $indexes = array(); foreach ($db->select("SHOW INDEX FROM ?#", $tablename) as $row) { $indexes[$row['Key_name']][] = $row; } foreach ($indexes as $indexRows) { $table->addIndex(Am_DbSync_Index::createFromDb($indexRows)); } $this->addTable($table); } }