Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.22% covered (warning)
72.22%
13 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Query
72.22% covered (warning)
72.22%
13 / 18
0.00% covered (danger)
0.00%
0 / 1
4.34
0.00% covered (danger)
0.00%
0 / 1
 run
72.22% covered (warning)
72.22%
13 / 18
0.00% covered (danger)
0.00%
0 / 1
4.34
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Dev Commands Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\CLI\Commands;
11
12use Framework\CLI\CLI;
13
14/**
15 * Class Query.
16 *
17 * @package dev-commands
18 */
19class Query extends DatabaseCommand
20{
21    protected string $description = 'Runs an SQL query.';
22
23    public function run() : void
24    {
25        $this->setDatabase();
26        $query = $this->console->getArgument(0);
27        if (empty($query)) {
28            $query = CLI::prompt('Query');
29        }
30        CLI::write(
31            CLI::style('Query: ', 'white') . CLI::style($query, 'yellow')
32        );
33        try {
34            $result = $this->getDatabase()->query($query);
35        } catch (\Exception $exception) {
36            CLI::beep();
37            CLI::error($exception->getMessage());
38            return;
39        }
40        $result = $result->fetchArrayAll();
41        if (empty($result)) {
42            CLI::write('No results.');
43            return;
44        }
45        CLI::table($result, \array_keys($result[0]));
46        CLI::write('Total: ' . \count($result));
47    }
48}