/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php

class ADEIDB {
 var $dbh;

 const MYSQL_ER_BAD_DB_ERROR = 1049;
 const MYSQL_ER_NO_SUCH_TABLE = 1146;
 const MYSQL_ER_DUP_ENTRY = 1062;
 const MYSQL_ER_WRONG_VALUE_COUNT_ON_ROW = 1136;
 const MYSQL_ER_SP_ALREADY_EXISTS = 1304;
 const MYSQL_ER_BAD_FIELD_ERROR = 1054;

 function __construct() {
    global $ADEI_DB;

    $this->dbh = $this->ConnectDB();
 }

 static function ConnectDB() {
    global $ADEI_DB;
    global $ADEI_CACHE_ENGINE;

    $dbh = mysql_connect($ADEI_DB['host'] . ($ADEI_DB['port']?(":" . $ADEI_DB['port']):""), $ADEI_DB['user'], $ADEI_DB['password']);
    if (!$dbh) throw new ADEIException(translate("Connection to the Caching MySQL Server is failed"));
    
    mysql_query("SET sql_mode = ''", $dbh);
    mysql_query("SET time_zone = '+0:00'", $dbh);

	    // We expect that INNODB is used if replication is on...
    if (!strcasecmp($ADEI_CACHE_ENGINE, "MYISAM")) {
	mysql_query("SET sql_log_bin = 0", $dbh);
    }
        // We don't need syncing even if INNODB and replication is used, re-caching couple of last minutes is completely fine
    mysql_query("SET sync_binlog = 0", $dbh);


    if (!@mysql_select_db($ADEI_DB['database'], $dbh)) {
		// ER_NO_DB_ERROR
	if (mysql_errno($dbh) == CACHEDB::MYSQL_ER_BAD_DB_ERROR) { 
	    if (mysql_query("CREATE DATABASE IF NOT EXISTS " . $ADEI_DB['database'], $dbh)) {
		if (!mysql_select_db($ADEI_DB['database'], $dbh))
		    throw new ADEIException(translate("Connection to the Caching MySQL Database is failed") . " (" . mysql_error($dbh) . ")");

		self::CreateStoredProcedures($dbh);
	    } else 
		throw new ADEIException(translate("Creation of the caching MySQL database is failed") . " (" . mysql_error($dbh) . ")");
	    
	    
	} else 
	    throw new ADEIException(translate("Connection to the Caching MySQL Database is failed") . " (" . mysql_error($dbh) . ")");
    }
    
    return $dbh;
 }

 function CreateStoredProcedures($dbh = false) {
    if (!$dbh) $dbh = $this->dbh;
    
	/* The starting points of timestamps should be floating and should not
	be referenced in other places of app */
    $query = "CREATE FUNCTION ADEI_TIMESTAMP(unix_timestamp INT, ns INT) RETURNS BIGINT DETERMINISTIC RETURN unix_timestamp*1000000000+ns";
    if ((!mysql_query($query, $dbh))&&(mysql_errno($dbh) != CACHEDB::MYSQL_ER_SP_ALREADY_EXISTS)) {
	throw new ADEIException(translate("CACHE is not able to create stored procedure (%s), error: %s", "ADEI_TIMESTAMP", mysql_error($dbh)));
    }

    $query = "CREATE FUNCTION EXTENDED_UNIX_TIMESTAMP(dt DATETIME) RETURNS BIGINT DETERMINISTIC RETURN TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00', dt)";
    if ((!mysql_query($query, $dbh))&&(mysql_errno($dbh) != CACHEDB::MYSQL_ER_SP_ALREADY_EXISTS)) {
	throw new ADEIException(translate("CACHE is not able to create stored procedure (%s), error: %s", "EXTENDED_UNIX_TIMESTAMP", mysql_error($dbh)));
    }
 }

 function LockCachingTable($table) {
    mysql_query("LOCK TABLES $table WRITE");
 }
 
 function UnLockCache() {
    mysql_query("UNLOCK TABLES");
 }

