Passed
Pull Request — main (#122)
by Andreas
02:06
created

CrateSchemaManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 96.61%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 13
eloc 54
c 3
b 1
f 0
dl 0
loc 113
ccs 57
cts 59
cp 0.9661
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A listTableDetails() 0 15 1
A _getPortableTableIndexesList() 0 15 2
A flatten() 0 11 3
A _getPortableTablesList() 0 9 2
A _getPortableTableColumnDefinition() 0 32 5
1
<?php
2
3
/**
4
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
5
 * license agreements.  See the NOTICE file distributed with this work for
6
 * additional information regarding copyright ownership.  Crate licenses
7
 * this file to you under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.  You may
9
 * obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
16
 * License for the specific language governing permissions and limitations
17
 * under the License.
18
 *
19
 * However, if you have executed another commercial license agreement
20
 * with Crate these terms will supersede the license and you may use the
21
 * software solely pursuant to the terms of the relevant commercial agreement.
22
 */
23
24
namespace Crate\DBAL\Schema;
25
26
use Crate\DBAL\Platforms\CratePlatform;
27
use Doctrine\DBAL\Schema\AbstractSchemaManager;
28
use Doctrine\DBAL\Schema\Column;
29
use Doctrine\DBAL\Schema\Table;
30
use Doctrine\DBAL\Types\Type;
31
32
/**
33
 * Schema manager for the CrateDB RDBMS.
34
 *
35
 * @extends AbstractSchemaManager<CratePlatform>
36
 */
37
class CrateSchemaManager extends AbstractSchemaManager
38
{
39
    /**
40
     * {@inheritdoc}
41
     *
42
     */
43
    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
44 8
    protected function _getPortableTableIndexesList($tableIndexes, $tableName = null): array
45
    {
46 8
        $buffer = [];
47 8
        foreach ($tableIndexes as $row) {
48 6
            $isPrimary = $row['constraint_type'] == 'PRIMARY KEY';
49 6
            $buffer[] = [
50 6
                'key_name' => $row['constraint_name'],
51 6
                'column_name' => $row['column_name'],
52 6
                'non_unique' => ! $isPrimary,
53 6
                'primary' => $isPrimary,
54 6
                'where' => '',
55 6
            ];
56
        }
57
58 8
        return parent::_getPortableTableIndexesList($buffer, $tableName);
59
    }
60
    /**
61
     * {@inheritDoc}
62
     */
63
    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
64 8
    protected function _getPortableTableColumnDefinition($tableColumn): Column
65
    {
66 8
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
67
68 8
        if (!isset($tableColumn['column_name'])) {
69
            $tableColumn['column_name'] = '';
70
        }
71
72
        // Normalize is_nullable to handle both boolean (< 6.1) and string (>= 6.1) values.
73 8
        if (!isset($tableColumn['is_nullable'])) {
74
            $tableColumn['is_nullable'] = 'YES';
75 8
        } elseif (is_bool($tableColumn['is_nullable'])) {
76
            // Convert boolean to string for CrateDB < 6.1.
77 4
            $tableColumn['is_nullable'] = $tableColumn['is_nullable'] ? 'YES' : 'NO';
78
        }
79
80 8
        $dbType = strtolower($tableColumn['data_type']);
81 8
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
82
83 8
        $options = array(
84 8
            'length'        => null,
85 8
            'notnull'       => $tableColumn['is_nullable'] !== 'YES',
86 8
            'default'       => null,
87 8
            'precision'     => null,
88 8
            'scale'         => null,
89 8
            'fixed'         => null,
90 8
            'unsigned'      => false,
91 8
            'autoincrement' => false,
92 8
            'comment'       => '',
93 8
        );
94
95 8
        return new Column($tableColumn['column_name'], Type::getType($type), $options);
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101
    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
102 32
    protected function _getPortableTablesList($tables)
103
    {
104 32
        $tableNames = array();
105 32
        foreach ($tables as $tableRow) {
106 26
            $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
107 26
            $tableNames[] = $tableRow['table_name']; // ignore schema for now
108
        }
109
110 32
        return $tableNames;
111
    }
112
113
    /**
114
     * Flattens a multidimensional array into a 1 dimensional array, where
115
     * keys are concatinated with '.'
116
     *
117
     * @return array
118
     */
119 6
    private static function flatten(array $array, string $prefix = ''): array
120
    {
121 6
        $result = array();
122 6
        foreach ($array as $key => $value) {
123 6
            if (is_array($value)) {
124 6
                $result = $result + self::flatten($value, $prefix . $key . '.');
125
            } else {
126 6
                $result[$prefix . $key] = $value;
127
            }
128
        }
129 6
        return $result;
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135 6
    public function listTableDetails($name): Table
136
    {
137 6
        $columns = $this->listTableColumns($name);
138 6
        $indexes = $this->listTableIndexes($name);
139 6
        $options = [];
140
141 6
        $s = $this->_conn->fetchAssociative($this->_platform->getTableOptionsSQL($name));
142
143 6
        $options['sharding_routing_column'] = $s['clustered_by'];
144 6
        $options['sharding_num_shards'] = $s['number_of_shards'];
145 6
        $options['partition_columns'] = $s['partitioned_by'];
146 6
        $options['table_options'] = self::flatten($s['settings']);
147 6
        $options['table_options']['number_of_replicas'] = $s['number_of_replicas'];
148 6
        $options['table_options']['column_policy'] = $s['column_policy'];
149 6
        return new Table($name, $columns, $indexes, [], [], $options);
150
    }
151
}
152