2 require ("result.inc");
19 var $_halt_on_error = true;
22 function CLASS_DATABASE ($database=DB_DATABASE,$user=DB_USER,$password=DB_PASS,$url=DB_HOST) {
23 $this->Database=$database;
24 $this->Password=$password;
29 function CLASS_DATABASE() {
30 $this->connect(DB_HOST,DB_USER,DB_PASS,DB_DATABASE);
33 function connect($url,$user,$password,$database, $halt_on_error = true) {
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);
44 // mysql_query('set character set utf8');
48 $this->_password=$password;
50 if ($this->_linkId == false || mysql_select_db($database, $this->_linkId) == false) {
51 $this->exception("1Database failed.");
55 $this->_database=$database;
60 function closeMysql() {
61 mysql_close($this->_linkId);
64 function query($sql) {
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;
75 elseif (!preg_match("/^select/i",$sql) && !$this->Master) {*/
76 $this->_linkId = false;
77 $this->connect(DB_HOST,DB_USER,DB_PASS,DB_DATABASE);
81 $this->_queryId = mysql_query($sql,$this->_linkId);
83 if ($_SESSION['debugging']) {
84 if ($this->Master) echo "Master::";
88 echo "<BR>".SubStr((Time()+SubStr(MicroTime(),0,8)-$timer_start),0,7);
91 if ($this->_queryId == false) {
92 $this->exception("query failed ::$sql::");
95 return new result($this->_queryId, $sql);
99 function executequery($sql) {
100 return($this->query($sql));
103 function executetransaction($queries) {
104 $this->executequery("set autocommit=0");
105 if (is_array($queries)) {
106 foreach ($queries as $query) {
107 $this->executequery($query);
110 $this->executequery("commit");
111 $this->executequery("set autocommit=1");
114 function executeupdate($sql) {
115 return($this->update($sql));
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;
125 $this->_queryId = @mysql_db_query($this->_database,$sql,$this->_linkId);
126 if ($this->_queryId == false) {
127 $this->exception("update failed.");
129 $rows=@mysql_affected_rows($this->_linkId);
133 function getLastInsertId() {
134 return(@mysql_insert_id($this->_linkId));
137 function exception($errorMessage) {
140 echo @mysql_error($this->_linkId)," (",@mysql_errno($this->_linkId),")";
143 if ($this->_halt_on_error) {
144 die("<pre>".$errorMessage."</pre>");
146 echo $errorMessage."<br>";