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

letsdrink / ouzo / 4784818091

pending completion
4784818091

push

github

Bartosz BaƄkowski
Fixed tests.

4833 of 5741 relevant lines covered (84.18%)

33.59 hits per line

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

12.9
/src/Ouzo/Core/Tools/Model/Template/Dialect/PostgresDialect.php
1
<?php
2
/*
3
 * Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo
4
 * This file is made available under the MIT License (view the LICENSE file for more information).
5
 */
6

7
namespace Ouzo\Tools\Model\Template\Dialect;
8

9
use Ouzo\Db;
10
use Ouzo\Tools\Model\Template\DatabaseColumn;
11
use Ouzo\Utilities\Arrays;
12

13
class PostgresDialect extends Dialect
14
{
15
    public function primaryKey(): string
16
    {
17
        return $this->getPrimaryKey($this->tableName());
×
18
    }
19

20
    public function sequence(): string
21
    {
22
        $tableColumns = $this->getTableColumns($this->tableName());
×
23
        return $this->getSequenceName($tableColumns, $this->primaryKey());
×
24
    }
25

26
    public function columns(): array
27
    {
28
        return array_values($this->getTableColumns($this->tableName()));
×
29
    }
30

31
    public function getSequenceName(array $tableColumns, string $primaryKey): string
32
    {
33
        $primaryColumnInfo = Arrays::getValue($tableColumns, $primaryKey);
1✔
34
        if (!$primaryColumnInfo || empty($primaryColumnInfo->default)) {
1✔
35
            return '';
×
36
        }
37
        preg_match("/nextval\('(?<sequence>.*)'.*\)/", strtolower($primaryColumnInfo->default), $matches);
1✔
38
        return Arrays::getValue($matches, 'sequence');
1✔
39
    }
40

41
    private function getPrimaryKey(string $tableName): string
42
    {
43
        $primaryKey = Db::getInstance()->query(
×
44
            "SELECT pg_attribute.attname
×
45
         FROM pg_index, pg_class, pg_attribute
46
         WHERE
47
            pg_class.oid = '$tableName'::REGCLASS AND
×
48
            indrelid = pg_class.oid AND
49
            pg_attribute.attrelid = pg_class.oid AND
50
            pg_attribute.attnum = ANY(pg_index.indkey)
51
            AND indisprimary;
52
        ")->fetch();
×
53
        if ($primaryKey) {
×
54
            return Arrays::getValue($primaryKey, 'attname');
×
55
        }
56
        return '';
×
57
    }
58

59
    private function getTableColumns(string $tableName): array
60
    {
61
        $schema = Db::getInstance()
×
62
            ->query("SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_name = '$tableName' ORDER BY ordinal_position")
×
63
            ->fetchAll();
×
64
        $tableColumns = [];
×
65
        foreach ($schema as $columnInfo) {
×
66
            $columnName = $columnInfo['column_name'];
×
67
            $columnDefault = $columnInfo['column_default'];
×
68
            $columnType = $this->postgresDataTypeToPhpType($columnInfo['data_type']);
×
69
            $tableColumns[$columnName] = new DatabaseColumn($columnName, $columnType, $columnDefault);
×
70
        }
71
        return $tableColumns;
×
72
    }
73

74
    private function postgresDataTypeToPhpType(string $dataType): string
75
    {
76
        return match ($dataType) {
×
77
            'boolean' => 'bool',
×
78
            'integer' => 'int',
×
79
            default => 'string'
×
80
        };
×
81
    }
82
}
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