|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BfwSql; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class to initialize a connection to a SQL server with PDO |
|
7
|
|
|
* |
|
8
|
|
|
* @package bfw-sql |
|
9
|
|
|
* @author Vermeulen Maxime <[email protected]> |
|
10
|
|
|
* @version 2.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class SqlConnect |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var \PDO $PDO PDO Connexion object |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $PDO; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \stdClass $connectionInfos All informations about the connection |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $connectionInfos; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string $type Connexion type (mysql/pgsql/etc) |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $type; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var integer $nbQuery (default 0) Number of request has been done |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $nbQuery = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor |
|
36
|
|
|
* Initialize the connection |
|
37
|
|
|
* |
|
38
|
|
|
* @param \stdClass $connectionInfos All informations about the connection |
|
39
|
|
|
* |
|
40
|
|
|
* @throw \PDOException If Connexion fail |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($connectionInfos) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->connectionInfos = $connectionInfos; |
|
45
|
|
|
|
|
46
|
|
|
$host = $connectionInfos->host; |
|
47
|
|
|
$baseName = $connectionInfos->baseName; |
|
48
|
|
|
|
|
49
|
|
|
$this->type = $connectionInfos->baseType; |
|
50
|
|
|
$this->PDO = new \PDO( |
|
51
|
|
|
$this->type.':host='.$host.';dbname='.$baseName, |
|
52
|
|
|
$connectionInfos->user, |
|
53
|
|
|
$connectionInfos->password |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
if ($connectionInfos->useUTF8 === true) { |
|
57
|
|
|
$this->setUtf8($connectionInfos->useUTF8); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Define charset to UTF-8 with mysql |
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function setUtf8() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->PDO->exec('SET NAMES utf8'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Protect a data with the system implemented by the pdo drivers used. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $string Data to protect |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function protect($string) |
|
79
|
|
|
{ |
|
80
|
|
|
$protectedString = $this->PDO->quote($string); |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* The quote method add the caracter ' on the start and the end of the |
|
84
|
|
|
* protected string. |
|
85
|
|
|
* So we remove this quote at the start and the end of the string. |
|
86
|
|
|
*/ |
|
87
|
|
|
return substr($protectedString, 1, strlen($protectedString)-2); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Getter to access at the property PDO |
|
92
|
|
|
* |
|
93
|
|
|
* @return \PDO |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getPDO() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->PDO; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Getter to access at the property connectionInfos |
|
102
|
|
|
* |
|
103
|
|
|
* @return \stdClass |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getConnectionInfos() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->connectionInfos; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Getter to access at the property type |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getType() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->type; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Getter to access at the property nbQuery |
|
122
|
|
|
* |
|
123
|
|
|
* @return integer |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getNbQuery() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->nbQuery; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Increment the number of query has been done |
|
132
|
|
|
* |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
|
|
public function upNbQuery() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->nbQuery++; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.