Completed
Push — master ( be4d4a...bfa2f5 )
by hook
02:34
created

Builder::select()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hoooklife\DynamodbPodm\Query;
4
5
use Hoooklife\DynamodbPodm\Collection;
6
use Hoooklife\DynamodbPodm\DB;
7
use Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar;
8
9
class Builder
10
{
11
12
    /**
13
     * 查询排序
14
     */
15
    public $limit;
16
    public $wheres = [];
17
    public $bindings = [];
18
    public $columns;
19
    public $table;
20
21
22
    /**
23
     * All of the available clause operators.
24
     *
25
     * @var array
26
     */
27
    public $operators = [
28
        '=', '<', '>', '<=', '>=', '<>', '!='
29
    ];
30
31
    public $reservedKey = [
32
        'key'
33
    ];
34
35
    /** @var DynamoDBGrammar */
36
    private $grammar;
37
38
    public function __construct($connection = 'default')
39
    {
40
        switch (DB::$config[$connection]['driver']) {
41
            case "dynamedb":
42
                $this->grammar = new DynamoDBGrammar($this, DB::$config[$connection]);
43
                break;
44
            default:
45
                throw new \Exception("bad driver");
46
        }
47
    }
48
49
50
    /**
51
     * Set the table which the query is targeting.
52
     *
53
     * @param  string $table
54
     * @return $this
55
     */
56
    public function from($table)
57
    {
58
        $this->table = $table;
59
        return $this;
60
    }
61
62
63
    /**
64
     * Add a basic where clause to the query.
65
     *
66
     * @param  string|array|\Closure $column
67
     * @param  mixed $operator
68
     * @param  mixed $value
69
     * @param  string $boolean
70
     * @return $this
71
     */
72
73
    public function where($column, $operator = null, $value = null, $boolean = 'and')
74
    {
75
        if (is_array($column)) {
76
            // 递归
77
            foreach ($column as $key => $value) {
78
                if (is_numeric($key) && is_array($value)) {
79
                    $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

79
                    $this->where(/** @scrutinizer ignore-type */ ...array_values($value));
Loading history...
80
                } else {
81
                    $this->where($key, '=', $value, $boolean);
82
                }
83
            }
84
            return $this;
85
            // return $this->addArrayOfWheres($column, $boolean);
86
        }
87
88
        if (func_num_args() === 2 || $this->invalidOperator($operator)) {
89
            list($value, $operator) = [$operator, '='];
90
        }
91
92
        // where in
93
        if (is_array($value)) {
94
            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

94
            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...
95
        }
96
97
        // is null
98
        if (is_null($value)) {
99
            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

99
            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...
100
        }
101
102
        $type = 'Basic';
103
        $this->wheres[] = compact(
104
            'type', 'column', 'operator', 'value', 'boolean'
105
        );
106
107
108
        // TODO 参数绑定
109
//         $this->addBinding($value, 'where');
110
111
        return $this;
112
    }
113
114
115
    /**
116
     * Determine if the given operator is supported.
117
     *
118
     * @param  string $operator
119
     * @return bool
120
     */
121
    protected function invalidOperator($operator)
122
    {
123
        return !in_array(strtolower($operator), $this->operators, true) &&
124
            !in_array(strtolower($operator), $this->grammar->getOperators(), true);
125
    }
126
127
128
    // /**
129
    //  * 向查询添加排序语句。
130
    //  *
131
    //  * @param  string  $column
132
    //  * @param  string  $direction
133
    //  * @return $this
134
    //  */
135
    // public function orderBy($columns, $direction = 'asc')
136
    // {
137
    //     $orders = is_array($columns) ? $columns : [$columns => $direction];
138
    //     $this->orders = array_merge($this->orders, $orders);
139
    //     return $this;
140
    // }
141
142
    // public function offset($value)
143
    // {
144
    //     $this->offset =  max(0, $value);
145
    //     return $this;
146
    // }
147
148
    //  /**
149
    //  * Alias to set the "offset" value of the query.
150
    //  *
151
    //  * @param  int  $value
152
    //  * @return \Illuminate\Database\Query\Builder|static
153
    //  */
154
    // public function skip($value)
155
    // {
156
    //     return $this->offset($value);
157
    // }
158
159
160
    /**
161
     * Alias to set the "limit" value of the query.
162
     *
163
     * @param  int $value
164
     * @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...
165
     */
166
    public function take($value)
167
    {
168
        return $this->limit($value);
169
    }
170
171
172
    /**
173
     * Set the "limit" value of the query.
174
     *
175
     * @param  int $value
176
     * @return $this
177
     */
178
    public function limit($value)
179
    {
180
        //  FIXME ??? 为啥等于0
181
        if ($value > 0) {
182
            $this->limit = $value;
183
        }
184
        return $this;
185
    }
186
187
    /**
188
     * Set the limit and offset for a given page.
189
     *
190
     * @param  int $page
191
     * @param  int $perPage
192
     * @return \Illuminate\Database\Query\Builder|static
193
     */
194
    public function forPage($page, $perPage = 15)
195
    {
196
        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

196
        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...
197
    }
198
199
200
    /**
201
     * Get the SQL representation of the query.
202
     *
203
     * @return string
204
     */
205
    public function toSql()
206
    {
207
        return $this->grammar->compileSelect($this);
0 ignored issues
show
Bug introduced by
The method compileSelect() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar. ( Ignorable by Annotation )

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

207
        return $this->grammar->/** @scrutinizer ignore-call */ compileSelect($this);

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...
208
    }
209
210
211
    /**
212
     * Get a single column's value from the first result of a query.
213
     *
214
     * @param  string $column
215
     * @return mixed
216
     */
217
    public function value($column)
218
    {
219
        $result = (array)$this->first([$column]);
220
        return count($result) > 0 ? reset($result) : null;
221
    }
222
223
224
    /**
225
     * Execute the query and get the first result.
226
     *
227
     * @param  array $columns
228
     * @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...
229
     */
230
    public function first($columns = ['*'])
231
    {
232
        return reset($this->take(1)->all($columns));
0 ignored issues
show
Bug introduced by
$this->take(1)->all($columns) of type Hoooklife\DynamodbPodm\Collection 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

232
        return reset(/** @scrutinizer ignore-type */ $this->take(1)->all($columns));
Loading history...
233
    }
234
235
236
    /**
237
     * Execute the query as a "select" statement.
238
     *
239
     * @param  array $columns
240
     * @return Collection
241
     */
242
    public function all($columns = ['*']): Collection
243
    {
244
        $columns = is_array($columns) ? $columns : func_get_args();
245
        $this->columns = $columns;
246
247
        return $this->grammar->all();
248
    }
249
250
    /**
251
     * @param array $data
252
     * @return \Aws\Result
253
     */
254
    public function insert(array $data)
255
    {
256
        return $this->grammar->insert($data);
257
    }
258
259
260
}
261