QueryTrait::getRowFieldFunction()   B
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 11
nc 6
nop 5
dl 0
loc 18
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Facades\Traits;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
6
7
use function is_array;
8
use function preg_match;
9
use function is_bool;
10
use function array_sum;
11
12
trait QueryTrait
13
{
14
    /**
15
     * Get the table fields
16
     *
17
     * @param string $table         The table name
18
     * @param array  $queryOptions  The query options
19
     *
20
     * @return array
21
     */
22
    private function getFields(string $table, array $queryOptions): array
23
    {
24
        // From edit.inc.php
25
        $fields = $this->driver->fields($table);
26
27
        //!!!! $queryOptions["select"] is never set here !!!!//
28
29
        $where = $this->driver->where($queryOptions, $fields);
30
        $update = $where;
31
        foreach ($fields as $name => $field) {
32
            $generated = $field->generated ?? false;
33
            if (!isset($field->privileges[$update ? "update" : "insert"]) ||
34
                $this->admin->fieldName($field) == "" || $generated) {
35
                unset($fields[$name]);
36
            }
37
        }
38
39
        return [$fields, $where, $update];
40
    }
41
42
    /**
43
     * @param array $fields
44
     * @param array $queryOptions
45
     *
46
     * @return array
47
     */
48
    private function getQuerySelect(array $fields, array $queryOptions): array
49
    {
50
        $select = [];
51
        foreach ($fields as $name => $field) {
52
            if (isset($field->privileges["select"])) {
53
                $as = $this->driver->convertField($field);
54
                if ($queryOptions["clone"] && $field->autoIncrement) {
55
                    $as = "''";
56
                }
57
                if ($this->driver->jush() == "sql" && preg_match("~enum|set~", $field->type)) {
58
                    $as = "1*" . $this->driver->escapeId($name);
59
                }
60
                $select[] = ($as ? "$as AS " : "") . $this->driver->escapeId($name);
61
            }
62
        }
63
        if (!$this->driver->support("table")) {
64
            $select = ["*"];
65
        }
66
        return $select;
67
    }
68
69
    /**
70
     * @param string $table
71
     * @param string $where
72
     * @param array $fields
73
     * @param array $queryOptions
74
     *
75
     * @return array|null
76
     */
77
    private function getQueryFirstRow(string $table, string $where, array $fields, array $queryOptions): ?array
78
    {
79
        // From edit.inc.php
80
        $row = null;
81
        if (($where)) {
82
            $select = $this->getQuerySelect($fields, $queryOptions);
83
            $row = [];
84
            if ($select) {
85
                $statement = $this->driver->select($table, $select, [$where], $select, [],
86
                    (isset($queryOptions["select"]) ? 2 : 1));
87
                if (($statement)) {
88
                    $row = $statement->fetchAssoc();
89
                }/* else {
90
                    $error = $this->driver->error();
91
                }*/
92
                // if(isset($queryOptions["select"]) && (!$row || $statement->fetchAssoc()))
93
                // {
94
                //     // $statement->rowCount() != 1 isn't available in all drivers
95
                //     $row = null;
96
                // }
97
            }
98
        }
99
        return $row;
100
    }
101
102
    /**
103
     * @param TableFieldEntity $field
104
     * @param string $name
105
     * @param array|null $row
106
     * @param string $update
107
     * @param array $queryOptions
108
     *
109
     * @return mixed
110
     */
111
    private function getRowFieldValue(TableFieldEntity $field, string $name, ?array $row, string $update, array $queryOptions)
112
    {
113
        // $default = $queryOptions["set"][$this->driver->bracketEscape($name)] ?? null;
114
        // if($default === null)
115
        // {
116
        $default = $field->default;
117
        if ($field->type == "bit" && preg_match("~^b'([01]*)'\$~", $default, $regs)) {
118
            $default = $regs[1];
119
        }
120
        // }
121
        if ($row === null) {
0 ignored issues
show
introduced by
The condition $row === null is always false.
Loading history...
122
            return !$update && $field->autoIncrement ? "" : (isset($queryOptions["select"]) ? false : $default);
123
        }
124
        if ($row[$name] != "" && $this->driver->jush() == "sql" && preg_match("~enum|set~", $field->type) ) {
125
            return is_array($row[$name]) ? array_sum($row[$name]) : +$row[$name];
126
        }
127
        return is_bool($row[$name]) ? +$row[$name] : $row[$name];
128
    }
129
130
    /**
131
     * @param TableFieldEntity $field
132
     * @param string $name
133
     * @param mixed $value
134
     * @param string $update
135
     * @param array $queryOptions
136
     *
137
     * @return string|null
138
     */
139
    private function getRowFieldFunction(TableFieldEntity $field, string $name, $value, string $update, array $queryOptions): ?string
140
    {
141
        if (!$update && $value == $field->default && preg_match('~^[\w.]+\(~', $value ?? '')) {
142
            return "SQL";
143
        }
144
        if ($queryOptions["save"]) {
145
            return (string)$queryOptions["function"][$name];
146
        }
147
        if ($update && preg_match('~^CURRENT_TIMESTAMP~i', $field->onUpdate)) {
148
            return 'now';
149
        }
150
        if ($value === null) {
151
            return 'NULL';
152
        }
153
        if ($value === false) {
154
            return null;
155
        }
156
        return '';
157
    }
158
}
159