source: trunk/inc/database.php

Last change on this file was 712, checked in by chronos, 11 years ago
  • Upraveno: Aktualizace inicializačního skriptu pro sql databázi na revizi 710.
  • Upraveno: Aktualizována třída Database na novější využívající třídu PDO a generující výjímky při chybě, které je pak nutno očetřit.
File size: 5.4 KB
Line 
1<?php
2
3// Extended database class
4// Date: 2011-11-25
5
6
7class DatabaseResult
8{
9 var $PDOStatement;
10 var $num_rows = 0;
11
12 function fetch_assoc()
13 {
14 return($this->PDOStatement->fetch(PDO::FETCH_ASSOC));
15 }
16
17 function fetch_array()
18 {
19 return($this->PDOStatement->fetch(PDO::FETCH_BOTH));
20 }
21
22 function fetch_row()
23 {
24 return($this->PDOStatement->fetch(PDO::FETCH_NUM));
25 }
26}
27
28class Database
29{
30 var $Prefix;
31 var $Functions;
32 var $Type;
33 var $PDO;
34 var $Error;
35 var $insert_id;
36 var $LastQuery;
37 var $ShowSQLError;
38 var $ShowSQLQuery;
39 var $LogFile;
40
41 function __construct()
42 {
43 $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
44 $this->Type = 'mysql'; // mysql, pgsql
45 $this->ShowSQLError = false;
46 $this->ShowSQLQuery = false;
47 $this->LogSQLQuery = false;
48 $this->LogFile = dirname(__FILE__).'/../Query.log';
49 }
50
51 function Connect($Host, $User, $Password, $Database)
52 {
53 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
54 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
55 else $ConnectionString = '';
56 $this->PDO = new PDO($ConnectionString, $User, $Password);
57 }
58
59 function select_db($Database)
60 {
61 $this->query('USE `'.$Database.'`');
62 }
63
64 function query($Query)
65 {
66 $this->LastQuery = $Query;
67 if($this->ShowSQLQuery == true)
68 echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.'</div>'."\n");
69 if($this->LogSQLQuery == true)
70 file_put_contents($this->LogFile, $Query."\n", FILE_APPEND);
71 $Result = new DatabaseResult();
72 $Result->PDOStatement = $this->PDO->query($Query);
73 if($Result->PDOStatement)
74 {
75 $Result->num_rows = $Result->PDOStatement->rowCount();
76 $this->insert_id = $this->PDO->lastInsertId();
77 } else
78 {
79 $this->Error = $this->PDO->errorInfo();
80 $this->Error = $this->Error[2];
81 if(($this->Error != '') and ($this->ShowSQLError == true))
82 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
83 throw new Exception('SQL Error: '.$this->Error);
84 }
85 return($Result);
86 }
87
88 function select($Table, $What = '*', $Condition = 1)
89 {
90 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
91 }
92
93 function delete($Table, $Condition)
94 {
95 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
96 }
97
98 function insert($Table, $Data)
99 {
100 $Name = '';
101 $Values = '';
102 foreach($Data as $Key => $Value)
103 {
104 $Name .= ',`'.$Key.'`';
105 if(!in_array($Value, $this->Functions))
106 {
107 if(is_null($Value)) $Value = 'NULL';
108 else $Value = $this->PDO->quote($Value);
109 }
110 $Values .= ','.$Value;
111 }
112 $Name = substr($Name, 1);
113 $Values = substr($Values, 1);
114 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
115 $this->insert_id = $this->PDO->lastInsertId();
116 }
117
118 function update($Table, $Condition, $Data)
119 {
120 $Values = '';
121 foreach($Data as $Key => $Value)
122 {
123 if(!in_array($Value, $this->Functions))
124 {
125 if(is_null($Value)) $Value = 'NULL';
126 else $Value = $this->PDO->quote($Value);
127 }
128 $Values .= ', `'.$Key.'`='.$Value;
129 }
130 $Values = substr($Values, 2);
131 $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
132 }
133
134 function replace($Table, $Data)
135 {
136 $Name = '';
137 $Values = '';
138 foreach($Data as $Key => $Value)
139 {
140 if(!in_array($Value, $this->Functions))
141 {
142 if(is_null($Value)) $Value = 'NULL';
143 else $Value = $this->PDO->quote($Value);
144 }
145 $Name .= ',`'.$Key.'`';
146 $Values .= ','.$Value;
147 }
148 $Name = substr($Name, 1);
149 $Values = substr($Values, 1);
150 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
151 $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
152 //echo($this->error().'<br>');
153 }
154
155 function charset($Charset)
156 {
157 $this->query('SET NAMES "'.$Charset.'"');
158 }
159
160 function real_escape_string($Text)
161 {
162 return(addslashes($Text));
163 }
164}
165
166function TimeToMysqlDateTime($Time)
167{
168 if($Time == NULL) return(NULL);
169 else return(date('Y-m-d H:i:s', $Time));
170}
171
172function TimeToMysqlDate($Time)
173{
174 if($Time == NULL) return(NULL);
175 else return(date('Y-m-d', $Time));
176}
177
178function TimeToMysqlTime($Time)
179{
180 if($Time == NULL) return(NULL);
181 else return(date('H:i:s', $Time));
182}
183
184function MysqlDateTimeToTime($DateTime)
185{
186 if($DateTime == '') return(0);
187 $Parts = explode(' ', $DateTime);
188 $DateParts = explode('-', $Parts[0]);
189 $TimeParts = explode(':', $Parts[1]);
190 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
191 return($Result);
192}
193
194function MysqlDateToTime($Date)
195{
196 if($Date == '') return(0);
197 return(MysqlDateTimeToTime($Date.' 0:0:0'));
198}
199
200function MysqlTimeToTime($Time)
201{
202 if($Time == '') return(0);
203 return(MysqlDateTimeToTime('0000-00-00 '.$Time));
204}
Note: See TracBrowser for help on using the repository browser.