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

drago-ex / database / 12945844337

24 Jan 2025 08:19AM UTC coverage: 92.0%. Remained the same
12945844337

push

github

scrs_zdenek
dev

46 of 50 relevant lines covered (92.0%)

0.92 hits per line

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

92.31
/src/Drago/Database/Repository.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\Exception;
14
use Dibi\Fluent;
15
use Dibi\Result;
16
use Drago\Attr\AttributeDetection;
17
use Drago\Attr\AttributeDetectionException;
18

19

20
/**
21
 * Repository base.
22
 * Provides basic CRUD operations for entities in the database.
23
 *
24
 * @property-read Connection $db The database connection used for queries.
25
 */
26
trait Repository
27
{
28
        use AttributeDetection;
29

30
        /**
31
         * Retrieves records from a table based on the provided conditions.
32
         *
33
         * @param mixed ...$cond Conditions to apply to the query (e.g. column = value).
34
         * @return Fluent A fluent query builder instance.
35
         * @throws AttributeDetectionException If attribute detection fails.
36
         */
37
        public function table(...$cond): Fluent
1✔
38
        {
39
                $query = $this->db->select('*')
1✔
40
                        ->from($this->getTableName());
1✔
41

42
                if ($cond) {
1✔
43
                        $query->where(...$cond);
1✔
44
                }
45

46
                return $query;
1✔
47
        }
48

49

50
        /**
51
         * Finds a record by its primary key (ID).
52
         *
53
         * @param int $id The ID of the record to retrieve.
54
         * @return Fluent A fluent query builder instance.
55
         * @throws AttributeDetectionException If attribute detection fails.
56
         */
57
        public function get(int $id): Fluent
1✔
58
        {
59
                return $this->table('%n = ?', $this->getPrimaryKey(), $id);
1✔
60
        }
61

62

63
        /**
64
         * Deletes a record by its primary key (ID).
65
         *
66
         * @param int $id The ID of the record to delete.
67
         * @return Result|int|null The result of the delete operation (number of affected rows or result object).
68
         * @throws Exception If a database error occurs.
69
         * @throws AttributeDetectionException If attribute detection fails.
70
         */
71
        public function remove(int $id): Result|int|null
1✔
72
        {
73
                return $this->db->delete($this->getTableName())
1✔
74
                        ->where('%n = ?', $this->getPrimaryKey(), $id)
1✔
75
                        ->execute();
1✔
76
        }
77

78

79
        /**
80
         * Saves a record in the database (either insert or update).
81
         *
82
         * @param mixed $values The data to insert or update.
83
         * @return Result|int|null The result of the insert or update operation.
84
         * @throws Exception If a database error occurs.
85
         * @throws AttributeDetectionException If attribute detection fails.
86
         */
87
        public function put(mixed $values): Result|int|null
1✔
88
        {
89
                $key = $this->getPrimaryKey();
1✔
90

91
                // Convert entity to array format if it's an instance of Entity or EntityOracle
92
                if ($values instanceof Entity) {
1✔
93
                        $values = $values->toArray();
1✔
94
                } elseif ($values instanceof EntityOracle) {
1✔
95
                        $values = $values->toArrayUpper();
×
96
                        $key = strtoupper($key);
×
97
                }
98

99
                $id = $values[$key] ?? null;
1✔
100

101
                $query = $id > 0
1✔
102
                        ? $this->db->update($this->getTableName(), $values)->where('%n = ?', $key, $id)
1✔
103
                        : $this->db->insert($this->getTableName(), $values);
1✔
104

105
                return $query->execute();
1✔
106
        }
107

108

109
        /**
110
         * Retrieves the ID of the last inserted record.
111
         *
112
         * @param string|null $sequence The sequence name to use (optional).
113
         * @return int The ID of the inserted record.
114
         * @throws Exception If a database error occurs.
115
         */
116
        public function getInsertId(?string $sequence = null): int
1✔
117
        {
118
                return $this->db->getInsertId($sequence);
1✔
119
        }
120
}
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