1: <?php
2:
3: /*
4: * Copyright (C) 2014 Luca
5: *
6: * This program is free software: you can redistribute it and/or modify
7: * it under the terms of the GNU Affero General Public License as published by
8: * the Free Software Foundation, either version 3 of the License, or
9: * (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU Affero General Public License for more details.
15: *
16: * You should have received a copy of the GNU Affero General Public License
17: * along with this program. If not, see <http://www.gnu.org/licenses/agpl-3.0.html>.
18: */
19:
20: /**
21: * Interfaccia di comunicazione con il db (Database type MySql-PDO)
22: *
23: * @author Luca Liscio & Marco Lettieri
24: * @version v 2.0-PDO 2014/06/25 16:03:20
25: * @copyright Copyright 2014 Luca Liscio
26: * @license http://www.gnu.org/licenses/agpl-3.0.html GNU/AGPL3
27: *
28: * @package HZGuestBook
29: * @subpackage Env
30: * @filesource
31: */
32:
33: class DbManager {
34:
35: private $_conn;
36:
37: /**
38: * All'atto della costruziine di un nuovo ogetto esegue la connessione al DB
39: *
40: * @param array $config contiene i parametri (type, host, uname, passwd, db) necessari alla connesione
41: * @throws PDOException
42: */
43: public function __construct($config) {
44: $connstr = $config['type'].":host=".$config['host'].";dbname=".$config['db'].";charset=utf8";
45: $this->_conn = new PDO($connstr, $config['uname'], $config['passwd']);
46: $this->_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
47: $this->_conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
48: }
49:
50: /**
51: * Chiude la connesione con il db
52: */
53: public function close(){
54: $this->_conn = null;
55: }
56:
57: /**
58: * Esegue un query sql e restituisce il risultato
59: *
60: * @param string $sql stringa contenente la query
61: * @return array $result contiene il resultset
62: */
63: public function &send_result_sql($sql){
64: //Send a sql query that returns a result
65: $stmt = $this->_conn->query($sql);
66: $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
67: return $res;
68: }
69:
70: /**
71: * Invia al db query di tipo comando e restituisce l'esito dell'esecuzione
72: *
73: * @param string $sql
74: * @return array restituisce l'esito della query
75: */
76: public function &send_command_sql($sql){
77: //Send a sql command that returns the number of rows affected
78: $af = $this->_conn->exec($sql);
79: $result["sql"] = $sql;
80: $result["nbrows"] = $af;
81: return $result;
82: }
83:
84: /**
85: * Restituisce l'ultimo id inserito
86: * @return mixed
87: */
88: public function sql_insert_id(){
89: return $this->_conn->lastInsertId();
90: }
91:
92: /**
93: * Formater for \' items
94: */
95: public function sql_format($data){
96: //When passing the data from a post form, the \' are already set, we will
97: //replace them with a system code, then replace the single ' by \' and re
98: //replace the old system code with \'
99: $formateddata = str_replace("\'", "@sc:bsqu@", $data);
100: $formateddata = str_replace("'", "\'", $formateddata);
101: $formateddata = str_replace("@sc:bsqu@", "\'", $formateddata);
102: return $formateddata;
103: }
104:
105: }