_getPortableTableColumnDefinition()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 27
ccs 18
cts 20
cp 0.9
rs 9.6666
cc 3
nc 4
nop 1
crap 3.009
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 8
        if (!isset($tableColumn['is_nullable'])) {
62
            $tableColumn['is_nullable'] = true;
63
        }
64
65 8
        $dbType = strtolower($tableColumn['data_type']);
66 8
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
67
68 8
        $options = array(
69 8
            'length'        => null,
70 8
            'notnull'       => ! $tableColumn['is_nullable'],
71 8
            'default'       => null,
72 8
            'precision'     => null,
73 8
            'scale'         => null,
74 8
            'fixed'         => null,
75 8
            'unsigned'      => false,
76 8
            'autoincrement' => false,
77 8
            'comment'       => '',
78 8
        );
79
80 8
        return new Column($tableColumn['column_name'], Type::getType($type), $options);
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86 32
    protected function _getPortableTablesList($tables)
87
    {
88 32
        $tableNames = array();
89 32
        foreach ($tables as $tableRow) {
90 28
            $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
91 28
            $tableNames[] = $tableRow['table_name']; // ignore schema for now
92
        }
93
94 32
        return $tableNames;
95
    }
96
97
    /**
98
     * Flattens a multidimensional array into a 1 dimensional array, where
99
     * keys are concatinated with '.'
100
     *
101
     * @return array
102
     */
103 6
    private function flatten(array $array, string $prefix = '') : array
104
    {
105 6
        $result = array();
106 6
        foreach ($array as $key => $value) {
107 6
            if (is_array($value)) {
108 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

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