 function CreateTable($name, $spec) {
    if (!@mysql_query("CREATE TABLE IF NOT EXIST `$name` ($spec)", $this->dbh)) {
	throw new ADEIException(translate("Creation of system table (%s) within CACHE database is failed", $name) . " (" . mysql_error($this->dbh) . ")");
    }
 }
 
 function AppendRecord($table, $values, $spec = false, $update = true) {
    if (is_array($update)) {
	$arr = array();
	foreach ($update as $key => $col) {
	    array_push($arr, "`$col` = '{$values[$key]}'");
	}
	if ($arr) $update = " ON DUPLICATE KEY UPDATE " . implode(",", $arr) . "";
	else $update = "";
    } else $update = "";
    
    $query = "INSERT INTO `$table` VALUES('" . implode("','", $values) . "')$update";
#    echo $query . "\n";
    if (!@mysql_query($query, $this->dbh)) {
	switch (mysql_errno($this->dbh)) {
	    case CACHE::MYSQL_ER_NO_SUCH_TABLE:
		if ($spec) {
		    $this->CreateTable($table, $spec);
		    if (@mysql_query($query, $this->dbh)) return;
		} else {
		    throw new ADEIException(translate("Table (%s) is not found within CACHE database", $table));
		}
	    break;
	    case CACHE::MYSQL_ER_DUP_ENTRY:
		if ($update === false) return;
		else if ($update === true) {
		    throw new ADEIException(translate("Record is already existing in the caching table (%s)", $table));
		}
	    break;
	}
	throw new ADEIException(translate("Can not append entry into the caching table (%s), error: %s", $table, mysql_error($this->dbh)));
    }
 }
 
 function DeleteRecord($table, $cond) {
    $res = "DELETE FROM $table";
    if (is_array($cond)) {
	$conds = array();
	foreach ($cond as $col => $value) {
	    if ($value === false) $value = 0;
	    array_push($conds, "`$col` = '$value'");
	}
	
	if ($conds) {
	    $res .= " WHERE (" . implode(") AND (", $conds) . ")";
	}
    } else if ($cond) $res .= " WHERE $cond";
    
#    echo $res . "\n";
    if (!@mysql_query($res, $this->dbh)) {
	throw new ADEIException(translate("Can not delete entry from the caching table (%s), error: %s", $table, mysql_error($this->dbh)));
    }
 }
 
 function Select($request) {
    $res = @mysql_query($request, $this->dbh);
    if (!$res) {
	switch (mysql_errno($this->dbh)) {
	    case CACHE::MYSQL_ER_NO_SUCH_TABLE:
	    break;
	    default:
		throw new ADEIException(translate("Can not query CACHE table, error") . ": " . mysql_error($this->dbh));
	}
	return array();
    }

    $list = array();
    while ($row = mysql_fetch_row($res)) {
	array_push($list, $row);
    }
    
    return $list;
 }
 
 function SelectRequest($table, $columns="*", array $req = NULL) {
    $res = "SELECT ";

    if (is_array($columns)) {
	if (!$columns) 
	    throw new ADEIException(translate("Select request is failed: column list is empty"));

	$res .= "`" . implode("`,`", $columns) . "`";
    } else $res .= $columns;

    $res .= " FROM " . $table;

    if ($req['condition']) $res = " WHERE " . $req['condition'];
    else if (is_array($req['columns_equal'])) {
	$conds = array();
	foreach ($req['columns_equal'] as $col => $value) {
	    if ($value === false) $value = 0;
	    array_push($conds, "`$col` = '$value'");
	}
	
	if ($conds) {
	    $res .= " WHERE (" . implode(") AND (", $conds) . ")";
	}
    }
    
    if ($req['group']) $res .= " GROUP BY " . $req['group'];
    if ($req['order']) $res .= " ORDER BY " . $req['order'];

    if ($req['limit']) $res .= " LIMIT " . $req['limit'];

    return $this->Select($res);
 }    
}

?>