Issues (265)

src/DatabaseInterface.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database;
13
14
use Cycle\Database\Driver\DriverInterface;
15
use Cycle\Database\Exception\StatementException;
16
use Cycle\Database\Query\DeleteQuery;
17
use Cycle\Database\Query\InsertQuery;
18
use Cycle\Database\Query\SelectQuery;
19
use Cycle\Database\Query\UpdateQuery;
20
21
/**
22
 * DatabaseInterface is high level abstraction used to represent single database. You must always
23
 * check database type using getType() method before writing plain SQL for execute and query methods
24
 * (unless you are locking your module/application to one database).
25
 *
26
 * @method DatabaseInterface withoutCache() Get a new Database instance without query cache or the same instance
27
 *         if no cache is used. Will be added the next major release.
28
 */
29
interface DatabaseInterface
30
{
31
    // Driver types
32
    public const WRITE = 0;
33
    public const READ = 1;
34
35
    /**
36
     * @psalm-return non-empty-string
37
     */
38
    public function getName(): string;
39
40
    /**
41
     * Database type matched to one of database constants. You MUST write SQL for execute and query
42
     * methods by respecting result of this method.
43
     *
44
     * @psalm-return non-empty-string
45
     */
46
    public function getType(): string;
47
48
    public function getDriver(int $type = self::WRITE): DriverInterface;
49
50
    /**
51
     * Return database with new isolation prefix.
52
     */
53
    public function withPrefix(string $prefix, bool $add = true): self;
54
55
    public function getPrefix(): string;
56
57
    /**
58
     * Check if table exists.
59
     *
60
     * @psalm-param non-empty-string $name
61
     */
62
    public function hasTable(string $name): bool;
63
64
    /**
65
     * Get all associated database tables.
66
     *
67
     * @return TableInterface[]
68
     */
69
    public function getTables(): array;
70
71
    /**
72
     * @psalm-param non-empty-string $name
73
     */
74
    public function table(string $name): TableInterface;
75
76
    /**
77
     * Execute statement and return number of affected rows.
78
     *
79
     * @psalm-param non-empty-string $query
80
     *
81
     * @param array $parameters Parameters to be binded into query.
82
     *
83
     * @throws StatementException
84
     */
85
    public function execute(string $query, array $parameters = []): int;
86
87
    /**
88
     * Execute statement and return query iterator.
89
     *
90
     * @psalm-param non-empty-string $query
91
     *
92
     * @param array  $parameters Parameters to be binded into query.
93
     *
94
     * @throws StatementException
95
     */
96
    public function query(string $query, array $parameters = []): StatementInterface;
97
98
    /**
99
     * Get instance of InsertBuilder associated with current Database.
100
     *
101
     * @param string $table Table where values should be inserted to.
102
     *
103
     * @see self::withoutCache() May be useful to disable query cache for batch inserts.
104
     */
105
    public function insert(string $table = ''): InsertQuery;
106
107
    /**
108
     * Get instance of UpdateBuilder associated with current Database.
109
     *
110
     * @param string $table  Table where rows should be updated in.
111
     * @param array  $values Initial set of columns to update associated with their values.
112
     * @param array  $where  Initial set of where rules specified as array.
113
     */
114
    public function update(string $table = '', array $values = [], array $where = []): UpdateQuery;
115
116
    /**
117
     * Get instance of DeleteBuilder associated with current Database.
118
     *
119
     * @param string $table Table where rows should be deleted from.
120
     * @param array  $where Initial set of where rules specified as array.
121
     */
122
    public function delete(string $table = '', array $where = []): DeleteQuery;
123
124
    /**
125
     * Get instance of SelectBuilder associated with current Database.
126
     *
127
     * @param array|string $columns Columns to select.
128
     */
129
    public function select(mixed $columns = '*'): SelectQuery;
130
131
    /**
132
     * Execute multiple commands defined by Closure function inside one transaction. Closure or
133
     * function must receive only one argument - DatabaseInterface instance.
134
     *
135
     * @link http://en.wikipedia.org/wiki/Database_transaction
136
     *
137
     * @template CallbackResult
138
     *
139
     * @param callable(DatabaseInterface): CallbackResult $callback
140
     *
141
     * @return CallbackResult
0 ignored issues
show
The type Cycle\Database\CallbackResult 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...
142
     * @throws \Throwable
143
     *
144
     */
145
    public function transaction(callable $callback, ?string $isolationLevel = null): mixed;
146
147
    /**
148
     * Start database transaction.
149
     *
150
     * @link http://en.wikipedia.org/wiki/Database_transaction
151
     */
152
    public function begin(?string $isolationLevel = null): bool;
153
154
    /**
155
     * Commit the active database transaction.
156
     */
157
    public function commit(): bool;
158
159
    /**
160
     * Rollback the active database transaction.
161
     */
162
    public function rollback(): bool;
163
}
164