db

Форк
0
/
Query.php 
140 строк · 3.6 Кб
1
<?php
2

3
namespace Upside\Db\SQL;
4

5
use Upside\Db\Connection;
6

7
class Query extends BaseStatement
8
{
9
    protected Connection $connection;
10

11
    protected array $tables;
12

13
    public function __construct(Connection $connection, array|string $tables, ?SQLStatement $statement = null)
14
    {
15
        parent::__construct($statement);
16

17
        $this->tables = \is_array($tables) ? $tables : [$tables];
18
        $this->connection = $connection;
19
    }
20

21
    protected function build_select(): Select
22
    {
23
        return new Select($this->connection, $this->tables, $this->sql);
24
    }
25

26
    protected function build_delete(): Delete
27
    {
28
        return new Delete($this->connection, $this->tables, $this->sql);
29
    }
30

31
    public function distinct(bool $value = true): SelectStatement
32
    {
33
        return $this->build_select()->distinct($value);
34
    }
35

36
    public function group_by(Expression|array|\Closure|string $columns): Select
37
    {
38
        return $this->build_select()->group_by($columns);
39
    }
40

41
    public function having(Expression|string|\Closure $column, ?\Closure $value = null): Select
42
    {
43
        return $this->build_select()->having($column, $value);
44
    }
45

46
    public function and_having(Expression|string|\Closure $column, ?\Closure $value = null): Select
47
    {
48
        return $this->build_select()->and_having($column, $value);
49
    }
50

51
    public function or_having(Expression|string|\Closure $column, ?\Closure $value = null): Select
52
    {
53
        return $this->build_select()->or_having($column, $value);
54
    }
55

56
    public function order_by(Expression|array|\Closure|string $columns, string $order = 'ASC', ?string $nulls = null): SelectStatement
57
    {
58
        return $this->build_select()->order_by($columns, $order, $nulls);
59
    }
60

61
    public function limit(int $value): SelectStatement
62
    {
63
        return $this->build_select()->limit($value);
64
    }
65

66
    public function offset(int $value): SelectStatement
67
    {
68
        return $this->build_select()->offset($value);
69
    }
70

71
    public function into(string $table, ?string $database = null): SelectStatement
72
    {
73
        return $this->build_select()->into($table, $database);
74
    }
75

76
    /**
77
     * @param array $columns (optional)
78
     *
79
     * @return  \Upside\Db\ResultSet
80
     */
81
    public function select($columns = [])
82
    {
83
        return $this->build_select()->select($columns);
84
    }
85

86
    public function column(Expression|string|\Closure $name): mixed
87
    {
88
        return $this->build_select()->column($name);
89
    }
90

91
    /**
92
     * @return  int
93
     */
94
    public function count(Expression|\Closure|string $column = '*', bool $distinct = false)
95
    {
96
        return $this->build_select()->count($column, $distinct);
97
    }
98

99
    /**
100
     * @return  int|float
101
     */
102
    public function avg(Expression|string|\Closure $column, bool $distinct = false)
103
    {
104
        return $this->build_select()->avg($column, $distinct);
105
    }
106

107
    /**
108
     * @return  int|float
109
     */
110
    public function sum(Expression|string|\Closure $column, bool $distinct = false)
111
    {
112
        return $this->build_select()->sum($column, $distinct);
113
    }
114

115
    /**
116
     * @return  int|float
117
     */
118
    public function min(Expression|string|\Closure $column, bool $distinct = false)
119
    {
120
        return $this->build_select()->min($column, $distinct);
121
    }
122

123
    /**
124
     * @return  int|float
125
     */
126
    public function max(Expression|string|\Closure $column, bool $distinct = false)
127
    {
128
        return $this->build_select()->max($column, $distinct);
129
    }
130

131
    /**
132
     * @param string[] $tables (optional)
133
     *
134
     * @return  int
135
     */
136
    public function delete(array $tables = [])
137
    {
138
        return $this->build_delete()->delete($tables);
139
    }
140
}
141

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.