Passed
Pull Request — main (#144)
by Andreas
03:36 queued 01:33
created

CrateSchemaManager::_getPortableTableIndexesList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.9332
cc 2
nc 2
nop 2
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 Doctrine\DBAL\Schema\AbstractSchemaManager;
25
use Doctrine\DBAL\Schema\Column;
26
use Doctrine\DBAL\Types\Type;
27
use Doctrine\DBAL\Schema\Table;
28
29
class CrateSchemaManager extends AbstractSchemaManager
30
{
31
    /**
32
     * {@inheritdoc}
33
     *
34
     */
35 8
    protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
36
    {
37 8
        $buffer = [];
38 8
        foreach ($tableIndexes as $row) {
39 6
            $isPrimary = $row['constraint_type'] == 'PRIMARY KEY';
40 6
            $buffer[] = [
41 6
                'key_name' => $row['constraint_name'],
42 6
                'column_name' => $row['column_name'],
43 6
                'non_unique' => ! $isPrimary,
44 6
                'primary' => $isPrimary,
45 6
                'where' => '',
46 6
            ];
47
        }
48
49 8
        return parent::_getPortableTableIndexesList($buffer, $tableName);
50
    }
51
    /**
52
     * {@inheritDoc}
53
     */
54 8
    protected function _getPortableTableColumnDefinition($tableColumn)
55
    {
56 8
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
57
58 8
        if (!isset($tableColumn['column_name'])) {
59
            $tableColumn['column_name'] = '';
60
        }
61
62
        // Normalize is_nullable to handle both boolean (< 6.1) and string (>= 6.1) values.
63 8
        if (!isset($tableColumn['is_nullable'])) {
64
            $tableColumn['is_nullable'] = 'YES';
65 8
        } elseif (is_bool($tableColumn['is_nullable'])) {
66
            // Convert boolean to string for CrateDB < 6.1.
67
            $tableColumn['is_nullable'] = $tableColumn['is_nullable'] ? 'YES' : 'NO';
68
        }
69
70 8
        $dbType = strtolower($tableColumn['data_type']);
71 8
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
72
73 8
        $options = array(
74 8
            'length'        => null,
75 8
            'notnull'       => $tableColumn['is_nullable'] !== 'YES',
76 8
            'default'       => null,
77 8
            'precision'     => null,
78 8
            'scale'         => null,
79 8
            'fixed'         => null,
80 8
            'unsigned'      => false,
81 8
            'autoincrement' => false,
82 8
            'comment'       => '',
83 8
        );
84
85 8
        return new Column($tableColumn['column_name'], Type::getType($type), $options);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 32
    protected function _getPortableTablesList($tables)
92
    {
93 32
        $tableNames = array();
94 32
        foreach ($tables as $tableRow) {
95 28
            $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
96 28
            $tableNames[] = $tableRow['table_name']; // ignore schema for now
97
        }
98
99 32
        return $tableNames;
100
    }
101
102
    /**
103
     * Flattens a multidimensional array into a 1 dimensional array, where
104
     * keys are concatinated with '.'
105
     *
106
     * @return array
107
     */
108 6
    private function flatten(array $array, string $prefix = '') : array
109
    {
110 6
        $result = array();
111 6
        foreach ($array as $key => $value) {
112 6
            if (is_array($value)) {
113 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

113
                $result = $result + self::/** @scrutinizer ignore-call */ flatten($value, $prefix . $key . '.');
Loading history...
114
            } else {
115 6
                $result[$prefix . $key] = $value;
116
            }
117
        }
118 6
        return $result;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124 6
    public function listTableDetails($tableName) : Table
125
    {
126 6
        $columns = $this->listTableColumns($tableName);
127 6
        $indexes = $this->listTableIndexes($tableName);
128 6
        $options = [];
129
130 6
        $s = $this->_conn->fetchAssoc($this->_platform->getTableOptionsSQL($tableName));
131
132 6
        $options['sharding_routing_column'] = $s['clustered_by'];
133 6
        $options['sharding_num_shards'] = $s['number_of_shards'];
134 6
        $options['partition_columns'] = $s['partitioned_by'];
135 6
        $options['table_options'] = self::flatten($s['settings']);
136 6
        $options['table_options']['number_of_replicas'] = $s['number_of_replicas'];
137 6
        $options['table_options']['column_policy'] = $s['column_policy'];
138 6
        return new Table($tableName, $columns, $indexes, [], [], $options);
139
    }
140
}
141