1 |
<? |
2 |
|
3 |
// ========================================================= |
4 |
class QueryStatement extends AbstractQueryStatement { |
5 |
|
6 |
} |
7 |
|
8 |
// =========================================================== |
9 |
class AbstractQuery extends QueryStatement { |
10 |
|
11 |
var $bool_loaded; |
12 |
var $filters; |
13 |
var $metainfo; |
14 |
|
15 |
var $resultset; |
16 |
var $row; |
17 |
var $lastInsertId; |
18 |
|
19 |
function AbstractQuery() { |
20 |
parent::AbstractQueryStatement(); |
21 |
$this->bool_loaded = 0; |
22 |
$this->filters = array(); |
23 |
} |
24 |
|
25 |
function &getResult() { |
26 |
if ($this->bool_hasToBeMapped) { |
27 |
$this->_doReverseMapping(); |
28 |
return $this->row; |
29 |
} else { |
30 |
return $this->row; |
31 |
} |
32 |
} |
33 |
|
34 |
function getMetaInfo() { |
35 |
if (isset($this->metainfo)) { return $this->metainfo; } |
36 |
} |
37 |
|
38 |
function _doReverseMapping() { |
39 |
|
40 |
// - - - - - - - - - - - - - - - - - - - - - |
41 |
// modify result |
42 |
$wrkHash_in = $this->row; |
43 |
$wrkHash_out = array(); |
44 |
|
45 |
reset($this->map_reverse); |
46 |
while( list($name_map, $name_attribute) = each($this->map_reverse) ) { |
47 |
// HACK: strip tablename from "$name_map" |
48 |
$name_map = substr($name_map, strrpos($name_map, '.') + 1); |
49 |
if (isset($wrkHash_in[$name_map])) { |
50 |
$wrkHash_out[$name_attribute] = $wrkHash_in[$name_map]; |
51 |
} else { |
52 |
// debugging (disable in productive mode / do not exit here!) |
53 |
//print "Could not find column \"{$this->metainfo['table']['name']}.$name_map\" "; |
54 |
//print "when attempting to reverse map to \"$name_attribute\".<br>"; |
55 |
//exit; |
56 |
} |
57 |
} |
58 |
|
59 |
$this->row = $wrkHash_out; |
60 |
|
61 |
} |
62 |
|
63 |
} |
64 |
|
65 |
|
66 |
// ========================================================= |
67 |
class DataQuery extends AbstractQuery { |
68 |
|
69 |
function query() { |
70 |
|
71 |
global $database; |
72 |
|
73 |
// get sql |
74 |
$sql = $this->getSql(); |
75 |
$sql = trim($sql); |
76 |
|
77 |
// debug |
78 |
//print "<font size=\"1\" color=\"darkred\"><u>sql:</u> </font><font size=\"1\" color=\"darkblue\">" . $sql . "<br></font>"; |
79 |
|
80 |
// check if query is empty |
81 |
if (!$sql) { return; } |
82 |
|
83 |
// query database |
84 |
$this->resultset = $database->query( $sql ); |
85 |
|
86 |
// do additional actions by CRUD |
87 |
unset($this->lastInsertId); |
88 |
switch ($this->crud) { |
89 |
case "C": |
90 |
$this->lastInsertId = $database->getLastInsertId(); |
91 |
break; |
92 |
case "R": |
93 |
$this->retrieveMetaInfo(); |
94 |
break; |
95 |
case "U": |
96 |
break; |
97 |
case "D": |
98 |
break; |
99 |
} |
100 |
|
101 |
} |
102 |
|
103 |
function retrieveMetaInfo() { |
104 |
global $database; |
105 |
if (isset($this->resultset)) { |
106 |
$this->metainfo = $database->retrieveMetaInfo($this->resultset); |
107 |
} |
108 |
} |
109 |
|
110 |
function next() { |
111 |
global $database; |
112 |
if ( isset($this->resultset) && ($this->row = $database->fetchRow($this->resultset)) ) { |
113 |
return 1; |
114 |
} |
115 |
} |
116 |
|
117 |
function getRowCount() { |
118 |
global $database; |
119 |
if (isset($this->resultset)) { |
120 |
return $database->getRowCount($this->resultset); |
121 |
} |
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
?> |