Exemplo n.º 1
0
if ($func == 'dump-schema') {
    $table = $argv[2];
    echo yaml::schema_to_yaml($table);
}
if ($func == 'load-schema') {
    $table = $argv[2];
    echo yaml::load_schema_from_yaml($table);
}
if ($func == 'drop-table') {
    $table = $argv[2];
    echo yaml::drop_table($table);
}
if ($func == 'convert-table') {
    $source = $argv[2];
    $target = $argv[3];
    echo yaml::convert_table($source, $target);
}
if ($func == 'dump-to-screen') {
    $table = $argv[2];
    $renderer = new ArrayToTextTable((array) yaml::dump_to_screen($table));
    $renderer->showHeaders(true);
    $renderer->render();
    echo "\r\n";
}
if ($func == '' or $func == 'help') {
    echo "dump-schema <tablename> \r\n";
    echo "load-schema <tablename> \r\n";
    echo "drop-table <tablename> \r\n";
    echo "convert-table <source> <target> \r\n";
    echo "dump-to-screen <table>\r\n";
}
 /**
  * Starts the main interactive terminal
  *
  * @return void
  */
 public function run()
 {
     $this->_shell->readHistory($this->_historyFile);
     $prompt = '=# ';
     $sql = '';
     ob_start();
     echo "Welcome to matrixsqlclient";
     if (!empty($GLOBALS['rev'])) {
         echo " (" . $GLOBALS['rev'] . ")";
     }
     echo ", the interactive database terminal in PHP.";
     echo "\n\nYou are now connected.";
     echo "\nDatabase type: " . $this->_db->getDbType() . $this->_db->getDbVersion() . ".\n\n";
     ob_end_flush();
     while (1) {
         // Prompt for input
         $line = $this->_shell->readline($this->_db->getDbName() . $prompt);
         if ($line === "") {
             echo "\n";
             continue;
         }
         // Exits
         if (mb_substr(trim($line), 0, 4) == 'exit' || mb_substr(trim($line), 0, 4) == 'quit' || mb_substr(trim($line), 0, 2) == '\\q') {
             echo "\n";
             exit;
         }
         if (mb_substr($line, mb_strlen($line) - 1, mb_strlen($line)) === chr(4)) {
             echo "\\q\n";
             exit;
         }
         // CTRL-C cancels any current query
         if (ord(mb_substr($line, mb_strlen($line) - 1, mb_strlen($line))) === 3) {
             $sql = '';
             $line = '';
             $prompt = '=# ';
             echo "\n";
             continue;
         }
         if (mb_strlen($line) > 0) {
             // Add this command to the history
             $this->_shell->addHistory(strtr($line, "\n", " "));
         }
         if (mb_substr(trim($line), 0, 7) == "\\timing") {
             $this->setOption("timing", !$this->_getOptionValue("timing"));
             if ($this->_getOptionValue("timing")) {
                 echo "\nTiming is on.";
             } else {
                 echo "\nTiming is off.";
             }
             echo "\n";
             continue;
         }
         // "\set" command
         if (strlen(trim($sql)) === 0 && mb_substr(trim($line), 0, 4) == "\\set") {
             $params = explode(" ", $line, 3);
             // "\set" with no options - show existing options/values
             if (count($params) === 1) {
                 $options = $this->_getOptions();
                 if (count($options) > 0) {
                     foreach ($this->_getOptions() as $option => $value) {
                         $value = $value === true ? "on" : $value;
                         $value = $value === false ? "off" : $value;
                         echo "\n" . $option . " = '" . $value . "'";
                     }
                 }
                 // "set" a particular value
             } else {
                 $params = array_pad($params, 3, "");
                 $this->setOption($params[1], $params[2]);
                 $this->_parseOptions();
             }
             echo "\n";
             continue;
         }
         $sql .= "\n" . $line;
         // If the SQL string is terminated with a semicolon, or the DB module wants
         // to accept it (eg. for a macro), then execute it
         if ($this->_db->matchesMacro($sql) || mb_strpos($sql, ';')) {
             echo "\n";
             $sql = trim($sql);
             try {
                 // Run the SQL
                 $this->restoreTerminal();
                 $source_data = @$this->_db->execute($sql);
             } catch (Exception $e) {
                 echo "\n" . $e->getMessage() . "\n";
                 $this->resetTerminal(true);
                 // Reset the prompt cause its a new query
                 $prompt = '=# ';
                 $sql = '';
                 echo "\n";
                 continue;
             }
             $this->resetTerminal(true);
             // If cancel request was triggered then just discard anything that might
             // come back
             if ($this->_cancel) {
                 $source_data = null;
                 $this->_cancel = false;
                 echo "Cancelled\n";
                 $prompt = '=# ';
                 $sql = '';
                 continue;
             }
             // If we get an array back, it's rows
             if (is_array($source_data)) {
                 $rowlimit = (int) $this->_getOptionValue("rowlimit");
                 if (count($source_data) > $rowlimit) {
                     $this->_addToLinesBuffer(explode("\n", "\n\nWARNING: Number of rows returned exceeded rowlimit.\nOnly the first {$rowlimit} rows are being shown. Use \\set rowlimit <num> to adjust.\n\n"));
                     $source_data = array_slice($source_data, 0, $rowlimit);
                 }
                 // Only render the table if rows were returned
                 if (!empty($source_data)) {
                     // Render the table
                     $table = new ArrayToTextTable($source_data);
                     $table->showHeaders(true);
                     $data = explode("\n", $table->render(true));
                     array_pop($data);
                     $this->_addToLinesBuffer($data);
                 }
                 // Build count summary (at end of table) and add to line buffer
                 $count_str = "(" . count($source_data) . " row";
                 if (count($source_data) !== 1) {
                     $count_str .= "s";
                 }
                 $count_str .= ")";
                 $this->_addToLinesBuffer(array($count_str, ""));
                 // Assuming it's a string...
             } else {
                 $this->_addToLinesBuffer(array($source_data));
             }
             if ($this->_getOptionValue("timing")) {
                 // Output amount of time this query took
                 $this->_addToLinesBuffer(array("Time: " . $this->_db->getQueryExecutionTime() . " ms"));
             }
             // Output the data
             $this->_printLines();
             // Reset the prompt cause its a new query
             $prompt = '=# ';
             $sql = '';
         } elseif (mb_strlen(trim($sql)) > 0) {
             // We're in the middle of some SQL, so modify the prompt slightly to show that
             // (like psql does)
             if (substr_count($sql, "(") > substr_count($sql, ")")) {
                 $prompt = '(# ';
             } else {
                 $prompt = '-# ';
             }
             echo "\n";
         }
     }
 }
 function handleStatusAction()
 {
     $backupJobGetter = new backupJobGetter();
     $backupJobGetter->setLogStream($this->log);
     $scheduledBackupGetter = new scheduledBackupGetter();
     $scheduledBackupGetter->setLogStream($this->log);
     $hostGetter = new hostGetter();
     $hostGetter->setLogStream($this->log);
     $runningJobs = $backupJobGetter->getRunning();
     $backupRows = array();
     foreach ($runningJobs as $job) {
         $info = $job->getInfo();
         $scheduledBackup = $job->getScheduledBackup();
         $host = $scheduledBackup->getHost();
         $hostInfo = $host->getInfo();
         $sbInfo = $scheduledBackup->getInfo();
         $backupRows[] = array('Job ID' => $info['backup_job_id'], 'Host' => $hostInfo['hostname'], 'Backup Name' => $sbInfo['name'], 'Start Time' => $info['start_time'], 'Status' => $info['status'], 'PID' => $info['pid']);
     }
     if (sizeOf($backupRows) > 0) {
         $textTable = new ArrayToTextTable($backupRows);
         $textTable->showHeaders(true);
         $tableOutput = $textTable->render(true);
         print "Currently Running Backups:\n\n" . $tableOutput . "\n\n";
     } else {
         print "There are no backups currently running.\n\n";
     }
 }