AbstractSQLDatabase::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
namespace DAL;
3
4
use \Exception;
5
use \mysqli_stmt;
6
7
abstract class AbstractSQLDatabase extends AbstractDatabase
8
{
9
10
    /**
11
     * @param SqlStatementBuilder $statement
12
     * @param array               $options
13
     * @param bool                $returnResult
14
     * @return array[]|null
15
     */
16
    abstract protected function sendQueryToDatabase(SqlStatementBuilder $statement, array $options, $returnResult);
17
18
    /**
19
     * @return SqlStatementBuilder
20
     */
21 18
    protected static function getSqlStatement()
22
    {
23 18
        return new SqlStatementBuilder();
24
    }
25
26
    /**
27
     * @param string $table   The table that will be accessed and written.
28
     * @param array  $fields  A list of fields and values to be used when creating a new record.
29
     * @param array  $options The list of options that will help with creating the records.
30
     * @return void
31
     */
32 4
    public function create($table, array $fields, array $options = [])
33
    {
34 4
        $expr = self::getSqlStatement();
35 4
        $expr->create($table, $fields);
36 4
        $this->sendQueryToDatabase($expr, $options, false);
37 4
    }
38
39
    /**
40
     * @param string         $table    The table that will be accessed and written.
41
     * @param array          $fields   A list of fields and values to be used when updating a record.
42
     * @param Condition|null $criteria The criteria that will filter the data.
43
     * @param array          $options  The list of options that will help with updating the records.
44
     * @return void
45
     */
46 2
    public function update($table, array $fields, Condition $criteria = null, array $options = [])
47
    {
48 2
        $expr = self::getSqlStatement();
49 2
        $expr->update($table, $fields, $criteria);
50 2
        $this->sendQueryToDatabase($expr, $options, false);
51 2
    }
52
53
    /**
54
     * @param string         $table    The table that will be accessed and written.
55
     * @param Condition|null $criteria The criteria that will filter the data.
56
     * @param array          $options  The list of options that will help with deleting the records.
57
     * @return void
58
     */
59 2
    public function delete($table, Condition $criteria = null, array $options = [])
60
    {
61 2
        $expr = self::getSqlStatement();
62 2
        $expr->delete($table, $criteria);
63 2
        $this->sendQueryToDatabase($expr, $options, false);
64 2
    }
65
66
    /**
67
     * @param string         $table The table that will be accessed and written.
68
     * @param Condition|null $criteria The criteria that will filter the records.
69
     * @param array          $options The list of options that will help with finding the records.
70
     * @return array[] Multiple records from the table that match the criteria.
0 ignored issues
show
Documentation introduced by
Should the return type not be array[]|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
71
     */
72 5
    public function findAll($table, Condition $criteria = null, array $options = [])
73
    {
74 5
        $expr = self::getSqlStatement();
75
76 5
        if (isset($options['fields'])) {
77
            $fields = $options['fields'];
78
            if (!is_array($fields)) {
79
                $fields = [$fields];
80
            }
81
        } else {
82 5
            $fields = ['*'];
83
        }
84
85 5
        if (isset($options['limit'])) {
86 3
            $limit = $options['limit'];
87 3
        } else {
88 2
            $limit = null;
89
        }
90
91 5
        $expr->select($fields, $table, $criteria, $limit);
92 5
        return $this->sendQueryToDatabase($expr, $options, true);
93
    }
94
95
    /**
96
     * @param string         $table The table that will be accessed and written.
97
     * @param Condition|null $criteria The criteria that will filter the records.
98
     * @param array          $options The list of options that help with counting the records.
99
     * @return int The number of records that match the criteria.
100
     */
101 3
    public function count($table, Condition $criteria = null, array $options = [])
102
    {
103 3
        $data = $this->selectOnlyOneValue('count(*) as x', $table, $criteria, $options);
104 3
        return (int) $data[0]['x'];
105
    }
106
107
    /**
108
     * @param string         $table The table that will be accessed and written.
109
     * @param Condition|null $criteria The criteria that will filter the records.
110
     * @param array          $options The list of options that will help with finding the records.
111
     * @return boolean A boolean value indicating if any record matches the criteria.
112
     */
113 2
    public function has($table, Condition $criteria = null, array $options = [])
114
    {
115 2
        $data = $this->selectOnlyOneValue('1 as x', $table, $criteria, $options);
116 2
        return isset($data[0]);
117
    }
118
119
    /**
120
     * @param string         $columns
121
     * @param string         $table
122
     * @param Condition|null $criteria
123
     * @param array          $options
124
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array[]|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
125
     */
126 5
    private function selectOnlyOneValue($columns, $table, Condition $criteria = null, array $options = [])
127
    {
128 5
        $expr = self::getSqlStatement();
129 5
        $expr->select([$columns], $table, $criteria);
130 5
        return $this->sendQueryToDatabase($expr, $options, true);
131
    }
132
133
    /**
134
     * Logs the given exception, and returns it.
135
     *
136
     * @param Exception $error The exception to log and return.
137
     * @return Exception The exception that was passed in as an argument.
138
     */
139 2
    protected function logException(Exception $error)
140
    {
141 2
        $this->getLogger()->error($error);
142 2
        return $error;
143
    }
144
145
    /**
146
     * @param SqlStatementBuilder $statement
147
     * @param array               $options
148
     * @return array<string|array> An array that contains the query and the bindings.
149
     */
150 18
    protected static function compileQueriesAndBindings(SqlStatementBuilder $statement, array $options)
151
    {
152 18
        $queries  = $statement->queries;
153 18
        $bindings = $statement->bindings;
154
155 18
        foreach ($queries as $key => $query) {
156 18
            if (isset($options['beforeSQL'])) {
157
                $queries[$key] = $options['beforeSQL'] . $query;
158
            }
159 18
            if (isset($options['afterSQL'])) {
160
                $queries[$key] = $query . $options['afterSQL'];
161
            }
162 18
        }
163
164 18
        if (isset($options['beforeBindings'])) {
165
            $bindings = array_merge($options['beforeBindings'], $bindings);
166
        }
167
168 18
        if (isset($options['afterBindings'])) {
169
            $bindings = array_merge($bindings, $options['afterBindings']);
170
        }
171
172 18
        $query = implode(';', $queries);
173
174 18
        return [$query, $bindings];
175
    }
176
}
177