Completed
Push — master ( f88857...648d22 )
by Oleg
04:16
created

Driver::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php /** DriverMicro */
2
3
namespace Micro\Db\Drivers;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * Driver class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Db\Drivers
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
abstract class Driver implements IDriver
20
{
21
    /** @var \PDO|null $conn Connection to DB */
22
    protected $conn;
23
24
25
    /**
26
     * Driver constructor.
27
     *
28
     * @access public
29
     *
30
     * @param string $dsn DSN connection string
31
     * @param array $config Configuration of connection
32
     * @param array $options Other options
33
     *
34
     * @result void
35
     * @throws Exception
36
     */
37
    public function __construct($dsn, array $config = [], array $options = [])
38
    {
39
        try {
40
            $this->conn = new \PDO($dsn, $config['username'], $config['password'], $options);
41
            $this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
42
43
        } catch (\PDOException $e) {
44
            if (!array_key_exists('ignoreFail', $config) || !$config['ignoreFail']) {
45
                throw new Exception('Connect to DB failed: ' . $e->getMessage());
46
            }
47
        }
48
    }
49
50
    /**
51
     * Destructor for this class
52
     *
53
     * @access public
54
     * @return void
55
     */
56
    public function __destruct()
57
    {
58
        $this->conn = null;
59
    }
60
61
    /**
62
     * Table exists in db
63
     *
64
     * @access public
65
     *
66
     * @param string $table Table name
67
     *
68
     * @return bool
69
     */
70
    public function tableExists($table)
71
    {
72
        return in_array($table, $this->listTables(), false);
73
    }
74
75
    /**
76
     * Field exists in table
77
     *
78
     * @access public
79
     *
80
     * @param string $field Field name
81
     * @param string $table Table name
82
     *
83
     * @return boolean
84
     */
85
    public function fieldExists($field, $table)
86
    {
87
        foreach ($this->listFields($table) AS $tbl) {
88
            if ($tbl['field'] === $field) {
89
                return true;
90
            }
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * Send RAW query to DB
98
     *
99
     * @access public
100
     *
101
     * @param string $query Raw query to db
102
     * @param array $params Params for query
103
     * @param int $fetchType Fetching type
104
     * @param string $fetchClass Fetching class
105
     *
106
     * @return \PDOStatement|array
107
     * @throws Exception
108
     */
109
    public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model')
110
    {
111
        $sth = $this->conn->prepare($query);
112
113
        if ($fetchType === \PDO::FETCH_CLASS) {
114
            /** @noinspection PhpMethodParametersCountMismatchInspection */
115
            $sth->setFetchMode($fetchType, $fetchClass, ['new' => false]);
116
        } else {
117
            $sth->setFetchMode($fetchType);
118
        }
119
120
        foreach ($params AS $name => $value) {
121
            $sth->bindValue($name, $value);
122
        }
123
124
        $sth->execute();
125
126
        return $sth->fetchAll();
127
    }
128
}