Completed
Push — master ( 552d53...b8eb91 )
by Jean-Christophe
02:02
created

Database   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 129
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A connect() 0 13 2
A query() 0 3 1
B prepareAndExecute() 0 16 7
A getStatement() 0 8 2
A execute() 0 3 1
A getServerName() 0 3 1
A setServerName() 0 3 1
A prepareStatement() 0 3 1
A bindValueFromStatement() 0 3 1
A lastInserId() 0 3 1
A getTablesName() 0 5 1
1
<?php
2
namespace micro\db;
3
use micro\cache\QueryCache;
4
5
/**
6
 * Classe d'accès aux Bases de données encapsulant un objet PDO
7
 * @author heron
8
 * @version 1.0.0.3
9
 * @package db
10
 *
11
 */
12
class Database {
13
	private $serverName;
14
	private $port;
15
	private $dbName;
16
	private $user;
17
	private $password;
18
	private $pdoObject;
19
	private $statements=[];
20
21
	/**
22
	 * Constructeur
23
	 * @param string $dbName
24
	 * @param string $serverName
25
	 * @param string $port
26
	 * @param string $user
27
	 * @param string $password
28
	 */
29
	public function __construct($dbName, $serverName = "localhost", $port = "3306",
30
			$user = "root", $password = "") {
31
		$this->dbName = $dbName;
32
		$this->serverName = $serverName;
33
		$this->port = $port;
34
		$this->user = $user;
35
		$this->password = $password;
36
	}
37
38
	/**
39
	 * Réalise la connexion à la base de données
40
	 */
41
	public function connect() {
42
		try {
43
			$this->pdoObject = new \PDO(
44
					'mysql:host=' . $this->serverName . ';dbname='
45
							. $this->dbName . ';port:' . $this->port,
46
					$this->user, $this->password);
47
			$this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
48
			$this->pdoObject->exec("SET CHARACTER SET utf8");
49
50
		} catch (\PDOException $e) {
51
			print "Error!: " . $e->getMessage() . "<br/>";
52
		}
53
	}
54
55
	/**
56
	 * Exécute l'instruction SQL passée en paramètre et retourne un statement
57
	 * @param string $sql
58
	 * @return PDOStatement
59
	 */
60
	public function query($sql) {
61
		return $this->pdoObject->query($sql);
62
	}
63
64
	public function prepareAndExecute($sql,$useCache=NULL){
65
		$cache=(QueryCache::$active && $useCache!==false) || (!QueryCache::$active && $useCache===true);
66
		if($cache){
67
			$result=QueryCache::fetch($sql);
68
		}
69
		if($result===false){
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
			$statement=$this->getStatement($sql);
71
			$statement->execute();
72
			$result= $statement->fetchAll();
73
			$statement->closeCursor();
74
			if($cache){
75
				QueryCache::store($sql, $result);
76
			}
77
		}
78
		return $result;
79
	}
80
81
	private function getStatement($sql){
82
		if(!isset($this->statements[$sql])){
83
			$this->statements[$sql]=$this->pdoObject->prepare($sql);
84
			$this->statements[$sql]->setFetchMode(\PDO::FETCH_ASSOC);
85
86
		}
87
		return $this->statements[$sql];
88
	}
89
90
	/**
91
	 * Exécute l'instruction sql $sql de mise à jour (INSERT, UPDATE ou DELETE)
92
	 * @return le nombre d'enregistrements affectés
93
	 */
94
	public function execute($sql) {
95
		return $this->pdoObject->exec($sql);
96
	}
97
98
	public function getServerName() {
99
		return $this->serverName;
100
	}
101
102
	public function setServerName($serverName) {
103
		$this->serverName = $serverName;
104
	}
105
106
	/**
107
	 * Prépare l'instruction $sql pour son exécution
108
	 * @param String $sql
109
	 * @return PDOStatement
110
	 */
111
	public function prepareStatement($sql){
112
		return $this->pdoObject->prepare($sql);
113
	}
114
115
	/**
116
	 * Affecte la valeur $value au paramétre $parameter
117
	 * @param PDOStatement $statement
118
	 * @param String $parameter
119
	 * @param mixed $value
120
	 * @return boolean
121
	 */
122
	public function bindValueFromStatement(\PDOStatement $statement,$parameter,$value){
123
		return $statement->bindValue(":".$parameter, $value);
124
	}
125
126
	/**
127
	 * retourne le dernier auto-increment généré
128
	 * @return integer
129
	 */
130
	public function lastInserId(){
131
		return $this->pdoObject->lastInsertId();
132
	}
133
134
	public function getTablesName(){
135
		$sql = 'SHOW TABLES';
136
		$query = $this->pdoObject->query($sql);
137
		return $query->fetchAll(\PDO::FETCH_COLUMN);
138
	}
139
140
}
141