Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Mysql   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 104
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
C init() 0 40 7
A getTableCols() 0 4 1
A tableExist() 0 4 1
A addCol() 0 10 4
A delCol() 0 7 3
A getTables() 0 9 2
A deleteTable() 0 7 2
1
<?php
2
3
/**
4
 * Mysql work class
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Inji\Db;
13
14
class Mysql extends \Inji\InjiObject {
15
16
    public $config = []; // настройки подключения выбраной базы
17
    public $connect = false; // ярлык соединения с MySQL
18
    public $encoding = 'utf-8'; // установленная кодировка
19
    public $db_name = 'test'; // выбраная в данный момент база
20
    public $table_prefix = 'inji_'; // префикс названий таблиц
21
    public $pdo = null;
22
    public $lastQuery = '';
23
    public $last_error = '';
24
    public $noConnectAbort = false;
25
    public $dbInstance;
26
27
    /**
28
     * Подключение к MySQL
29
     */
30
    public function init($connect_options) {
31
        extract($connect_options);
32
        if (isset($db_name)) {
33
            $this->db_name = $db_name;
34
        }
35
        if (isset($encoding)) {
36
            $this->encoding = $encoding;
37
        }
38
        if (isset($table_prefix)) {
39
            $this->table_prefix = $table_prefix;
40
        }
41
        if (isset($noConnectAbort)) {
42
            $this->noConnectAbort = $noConnectAbort;
43
        }
44
45
        $dsn = "mysql:host=$host;port=$port;dbname=$db_name;charset=$encoding";
46
        $dt = new \DateTime();
47
        $offset = $dt->format("P");
48
        $opt = array(
49
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
50
            \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
51
            \PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '{$offset}'"
52
        );
53
54
        $this->pdo = new \PDO($dsn, $user, $pass, $opt);
55
        $error = $this->pdo->errorInfo();
56
        if ((int) $error[0]) {
57
            if ($this->noConnectAbort) {
58
                return false;
59
            } else {
60
                INJI_SYSTEM_ERROR($error[2], true);
61
            }
62
        } else {
63
            $this->connect = true;
64
            $query = new Mysql\Query($this);
65
            $query->query("SET SQL_BIG_SELECTS=1");
66
            $query->query("SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION'");
67
            return true;
68
        }
69
    }
70
71
    public function getTableCols($table_name) {
72
        $query = new Mysql\Query($this);
73
        return $query->query("SHOW COLUMNS FROM `{$this->db_name}`.{$this->table_prefix}{$table_name}")->getArray('Field');
74
    }
75
76
    public function tableExist($tableName) {
77
        $query = new Mysql\Query($this);
78
        return (bool) $query->query("SHOW TABLES FROM `{$this->db_name}` LIKE '{$this->table_prefix}{$tableName}'")->getArray();
79
    }
80
81
    public function addCol($table = false, $name = false, $param = 'TEXT NOT NULL') {
82
        if (!$table || !$name) {
83
            return false;
84
        }
85
        if ($param == 'pk') {
86
            $param = "int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`{$name}`)";
87
        }
88
        $query = new Mysql\Query($this);
89
        return $query->query("ALTER TABLE `{$this->db_name}`.`{$this->table_prefix}{$table}` ADD `{$name}` {$param}");
90
    }
91
92
    public function delCol($table = false, $name = false) {
93
        if (!$table || !$name) {
94
            return false;
95
        }
96
        $query = new Mysql\Query($this);
97
        return $query->query("ALTER TABLE `{$this->db_name}`.`{$this->table_prefix}{$table}` DROP `{$name}`");
98
    }
99
100
    public function getTables() {
101
        $query = new Mysql\Query($this);
102
        $data = $query->query("SHOW TABLES")->getArray();
103
        $tables = [];
104
        foreach ($data as $info) {
105
            $tables[] = $info['Tables_in_' . $this->db_name];
106
        }
107
        return $tables;
108
    }
109
110
    public function deleteTable($tableName) {
111
        if (!$tableName) {
112
            return true;
113
        }
114
        $query = new Mysql\Query($this);
115
        return $query->query("DROP TABLE IF EXISTS {$tableName}")->fetch();
116
    }
117
}