Passed
Push — develop ( d2d625...405251 )
by nguereza
03:36
created

QueryBuilder::getConnection()   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 0
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   http://www.iacademy.cf
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\InsertStatement;
53
54
/**
55
 * Class QueryBuilder
56
 * @package Platine\Database
57
 */
58
class QueryBuilder
59
{
60
61
    /**
62
     * The Connection instance
63
     * @var Connection
64
     */
65
    protected Connection $connection;
66
67
    /**
68
     * The Schema instance
69
     * @var Schema
70
     */
71
    protected Schema $schema;
72
73
    /**
74
     * Class constructor
75
     * @param Connection $connection
76
     */
77
    public function __construct(Connection $connection)
78
    {
79
        $this->connection = $connection;
80
        $this->schema = $this->connection->getSchema();
81
    }
82
83
    /**
84
     * Return the connection instance
85
     * @return Connection
86
     */
87
    public function getConnection(): Connection
88
    {
89
        return $this->connection;
90
    }
91
92
    /**
93
     * Return the instance of schema
94
     * @return Schema
95
     */
96
    public function schema(): Schema
97
    {
98
        return $this->schema;
99
    }
100
101
    /**
102
     * Shortcut to Connection::query
103
     * @param string $sql
104
     * @param array<int, mixed> $params
105
     * @return ResultSet
106
     */
107
    public function query(string $sql, array $params = []): ResultSet
108
    {
109
        return $this->connection->query($sql, $params);
110
    }
111
112
    /**
113
     * Shortcut to Connection::transaction
114
     * @param callable $callback
115
     * @return mixed
116
     */
117
    public function transaction(callable $callback)
118
    {
119
        return $this->connection->transaction($callback, $this);
120
    }
121
122
    /**
123
     * Execute a query in order to fetch or to delete records.
124
     * @param string|array<string> $tables Table name or an array of tables
125
     * @return QueryCommand
126
     */
127
    public function from($tables): QueryCommand
128
    {
129
        return new QueryCommand($this->connection, $tables);
130
    }
131
132
    /**
133
     * Insert new records into a table.
134
     * @param array<string, mixed> $values An array of values.
135
     * @return InsertCommand|InsertStatement
136
     */
137
    public function insert(array $values): InsertStatement
138
    {
139
        return (new InsertCommand($this->connection))->insert($values);
140
    }
141
142
    /**
143
     * Update records.
144
     * @param string $table
145
     * @return UpdateCommand
146
     */
147
    public function update(string $table): UpdateCommand
148
    {
149
        return new UpdateCommand($this->connection, $table);
150
    }
151
}
152