/** * Copy single table * * @param type $table */ public function copyTable($table) { // disable triggers echo CLI::dotFill('copy ' . $table, DOT_FILL); $res = $this->db->query("ALTER TABLE public.{$table} DISABLE TRIGGER ALL;"); if (!$res) { echo CLI::color("fail", red); throw new \Exception("Error during disable triggers"); } $columns = array(); $values = array(); $q = "SELECT c1.column_name AS column " . ", ( c1.is_nullable = 'YES' AND c2.is_nullable = 'NO' )::integer as became_not_null" . ", c1.data_type as old_data_type" . ", c2.data_type as data_type" . ", c2.column_default as column_default" . " FROM information_schema.columns c1 " . "JOIN information_schema.columns c2 ON ( c1.table_name = c2.table_name AND c1.column_name = c2.column_name ) " . "WHERE c1.table_name = '{$table}' AND c1.table_schema = '{$this->schema}' AND c2.table_schema = 'public';"; $res = $this->db->query($q); if (!$res) { echo CLI::color("fail", red); throw new \Exception("Error during select columns"); } foreach ($res as $row) { $columns[] = '"' . $row['column'] . '"'; $val = '"' . $row['column'] . '"'; if ($row['old_data_type'] != $row['data_type']) { $val .= '::' . $row['data_type']; } if ($row['became_not_null'] > 0) { $coalesce = $row['column_default']; if (strlen($coalesce) <= 0) { $coalesce = "''"; if ($row['data_type'] == 'integer') { $coalesce = "0"; } if ($row['data_type'] == 'numeric') { $coalesce = "0"; } if ($row['data_type'] == 'tsvector') { $coalesce = "to_tsvector(''::text)"; } } $val = 'COALESCE( ' . $val . ', ' . $coalesce . ' )'; } $values[] = $val; } $q = "SELECT column_name, data_type FROM information_schema.columns c WHERE table_schema = 'public' AND table_name = '{$table}' " . "AND NOT EXISTS ( SELECT * FROM information_schema.columns WHERE table_schema = '{$this->schema}' AND table_name = '{$table}' AND column_name = c.column_name ) " . "AND is_nullable = 'NO' AND column_default IS NULL;"; $res = $this->db->query($q); if (!$res) { echo CLI::color("fail", red); throw new \Exception("Error during select data types"); } foreach ($res as $row) { $type = $row['data_type']; $value = "''"; if ($type == 'integer') { $value = "0"; } if ($type == 'numeric') { $value = "0"; } if ($type == 'tsvector') { $value = "to_tsvector(''::text)"; } $columns[] = '"' . $row['column_name'] . '"'; $values[] = $value; } $columns_list = implode(", ", $columns); $values_list = implode(", ", $values); $q = "INSERT INTO public.{$table} ( {$columns_list} ) SELECT {$values_list} FROM {$this->schema}.{$table};"; $res = $this->db->query($q); if (!$res) { echo CLI::color("fail", red); throw new \Exception("Error during insert"); } $res = $this->db->query("ALTER TABLE public.{$table} ENABLE TRIGGER ALL;"); if (!$res) { echo CLI::color("fail", red); throw new \Exception("Error during enable trigger"); } echo CLI::color("done", green); echo "\n"; }
if (is_array($config['inserts_dir'])) { $inserts = array(); foreach ($config['inserts_dir'] as $insert) { $inserts = array_merge($inserts, glob($insert . DIRECTORY_SEPARATOR . "**" . DIRECTORY_SEPARATOR . "*.sql"), glob($insert . DIRECTORY_SEPARATOR . "*.sql")); } } else { $inserts = array_merge(glob($config['inserts_dir'] . DIRECTORY_SEPARATOR . "**" . DIRECTORY_SEPARATOR . "*.sql"), glob($config['inserts_dir'] . DIRECTORY_SEPARATOR . "*.sql")); } foreach ($inserts as $insert) { $initiator->addSource($insert); } echo CLI::color("done", green); echo "\n"; } if (key_exists("sequences_dir", $config)) { echo CLI::dotFill('loading sequences', DOT_FILL); if (is_array($config['sequences_dir'])) { $sequences = array(); foreach ($config['sequences_dir'] as $sequence) { $sequences = array_merge($sequences, glob($sequence . DIRECTORY_SEPARATOR . "**" . DIRECTORY_SEPARATOR . "*.sql"), glob($sequence . DIRECTORY_SEPARATOR . "*.sql")); } } else { $sequences = array_merge(glob($config['sequences_dir'] . DIRECTORY_SEPARATOR . "**" . DIRECTORY_SEPARATOR . "*.sql"), glob($config['sequences_dir'] . DIRECTORY_SEPARATOR . "*.sql")); } foreach ($sequences as $sequence) { $initiator->addSource($sequence); } echo CLI::color("done", green); echo "\n"; } // @todo: modyfikatory
if (!in_array($c["driver"], $drivers)) { // Creating schema $count = 0; echo CLI::dotFill('creating schema', DOT_FILL); $manager->createSchema(); echo CLI::color("done", green); echo "\n"; } // Initializing structure echo CLI::dotFill('initializing', DOT_FILL); $manager->initialize(); echo CLI::color("done", green); echo "\n"; if (!in_array($c["driver"], $drivers)) { // Removing old schemas echo CLI::dotFill('removing backups', DOT_FILL); $manager->removeBackups(); echo CLI::color("done", green); echo "\n"; $drivers[] = $c["driver"]; } $result = $manager->db->exec('COMMIT;'); if ($result === false) { $errorInfo = $this->db->errorInfo(); throw new Exception('SQL ERROR: ' . "\n" . $errorInfo[2]); } echo CLI::color("SUCCESS", 'white', 'green'); echo "\n"; } } catch (Exception $e) { if (isset($manager->db)) {
public function generate() { $warning = false; if (!is_dir($this->model_path)) { throw new \Exception("[RepelGenerator] Given model path does not exist, or is not a directory! ({$this->model_path})"); } if (!is_dir($this->base_path)) { mkdir($this->base_path); } else { // Check if proper directory $dh = opendir($this->base_path); $ignore = array('.', '..'); $warning = false; while (false !== ($filename = readdir($dh))) { if (in_array($filename, $ignore)) { continue; } if (preg_match('/^R.*Base.php$/', $filename)) { unlink($this->base_path . $filename); } else { $warning = true; } } } if ($warning) { echo CLI::warning("Warning! Irrelevant files found in base_path!"); } foreach ($this->adapter->getSchemaTables($this->key) as $t) { foreach ($this->adapter->getTables() as $table) { if ($table->name === $t) { echo CLI::dotFill($table->name . ' (' . CLI::color($table->type, dark_gray) . ')', DOT_FILL + 11); $this->clear(); $table_filename = self::getTableName($table->name); $query_filename = self::getQueryName($table->name); $table_base_filename = self::getTableBaseName($table->name); $query_base_filename = self::getQueryBaseName($table->name); if (!file_exists($this->model_path . $table_filename . '.php')) { file_put_contents($this->model_path . $table_filename . '.php', $this->generateTable($table)); } if (!file_exists($this->model_path . $query_filename . '.php')) { file_put_contents($this->model_path . $query_filename . '.php', $this->generateTableQuery($table)); } file_put_contents($this->base_path . $table_base_filename . '.php', $this->generateTableBase($table)); file_put_contents($this->base_path . $query_base_filename . '.php', $this->generateTableQueryBase($table)); echo CLI::color("done", green) . "\n"; } } } }