Column::primaryKey()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php namespace Tequilarapido\Database;
2
3
use Tequilarapido\Database\Database;
4
5
class Column
6
{
7
    protected static $db_primarykeys = array();
8
9
    public function scanTextColumns($database, $exclude = array())
10
    {
11
        // Text Columns
12
        $db_text_columns = $this->textColumns($database);
13
14
        $db_columns = array();
15
        foreach ($db_text_columns as $column) {
16
            if (is_numeric($column['TABLE_NAME'])) {
17
                continue;
18
            }
19
20
            if (in_array($column['TABLE_NAME'], $exclude)) {
21
                continue;
22
            }
23
24
            if (empty($db_columns[$column['TABLE_NAME']])) {
25
                $db_columns[$column['TABLE_NAME']] = array();
26
                $db_columns[$column['TABLE_NAME']]['columns'] = array();
27
                $db_columns[$column['TABLE_NAME']]['pk'] = $this->primaryKey($column['TABLE_NAME']);
28
            }
29
30
            $db_columns[$column['TABLE_NAME']]['columns'][] = $column['COLUMN_NAME'];
31
        }
32
33
        return $db_columns;
34
    }
35
36
    public function primaryKey($table)
37
    {
38
        // Have we already a PK for this table
39
        if (!empty(static::$db_primarykeys[$table])) {
40
            return static::$db_primarykeys[$table];
41
        }
42
43
        $query = "SHOW KEYS FROM $table ";
44
        $results = Database::select($query);
45
        $keys = array();
46
        foreach ($results as $row) {
47
            if ($row['Key_name'] == 'PRIMARY') {
48
                $keys[$row['Seq_in_index'] - 1] = $row['Column_name'];
49
            }
50
        }
51
52
        static::$db_primarykeys[$table] = current($keys);
53
        return static::$db_primarykeys[$table];
54
    }
55
56 View Code Duplication
    public static function textColumns($database)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        $query = "SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS ";
59
        $query .= "WHERE 1 ";
60
        $query .= "AND TABLE_SCHEMA = ? ";
61
        $query .= "AND ( DATA_TYPE LIKE '%char%' OR DATA_TYPE LIKE '%text%' OR DATA_TYPE LIKE '%BLOB%' )";
62
63
        return Database::select($query, array($database));
64
    }
65
66
}