Skip to content

Commit a15997a

Browse files
committed
init
0 parents  commit a15997a

9 files changed

+115
-0
lines changed

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "leven-framework/dba-common",
3+
"type": "library",
4+
"suggest": {
5+
"leven-framework/dba-mock": "Mock a database using e.g. a local file",
6+
"leven-framework/dba-mysql": "PDO MySQL adapter"
7+
},
8+
"autoload": {
9+
"psr-4": {
10+
"Leven\\DBA\\Common\\": "src/"
11+
}
12+
}
13+
}

src/DatabaseAdapterInterface.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php namespace Leven\DBA\Common;
2+
3+
use Leven\DBA\Common\Exception\{ArgumentValidationException, Driver\DriverException, EmptyResultException};
4+
5+
interface DatabaseAdapterInterface {
6+
7+
public function escapeValue(string $string);
8+
public function escapeName(string $string);
9+
10+
/**
11+
* @throws DriverException
12+
*/
13+
public function schema(string $table): array;
14+
15+
/**
16+
* @throws DriverException
17+
*/
18+
public function count(string $table): int;
19+
20+
/**
21+
* @throws ArgumentValidationException
22+
* @throws DriverException
23+
* @throws EmptyResultException
24+
*/
25+
public function get(string $table, array|string $columns, array $conditions, array $options): DatabaseAdapterResponse;
26+
27+
/**
28+
* @throws ArgumentValidationException
29+
* @throws DriverException
30+
*/
31+
public function insert(string $table, array $data): DatabaseAdapterResponse;
32+
33+
/**
34+
* @throws ArgumentValidationException
35+
* @throws DriverException
36+
*/
37+
public function update(string $table, array $data, array $conditions, array $options): DatabaseAdapterResponse;
38+
39+
/**
40+
* @throws ArgumentValidationException
41+
* @throws DriverException
42+
*/
43+
public function delete(string $table, array $conditions, array $options): DatabaseAdapterResponse;
44+
45+
46+
47+
/**
48+
* @throws DriverException
49+
*/
50+
public function txnBegin();
51+
52+
/**
53+
* @throws DriverException
54+
*/
55+
public function txnCommit();
56+
57+
/**
58+
* @throws DriverException
59+
*/
60+
public function txnRollback();
61+
62+
}

src/DatabaseAdapterResponse.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace Leven\DBA\Common;
2+
3+
use ArrayIterator, ArrayObject, IteratorAggregate;
4+
5+
class DatabaseAdapterResponse implements IteratorAggregate {
6+
7+
public function __construct(
8+
public string|null $query = null,
9+
public int $count = 0,
10+
public array $rows = [],
11+
public array|null $row = null,
12+
public int|string|null $lastID = null
13+
) { }
14+
15+
public function getIterator(): ArrayIterator
16+
{
17+
$o = new ArrayObject($this->rows);
18+
return $o->getIterator();
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php namespace Leven\DBA\Common\Exception;
2+
3+
final class ArgumentValidationException extends DatabaseAdapterException {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php namespace Leven\DBA\Common\Exception;
2+
3+
abstract class DatabaseAdapterException extends \Exception {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php namespace Leven\DBA\Common\Exception\Driver;
2+
3+
use Leven\DBA\Common\Exception\DatabaseAdapterException;
4+
5+
class DriverException extends DatabaseAdapterException {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php namespace Leven\DBA\Common\Exception\Driver;
2+
3+
final class NotImplementedException extends DriverException {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php namespace Leven\DBA\Common\Exception\Driver;
2+
3+
final class TxnNotActiveException extends DriverException {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php namespace Leven\DBA\Common\Exception;
2+
3+
final class EmptyResultException extends DatabaseAdapterException {}

0 commit comments

Comments
 (0)