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

drago-ex / database / 8371335617

21 Mar 2024 07:43AM UTC coverage: 92.308% (-1.7%) from 94.0%
8371335617

push

github

scrs_zdenek
Update

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

1 existing line in 1 file now uncovered.

48 of 52 relevant lines covered (92.31%)

2.77 hits per line

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

92.86
/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
                $primaryKey = $this->getPrimaryKey();
3✔
78
                if ($data instanceof Entity) {
3✔
79
                        $data = $data->toArray();
3✔
80

81
                } elseif ($data instanceof EntityOracle) {
3✔
UNCOV
82
                        $data = $data->toArrayUpper();
×
NEW
83
                        $primaryKey = strtoupper($primaryKey);
×
84
                }
85

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

93

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