51ff3226 |
1 | <?php |
2 | require ("result.inc"); |
3 | |
4 | class CLASS_DATABASE { |
5 | |
6 | /* |
7 | var $Database=""; |
8 | var $User=""; |
9 | var $Password=""; |
10 | var $Url=""; |
11 | */ |
12 | |
13 | var $Master = true; |
14 | var $_linkId = false; |
15 | var $_url = ""; |
16 | var $_user = ""; |
17 | var $_password = ""; |
18 | var $_database = ""; |
19 | var $_halt_on_error = true; |
20 | |
21 | /* |
22 | function CLASS_DATABASE ($database=DB_DATABASE,$user=DB_USER,$password=DB_PASS,$url=DB_HOST) { |
23 | $this->Database=$database; |
24 | $this->Password=$password; |
25 | $this->User=$user; |
26 | $this->Url=$url; |
27 | */ |
28 | |
29 | function CLASS_DATABASE() { |
30 | $this->connect(DB_HOST,DB_USER,DB_PASS,DB_DATABASE); |
31 | } |
32 | |
33 | function connect($url,$user,$password,$database, $halt_on_error = true) { |
34 | global $error; |
35 | $this->_halt_on_error = $halt_on_error; |
36 | if ($this->_linkId == false) { |
37 | $this->_linkId=mysql_connect($url, $user, $password); |
38 | if ($this->_linkId == false) { |
39 | $error='chcipla databaza'; |
40 | $this->exception($error); |
41 | return false; |
42 | //die(); |
43 | }// else { |
44 | // mysql_query('set character set utf8'); |
45 | //} |
46 | $this->_url=$url; |
47 | $this->_user=$user; |
48 | $this->_password=$password; |
49 | |
50 | if ($this->_linkId == false || mysql_select_db($database, $this->_linkId) == false) { |
51 | $this->exception("1Database failed."); |
52 | return false; |
53 | die(); |
54 | } |
55 | $this->_database=$database; |
56 | } |
57 | return true; |
58 | } |
59 | |
60 | function closeMysql() { |
61 | mysql_close($this->_linkId); |
62 | } |
63 | |
64 | function query($sql) { |
65 | |
66 | // if (preg_match("/^select/i",$sql) && (rand(0,1000)>MASTER2SLAVE) && $this->Master) { |
67 | // every select query goes to onyx + opraveny regexp, aby matchoval vnorene selecty (br) |
68 | /* if (preg_match('/^\(?select/i',$sql) && $this->Master) { |
69 | $this->_linkId = false; |
70 | $this->connect(SLAVE_HOST,SLAVE_USER,SLAVE_PASS,SLAVE_DATABASE); |
71 | $this->Master = false; |
72 | |
73 | } |
74 | |
75 | elseif (!preg_match("/^select/i",$sql) && !$this->Master) {*/ |
76 | $this->_linkId = false; |
77 | $this->connect(DB_HOST,DB_USER,DB_PASS,DB_DATABASE); |
78 | $this->Master = true; |
79 | // } |
80 | |
81 | $this->_queryId = mysql_query($sql,$this->_linkId); |
82 | |
83 | if ($_SESSION['debugging']) { |
84 | if ($this->Master) echo "Master::"; |
85 | else echo "Slave::"; |
86 | echo $sql; |
87 | global $timer_start; |
88 | echo "<BR>".SubStr((Time()+SubStr(MicroTime(),0,8)-$timer_start),0,7); |
89 | } |
90 | |
91 | if ($this->_queryId == false) { |
92 | $this->exception("query failed ::$sql::"); |
93 | } |
94 | |
95 | return new result($this->_queryId, $sql); |
96 | } |
97 | |
98 | |
99 | function executequery($sql) { |
100 | return($this->query($sql)); |
101 | } |
102 | |
103 | function executetransaction($queries) { |
104 | $this->executequery("set autocommit=0"); |
105 | if (is_array($queries)) { |
106 | foreach ($queries as $query) { |
107 | $this->executequery($query); |
108 | } |
109 | } |
110 | $this->executequery("commit"); |
111 | $this->executequery("set autocommit=1"); |
112 | } |
113 | |
114 | function executeupdate($sql) { |
115 | return($this->update($sql)); |
116 | } |
117 | |
118 | function update($sql) { |
119 | if (!$this->Master) { |
120 | $this->_linkId = false; |
121 | $this->connect(DB_HOST,DB_USER,DB_PASS,DB_DATABASE); |
122 | $this->Master = true; |
123 | } |
124 | |
125 | $this->_queryId = @mysql_db_query($this->_database,$sql,$this->_linkId); |
126 | if ($this->_queryId == false) { |
127 | $this->exception("update failed."); |
128 | } |
129 | $rows=@mysql_affected_rows($this->_linkId); |
130 | return($rows); |
131 | } |
132 | |
133 | function getLastInsertId() { |
134 | return(@mysql_insert_id($this->_linkId)); |
135 | } |
136 | |
137 | function exception($errorMessage) { |
138 | |
139 | echo "<!-- "; |
140 | echo @mysql_error($this->_linkId)," (",@mysql_errno($this->_linkId),")"; |
141 | echo "-->"; |
142 | |
143 | if ($this->_halt_on_error) { |
144 | die("<pre>".$errorMessage."</pre>"); |
145 | } else { |
146 | echo $errorMessage."<br>"; |
147 | return false; |
148 | } |
149 | } |
150 | } |
151 | ?> |