Completed
Push — master ( 361012...7b4150 )
by Jean-Christophe
02:01
created

Database::getTablesName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace micro\db;
3
/**
4
 * Classe d'accès aux Bases de données encapsulant un objet PDO
5
 * @author heron
6
 * @version 1.0.0.3
7
 * @package db
8
 *
9
 */
10
class Database {
11
	private $serverName;
12
	private $port;
13
	private $dbName;
14
	private $user;
15
	private $password;
16
	private $pdoObject;
17
18
	/**
19
	 * Constructeur
20
	 * @param string $dbName
21
	 * @param string $serverName
22
	 * @param string $port
23
	 * @param string $user
24
	 * @param string $password
25
	 */
26
	public function __construct($dbName, $serverName = "localhost", $port = "3306",
27
			$user = "root", $password = "") {
28
		$this->dbName = $dbName;
29
		$this->serverName = $serverName;
30
		$this->port = $port;
31
		$this->user = $user;
32
		$this->password = $password;
33
	}
34
35
	/**
36
	 * Réalise la connexion à la base de données
37
	 */
38
	public function connect() {
39
		try {
40
			$this->pdoObject = new \PDO(
41
					'mysql:host=' . $this->serverName . ';dbname='
42
							. $this->dbName . ';port:' . $this->port,
43
					$this->user, $this->password);
44
			$this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
45
			$this->pdoObject->exec("SET CHARACTER SET utf8");
46
47
		} catch (\PDOException $e) {
48
			print "Error!: " . $e->getMessage() . "<br/>";
49
		}
50
	}
51
52
	/**
53
	 * Exécute l'instruction SQL passée en paramètre et retourne un statement
54
	 * @param string $sql
55
	 * @return PDOStatement
56
	 */
57
	public function query($sql) {
58
		return $this->pdoObject->query($sql);
59
	}
60
61
	/**
62
	 * Exécute l'instruction sql $sql de mise à jour (INSERT, UPDATE ou DELETE)
63
	 * @return le nombre d'enregistrements affectés
64
	 */
65
	public function execute($sql) {
66
		return $this->pdoObject->exec($sql);
67
	}
68
69
	public function getServerName() {
70
		return $this->serverName;
71
	}
72
73
	public function setServerName($serverName) {
74
		$this->serverName = $serverName;
75
	}
76
77
	/**
78
	 * Prépare l'instruction $sql pour son exécution
79
	 * @param String $sql
80
	 * @return PDOStatement
81
	 */
82
	public function prepareStatement($sql){
83
		return $this->pdoObject->prepare($sql);
84
	}
85
86
	/**
87
	 * Affecte la valeur $value au paramétre $parameter
88
	 * @param PDOStatement $statement
89
	 * @param String $parameter
90
	 * @param mixed $value
91
	 * @return boolean
92
	 */
93
	public function bindValueFromStatement(\PDOStatement $statement,$parameter,$value){
94
		return $statement->bindValue(":".$parameter, $value);
95
	}
96
97
	/**
98
	 * retourne le dernier auto-increment généré
99
	 * @return integer
100
	 */
101
	public function lastInserId(){
102
		return $this->pdoObject->lastInsertId();
103
	}
104
105
	public function getTablesName(){
106
		$sql = 'SHOW TABLES';
107
		$query = $this->pdoObject->query($sql);
108
		return $query->fetchAll(\PDO::FETCH_COLUMN);
109
	}
110
111
}
112