SimplePDO::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace SimplePDO;
3
4
use SimplePDO\Configuration;
5
use SimplePDO\SimplePDOStatement;
6
use SimplePDO\Drivers\SqliteDriver;
7
use SimplePDO\Drivers\MySQLDriver;
8
9
class SimplePDO extends SimplePDOStatement
10
{
11
    
12
    /**
13
     * PDO connection object.
14
     *
15
     * @var object
16
     */
17
    private $dbconnect;
18
    
19
    /**
20
     * PDOStatement object.
21
     *
22
     * @var object
23
     */
24
    public $stmt;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param  object PDO2\Configuration object
30
     * @return void
31
     *
32
     * @throws RuntimeException if no supported PDO driver found used
33
     */
34
    public function __construct(Configuration $configuration)
35
    {
36
        switch ($configuration->get('GENERAL', 'DRIVER'))
37
        {
38
            case 'SQLITE':
39
            case 'SQLITE2':
40
                $dsn = SqliteDriver::dsn($configuration);
41
                $username = null;
42
                $password = null;
43
                break;
44
            case 'MYSQL':
45
                $dsn = MySQLDriver::dsn($configuration);
46
                $username = $configuration->get('MYSQL', 'USER');
47
                $password = $configuration->get('MYSQL', 'PASSWORD');
48
                break;
49
            default:
50
                throw new \RuntimeException(__METHOD__ . ": PDO driver '{$configuration->get('GENERAL', 'DRIVER')}' is not supported");
51
        }
52
53
        $this->dbconnect = new \PDO(
54
            $dsn,
55
            $username,
56
            $password,
57
            [
58
                \PDO::ATTR_PERSISTENT			=> false,
59
                \PDO::ATTR_ERRMODE				=> \PDO::ERRMODE_EXCEPTION,
60
                \PDO::ATTR_DEFAULT_FETCH_MODE	=> \PDO::FETCH_ASSOC
61
            ]
62
        );
63
    }
64
65
    /**
66
     * Destructor.
67
     *
68
     * @return void
69
     */
70
    public function __destruct()
71
    {
72
        $this->stmt = null;
73
        $this->dbconnect = null;
74
    }
75
76
    /**
77
     * Disconnect from database.
78
     *
79
     * @return void
80
     */
81
    final public function disconnect()
82
	{
83
		$this->__destruct();
84
	}
85
86
    /**
87
     * Initiates a transaction .
88
     *
89
     * @return object
90
     */
91
    final public function beginTransaction()
92
    {
93
        $this->dbconnect->beginTransaction();
94
        return $this;
95
    }
96
97
    /**
98
     * Commits a transaction.
99
     *
100
     * @return object
101
     */
102
    final public function commit()
103
    {
104
        $this->dbconnect->commit();
105
        return $this;
106
    }
107
108
    /**
109
     *  Rolls back a transaction.
110
     *
111
     * @return object
112
     */
113
    final public function rollBack()
114
    {
115
        $this->dbconnect->rollBack();
116
        return $this;
117
    }
118
119
    /**
120
     * Prepares a statement for execution and 
121
     * returns a statement object.
122
     *
123
     * @param  string $statement valid SQL statement 
124
     * @return object
125
     */
126
    final public function prepare($statement)
127
    {
128
        $this->stmt = $this->dbconnect->prepare($statement);
129
        $this->queryStrings($statement);
130
        return $this;
131
    }
132
    
133
    /**
134
     * Binds a parameter to the specified variable name.
135
     *
136
     * @param  mixed  $parameter parameter identifier
137
     * @param  mixed  $variable  name of the variable to bind to the SQL statement parameter
138
     * @param  int    $data_type explicit data type for the parameter
139
     * @return object
140
     */
141
    final public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR)
142
	{
143
		$this->stmt->bindParam($parameter, $variable, $data_type);
144
        $this->queryParams($parameter, $variable, $data_type);
145
		return $this;
146
	}
147
148
    /**
149
     * Executes a prepared statement
150
     *
151
     * @return object
152
     */
153
	final public function execute()
154
	{
155
		$this->stmt->execute();
156
		return $this;
157
	}
158
    
159
    /**
160
     * Set the default fetch mode for this statement.
161
     *
162
     * @return object
163
     */
164
    final public function setFetchMode()
165
    {
166
        if (func_num_args()== 2)
167
        {
168
            $this->stmt->setFetchMode(func_get_arg(0), func_get_arg(1));
169
            return $this;
170
        }
171
172
        $this->stmt->setFetchMode(func_get_arg(0));
173
        return $this;
174
    }
175
176
}
177