コード例 #1
0
ファイル: Initiator.php プロジェクト: haifai-labs/repel-orm
 public function initWithConnection()
 {
     foreach ($this->sources as $source) {
         echo $source->file_path . "...";
         if (strlen(trim($source->file_content))) {
             $res = $this->database->exec($source->file_content);
             if ($res === false) {
                 $errorInfo = $this->database->errorInfo();
                 throw new \Exception('SQL ERROR: ' . "\n" . $errorInfo[2]);
             }
             echo CLI::color("done", green);
             echo "\n";
         } else {
             echo " empty.\n";
         }
     }
 }
コード例 #2
0
ファイル: Adapter.php プロジェクト: haifai-labs/repel-orm
 public function generate()
 {
     echo CLI::h2('Generate', HEADER_FILL);
     foreach ($this->generators as $generator) {
         $generator->generate();
     }
     echo CLI::h2('-----', HEADER_FILL);
 }
コード例 #3
0
 /**
  * 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";
 }
コード例 #4
0
ファイル: records.php プロジェクト: haifai-labs/repel-orm
        } else {
            echo "Database config file not found.";
            die;
        }
    } else {
        echo "No database config file.";
        die;
    }
    foreach ($config['databases'] as $key => $c) {
        if (!key_exists('model_directory_path', $c)) {
            echo CLI::failure("No 'model_directory_path' definition in database {$key}");
            return false;
        }
        $adapter = new Adapter($config['databases'], $c["adapter"]);
        if (key_exists('relationship_file', $c)) {
            $adapter->addFetcher(new Fetcher\PhpManyToManyFetcher($c["relationship_file"], $key));
        }
        switch ($c['adapter']) {
            case 'pgsql':
                $adapter->addFetcher(new Fetcher\PostgreSQLFetcher($key));
                break;
            default:
                break;
        }
        $adapter->fetch()->addGenerator(new Generator\RepelGenerator($c, $key))->generate();
        echo CLI::success();
    }
} catch (Exception $ex) {
    echo CLI::failure($ex);
    die;
}
コード例 #5
0
ファイル: create.php プロジェクト: haifai-labs/repel-orm
        $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)) {
        $result = $manager->db->exec('ROLLBACK;');
    }
    echo CLI::color("failed", red);
    echo "\n";
    echo "\n";
    echo CLI::color($e->getMessage(), 'white', 'red');
    echo "\n";
    echo "\n";
    die;
}
コード例 #6
0
 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";
             }
         }
     }
 }