1 |
<? |
2 |
|
3 |
// ================================================================== |
4 |
// main "PDLModel"-object |
5 |
// represents a pdl-model which consists of pdl-objects and/or pdl-associations and some other metadata |
6 |
class PDLModel { |
7 |
|
8 |
// the name of this model |
9 |
var $name; |
10 |
|
11 |
// hash with references to PDLModelObject-objects |
12 |
var $pdlModelObjects; |
13 |
|
14 |
// hash with references to PDLModelDataOperation-objects |
15 |
var $pdlModelDataOperations; |
16 |
|
17 |
// hash with references to PDLModelAssociation-objects |
18 |
var $pdlModelAssociations; |
19 |
|
20 |
// some metadata |
21 |
var $meta_VariableTypes; |
22 |
|
23 |
|
24 |
// ------------------------------------------------------- |
25 |
function PDLModel($modelname, $filename) { |
26 |
|
27 |
slog("PDLModel: PDLModel($modelname, $filename)"); |
28 |
|
29 |
// set the model-name |
30 |
$this->name = $modelname; |
31 |
|
32 |
// clear pdl-objects and pdl-associations |
33 |
$this->pdlModelObjects = array(); |
34 |
$this->pdlModelDataOperations = array(); |
35 |
$this->pdlModelAssociations = array(); |
36 |
|
37 |
// initialize metadata |
38 |
$tmp_vartypes = "BigInteger,BigDecimal,Boolean,Byte,Character,Date,Double,Float,Integer,Long,Short,String,Blob"; |
39 |
$this->meta_VariableTypes = split(',', $tmp_vartypes); |
40 |
|
41 |
// create new "PDLReader"-object ... |
42 |
$pdlreader =& new PDLReader(); |
43 |
// ... and parse PDL-file |
44 |
$pdlreader->parsePdlFile($filename, &$this); |
45 |
|
46 |
} |
47 |
|
48 |
|
49 |
// ------------------------------------------------------- |
50 |
// adds the reference to a given PDLModelObject to local "pdlModelObjects"-hash (with its object-type as key) |
51 |
function addObject($objecttype, &$pdlModelObject) { |
52 |
// check for having defined any events |
53 |
if (count($pdlModelObject->events)) { |
54 |
$pdlModelObject->bool_autoevent = 0; |
55 |
} else { |
56 |
// TODO: what shall happen to "bool_autoevent"? it isn't used for now |
57 |
// either flag "PDLModelObject" to "auto-event" ... |
58 |
// $pdlModelObject->bool_autoevent = 1; |
59 |
// ... or create all needed events right now |
60 |
$pdlModelObject->createEventsFromAttributeMaps(); |
61 |
} |
62 |
$this->pdlModelObjects[$objecttype] =& $pdlModelObject; |
63 |
} |
64 |
|
65 |
// ------------------------------------------------------- |
66 |
// returns the reference to a PDLModelObject by a given ObjectType |
67 |
function &getObject($objecttype) { |
68 |
return $this->pdlModelObjects[$objecttype]; |
69 |
} |
70 |
|
71 |
// ------------------------------------------------------- |
72 |
// |
73 |
function existsObject($objecttype) { |
74 |
return isset($this->pdlModelObjects[$objecttype]); |
75 |
} |
76 |
|
77 |
|
78 |
// ------------------------------------------------------- |
79 |
// adds the reference to a given PDLModelObject to local "pdlModelObjects"-hash (with its object-type as key) |
80 |
function addDataOperation($operationname, &$pdlModelDataOperation) { |
81 |
$this->pdlModelDataOperations[$operationname] =& $pdlModelDataOperation; |
82 |
} |
83 |
|
84 |
// ------------------------------------------------------- |
85 |
// returns the reference to a PDLModelDataOperation by a given operationname |
86 |
function &getDataOperation($operationname) { |
87 |
return $this->pdlModelDataOperations[$operationname]; |
88 |
} |
89 |
|
90 |
} |
91 |
|
92 |
|
93 |
// ------------------------------------------------------- |
94 |
// main "PDLModelObject"-object |
95 |
// represents a pdl-ModelObject which is used to keep track of "PDLModelEvent"-objects (register and query) |
96 |
class PDLModelObject { |
97 |
// ------------------------------------------------------- |
98 |
|
99 |
// the ObjectType of this PDLModelObject |
100 |
var $type; |
101 |
var $objectkey; |
102 |
|
103 |
// is this object an auto-event-object? |
104 |
var $bool_autoevent; |
105 |
|
106 |
// a hash of references to "PDLModelAttribute"-objects |
107 |
var $attributes; |
108 |
|
109 |
// a hash of references to "PDLModelEvent"-objects |
110 |
var $events; |
111 |
|
112 |
// ------ |
113 |
// constructor |
114 |
function PDLModelObject($objecttype) { |
115 |
slog("PDLModelObject: PDLModelObject($objecttype)"); |
116 |
|
117 |
// set ObjectType |
118 |
$this->type = $objecttype; |
119 |
|
120 |
// clear ObjectKey |
121 |
$this->objectkey = ''; |
122 |
|
123 |
$this->bool_autoevent = 0; |
124 |
|
125 |
// clear attributes |
126 |
$this->attributes = array(); |
127 |
// clear events |
128 |
$this->events = array(); |
129 |
} |
130 |
|
131 |
// ------ |
132 |
// creates an object-attribute ("PDLModelAttribute") and adds reference to local "attributes"-hash |
133 |
function &addAttribute($args) { |
134 |
//slog("PDLModelObject: addAttribute($attributename, !content!)"); |
135 |
list($name, $type, $map_name, $map_type_specific) = $args; |
136 |
$r_attr =& new PDLModelAttribute($name, $type, $map_name, $map_type_specific); |
137 |
$this->attributes[$name] =& $r_attr; |
138 |
return $r_attr; |
139 |
} |
140 |
|
141 |
|
142 |
// ------------------------------------------------------------ |
143 |
// creates an event ("PDLModelEvent") and adds reference to local "events"-hash |
144 |
function &_addEvent($args) { |
145 |
list($name, $unreadySql) = $args; |
146 |
$r_ev =& new PDLModelEvent($name, $unreadySql); |
147 |
$this->events[$name] =& $r_ev; |
148 |
return $r_ev; |
149 |
} |
150 |
|
151 |
|
152 |
// ------------------------------------------------------------ |
153 |
// creates an event and maps its metadata |
154 |
function &addEventAndOrMap($args) { |
155 |
list($name, $unreadySql, $map) = $args; |
156 |
$this->_setMap_AllAttributes($map); |
157 |
$r_ev =& $this->_addEvent(array($name, $unreadySql)); |
158 |
return $r_ev; |
159 |
} |
160 |
|
161 |
|
162 |
// ------------------------------------------------------------ |
163 |
function createEventsFromAttributeMaps() { |
164 |
|
165 |
|
166 |
$attributes_ForNameReplacement = array(); |
167 |
$attributes_ForValueReplacement = array(); |
168 |
$columns = array(); |
169 |
$map_attribute2column_ForNameReplacement = array(); |
170 |
|
171 |
$eventpart_tablename = ''; |
172 |
$eventpart_attributes_ForNameReplacement = ''; |
173 |
$eventpart_attributes_ForValueReplacement = ''; |
174 |
$eventpart_attributes_ForValueReplacement_ForInsert = ''; |
175 |
$eventpart_columns = ''; |
176 |
|
177 |
// (insert) |
178 |
$map_column2attribute_ForValueReplacement = array(); |
179 |
$eventpart_columns2attributes_ForValueReplacement = ''; |
180 |
|
181 |
|
182 |
|
183 |
// - - - - - - - - - - - - - - - - - - - - - |
184 |
// 1. build attribute-, column-list and mapping-table |
185 |
reset($this->attributes); |
186 |
while($attribute =& current($this->attributes)) { |
187 |
$attributename = $attribute->name; |
188 |
$attributename_ForNameReplacement = ':' . $attribute->name; |
189 |
$attributename_ForValueReplacement = '!' . $attribute->name; |
190 |
$mapname = $attribute->map_name; |
191 |
// bugfix: before we continue, we should check if "$map_value" is valid, else: skip this |
192 |
if ($mapname) { |
193 |
// attribute |
194 |
array_push($attributes_ForNameReplacement, $attributename_ForNameReplacement); |
195 |
array_push($attributes_ForValueReplacement, $attributename_ForValueReplacement); |
196 |
// column |
197 |
$meta_sql = $attribute['meta_sql']; |
198 |
array_push($columns, $mapname); |
199 |
// map |
200 |
$map_attribute2column_ForNameReplacement[$attributename_ForNameReplacement] = $mapname; |
201 |
$map_column2attribute_ForValueReplacement[$mapname] = $attributename_ForValueReplacement; |
202 |
} |
203 |
next($this->attributes); |
204 |
} |
205 |
$eventpart_attributes_ForNameReplacement = join($attributes_ForNameReplacement, ', '); |
206 |
$eventpart_attributes_ForValueReplacement = join($attributes_ForValueReplacement, ', '); |
207 |
$eventpart_columns = join($columns, ', '); |
208 |
$eventpart_columns2attributes_ForValueReplacement = getAttribStringFromHash($map_column2attribute_ForValueReplacement, ', ', '=', ''); |
209 |
|
210 |
|
211 |
// HACK: |
212 |
$eventpart_tablename = substr($columns[0], 0, strpos($columns[0], '.')); |
213 |
|
214 |
|
215 |
// - - - - - - - - - - - - - - - - - - - - - |
216 |
// 2. create auto-events and store their references |
217 |
|
218 |
// HACKs!? |
219 |
|
220 |
// "retrieve single" |
221 |
$unreadySql = "SELECT $eventpart_columns FROM $eventpart_tablename"; |
222 |
$this->_addEvent(array('retrieve single', $unreadySql)); |
223 |
|
224 |
// "retrieve all" |
225 |
$unreadySql = "SELECT $eventpart_columns FROM $eventpart_tablename"; |
226 |
$this->_addEvent(array('retrieve all', $unreadySql)); |
227 |
|
228 |
// "update" |
229 |
$unreadySql = "UPDATE $eventpart_tablename SET $eventpart_columns2attributes_ForValueReplacement"; |
230 |
$this->_addEvent(array('update', $unreadySql)); |
231 |
|
232 |
// "add" |
233 |
$unreadySql = "INSERT INTO $eventpart_tablename ($eventpart_columns) VALUES ($eventpart_attributes_ForValueReplacement)"; |
234 |
$this->_addEvent(array('add', $unreadySql)); |
235 |
|
236 |
// "remove" |
237 |
$unreadySql = "DELETE FROM $eventpart_tablename"; |
238 |
$this->_addEvent(array('remove', $unreadySql)); |
239 |
|
240 |
|
241 |
} |
242 |
|
243 |
|
244 |
// ------------------------------------------------------------ |
245 |
function setObjectKey($objectkey = '') { |
246 |
if ($objectkey) { |
247 |
$this->objectkey = $objectkey; |
248 |
array_push($this->attributes[$objectkey]->meta_sql['attribs'], "PRIMARY KEY"); |
249 |
} |
250 |
} |
251 |
|
252 |
// ------ |
253 |
// sets the mapping of a single object-attribute |
254 |
function _setMap_SingleAttribute($attributename, $mapname) { |
255 |
slog("PDLModelObject: setMap(!map!)"); |
256 |
$this->attributes[$attributename]->set_Mapping($mapname); |
257 |
} |
258 |
|
259 |
// ------ |
260 |
// sets the mapping of all object-attributes |
261 |
function _setMap_AllAttributes($map) { |
262 |
slog("PDLModelObject: setMap(!map!)"); |
263 |
while(list($attributename, $mapname) = each($map)) { |
264 |
if (isset($this->attributes[$attributename])) { |
265 |
$this->_setMap_SingleAttribute($attributename, $mapname); |
266 |
} |
267 |
} |
268 |
} |
269 |
|
270 |
// ------ |
271 |
// returns all "PDLModelAttribute"-objects |
272 |
function getAttributes() { |
273 |
return $this->attributes; |
274 |
} |
275 |
|
276 |
// ------ |
277 |
// returns an event by given eventname |
278 |
function &getEvent($eventname) { |
279 |
slog("PDLModelObject: &getEvent($eventname)"); |
280 |
return $this->events[$eventname]; |
281 |
} |
282 |
|
283 |
|
284 |
|
285 |
/* |
286 |
function getEventContent($eventname) { |
287 |
print "PDLModelObject_getEventContent: \"$eventname\"<br>"; |
288 |
return $this->events[$eventname]->getContent(); |
289 |
} |
290 |
*/ |
291 |
|
292 |
} |
293 |
|
294 |
|
295 |
// -------------------------------------------------------------------------------------------------------------- |
296 |
// main "PDLModelAttribute"-object |
297 |
// represents a pdl-model-attribute which is used to hold metadata about "object-attributes" |
298 |
class PDLModelAttribute { |
299 |
|
300 |
// the attributes of this attribute |
301 |
var $name; |
302 |
var $type; |
303 |
var $map_name; |
304 |
var $map_name_full; |
305 |
var $map_type_specific; |
306 |
|
307 |
// sql-meta-information |
308 |
var $meta_sql; |
309 |
|
310 |
// constructor |
311 |
function PDLModelAttribute($name = '', $type = '', $map_name = '', $map_type_specific = '') { |
312 |
|
313 |
slog("PDLModelAttribute: PDLModelAttribute($name, !content_raw!)"); |
314 |
|
315 |
// setting event-name ... |
316 |
$this->name = $name; |
317 |
$this->type = $type; |
318 |
$this->map_name = $map_name; |
319 |
$this->map_type_specific = $map_type_specific; |
320 |
|
321 |
// initialize the "meta_sql"-array with used keys |
322 |
$this->meta_sql = array('table' => '', 'column' => '', 'type' => '', 'length' => '', 'attribs' => array()); |
323 |
|
324 |
// do some work |
325 |
$this->_create_meta_sql(); |
326 |
|
327 |
} |
328 |
|
329 |
function set_Mapping($map_name) { |
330 |
$this->map_name = $map_name; |
331 |
$this->_create_meta_sql(); |
332 |
} |
333 |
|
334 |
function _create_meta_sql() { |
335 |
|
336 |
// set table / columnname |
337 |
if (stristr($this->map_name, '.')) { |
338 |
$tmp_arr = split('\.', $this->map_name); |
339 |
$this->meta_sql['table'] = $tmp_arr[0]; |
340 |
$this->meta_sql['column'] = $tmp_arr[1]; |
341 |
} else { |
342 |
// set columnname only |
343 |
$this->meta_sql['column'] = $this->map_name; |
344 |
} |
345 |
|
346 |
/* |
347 |
The PDL Attribute Types are the allowed Java types for attributes defined in PDL. |
348 |
Therefore, every type seen here is the Java type returned by the Persistence System. |
349 |
|
350 |
BigInteger |
351 |
BigDecimal |
352 |
Boolean |
353 |
Byte |
354 |
Character |
355 |
Date |
356 |
Double |
357 |
Float |
358 |
Integer |
359 |
Long |
360 |
Short |
361 |
String (this should be used for both Strings and Clobs) |
362 |
Blob (actually returns a Byte[] and will be depricated once |
363 |
the PDL compiler supports arrays) |
364 |
*/ |
365 |
|
366 |
if ($this->map_type_specific) { |
367 |
$this->meta_sql['type'] = $this->map_type_specific; |
368 |
} else { |
369 |
switch ($this->type) { |
370 |
case 'BigDecimal': |
371 |
$this->meta_sql['type'] = 'bigint'; |
372 |
$this->meta_sql['length'] = 20; |
373 |
break; |
374 |
case 'String': |
375 |
$this->meta_sql['type'] = 'varchar'; |
376 |
$this->meta_sql['length'] = 255; |
377 |
break; |
378 |
} |
379 |
} |
380 |
|
381 |
|
382 |
} |
383 |
|
384 |
|
385 |
} |
386 |
|
387 |
|
388 |
|
389 |
// ------------------------------------------------------- |
390 |
// main "PDLModelEvent"-object |
391 |
// represents a pdl-model-event which is used to hold metadata about "object-attributes" <-> "rdbms-columns" - mappings |
392 |
class PDLModelAbstractDataOperation { |
393 |
// ------------------------------------------------------- |
394 |
|
395 |
// the name of this event |
396 |
var $name; |
397 |
var $unreadySql; |
398 |
|
399 |
// ------ |
400 |
// constructor |
401 |
function PDLModelAbstractDataOperation($name, $unreadySql) { |
402 |
slog("PDLModelEvent: PDLModelEvent($name, !content_raw!)"); |
403 |
// setting event-name ... |
404 |
$this->name = $name; |
405 |
// ... and the raw content |
406 |
$this->unreadySql = $unreadySql; |
407 |
} |
408 |
|
409 |
function get_unreadySql() { |
410 |
return $this->unreadySql; |
411 |
} |
412 |
|
413 |
} |
414 |
|
415 |
class PDLModelDataOperation extends PDLModelAbstractDataOperation { } |
416 |
class PDLModelEvent extends PDLModelAbstractDataOperation { } |
417 |
|
418 |
?> |