Passed
Pull Request — master (#190)
by Arman
02:54
created

Query   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 86
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A lastQuery() 0 7 2
A execute() 0 7 2
A queryLog() 0 7 2
A lastStatement() 0 7 2
A fetchColumns() 0 12 2
A query() 0 7 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Database\Adapters\Idiorm\Statements;
16
17
use Quantum\Libraries\Database\Exceptions\DatabaseException;
18
19
/**
20
 * Trait Query
21
 * @package Quantum\Libraries\Database
22
 */
23
trait Query
24
{
25
26
    /**
27
     * @inheritDoc
28
     * @throws DatabaseException
29
     */
30
    public static function execute(string $query, array $parameters = []): bool
31
    {
32
        if (!self::getConnection()) {
33
            throw DatabaseException::missingConfig();
34
        }
35
36
        return (self::$ormClass)::raw_execute($query, $parameters);
37
    }
38
39
    /**
40
     * @inheritDoc
41
     * @throws DatabaseException
42
     */
43
    public static function query(string $query, array $parameters = []): array
44
    {
45
        if (!self::getConnection()) {
46
            throw DatabaseException::missingConfig();
47
        }
48
49
        return (self::$ormClass)::for_table('dummy')->raw_query($query, $parameters)->find_array();
50
    }
51
52
    /**
53
     * @inheritDoc
54
     * @throws DatabaseException
55
     */
56
    public static function lastQuery(): ?string
57
    {
58
        if (!self::getConnection()) {
59
            throw DatabaseException::missingConfig();
60
        }
61
62
        return (self::$ormClass)::get_last_query();
63
    }
64
65
    /**
66
     * @inheritDoc
67
     * @throws DatabaseException
68
     */
69
    public static function lastStatement(): object
70
    {
71
        if (!self::getConnection()) {
72
            throw DatabaseException::missingConfig();
73
        }
74
75
        return (self::$ormClass)::get_last_statement();
76
    }
77
78
    /**
79
     * @inheritDoc
80
     * @throws DatabaseException
81
     */
82
    public static function queryLog(): array
83
    {
84
        if (!self::getConnection()) {
85
            throw DatabaseException::missingConfig();
86
        }
87
88
        return (self::$ormClass)::get_query_log();
89
    }
90
91
    /**
92
     * Fetches columns of the table
93
     * @param string $table
94
     * @return array
95
     * @throws DatabaseException
96
     */
97
    public static function fetchColumns(string $table): array
98
    {
99
        $columns = [];
100
101
        self::query('SELECT * FROM ' . $table);
102
        $statement = self::lastStatement();
103
104
        for ($i = 0; $i < $statement->columnCount(); $i++) {
105
            $columns[] = $statement->getColumnMeta($i)['name'];
106
        }
107
108
        return $columns;
109
    }
110
}