Exemplo n.º 1
0
 public static function table($data)
 {
     // Determine columns and width.
     $rows = sizeof($data);
     $columns = sizeof($data[0]);
     $column_widths = [];
     foreach ($data as $row) {
         foreach ($row as $column => $item) {
             if (!isset($column_widths[$column])) {
                 $column_widths[$column] = 0;
             }
             $column_widths[$column] = min(max(strlen($column), strlen($item), $column_widths[$column]), 30);
         }
     }
     $column_width = array_sum($column_widths);
     $width = $column_width + 1 + 3 * $columns;
     // Build separator.
     $separator = "+";
     foreach ($data[0] as $column => $item) {
         $separator .= str_repeat("-", $column_widths[$column] + 2) . '+';
     }
     $separator .= "\r\n";
     //$separator = "+" . str_repeat("-", $width - 2) . "+\r\n";
     // Print the header.
     $t = "";
     $t .= $separator;
     $t .= "| ";
     foreach ($data[0] as $key => $_) {
         $key = CLIUtilities::str_limit($key, $column_widths[$key]);
         $t .= $key;
         $t .= str_repeat(" ", $column_widths[$key] - strlen($key));
         $t .= " | ";
     }
     $t = substr($t, 0, -1) . "\r\n";
     $t .= $separator;
     // Print the rows.
     foreach ($data as $row) {
         $t .= "| ";
         foreach ($row as $column => $text) {
             $text = CLIUtilities::str_limit($text, $column_widths[$column]);
             $t .= $text;
             $t .= str_repeat(" ", $column_widths[$column] - strlen($text));
             $t .= " | ";
         }
         $t = substr($t, 0, -1) . "\r\n";
         //$t .= $separator;
     }
     $t .= $separator;
     return $t;
 }
Exemplo n.º 2
0
 public function execute($query)
 {
     $query = trim($query);
     if (strlen($query) == 0) {
         return;
     }
     printf("\r\n");
     $result = null;
     try {
         $result = $this->database->prepare($query)->execute();
         if ($result->size > 0) {
             printf(CLIUtilities::table($result->rows));
         }
         if ($result->insertId != 0) {
             printf("Inserted record with primary key %d\r\n", $result->insertId);
         }
         printf("\r\n");
         printf("Time: %dms\r\n", $result->time);
         printf("Rows Affected: %d\r\n", $result->affected);
         printf("\r\n");
     } catch (DatabaseException $e) {
         printf("%s\r\n\r\n", $e->getError());
     }
 }