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

drago-ex / database / 8374342631

21 Mar 2024 11:42AM UTC coverage: 92.0% (-0.3%) from 92.308%
8374342631

push

github

scrs_zdenek
Update

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

46 of 50 relevant lines covered (92.0%)

2.76 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
 * @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(...$args): Fluent
3✔
33
        {
34
                $query = $this->db->select('*')
3✔
35
                        ->from($this->getDatabaseTable());
3✔
36

37
                if ($args) {
3✔
38
                        $query->where(...$args);
3✔
39
                }
40

41
                return $query;
3✔
42
        }
43

44

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

54

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

67

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

79
                } elseif ($values instanceof EntityOracle) {
3✔
UNCOV
80
                        $values = $values->toArrayUpper();
×
UNCOV
81
                        $key = strtoupper($key);
×
82
                }
83

84
                $id = $values[$key] ?? null;
3✔
85
                $query = $id > 0
3✔
86
                        ? $this->db->update($this->getDatabaseTable(), $values)->where("$key = ?", $id)
3✔
87
                        : $this->db->insert($this->getDatabaseTable(), $values);
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