source: branches/Modular/Common/Database.php@ 387

Last change on this file since 387 was 358, checked in by chronos, 13 years ago
  • Opraveno: Generovalo se na výstup chybně odřádkování před začátek dokumentu díky nadbytečnému odřádkování za ukončením php v souboru Meals.php. Pak nebylo možné generovat dynamicky obrázky.
  • Upraveno: Generování topologie přesunuto do systémového modulu NetworkTopology.
File size: 3.8 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());
15 }
16
17 function fetch_array()
18 {
19 return($this->PDOStatement->fetch());
20 }
21
22 function fetch_row()
23 {
24 return($this->PDOStatement->fetch());
25 }
26}
27
28class Database
29{
30 var $Prefix = '';
31 var $Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
32 var $Type = 'mysql'; // mysql, pgsql
33 var $PDO;
34 var $Error = '';
35 var $insert_id;
36 var $LastQuery = '';
37 var $ShowSQLError = false;
38 var $ShowSQLQuery = false;
39
40 function __construct($Host, $User, $Password, $Database)
41 {
42 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
43 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
44 else $ConnectionString = '';
45 $this->PDO = new PDO($ConnectionString, $User, $Password);
46 $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
47 }
48
49 function select_db($Database)
50 {
51 $this->query('USE '.$Database);
52 }
53
54 function query($Query)
55 {
56 try
57 {
58 $this->LastQuery = $Query;
59 if($this->ShowSQLQuery == true)
60 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");
61 $Result = new DatabaseResult();
62 $Result->PDOStatement = $this->PDO->query($Query);
63 if($Result->PDOStatement)
64 $Result->num_rows = $Result->PDOStatement->rowCount();
65 } catch(PDOException $E)
66 {
67 $this->Error = $E->getMessage();
68 if(($this->Error != '') and ($this->ShowSQLError == true))
69 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
70 }
71 return($Result);
72 }
73
74 function select($Table, $What = '*', $Condition = 1)
75 {
76 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
77 }
78
79 function delete($Table, $Condition)
80 {
81 $this->PDO->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
82 }
83
84 function insert($Table, $Data)
85 {
86 $Name = '';
87 $Values = '';
88 foreach($Data as $Key => $Value)
89 {
90 $Name .= ',`'.$Key.'`';
91 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
92 $Values .= ','.$Value;
93 }
94 $Name = substr($Name, 1);
95 $Values = substr($Values, 1);
96 $this->PDO->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
97 $this->insert_id = $this->PDO->lastInsertId();
98 }
99
100 function update($Table, $Condition, $Data)
101 {
102 $Values = '';
103 foreach($Data as $Key => $Value)
104 {
105 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
106 $Values .= ', `'.$Key.'`='.$Value;
107 }
108 $Values = substr($Values, 2);
109 $this->PDO->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
110 }
111
112 function replace($Table, $Data)
113 {
114 $Name = '';
115 $Values = '';
116 foreach($Data as $Key => $Value)
117 {
118 if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
119 $Name .= ',`'.$Key.'`';
120 $Values .= ','.$Value;
121 }
122 $Name = substr($Name, 1);
123 $Values = substr($Values, 1);
124 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
125 $this->PDO->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
126 //echo($this->error().'<br>');
127 }
128
129 function charset($Charset)
130 {
131 $this->PDO->query('SET NAMES "'.$Charset.'"');
132 }
133
134 function real_escape_string($Text)
135 {
136 return(addslashes($Text));
137 }
138
139}
140
141?>
Note: See TracBrowser for help on using the repository browser.