Skip to content

Commit 57d9605

Browse files
authored
Update Database.php
1 parent 0e6c823 commit 57d9605

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

src/Drago/Database/Database.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Drago\Database;
1111

1212
use Dibi\Connection;
13+
use Dibi\DriverException;
1314
use Dibi\Exception;
1415
use Dibi\Result;
1516
use Drago\Attr\AttributeDetection;
@@ -110,6 +111,36 @@ public function delete(string $column, mixed $args): ExtraFluent
110111
}
111112

112113

114+
/**
115+
* Insert a new record into the table.
116+
*
117+
* @param mixed ...$values Values or SQL fragments to insert.
118+
* @return ExtraFluent<T> The fluent query builder for inserting the record.
119+
* @throws AttributeDetectionException If the table name or class is not defined.
120+
*/
121+
public function insert(...$values): ExtraFluent
122+
{
123+
return $this->command()
124+
->insert(...$values)
125+
->into($this->getTableName());
126+
}
127+
128+
129+
/**
130+
* Update records in the table.
131+
*
132+
* @param mixed ...$values Values or SQL fragments to update.
133+
* @return ExtraFluent<T> The fluent query builder for updating records.
134+
* @throws AttributeDetectionException If the table name or class is not defined.
135+
*/
136+
public function update(...$values): ExtraFluent
137+
{
138+
return $this->command()
139+
->update(...$values)
140+
->from($this->getTableName());
141+
}
142+
143+
113144
/**
114145
* Insert or update a record.
115146
*
@@ -121,20 +152,20 @@ public function delete(string $column, mixed $args): ExtraFluent
121152
public function save(mixed $values): Result|int|null
122153
{
123154
$key = $this->getPrimaryKey();
124-
$table = $this->getTableName();
125155

126156
// Convert entity to array if necessary
127157
if ($values instanceof Entity) {
128158
$values = $values->toArray();
159+
129160
} elseif ($values instanceof EntityOracle) {
130161
$values = $values->toArrayUpper();
131162
$key = strtoupper($key);
132163
}
133164

134165
$id = $values[$key] ?? null;
135166
$query = $id > 0
136-
? $this->getConnection()->update($table, $values)->where('%n = ?', $key, $id)
137-
: $this->getConnection()->insert($table, $values);
167+
? $this->update($values)->where('%n = ?', $key, $id)
168+
: $this->insert($values);
138169

139170
return $query->execute();
140171
}
@@ -155,8 +186,9 @@ public function getInsertId(?string $sequence = null): int
155186

156187

157188
/**
158-
* Begins a transaction (if supported).
159-
* @throws DriverException
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.
160192
*/
161193
public function beginTransaction(?string $savepoint = null): void
162194
{
@@ -166,8 +198,9 @@ public function beginTransaction(?string $savepoint = null): void
166198

167199

168200
/**
169-
* Commits statements in a transaction.
170-
* @throws DriverException
201+
* Commits the transaction (optionally to a savepoint).
202+
* @param string|null $savepoint Optional savepoint name.
203+
* @throws DriverException If commit fails.
171204
*/
172205
public function commit(?string $savepoint = null): void
173206
{
@@ -177,8 +210,9 @@ public function commit(?string $savepoint = null): void
177210

178211

179212
/**
180-
* Rollback changes in a transaction.
181-
* @throws DriverException
213+
* Rolls back the transaction (optionally to a savepoint).
214+
* @param string|null $savepoint Optional savepoint name.
215+
* @throws DriverException If rollback fails.
182216
*/
183217
public function rollBack(?string $savepoint = null): void
184218
{

0 commit comments

Comments
 (0)