Completed
Push — master ( d4a92d...b23853 )
by hook
05:07 queued 02:55
created

Builder::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hoooklife\DynamodbPodm\Query;
4
5
use Hoooklife\DynamodbPodm\DB;
6
use Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar;
7
8
class Builder
9
{
10
11
    /**
12
     * 查询排序
13
     */
14
    public $limit;
15
    public $wheres;
16
    public $columns;
17
    public $table;
18
19
20
    /**
21
     * All of the available clause operators.
22
     *
23
     * @var array
24
     */
25
    public $operators = [
26
        '=', '<', '>', '<=', '>=', '<>', '!='
27
    ];
28
29
    public function __construct($connection = 'default')
30
    {
31
        switch (DB::$config[$connection]['driver']) {
32
            case "dynamedb":
33
                $this->grammar = new DynamoDBGrammar($this, DB::$config[$connection]);
0 ignored issues
show
Bug Best Practice introduced by
The property grammar does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
                break;
35
            default:
36
                throw new Exception("bad driver");
0 ignored issues
show
Bug introduced by
The type Hoooklife\DynamodbPodm\Query\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
37
        }
38
    }
39
40
41
    /**
42
     * 设置要查出的列
43
     *
44
     * @param  array|mixed $columns
45
     * @return $this
46
     */
47
    public function select($columns = ['*'])
48
    {
49
        $this->columns = is_array($columns) ? $columns : func_get_args();
50
        return $this;
51
    }
52
53
54
    /**
55
     * Add a new select column to the query.
56
     *
57
     * @param  array|mixed $column
58
     * @return $this
59
     */
60
    public function addSelect($columns)
61
    {
62
        $columns = is_array($columns) ? $columns : func_get_args();
63
        $this->columns = array_merge((array)$this->columns, $columns);
64
        return $this;
65
    }
66
67
68
    /**
69
     * Set the table which the query is targeting.
70
     *
71
     * @param  string $table
72
     * @return $this
73
     */
74
    public function from($table)
75
    {
76
        $this->table = $table;
77
        return $this;
78
    }
79
80
81
    /**
82
     * Add a basic where clause to the query.
83
     *
84
     * @param  string|array|\Closure $column
85
     * @param  mixed $operator
86
     * @param  mixed $value
87
     * @param  string $boolean
88
     * @return $this
89
     */
90
91
    public function where($column, $operator = null, $value = null, $boolean = 'and')
92
    {
93
        if (is_array($column)) {
94
            // 递归
95
            foreach ($column as $key => $value) {
96
                if (is_numeric($key) && is_array($value)) {
97
                    $this->where(...array_values($value));
0 ignored issues
show
Bug introduced by
array_values($value) is expanded, but the parameter $column of Hoooklife\DynamodbPodm\Query\Builder::where() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
                    $this->where(/** @scrutinizer ignore-type */ ...array_values($value));
Loading history...
98
                } else {
99
                    $this->where($key, '=', $value, $boolean);
100
                }
101
            }
102
            return $this;
103
            // return $this->addArrayOfWheres($column, $boolean);
104
        }
105
106
        if (func_num_args() === 2 || $this->invalidOperator($operator)) {
107
            list($value, $operator) = [$operator, '='];
108
        }
109
110
        // where in
111
        if (is_array($value)) {
112
            return $this->whereIn($column, $boolean);
0 ignored issues
show
Bug introduced by
The method whereIn() does not exist on Hoooklife\DynamodbPodm\Query\Builder. Did you maybe mean where()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
            return $this->/** @scrutinizer ignore-call */ whereIn($column, $boolean);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
        }
114
115
        // is null
116
        if (is_null($value)) {
117
            return $this->whereNull($column, $boolean, $operator !== '=');
0 ignored issues
show
Bug introduced by
The method whereNull() does not exist on Hoooklife\DynamodbPodm\Query\Builder. Did you maybe mean where()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
            return $this->/** @scrutinizer ignore-call */ whereNull($column, $boolean, $operator !== '=');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
        }
119
120
121
        $type = 'Basic';
122
        $this->wheres[] = compact(
123
            'type', 'column', 'operator', 'value', 'boolean'
124
        );
125
126
127
        // TODO 参数绑定
128
        // $this->addBinding($value, 'where');
129
130
        return $this;
131
    }
132
133
134
    /**
135
     * Determine if the given operator is supported.
136
     *
137
     * @param  string $operator
138
     * @return bool
139
     */
140
    protected function invalidOperator($operator)
141
    {
142
        return !in_array(strtolower($operator), $this->operators, true) &&
143
            !in_array(strtolower($operator), $this->grammar->getOperators(), true);
144
    }
145
146
147
    // /**
148
    //  * 向查询添加排序语句。
149
    //  *
150
    //  * @param  string  $column
151
    //  * @param  string  $direction
152
    //  * @return $this
153
    //  */
154
    // public function orderBy($columns, $direction = 'asc')
155
    // {
156
    //     $orders = is_array($columns) ? $columns : [$columns => $direction];
157
    //     $this->orders = array_merge($this->orders, $orders);
158
    //     return $this;
159
    // }
160
161
    // public function offset($value)
162
    // {
163
    //     $this->offset =  max(0, $value);
164
    //     return $this;
165
    // }
166
167
    //  /**
168
    //  * Alias to set the "offset" value of the query.
169
    //  *
170
    //  * @param  int  $value
171
    //  * @return \Illuminate\Database\Query\Builder|static
172
    //  */
173
    // public function skip($value)
174
    // {
175
    //     return $this->offset($value);
176
    // }
177
178
179
    /**
180
     * Alias to set the "limit" value of the query.
181
     *
182
     * @param  int $value
183
     * @return \Illuminate\Database\Query\Builder|static
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Query\Builder 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...
184
     */
185
    public function take($value)
186
    {
187
        return $this->limit($value);
188
    }
189
190
191
    /**
192
     * Set the "limit" value of the query.
193
     *
194
     * @param  int $value
195
     * @return $this
196
     */
197
    public function limit($value)
198
    {
199
        //  FIXME ??? 为啥等于0
200
        if ($value > 0) {
201
            $this->limit = $value;
202
        }
203
        return $this;
204
    }
205
206
    /**
207
     * Set the limit and offset for a given page.
208
     *
209
     * @param  int $page
210
     * @param  int $perPage
211
     * @return \Illuminate\Database\Query\Builder|static
212
     */
213
    public function forPage($page, $perPage = 15)
214
    {
215
        return $this->skip(($page - 1) * $perPage)->take($perPage);
0 ignored issues
show
Bug introduced by
The method skip() does not exist on Hoooklife\DynamodbPodm\Query\Builder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
        return $this->/** @scrutinizer ignore-call */ skip(($page - 1) * $perPage)->take($perPage);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
216
    }
217
218
219
    /**
220
     * Get the SQL representation of the query.
221
     *
222
     * @return string
223
     */
224
    public function toSql()
225
    {
226
        return $this->grammar->compileSelect($this);
227
    }
228
229
230
    /**
231
     * Get a single column's value from the first result of a query.
232
     *
233
     * @param  string $column
234
     * @return mixed
235
     */
236
    public function value($column)
237
    {
238
        $result = (array)$this->first([$column]);
239
        return count($result) > 0 ? reset($result) : null;
240
    }
241
242
243
    /**
244
     * Execute the query and get the first result.
245
     *
246
     * @param  array $columns
247
     * @return \Illuminate\Database\Eloquent\Model|object|static|null
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model 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...
248
     */
249
    public function first($columns = ['*'])
250
    {
251
        return reset($this->take(1)->all($columns));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->take(1)->all($columns) targeting Hoooklife\DynamodbPodm\Query\Builder::all() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
$this->take(1)->all($columns) of type void is incompatible with the type array expected by parameter $array of reset(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

251
        return reset(/** @scrutinizer ignore-type */ $this->take(1)->all($columns));
Loading history...
252
    }
253
254
255
    /**
256
     * Execute the query as a "select" statement.
257
     *
258
     * @param  array $columns
259
     * @return void
260
     */
261
    public function all($columns = ['*'])
0 ignored issues
show
Unused Code introduced by
The parameter $columns is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

261
    public function all(/** @scrutinizer ignore-unused */ $columns = ['*'])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
262
    {
263
        return $this->grammar->all();
264
    }
265
266
}
267