Database::connect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php namespace Tequilarapido\Database;
2
3
use Illuminate\Database\Capsule\Manager as Capsule;
4
5
class Database
6
{
7
8
    /**
9
     * Initialize Illuminate Database connection
10
     *
11
     * @param $dbConfig
12
     */
13
    public function connect($dbConfig)
14
    {
15
        $capsule = new Capsule;
16
        $capsule->addConnection($dbConfig);
17
        $capsule->bootEloquent();
18
        $capsule->setAsGlobal();
19
    }
20
21
22
    public function size($database)
23
    {
24
        $query = '';
25
        $query .= 'SELECT table_schema, Sum(data_length + index_length) as size ';
26
        $query .= 'FROM   information_schema.tables ';
27
        $query .= 'WHERE table_schema = ? ';
28
        $query .= 'GROUP BY table_schema';
29
30
        $res = static::select($query, array($database));
31
        if (!empty($res[0]['size'])) {
32
            return round($res[0]['size'] / 1024 / 1024);
33
        }
34
35
        return false;
36
    }
37
38
    //
39
    // Helpers
40
    //
41
42
    public static function select($query, $bindings = array())
43
    {
44
        return Capsule::connection()->select($query, $bindings);
45
    }
46
47
    public static function update($query, $bindings = array())
48
    {
49
        return Capsule::connection()->update($query, $bindings);
50
    }
51
52
    public static function statement($query, $bindings = array())
53
    {
54
        return Capsule::connection()->statement($query, $bindings);
55
    }
56
57
    public static function enableQueryLog()
58
    {
59
        Capsule::connection()->enableQueryLog();
60
    }
61
62
    public static function getLastQuery()
63
    {
64
        $queries = Capsule::connection()->getQueryLog();
65
        return end($queries);
66
    }
67
68
    public static function connection()
69
    {
70
        return Capsule::connection();
71
    }
72
73
    public static function schema()
74
    {
75
        return Capsule::schema();
76
    }
77
78
    public static function table($table)
79
    {
80
        return Capsule::table($table);
81
    }
82
83
    public static function beginTransaction()
84
    {
85
        return Capsule::connection()->beginTransaction();
86
    }
87
88
    public static function commit()
89
    {
90
        return Capsule::connection()->commit();
91
    }
92
93
    public static function rollback()
94
    {
95
        return Capsule::connection()->rollBack();
96
    }
97
98
99
}