• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

drago-ex / database / 21059733966

16 Jan 2026 07:59AM UTC coverage: 79.2% (+0.8%) from 78.4%
21059733966

push

github

web-flow
Update Database.php

9 of 10 new or added lines in 1 file covered. (90.0%)

1 existing line in 1 file now uncovered.

99 of 125 relevant lines covered (79.2%)

1.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.17
/src/Drago/Database/Database.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * Drago Extension
7
 * Package built on Nette Framework
8
 */
9

10
namespace Drago\Database;
11

12
use Dibi\Connection;
13
use Dibi\DriverException;
14
use Dibi\Exception;
15
use Dibi\Result;
16
use Drago\Attr\AttributeDetection;
17
use Drago\Attr\AttributeDetectionException;
18

19

20
/**
21
 * @template T
22
 * @property-read Connection $connection
23
 */
24
trait Database
25
{
26
        use AttributeDetection;
27

28
        /**
29
         * Get the database connection.
30
         *
31
         * @return Connection The database connection instance.
32
         */
33
        public function getConnection(): Connection
34
        {
35
                return $this->connection;
2✔
36
        }
37

38

39
        /**
40
         * Create a new ExtraFluent query builder.
41
         *
42
         * @return ExtraFluent<T> A new instance of ExtraFluent to build queries.
43
         * @throws AttributeDetectionException If the table name or class is not defined.
44
         */
45
        public function command(): ExtraFluent
46
        {
47
                $fluent = new ExtraFluent($this->getConnection());
2✔
48
                $fluent->className = $this->getClassName();
2✔
49
                return $fluent;
2✔
50
        }
51

52

53
        /**
54
         * Read records from the table.
55
         *
56
         * @param mixed ...$args Arguments for selecting columns.
57
         * @return ExtraFluent<T> The fluent query builder with the select statement.
58
         * @throws AttributeDetectionException If the table name or class is not defined.
59
         */
60
        public function read(...$args): ExtraFluent
2✔
61
        {
62
                return $this->command()
2✔
63
                        ->select(...$args)
2✔
64
                        ->from($this->getTableName());
2✔
65
        }
66

67

68
        /**
69
         * Find records by column name.
70
         *
71
         * @param string $column The column name to search.
72
         * @param mixed $args The value to match against the column.
73
         * @return ExtraFluent<T> The fluent query builder with the where condition.
74
         * @throws AttributeDetectionException If the table name or class is not defined.
75
         */
76
        public function find(string $column, mixed $args): ExtraFluent
2✔
77
        {
78
                return $this->read('*')
2✔
79
                        ->where('%n = ?', $column, $args);
2✔
80
        }
81

82

83
        /**
84
         * Get a record by its primary key.
85
         *
86
         * @param int $id The primary key of the record to fetch.
87
         * @return ExtraFluent<T> The fluent query builder for fetching the record.
88
         * @throws AttributeDetectionException If the table name or class is not defined.
89
         */
90
        public function get(int $id): ExtraFluent
2✔
91
        {
92
                return $this->read('*')
2✔
93
                        ->where('%n = ?', $this->getPrimaryKey(), $id);
2✔
94
        }
95

96

97
        /**
98
         * Delete a record by a specific column or by primary key if column is not provided.
99
         *
100
         * @param mixed $target Column name or value for primary key.
101
         * @param mixed|null $args Value to match (ignored if only primary key is used).
102
         * @return ExtraFluent The fluent query builder for deleting the record.
103
         * @throws AttributeDetectionException If table or class is not defined.
104
         */
105
        public function delete(mixed $target, mixed $args = null): ExtraFluent
2✔
106
        {
107
                $column = $args === null ? $this->getPrimaryKey() : $target;
2✔
108
                $value  = $args ?? $target;
2✔
109

110
                return $this->command()
2✔
111
                        ->delete()
2✔
112
                        ->from($this->getTableName())
2✔
113
                        ->where('%n = ?', $column, $value);
2✔
114
        }
115

116

117
        /**
118
         * Insert a new record into the table.
119
         *
120
         * @param array|iterable $args Values to insert (associative array: column => value).
121
         * @return ExtraFluent<T> The fluent query builder for inserting the record.
122
         * @throws AttributeDetectionException If the table name or class is not defined.
123
         */
124
        public function insert(iterable $args): ExtraFluent
2✔
125
        {
126
                return $this->command()
2✔
127
                        ->insert()
2✔
128
                        ->into($this->getTableName())
2✔
129
                        ->values($args);
2✔
130
        }
131

132

133
        /**
134
         * Update records in the table.
135
         *
136
         * @param array|iterable $args Values to update (associative array: column => value).
137
         * @return ExtraFluent<T> The fluent query builder for updating records.
138
         * @throws AttributeDetectionException If the table name or class is not defined.
139
         */
140
        public function update(iterable $args): ExtraFluent
2✔
141
        {
142
                return $this->command()
2✔
143
                        ->update($this->getTableName())
2✔
144
                        ->set($args);
2✔
145
        }
146

147

148
        /**
149
         * Insert or update a record.
150
         *
151
         * @param array|iterable $args The values to insert or update in the table.
152
         * @return Result|int|null The result of the query execution.
153
         * @throws AttributeDetectionException If the table name or class is not defined.
154
         * @throws Exception If an error occurs while executing the query.
155
         */
156
        public function save(iterable $args): Result|int|null
2✔
157
        {
158
                $key = $this->getPrimaryKey();
2✔
159
                if ($args instanceof EntityOracle) {
2✔
NEW
160
                        $args = $args->toArrayUpper();
×
UNCOV
161
                        $key = strtoupper($key);
×
162
                }
163

164
                $id = $args[$key] ?? null;
2✔
165

166
                $query = $id > 0
2✔
167
                        ? $this->update($args)->where('%n = ?', $key, $id)
2✔
168
                        : $this->insert($args);
2✔
169

170
                return $query->execute();
2✔
171
        }
172

173

174
        /**
175
         * Get the id of the last inserted record.
176
         *
177
         * @param string|null $sequence The sequence name (optional).
178
         * @return int The id of the last inserted record.
179
         * @throws Exception If an error occurs while fetching the insert id.
180
         */
181
        public function getInsertId(?string $sequence = null): int
182
        {
183
                return $this->getConnection()
×
184
                        ->getInsertId($sequence);
×
185
        }
186

187

188
        /**
189
         * Begins a transaction (optionally with savepoint).
190
         * @param string|null $savepoint Optional savepoint name.
191
         * @throws DriverException If the driver doesn't support transactions.
192
         */
193
        public function beginTransaction(?string $savepoint = null): void
194
        {
195
                $this->getConnection()
×
196
                        ->begin($savepoint);
×
197
        }
198

199

200
        /**
201
         * Commits the transaction (optionally to a savepoint).
202
         * @param string|null $savepoint Optional savepoint name.
203
         * @throws DriverException If commit fails.
204
         */
205
        public function commit(?string $savepoint = null): void
206
        {
207
                $this->getConnection()
×
208
                        ->commit($savepoint);
×
209
        }
210

211

212
        /**
213
         * Rolls back the transaction (optionally to a savepoint).
214
         * @param string|null $savepoint Optional savepoint name.
215
         * @throws DriverException If rollback fails.
216
         */
217
        public function rollBack(?string $savepoint = null): void
218
        {
219
                $this->getConnection()
×
220
                        ->rollback($savepoint);
×
221
        }
222
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc