Passed
Push — develop ( 46a4a0...bc6bf4 )
by nguereza
02:24
created

QueryBuilder::delete()   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
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Database.php
33
 *
34
 *  The Database class
35
 *
36
 *  @package    Platine\Database
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database;
48
49
use Platine\Database\Query\Query as QueryCommand;
50
use Platine\Database\Query\Insert as InsertCommand;
51
use Platine\Database\Query\Update as UpdateCommand;
52
use Platine\Database\Query\Delete as DeleteCommand;
53
use Platine\Database\Query\InsertStatement;
54
55
/**
56
 * @class QueryBuilder
57
 * @package Platine\Database
58
 */
59
class QueryBuilder
60
{
61
    /**
62
     * The Schema instance
63
     * @var Schema
64
     */
65
    protected Schema $schema;
66
67
    /**
68
     * Class constructor
69
     * @param Connection $connection
70
     */
71
    public function __construct(protected Connection $connection)
72
    {
73
        $this->schema = $this->connection->getSchema();
74
    }
75
76
    /**
77
     * Return the connection instance
78
     * @return Connection
79
     */
80
    public function getConnection(): Connection
81
    {
82
        return $this->connection;
83
    }
84
85
    /**
86
     * Return the instance of schema
87
     * @return Schema
88
     */
89
    public function schema(): Schema
90
    {
91
        return $this->schema;
92
    }
93
94
    /**
95
     * Shortcut to Connection::query
96
     * @param string $sql
97
     * @param array<int, mixed> $params
98
     * @return ResultSet
99
     */
100
    public function query(string $sql, array $params = []): ResultSet
101
    {
102
        return $this->connection->query($sql, $params);
103
    }
104
105
    /**
106
     * Shortcut to Connection::transaction
107
     * @param callable $callback
108
     * @return mixed
109
     */
110
    public function transaction(callable $callback): mixed
111
    {
112
        return $this->connection->transaction($callback, $this);
113
    }
114
115
    /**
116
     * Execute a query in order to fetch or to delete records.
117
     * @param string|array<string> $tables Table name or an array of tables
118
     * @return QueryCommand
119
     */
120
    public function from(string|array $tables): QueryCommand
121
    {
122
        return new QueryCommand($this->connection, $tables);
123
    }
124
125
    /**
126
     * Insert new records into a table.
127
     * @param array<string, mixed> $values An array of values.
128
     * @return InsertCommand|InsertStatement
129
     */
130
    public function insert(array $values): InsertStatement
131
    {
132
        return (new InsertCommand($this->connection))->insert($values);
133
    }
134
135
    /**
136
     * Update records.
137
     * @param string $table
138
     * @return UpdateCommand
139
     */
140
    public function update(string $table): UpdateCommand
141
    {
142
        return new UpdateCommand($this->connection, $table);
143
    }
144
145
    /**
146
     * Delete records.
147
     * @param string $table
148
     * @return DeleteCommand
149
     */
150
    public function delete(string $table): DeleteCommand
151
    {
152
        return new DeleteCommand($this->connection, $table);
153
    }
154
}
155