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

CrateSchemaManager::_getPortableTablesList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
namespace Crate\DBAL\Schema;
23
24
use Crate\DBAL\Platforms\CratePlatform;
25
use Doctrine\DBAL\Schema\AbstractSchemaManager;
26
use Doctrine\DBAL\Schema\Column;
27
use Doctrine\DBAL\Types\Type;
28
use Doctrine\DBAL\Schema\Table;
29
30
/**
31
 * Schema manager for the CrateDB RDBMS.
32
 *
33
 * @extends AbstractSchemaManager<CratePlatform>
34
 */
35
class CrateSchemaManager extends AbstractSchemaManager
36
{
37
    /**
38
     * {@inheritdoc}
39
     *
40
     */
41
    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
42 8
    protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
43
    {
44 8
        $buffer = [];
45 8
        foreach ($tableIndexes as $row) {
46 6
            $isPrimary = $row['constraint_type'] == 'PRIMARY KEY';
47 6
            $buffer[] = [
48 6
                'key_name' => $row['constraint_name'],
49 6
                'column_name' => $row['column_name'],
50 6
                'non_unique' => ! $isPrimary,
51 6
                'primary' => $isPrimary,
52 6
                'where' => '',
53 6
            ];
54
        }
55
56 8
        return parent::_getPortableTableIndexesList($buffer, $tableName);
57
    }
58
    /**
59
     * {@inheritDoc}
60
     */
61
    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
62 8
    protected function _getPortableTableColumnDefinition($tableColumn)
63
    {
64 8
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
65
66 8
        if (!isset($tableColumn['column_name'])) {
67
            $tableColumn['column_name'] = '';
68
        }
69
70
        // Normalize is_nullable to handle both boolean (< 6.1) and string (>= 6.1) values.
71 8
        if (!isset($tableColumn['is_nullable'])) {
72
            $tableColumn['is_nullable'] = 'YES';
73 8
        } elseif (is_bool($tableColumn['is_nullable'])) {
74
            // Convert boolean to string for CrateDB < 6.1.
75 4
            $tableColumn['is_nullable'] = $tableColumn['is_nullable'] ? 'YES' : 'NO';
76
        }
77
78 8
        $dbType = strtolower($tableColumn['data_type']);
79 8
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
80
81 8
        $options = array(
82 8
            'length'        => null,
83 8
            'notnull'       => $tableColumn['is_nullable'] !== 'YES',
84 8
            'default'       => null,
85 8
            'precision'     => null,
86 8
            'scale'         => null,
87 8
            'fixed'         => null,
88 8
            'unsigned'      => false,
89 8
            'autoincrement' => false,
90 8
            'comment'       => '',
91 8
        );
92
93 8
        return new Column($tableColumn['column_name'], Type::getType($type), $options);
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
100 32
    protected function _getPortableTablesList($tables)
101
    {
102 32
        $tableNames = array();
103 32
        foreach ($tables as $tableRow) {
104 26
            $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
105 26
            $tableNames[] = $tableRow['table_name']; // ignore schema for now
106
        }
107
108 32
        return $tableNames;
109
    }
110
111
    /**
112
     * Flattens a multidimensional array into a 1 dimensional array, where
113
     * keys are concatinated with '.'
114
     *
115
     * @return array
116
     */
117 6
    private function flatten(array $array, string $prefix = '') : array
118
    {
119 6
        $result = array();
120 6
        foreach ($array as $key => $value) {
121 6
            if (is_array($value)) {
122 6
                $result = $result + self::flatten($value, $prefix . $key . '.');
0 ignored issues
show
Bug Best Practice introduced by
The method Crate\DBAL\Schema\CrateSchemaManager::flatten() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
                $result = $result + self::/** @scrutinizer ignore-call */ flatten($value, $prefix . $key . '.');
Loading history...
123
            } else {
124 6
                $result[$prefix . $key] = $value;
125
            }
126
        }
127 6
        return $result;
128
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 6
    public function listTableDetails($name) : Table
134
    {
135 6
        $columns = $this->listTableColumns($name);
136 6
        $indexes = $this->listTableIndexes($name);
137 6
        $options = [];
138
139 6
        $s = $this->_conn->fetchAssociative($this->_platform->getTableOptionsSQL($name));
140
141 6
        $options['sharding_routing_column'] = $s['clustered_by'];
142 6
        $options['sharding_num_shards'] = $s['number_of_shards'];
143 6
        $options['partition_columns'] = $s['partitioned_by'];
144 6
        $options['table_options'] = self::flatten($s['settings']);
0 ignored issues
show
Bug Best Practice introduced by
The method Crate\DBAL\Schema\CrateSchemaManager::flatten() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        /** @scrutinizer ignore-call */ 
145
        $options['table_options'] = self::flatten($s['settings']);
Loading history...
145 6
        $options['table_options']['number_of_replicas'] = $s['number_of_replicas'];
146 6
        $options['table_options']['column_policy'] = $s['column_policy'];
147 6
        return new Table($name, $columns, $indexes, [], [], $options);
148
    }
149
}
150