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

drago-ex / database / 8356500215

20 Mar 2024 09:21AM UTC coverage: 94.0% (-2.0%) from 96.0%
8356500215

push

github

scrs_zdenek
dev

47 of 50 relevant lines covered (94.0%)

2.82 hits per line

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

96.15
/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
 * @property-read Connection $db
23
 */
24
trait Repository
25
{
26
        use AttributeDetection;
27

28
        /**
29
         * Get records from table.
30
         * @throws AttributeDetectionException
31
         */
32
        public function table(?string $column = null, ...$args): Fluent
3✔
33
        {
34
                $query = $this->db
3✔
35
                        ->select('*')
3✔
36
                        ->from($this->getDatabaseTable());
3✔
37

38
                if ($column && $args) {
3✔
39
                        $query->where("$column = ?", $args);
3✔
40
                }
41

42
                return $query;
3✔
43
        }
44

45

46
        /**
47
         * Find record by id.
48
         * @throws AttributeDetectionException
49
         */
50
        public function get(int $id): Fluent
3✔
51
        {
52
                return $this->table($this->getPrimaryKey(), $id);
3✔
53
        }
54

55

56
        /**
57
         * Deleting a records by the primary key.
58
         * @throws Exception
59
         * @throws AttributeDetectionException
60
         */
61
        public function remove(int $id): Result|int|null
3✔
62
        {
63
                return $this->db
3✔
64
                        ->delete($this->getDatabaseTable())
3✔
65
                        ->where("{$this->getPrimaryKey()} = ?", $id)
3✔
66
                        ->execute();
3✔
67
        }
68

69

70
        /**
71
         * Saving a records.
72
         * @throws Exception
73
         * @throws AttributeDetectionException
74
         */
75
        public function put(mixed $data): Result|int|null
3✔
76
        {
77
                if ($data instanceof Entity) {
3✔
78
                        $data = $data->toArray();
3✔
79

80
                } elseif ($data instanceof EntityOracle) {
3✔
81
                        $data = $data->toArrayUpper();
×
82
                }
83

84
                $id = $data[$this->getPrimaryKey()] ?? null;
3✔
85
                $query = $id > 0
3✔
86
                        ? $this->db->update($this->getDatabaseTable(), $data)->where("{$this->getPrimaryKey()} = ?", $id)
3✔
87
                        : $this->db->insert($this->getDatabaseTable(), $data);
3✔
88
                return $query->execute();
3✔
89
        }
90

91

92
        /**
93
         * Get the id of the inserted record.
94
         * @throws Exception
95
         */
96
        public function getInsertId(?string $sequence = null): int
3✔
97
        {
98
                return $this->db->getInsertId($sequence);
3✔
99
        }
100
}
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