Passed
Branch master (bcc732)
by Bob
02:55
created

Db.php ➔ openDB()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
use Monolog\Handler\StreamHandler;
27
use Monolog\Logger;
28
29
/**
30
 * @return null|PDO
31
 * @internal param null|string $db
32
 */
33
function openDB()
34
{
35
    $logger = new Logger('Db');
36
    $logger->pushHandler(new StreamHandler(__DIR__ . '/../../log/libraryError.log', Logger::DEBUG));
37
    $db = __DIR__ . '/../../database/dramiel.sqlite';
38
39
    $dsn = "sqlite:$db";
40
    try {
41
        $pdo = new PDO($dsn, '', '', array(
42
                PDO::ATTR_PERSISTENT => false,
43
                PDO::ATTR_EMULATE_PREPARES => true,
44
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
45
            )
46
        );
47
    } catch (Exception $e) {
48
        $logger->error($e->getMessage());
49
        $pdo = null;
50
        return $pdo;
51
    }
52
53
    return $pdo;
54
}
55
56
/**
57
 * @param string $query
58
 * @param string $field
59
 * @param array $params
60
 * @return string
61
 * @internal param string $db
62
 */
63
function dbQueryField($query, $field, array $params = array())
64
{
65
    $pdo = openDB();
66
    if ($pdo == NULL) {
67
        return null;
68
    }
69
70
    $stmt = $pdo->prepare($query);
71
    $stmt->execute($params);
72
73
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
74
    $stmt->closeCursor();
75
76
    if (count($result) == 0) {
77
        return null;
78
    }
79
80
    $resultRow = $result[0];
81
    return $resultRow[$field];
82
}
83
84
/**
85
 * @param string $query
86
 * @param array $params
87
 * @return null|void
88
 * @internal param string $db
89
 */
90
function dbQueryRow($query, array $params = array())
91
{
92
    $pdo = openDB();
93
    if ($pdo == NULL) {
94
        return null;
95
    }
96
97
    $stmt = $pdo->prepare($query);
98
    $stmt->execute($params);
99
100
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
101
    $stmt->closeCursor();
102
103
    if (count($result) >= 1) {
104
        return $result[0];
105
    }
106
    return null;
107
}
108
109
/**
110
 * @param string $query
111
 * @param array $params
112
 * @return array|void
113
 * @internal param string $db
114
 */
115
function dbQuery($query, array $params = array())
116
{
117
    $pdo = openDB();
118
    if ($pdo == NULL) {
119
        return null;
120
    }
121
122
    $stmt = $pdo->prepare($query);
123
    $stmt->execute($params);
124
125
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
126
    $stmt->closeCursor();
127
128
    return $result;
129
}
130
131
/**
132
 * @param string $query
133
 * @param array $params
134
 * @internal param string $db
135
 */
136
function dbExecute($query, array $params = array())
137
{
138
    $pdo = openDB();
139
    if ($pdo == NULL) {
140
        return;
141
    }
142
143
    // This is ugly, but, yeah..
144
    if (strstr($query, ';')) {
145
        $explodedQuery = explode(';', $query);
146
        $stmt = null;
147
        foreach ($explodedQuery as $newQry) {
148
            $stmt = $pdo->prepare($newQry);
149
            $stmt->execute($params);
150
        }
151
        $stmt->closeCursor();
152
    } else {
153
        $stmt = $pdo->prepare($query);
154
        $stmt->execute($params);
155
        $stmt->closeCursor();
156
    }
157
}