| 1 |
<? |
| 2 |
|
| 3 |
// ------------------------------------------------ |
| 4 |
// our DataStore |
| 5 |
// instantiates a DataStore-Session |
| 6 |
class DataStore { |
| 7 |
// ------------------------------------------------ |
| 8 |
|
| 9 |
var $session_current; |
| 10 |
var $objectfactory; |
| 11 |
|
| 12 |
// constructor |
| 13 |
// create a new DataStore-Session |
| 14 |
function DataStore($cfg) { |
| 15 |
slog("DataStore: new DataStore()"); |
| 16 |
$this->objectfactory =& new ObjectFactory(); |
| 17 |
$this->session_current =& new DSSession($cfg); |
| 18 |
$this->init(); |
| 19 |
} |
| 20 |
|
| 21 |
function init() { |
| 22 |
$this->objectfactory->init(&$this); |
| 23 |
$this->session_current->init(&$this); |
| 24 |
} |
| 25 |
|
| 26 |
function getDataObject($type) { |
| 27 |
return $this->session_current->getDataObject($type); |
| 28 |
} |
| 29 |
function getDataCollection($type) { |
| 30 |
return $this->session_current->getDataCollection($type); |
| 31 |
} |
| 32 |
function getDataOperation($name) { |
| 33 |
return $this->session_current->getDataOperation($name); |
| 34 |
} |
| 35 |
|
| 36 |
// return a new DataStore-Session |
| 37 |
function &getSession() { |
| 38 |
slog("DataStore: &getSession()"); |
| 39 |
global $database; |
| 40 |
$database->ref_dssession =& $this->session_current; |
| 41 |
return $this->session_current; |
| 42 |
} |
| 43 |
|
| 44 |
// return a new DataStore-ObjectFactory |
| 45 |
function &getObjectFactory() { |
| 46 |
slog("DataStore: &getObjectFactory()"); |
| 47 |
return $this->objectfactory; |
| 48 |
} |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
// ------------------------------------------------ |
| 55 |
// our ObjectFactory |
| 56 |
// normally instantiated inside a SessionManager-Session |
| 57 |
class ObjectFactory { |
| 58 |
// ------------------------------------------------ |
| 59 |
|
| 60 |
// a hash for remembering which classes we already built |
| 61 |
var $declaredClasses; |
| 62 |
var $session; |
| 63 |
|
| 64 |
// constructor |
| 65 |
function &ObjectFactory() { |
| 66 |
slog("ObjectFactory: new &ObjectFactory()"); |
| 67 |
// clear declared-classes-hash |
| 68 |
//global $glbl_declaredClasses; |
| 69 |
//$glbl_declaredClasses = array(); |
| 70 |
|
| 71 |
//$this->session =& $parentsession; |
| 72 |
} |
| 73 |
|
| 74 |
function init(&$datastore) { |
| 75 |
slog("ObjectFactory: init(&$datastore)"); |
| 76 |
$this->session =& $datastore->getSession(); |
| 77 |
} |
| 78 |
|
| 79 |
// ------ |
| 80 |
// declares a new class "on the fly" which might extend another (existing) class |
| 81 |
function declareClass($objecttype, $extends) { |
| 82 |
// ------ |
| 83 |
|
| 84 |
slog("ObjectFactory: declareClass($objecttype, $extends)"); |
| 85 |
|
| 86 |
global $glbl_declaredClasses; |
| 87 |
|
| 88 |
// perhaps everything has already been done? ... |
| 89 |
if (!isset($glbl_declaredClasses[$objecttype])) { |
| 90 |
// ... no, we do all the work now |
| 91 |
|
| 92 |
// remember class |
| 93 |
$glbl_declaredClasses[$objecttype] = 1; |
| 94 |
|
| 95 |
// does the class extend from another existing one? |
| 96 |
$extends_part = ''; |
| 97 |
if ($extends) { $extends_part = "extends $extends"; } |
| 98 |
|
| 99 |
// build evalstring |
| 100 |
$evstring = "class $objecttype $extends_part {}"; |
| 101 |
|
| 102 |
// evaluate evalstring |
| 103 |
eval($evstring); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// ------ |
| 108 |
// creates and returns a new DataObject |
| 109 |
function &createEmptyDSDataObject($objecttype) { |
| 110 |
|
| 111 |
// debugging |
| 112 |
slog("ObjectFactory: &createEmptyDataObject($objecttype)"); |
| 113 |
|
| 114 |
// declare a class which extends "DSDataObject" |
| 115 |
$this->declareClass($objecttype, 'DSDataObject'); |
| 116 |
|
| 117 |
// create and return new object instantiated from class declared above..... ( tricky, heh? :-) ) |
| 118 |
return new $objecttype($objecttype, &$this->session); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
// ------ |
| 123 |
// creates and returns a new DataObject |
| 124 |
function &createEmptyDSDataCollection($objecttype) { |
| 125 |
|
| 126 |
// create and return new DataCollection |
| 127 |
return new DSDataCollection($objecttype, &$this->session); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
// ============================================================== |
| 137 |
// the "DataStore"-"Session" |
| 138 |
class DSSession { |
| 139 |
|
| 140 |
// the session's "PDLEngine"-Object, with which we can retrieve all metadata |
| 141 |
var $pdlengine; |
| 142 |
|
| 143 |
// the session's ObjectFactory |
| 144 |
var $objectfactory; |
| 145 |
|
| 146 |
|
| 147 |
// -------------------------------------------------------------- |
| 148 |
// constructor |
| 149 |
function DSSession($cfg) { |
| 150 |
|
| 151 |
// debugging |
| 152 |
slog("DSSession: new &DSSession()"); |
| 153 |
|
| 154 |
// create a new "PDLEngine"-object |
| 155 |
$this->pdlengine =& new PDLEngine($cfg); |
| 156 |
|
| 157 |
// create a new ObjectFactory |
| 158 |
//$this->objectfactory =& $datastore->getObjectFactory(); |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
// -------------------------------------------------------------- |
| 164 |
// initialize session |
| 165 |
function init(&$datastore) { |
| 166 |
|
| 167 |
// debugging |
| 168 |
slog("DSSession: init(&$datastore)"); |
| 169 |
|
| 170 |
// get reference to an "ObjectFactory" |
| 171 |
$this->objectfactory =& $datastore->getObjectFactory(); |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
// -------------------------------------------------------------- |
| 177 |
// returns a new "DSDataObject" of given ObjectType |
| 178 |
function &getDataObject($objecttype) { |
| 179 |
|
| 180 |
// debugging |
| 181 |
slog("DSSession: &getDataObject($objecttype)"); |
| 182 |
|
| 183 |
// if we can get metadata from pdl ... |
| 184 |
if (($r_mo =& $this->pdlengine->getPDLObject($objecttype))) { |
| 185 |
// ... create and return new "DSDataObject" |
| 186 |
return $this->objectfactory->createEmptyDSDataObject($r_mo->type); |
| 187 |
} else { |
| 188 |
print("DSSession->getDataObject($objecttype) failed.<br>"); |
| 189 |
} |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
// -------------------------------------------------------------- |
| 194 |
// returns a new "DSDataCollectionObject" of given ObjectType |
| 195 |
function &getDataCollection($objecttype) { |
| 196 |
|
| 197 |
// debugging |
| 198 |
slog("DSSession: &getDataCollection($objecttype)"); |
| 199 |
|
| 200 |
// if we can get metadata from pdl ... |
| 201 |
if (($r_mo =& $this->pdlengine->getPDLObject($objecttype))) { |
| 202 |
// ... create and return new "DSDataCollection" |
| 203 |
return $this->objectfactory->createEmptyDSDataCollection($r_mo->type); |
| 204 |
} else { |
| 205 |
print("<font color=\"red\">DSSession->getDataCollection() failed, could not retrieve meta-information (from pdl-file) about object \"$objecttype\"</font><br>"); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
function &getDataOperation($name) { |
| 210 |
|
| 211 |
// if we can get metadata from pdl ... |
| 212 |
if (!($r_do =& $this->pdlengine->getPDLDataOperation($name))) { |
| 213 |
trigger_error("DSSession->&getDataOperation(): Could not find PDLDataOperation \"$name\"", E_USER_ERROR); |
| 214 |
} |
| 215 |
$unreadySql = $r_do->get_unreadySql(); |
| 216 |
|
| 217 |
// create new "DataQuery" we'll return later |
| 218 |
$dq = new DataQuery(); |
| 219 |
|
| 220 |
// initialize "DataQuery"s sql-part with the "unready" sql-statement |
| 221 |
//$this->ref_dataquery->setQuery($unreadySql); |
| 222 |
$dq->setQuery($unreadySql); |
| 223 |
|
| 224 |
// initialize "DataQuery"s map-part with mapping-informations |
| 225 |
// $this->ref_dataquery->setMap($this->ref_pdlModelObjectAttributes); |
| 226 |
|
| 227 |
return $dq; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
// ============================================================== |
| 238 |
class DSFilter { |
| 239 |
|
| 240 |
// here the parts of the filter-term are stored (hash) |
| 241 |
var $store_input_term; |
| 242 |
// a hash keeping the values got by "DSFilter->set($varname, $value)" |
| 243 |
var $store_input_values; |
| 244 |
// the output-information |
| 245 |
var $store_output_terms; |
| 246 |
|
| 247 |
//var $logicalOperator; |
| 248 |
|
| 249 |
// have we been processed? |
| 250 |
var $bool_processed; |
| 251 |
|
| 252 |
|
| 253 |
// -------------------------------------------------------------- |
| 254 |
function DSFilter($filterstring, $logicalOperator) { |
| 255 |
|
| 256 |
// initialize class-variables |
| 257 |
$this->store_input_term = ''; |
| 258 |
$this->store_input_values = array(); |
| 259 |
$this->store_output_terms = array(); |
| 260 |
$this->logicalOperator = $logicalOperator; |
| 261 |
|
| 262 |
$this->bool_processed = 0; |
| 263 |
|
| 264 |
// debugging |
| 265 |
slog("DSFilter: DSFilter($filterstring)"); |
| 266 |
|
| 267 |
// TODO: |
| 268 |
// do determination of *all* kinds of filter-formats here ... (or move this as a complete block to another proper/better location, |
| 269 |
// or encapsulate it into a "FilterParserEngine" or "FilterFactory" or something like this!) |
| 270 |
// e.g. these two kinds ("where ... like ...", "match ... against ...") and (new ones: "<column> IS NULL", "<column> IN (<list>)") |
| 271 |
// ... and handle special reactions on these also here, so we can move on from below this block in a generic way :-) |
| 272 |
|
| 273 |
|
| 274 |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 275 |
// 1. do we have a "WHERE ... LIKE ..." - kind of filter? |
| 276 |
|
| 277 |
if ( preg_match('/(.+) (.+) (.+)/', $filterstring, $regs) ) { |
| 278 |
$term = array( |
| 279 |
'term' => $filterstring, |
| 280 |
'attribute' => trim($regs[1]), // TODO: is really the left argument the attribute? we assume it for now ... |
| 281 |
'lop' => trim($regs[2]), // TODO: lop-juggling (abstract the logical operator via meta-information!?) |
| 282 |
'value' => trim($regs[3]), // TODO: is really the right argument the value? we assume it for now ... |
| 283 |
); |
| 284 |
|
| 285 |
// determine value-encapsulator |
| 286 |
$bool_isNull = (($term['lop'] == 'IS') && ($term['value'] == '<Null>')); |
| 287 |
$bool_isListComparison = ($term['lop'] == 'IN'); |
| 288 |
if ($bool_isNull || $bool_isListComparison) { |
| 289 |
$valueEncapsulator = ""; |
| 290 |
} else { |
| 291 |
$valueEncapsulator = "'"; |
| 292 |
} |
| 293 |
$term['valueEncapsulator'] = $valueEncapsulator; |
| 294 |
|
| 295 |
$this->store_input_term = $term; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
/* |
| 300 |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 301 |
// 2. do we have a "MATCH ... AGAINST ..." - kind of filter? |
| 302 |
|
| 303 |
if ( eregi('MATCH (.+) AGAINST (.+)', $filterstring, $regs) ) { |
| 304 |
$term = array( |
| 305 |
'term' => $filterstring, |
| 306 |
'attribute' => trim($regs[1]), |
| 307 |
'lop' => 'MATCH AGAINST', // TODO: lop-juggling (abstract the logical operator via meta-information!?) |
| 308 |
'value' => trim($regs[2]), |
| 309 |
); |
| 310 |
$term['valueEncapsulator'] = "'"; |
| 311 |
$this->store_input_term = $term; |
| 312 |
} |
| 313 |
*/ |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
// -------------------------------------------------------------- |
| 319 |
function set($varname, $value) { |
| 320 |
slog("DSFilter: set($varname, $value)"); |
| 321 |
$this->store_input_values[$varname] = $value; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
// -------------------------------------------------------------- |
| 326 |
function getParsed() { |
| 327 |
|
| 328 |
if (!$this->bool_processed) { |
| 329 |
$this->_process(); |
| 330 |
} |
| 331 |
|
| 332 |
$retval = ''; |
| 333 |
$bool_first = 1; |
| 334 |
reset($this->store_output_terms); |
| 335 |
while($term = current($this->store_output_terms)) { |
| 336 |
if (!$bool_first) { |
| 337 |
$retval .= ' ' . $this->logicalOperator . ' '; |
| 338 |
} |
| 339 |
$retval .= '(' . $term['attribute'] . ' ' . $term['lop'] . ' ' . $term['value'] . ')'; |
| 340 |
$bool_first = 0; |
| 341 |
next($this->store_output_terms); |
| 342 |
} |
| 343 |
|
| 344 |
//print $retval . "<br>"; |
| 345 |
return $retval; |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
// -------------------------------------------------------------- |
| 351 |
function _process() { |
| 352 |
|
| 353 |
// debugging |
| 354 |
slog("DSFilter: getParsed()"); |
| 355 |
|
| 356 |
$term = $this->store_input_term; |
| 357 |
$terms_created = array(); |
| 358 |
|
| 359 |
reset($this->store_input_values); |
| 360 |
while ( list($name, $value) = each($this->store_input_values) ) { |
| 361 |
|
| 362 |
$termkey = $name; |
| 363 |
|
| 364 |
if (is_array($value)) { |
| 365 |
$value_arr = $value; |
| 366 |
for ($i = 0; $i < count($value_arr); $i++) { |
| 367 |
$value = $value_arr[$i]; |
| 368 |
$this->_process_term($term, $value, $terms_created[$termkey . '_' . $i]); |
| 369 |
} |
| 370 |
} else { |
| 371 |
$this->_process_term($term, $value, $terms_created[$termkey]); |
| 372 |
} |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
//dumpVar($terms_created); |
| 377 |
|
| 378 |
// store a processed term to the output-terms for usage in "->getParsed()"! |
| 379 |
//array_push($this->store_output_terms, $term); |
| 380 |
$this->store_output_terms = $terms_created; |
| 381 |
|
| 382 |
// declare us as processed |
| 383 |
$this->bool_processed = 1; |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
// -------------------------------------------------------------- |
| 389 |
function _process_term($term, $value, &$term_output) { |
| 390 |
|
| 391 |
$bool_encapsulateValueInBrackets = 0; |
| 392 |
|
| 393 |
// - - - - - - - - - - - - - - - |
| 394 |
// 1. patches related to attributes |
| 395 |
// determine if the "attribute-string" is really a list |
| 396 |
// if (preg_match("/\((.+)\)/", $term['attribute'], $regs)) { |
| 397 |
//$list_attributes = $regs[1]; |
| 398 |
//$list_attributes = preg_replace("/(\w+)/", ":\\1", $list_attributes); |
| 399 |
//$term['attribute'] = "(" . $list_attributes . ")"; |
| 400 |
// $term['attribute'] = preg_replace("/(\w+)/", ":\\1", $regs[1]); |
| 401 |
if ($term['attribute'] = preg_replace("/(\w+)/", ":\\1", $term['attribute'])) { |
| 402 |
} else { |
| 403 |
// prefix single attributename with ':' |
| 404 |
$term['attribute'] = ":" . $term['attribute']; |
| 405 |
} |
| 406 |
|
| 407 |
// - - - - - - - - - - - - - - - |
| 408 |
// 2. patches related to logical operators |
| 409 |
if ($term['lop'] == 'MATCH_AGAINST') { |
| 410 |
$term['attribute'] = "MATCH " . $term['attribute']; |
| 411 |
$term['lop'] = "AGAINST"; |
| 412 |
$bool_encapsulateValueInBrackets = 1; |
| 413 |
} |
| 414 |
|
| 415 |
// - - - - - - - - - - - - - - - |
| 416 |
// 3. patches related to values |
| 417 |
|
| 418 |
if ($value == '<Null>') { |
| 419 |
$term['lop'] = 'IS'; |
| 420 |
$term['value'] = 'NULL'; |
| 421 |
} else { |
| 422 |
// set the "real" value |
| 423 |
$term['value'] = $term['valueEncapsulator'] . $value . $term['valueEncapsulator']; |
| 424 |
} |
| 425 |
|
| 426 |
if (preg_match('/IN \((.+)\)/', $value, $regs)) { |
| 427 |
$term['lop'] = 'IN'; |
| 428 |
$term['value'] = $regs[1]; |
| 429 |
$bool_encapsulateValueInBrackets = 1; |
| 430 |
} |
| 431 |
|
| 432 |
// 4. do some cleanup |
| 433 |
if ($bool_encapsulateValueInBrackets) { |
| 434 |
$term['value'] = '(' . $term['value'] . ')'; |
| 435 |
} |
| 436 |
|
| 437 |
$term_output = $term; |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
// ============================================================== |
| 447 |
class DSSortOrder { |
| 448 |
|
| 449 |
var $attributename; |
| 450 |
var $order_ascdesc; |
| 451 |
|
| 452 |
function DSSortOrder($attributename, $order_ascdesc) { |
| 453 |
|
| 454 |
//slog("DSOrder: DSOrder($column, $mode)"); |
| 455 |
|
| 456 |
$this->attributename = $attributename; |
| 457 |
$this->order_ascdesc = $order_ascdesc; |
| 458 |
} |
| 459 |
|
| 460 |
/* |
| 461 |
function set($varname, $value) { |
| 462 |
$this->filterstring_parsed2 = str_replace(':' . $varname, "'$value'", $this->filterstring_parsed1); |
| 463 |
} |
| 464 |
*/ |
| 465 |
|
| 466 |
function getAttribute() { |
| 467 |
return ':' . $this->attributename; |
| 468 |
} |
| 469 |
function getOrderAscDesc() { |
| 470 |
return strtoupper($this->order_ascdesc); |
| 471 |
} |
| 472 |
|
| 473 |
} |
| 474 |
?> |