Test Setup Failed
Branch main (5746ae)
by Sammy
05:59
created

Crudites.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Crudités, it's a cup of carrots sticks (but are they organic ?)
5
 * Codd's Relational model, Unicity, Definitions, Introspection, Tests, Execution & Sets
6
 * Create - Retrieve - Update - Delete
7
 * API for writing and running SQL queries
8
 */
9
10
namespace HexMakina\Crudites;
11
12
use \HexMakina\Crudites\Queries\BaseQuery;
13
use \HexMakina\Crudites\Interfaces\SelectInterface;
14
use \HexMakina\Crudites\Interfaces\DatabaseInterface;
15
use \HexMakina\Crudites\CruditesException;
16
17
class Crudites
18
{
19
    private static $database = null;
20
21
    public static function setDatabase(DatabaseInterface $db)
22
    {
23
        self::$database = $db;
24
    }
25
26
    public static function inspect($table_name)
27
    {
28
        if (is_null(self::$database)) {
29
            throw new CruditesException('NO_DATABASE');
30
        }
31
32
        try {
33
            return self::$database->inspect($table_name);
34
        } catch (\Exception $e) {
35
            throw new CruditesException('TABLE_INTROSPECTION::'.$table_name);
36
        }
37
    }
38
39
    public static function connect($dsn=null, $user=null, $pass=null)
40
    {
41
        // no props, means connection already exists, verify and return
42
        if (!isset($dsn, $user, $pass)) {
43
            if (is_null(self::$database)) {
44
                throw new CruditesException('CONNECTION_MISSING');
45
            }
46
47
            return self::$database->connection();
48
        }
49
50
        $conx = new Connection($dsn, $user, $pass);
51
        return $conx;
52
    }
53
54
    //------------------------------------------------------------  DataRetrieval
55
    // success: return AIPK-indexed array of results (associative array or object)
56
    public static function count(Select $Query)
0 ignored issues
show
The type HexMakina\Crudites\Select was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
    {
58
        $Query->select_also(['COUNT(*) as count']);
59
        $res = $Query->ret_col();
60
        if (is_array($res)) {
61
            return intval(current($res));
62
        }
63
        return null;
64
    }
65
66
    // success: return AIPK-indexed array of results (associative array or object)
67
    public static function retrieve(SelectInterface $Query): array
68
    {
69
        $pk_name = implode('_', array_keys($Query->table()->primary_keys()));
70
71
        $ret = [];
72
73
        if ($Query->run()->is_success()) {
74
            foreach ($Query->ret_ass() as $rec) {
75
                $ret[$rec[$pk_name]] = $rec;
76
            }
77
        }
78
79
        return $ret;
80
    }
81
82
    public static function raw($sql, $dat_ass = [])
83
    {
84
        $conx = self::connect();
85
        if (empty($dat_ass)) {
86
            $res = $conx->query($sql);
87
            //TODO query | alter !
88
            //$res = $conx->alter($sql);
89
        } else {
90
            $stmt = $conx->prepare($sql);
91
            $res = $stmt->execute($dat_ass);
92
        }
93
        return $res;
94
    }
95
96
    public static function distinct_for($table, $column_name, $filter_by_value = null)
97
    {
98
        $table = self::table_name_to_Table($table);
99
100
        if (is_null($table->column($column_name))) {
101
            throw new CruditesException('TABLE_REQUIRES_COLUMN');
102
        }
103
104
        $Query = $table->select(["DISTINCT `$column_name`"])->aw_not_empty($column_name)->order_by([$table->name(), $column_name, 'ASC']);
105
106
        if (!is_null($filter_by_value)) {
107
            $Query->aw_like($column_name, "%$filter_by_value%");
108
        }
109
110
        $Query->order_by($column_name, 'DESC');
111
        // ddt($Query);
112
        return $Query->ret_col();
113
    }
114
115
    public static function distinct_for_with_id($table, $column_name, $filter_by_value = null)
116
    {
117
        $table = self::table_name_to_Table($table);
118
119
        if (is_null($table->column($column_name))) {
120
            throw new CruditesException('TABLE_REQUIRES_COLUMN');
121
        }
122
123
        $Query = $table->select(["DISTINCT `id`,`$column_name`"])->aw_not_empty($column_name)->order_by([$table->name(), $column_name, 'ASC']);
124
125
        if (!is_null($filter_by_value)) {
126
            $Query->aw_like($column_name, "%$filter_by_value%");
127
        }
128
129
        return $Query->ret_par();
130
    }
131
132
    //------------------------------------------------------------  DataManipulation Helpers
133
    // returns true on success, false on failure or throws an exception
134
    // throws Exception on failure
135
    public static function toggle_boolean($table, $boolean_column_name, $id): bool
136
    {
137
138
        $table = self::table_name_to_Table($table);
139
140
        if (is_null($column = $table->column($boolean_column_name)) || !$column->type()->isBoolean()) {
141
            return false;
142
        }
143
144
        // TODO: still using 'id' instead of table->primaries
145
        $Query = $table->update();
146
        $Query->statement("UPDATE " . $table->name() . " SET $boolean_column_name = !$boolean_column_name WHERE id=:id");
147
        $Query->bindings([':id' => $id]);
148
        $Query->run();
149
150
        return $Query->is_success();
151
    }
152
153
    private static function table_name_to_Table($table)
154
    {
155
        return is_string($table) ? self::inspect($table) : $table;
156
    }
157
}
158