Completed
Push — master ( aaac57...e2ab3e )
by Oleg
05:20
created

Driver::clearTable()   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 1
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
     * Get driver type of current connection
52
     *
53
     * @access public
54
     * @return string
55
     */
56
    public function getDriverType()
57
    {
58
        return $this->conn->getAttribute(\PDO::ATTR_DRIVER_NAME);
59
    }
60
61
    /**
62
     * Destructor for this class
63
     *
64
     * @access public
65
     * @return void
66
     */
67
    public function __destruct()
68
    {
69
        $this->conn = null;
70
    }
71
72
    /**
73
     * Table exists in db
74
     *
75
     * @access public
76
     *
77
     * @param string $table Table name
78
     *
79
     * @return bool
80
     */
81
    public function tableExists($table)
82
    {
83
        return in_array($table, $this->listTables(), false);
84
    }
85
86
    /**
87
     * Clear all data from table
88
     *
89
     * @access public
90
     *
91
     * @param string $name Table name
92
     *
93
     * @return int
94
     */
95
    public function clearTable($name)
96
    {
97
        return $this->conn->exec("TRUNCATE {$name};");
98
    }
99
100
    /**
101
     * Field exists in table
102
     *
103
     * @access public
104
     *
105
     * @param string $field Field name
106
     * @param string $table Table name
107
     *
108
     * @return boolean
109
     */
110
    public function fieldExists($field, $table)
111
    {
112
        foreach ($this->listFields($table) AS $tbl) {
113
            if ($tbl['field'] === $field) {
114
                return true;
115
            }
116
        }
117
118
        return false;
119
    }
120
121
    /**
122
     * Send RAW query to DB
123
     *
124
     * @access public
125
     *
126
     * @param string $query Raw query to db
127
     * @param array $params Params for query
128
     * @param int $fetchType Fetching type
129
     * @param string $fetchClass Fetching class
130
     *
131
     * @return \PDOStatement|array
132
     * @throws Exception
133
     */
134
    public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model')
135
    {
136
        $sth = $this->conn->prepare($query);
137
138
        if ($fetchType === \PDO::FETCH_CLASS) {
139
            /** @noinspection PhpMethodParametersCountMismatchInspection */
140
            $sth->setFetchMode($fetchType, $fetchClass, ['new' => false]);
141
        } else {
142
            $sth->setFetchMode($fetchType);
143
        }
144
145
        foreach ($params AS $name => $value) {
146
            $sth->bindValue($name, $value);
147
        }
148
149
        $sth->execute();
150
151
        return $sth->fetchAll();
152
    }
153
}