Passed
Push — master ( 380c0f...083792 )
by Nils
05:05
created

Database::update()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 3
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass
11
 * @version    API
12
 *
13
 * @file      Database.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2024 Teampass.net
19
 *
20
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
21
 * ---
22
 *
23
 * @see       https://www.teampass.net
24
 */
25
class Database
26
{
27
    protected $connection = null;
28
29
    public function __construct()
30
    {
31
        try {
32
            $this->connection = new \mysqli(DB_HOST, DB_USER, DB_PASSWD_CLEAR, DB_NAME);
33
34
            if ( mysqli_connect_errno()) {
35
                throw new Exception("Could not connect to database.");   
36
            }
37
        } catch (Exception $e) {
38
            throw new Exception($e->getMessage());   
39
        }           
40
    }
41
42
    public function select($query = "" , $params = [])
43
    {
44
        try {
45
            $stmt = $this->executeStatement( $query , $params );
46
            $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);               
47
            $stmt->close();
48
49
            return $result;
50
        } catch(Exception $e) {
51
            throw New Exception( $e->getMessage() );
52
        }
53
    }
54
55
    public function update($query = "", $params = [])
56
    {
57
        try {
58
            $stmt = $this->executeStatement($query, $params);
59
            $affectedRows = $stmt->affected_rows;
60
            $stmt->close();
61
62
            return $affectedRows;
63
        } catch (Exception $e) {
64
            throw new Exception($e->getMessage());
65
        }
66
    }
67
68
69
    private function executeStatement($query = "", $params = [])
70
    {
71
        try {
72
            $stmt = $this->connection->prepare($query);
73
74
            if ($stmt === false) {
75
                throw new Exception("Unable to do prepared statement: " . $query);
76
            }
77
78
            if ($params) {
79
                // Supposons que $params[0] contient les types des paramètres comme une chaîne ("ssi")
80
                // et $params[1...] contiennent les valeurs
81
                $types = $params[0];
82
                $values = array_slice($params, 1);
83
                $stmt->bind_param($types, ...$values);
84
            }
85
86
            $stmt->execute();
87
88
            return $stmt;
89
        } catch (Exception $e) {
90
            throw new Exception($e->getMessage());
91
        }
92
    }
93
94
}