Trasovatelne templaty
[mirrors/Kyberia-bloodline.git] / trash / database.inc-bak
CommitLineData
51ff3226 1<?php
2require ("result.inc");
3
4class CLASS_DATABASE {
5
6/*
7var $Database="";
8var $User="";
9var $Password="";
10var $Url="";
11*/
12
13var $Master = true;
14var $_linkId = false;
15var $_url = "";
16var $_user = "";
17var $_password = "";
18var $_database = "";
19var $_halt_on_error = true;
20
21/*
22function 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
29function CLASS_DATABASE() {
30 $this->connect(DB_HOST,DB_USER,DB_PASS,DB_DATABASE);
31}
32
33function 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
60function closeMysql() {
61 mysql_close($this->_linkId);
62}
63
64function 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
99function executequery($sql) {
100 return($this->query($sql));
101}
102
103function 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
114function executeupdate($sql) {
115 return($this->update($sql));
116}
117
118function 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
133function getLastInsertId() {
134 return(@mysql_insert_id($this->_linkId));
135}
136
137function 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?>
This page took 0.373005 seconds and 4 git commands to generate.