00001 <?php
00087 $__zigmoyd__Orm_drv = new stdClass();
00093 class zOrm_core extends zCore{
00097 var $lex;
00101 var $tableAlias;
00105 var $realTableName;
00106
00109 var $colv;
00110
00113 var $queryType;
00114
00120 var $driver;
00124 var $connAlias;
00125
00128 var $__struct;
00129
00132 var $numRows;
00136 var $affectedRows;
00140 var $rawExported = null;
00144 var $pk;
00148 var $fk;
00155 var $validation;
00159 var $colQuote;
00163 var $__rdbmsJoin = false;
00167 var $__indexKey;
00171 var $rawSql;
00175 var $trgUpdate = false;
00179 var $trgUpdateCol = array();
00183 var $trgInsert = false;
00187 var $trgInsertCol = array();
00191 var $trgExport = false;
00195 var $trgExportCol = array();
00199 var $trgDelete = false;
00203 var $isJoined = false;
00207 var $joiner = null;
00208
00214 function init(){
00215 $this->tableAlias = $this->__struct->alias;
00216 zLogger::debug("Initializing zigmoyd Orm Layer of $this->tableAlias Table", "orm.$this->tableAlias");
00217 global $__ZigOrm;
00218 if(!defined('ZIGORM_ORDER_ASC')){define('ZIGORM_ORDER_ASC', 'a');}
00219 if(!defined('ZIGORM_ORDER_DESC')){define('ZIGORM_ORDER_DESC', 'd');}
00220 if(!defined('ZIGORM_Q_SELECT')){define('ZIGORM_Q_SELECT', 's');}
00221 if(!defined('ZIGORM_Q_INSERT')){define('ZIGORM_Q_INSERT', 'i');}
00222 if(!defined('ZIGORM_Q_UPDATE')){define('ZIGORM_Q_UPDATE', 'u');}
00223 if(!defined('ZIGORM_Q_REMOVE')){define('ZIGORM_Q_REMOVE', 'r');}
00224 if(!defined('ZIGORM_Q_SQL')){define('ZIGORM_Q_SQL', 'q');}
00225 if(!defined('ZIGORM_Q_TRUNCATE')){define('ZIGORM_Q_TRUNCATE', 't');}
00226 if(!defined('ZIGORM_NO_FLUSH')){define('ZIGORM_NO_FLUSH', false);}
00227 if(!defined('ZIGORM_FLUSH')){define('ZIGORM_FLUSH', true);}
00228 if(!defined('ZIGORM_NO_QUOTE'))define('ZIGORM_NO_QUOTE', false);
00229 if(!defined('ZIGORM_QUOTE'))define('ZIGORM_QUOTE', true);
00230 if(!defined('ZIGORM_Q_JOIN')){define('ZIGORM_Q_JOIN', 'j');}
00231 if(!defined('ZIGORM_Q_AUTOJOIN')){define('ZIGORM_Q_AUTOJOIN', 'aj');}
00232 if(!defined('ZIGORM_JOIN_CROSS'))define('ZIGORM_JOIN_CROSS', 'jc');
00233 if(!defined('ZIGORM_JOIN_LEFT'))define('ZIGORM_JOIN_LEFT', 'jl');
00234 if(!defined('ZIGORM_JOIN_RIGHT'))define('ZIGORM_JOIN_RIGHT', 'jr');
00235 if(!defined('ZIGORM_JOIN_FULL'))define('ZIGORM_JOIN_FULL', 'jf');
00236 if(!defined('ZIGORM_JOIN_INNER'))define('ZIGORM_JOIN_INNER', 'ji');
00237 if(!defined('ZIGORM_JOIN_NATURAL'))define('ZIGORM_JOIN_NATURAL', 'jn');
00238 if(!defined('ZIGORM_OP_EQUALS'))define('ZIGORM_OP_EQUALS', '=');
00239 if(!defined('ZIGORM_OP_GREATER'))define('ZIGORM_OP_GREATER', '>');
00240 if(!defined('ZIGORM_OP_LESS'))define('ZIGORM_OP_LESS', '<');
00241 if(!defined('ZIGORM_OP_GREATER_EQUALS'))define('ZIGORM_OP_GREATER_EQUALS', '>=');
00242 if(!defined('ZIGORM_OP_LESS_EQUALS'))define('ZIGORM_OP_LESS_EQUALS', '<=');
00243 if(!defined('ZIGORM_OP_NOT_EQUALS'))define('ZIGORM_OP_NOT_EQUALS', '<>');
00244 $this->tableAlias = $this->__struct->alias;
00245 $__ZigOrm->dataDict->tables[$this->realTableName] = $this->tableAlias;
00246
00247 $tmp = $this->__struct->Attributes;
00248 foreach($tmp as $realColName => $colObj){
00249 $this->colv[$realColName] = $colObj->alias;
00250 }
00251
00252 $this->validation = new stdClass();
00253 $this->initLex();
00254 $this->attachDriver();
00255 $this->getPk();
00256 if(Z_ORM_AUTO_FK_LOOKUP){
00257 $this->getFk();
00258 }
00259 }
00265 function initLex(){
00266 zLogger::debug("Initializing SQL Lexims Map $this->tableAlias Table", "orm.$this->tableAlias");
00267 $lex = array();
00268 $lex['table'] = $this->realTableName;
00269 $lex['distinct'] = false;
00270 $lex['where'] = false;
00271 $lex['whereLex'] = false;
00272 $lex['order'] = array();
00273 $lex['limit'] = false;
00274 $lex['group'] = false;
00275 $lex['having'] = false;
00276 $src = $this->__struct->Attributes;
00277 foreach($src as $realColName => $colObj){
00278 $lex['aliasProperCaps'][$realColName] = $colObj->alias;
00279 $lex['reverseAlias'][$colObj->alias] = $realColName;
00280 $lex['alias'][strtolower($colObj->alias)] = $colObj->alias;
00281 $lex['select'][$realColName] = false;
00282 $lex['attributes'] = array();
00283 if($colObj->permission == 'writable'){
00284
00285 $lex['__attr'][$realColName] = true;
00286 $this->colQuote[$realColName] = true;
00287 }
00288 }
00289 $lex['join'] = array();
00290 $lex['join']['col'] = array();
00291 $lex['join']['on'] = array();
00292 $lex['join']['type'] = '';
00293
00294 $lex['whereLex'] = array();
00295 $this->rawExported = null;
00296 $this->lex = $lex;
00297 $this->trgUpdate = false;
00298 $this->trgUpdateCol = array();
00299 $this->trgInsert = false;
00300 $this->trgInsertCol = array();
00301 $this->trgExport = false;
00302 $this->trgExportCol = array();
00303 $this->trgDelete = false;
00304 }
00310 function attachDriver(){
00311 zLogger::debug("Using MySql driver for $this->tableAlias Table", "orm.$this->tableAlias");
00312 include_once(ZIGROOT.DRS.MOD_KEY_NAME_EXPLICIT.DRS."orm".DRS."driver".DRS."mysql.php");
00313 $GLOBALS['__zigmoyd__Orm_drv'] = new OrmDriver_mysql();
00314 }
00330 function queryType($qType){
00331 $this->queryType = $qType;
00332 }
00337 function exec($cleanUp=true){
00338 zLogger::debug("executing Query on $this->tableAlias Table", "orm.$this->tableAlias");
00339 $GLOBALS['__zigmoyd__Orm_drv']->connAlias($this->connAlias);
00340 if(!$GLOBALS['__zigmoyd__Orm_drv']->connect()){
00341 perror("<code>zigmoyd.orm.core.execute</code><br />Failed To Connect $this->connAlias");
00342 return false;
00343 }
00344 $GLOBALS['__zigmoyd__Orm_drv']->pasteLex($this->lex);
00345 $GLOBALS['__zigmoyd__Orm_drv']->setCaller(&$this);
00346 $GLOBALS['__zigmoyd__Orm_drv']->qType($this->queryType);
00347 if(!$GLOBALS['__zigmoyd__Orm_drv']->execute()){
00348 perror("<code>zigmoyd.orm.core.execute</code><br />Failed to execute Query");
00349 return false;
00350 }
00351 $this->rawSql = $GLOBALS['__zigmoyd__Orm_drv']->sql;
00352 if($cleanUp)$this->initLex();
00353 $this->rawExported = $GLOBALS['__zigmoyd__Orm_drv']->exportRaw();
00354 $this->numRows = $GLOBALS['__zigmoyd__Orm_drv']->numRows();
00355 $this->affectedRows = $GLOBALS['__zigmoyd__Orm_drv']->affectedRows();
00356 if(!$GLOBALS['__zigmoyd__Orm_drv']->disConnect()){
00357 perror("<code>zigmoyd.orm.core.execute</code><br />Failed TO Disconnect");
00358 return false;
00359 }
00360 return true;
00361 }
00367 function colExist($realColName){
00368 return isset($this->lex['select'][$realColName]);
00369 }
00375 function colAliasExists($colAliasName){
00376 return isset($this->lex['reverseAlias'][$colAliasName]);
00377 }
00384 function readCol($colRealName, $flag=true){
00385 if(!$this->colExist($colRealName)){
00386 perror("Invalid Column Real Name");
00387 return false;
00388 }
00389 $this->lex['select'][$colRealName] = $flag;
00390 }
00397 function readAlias($colAliasName, $flag=true){
00398 if(!$this->colAliasExists($colAliasName)){
00399 perror("Invalid Column Alias Name $colAliasName");
00400 }
00401 if(!$this->colExist($this->alias2Real($colAliasName))){
00402 perror("Invalid Column $colAliasName");
00403 }
00404 $this->lex['select'][$this->lex['reverseAlias'][$colAliasName]] = $flag;
00405 return true;
00406 }
00414 function writeCol($realColName, $val, $quote=ZIGORM_QUOTE){
00415 if($this->__struct->Attributes[$realColName]->permission == 'writable'){
00416 $this->lex['attributes'][$realColName] = $val;
00417 $this->colQuote[$realColName] = ($quote==ZIGORM_QUOTE);
00418 return true;
00419 }else{
00420 perror("<code>zigmoyd.orm.core.writeCol($realColName, $val)</code><br />Column $realColName is not writable");
00421 return false;
00422 }
00423 }
00431 function writeColAlias($colAliasName, $val, $quote=ZIGORM_QUOTE, $silent=false){
00432 if($this->__struct->Attributes[$this->lex['reverseAlias'][$colAliasName]]->permission == 'writable'){
00433 $this->lex['attributes'][$this->lex['reverseAlias'][$colAliasName]] = $val;
00434 $this->colQuote[$this->alias2Real($colAliasName)] = ($quote==ZIGORM_QUOTE);
00435 return true;
00436 }else{
00437 if(!$silent)perror("<code>zigmoyd.orm.core.writeAlias($colAliasName, $val)</code><br />Column $colAliasName is not writable");
00438 return false;
00439 }
00440 }
00445 function real2Alias($realColName){
00446 if($this->colExist($realColName)){
00447 return $this->lex['aliasProperCaps'][$realColName];
00448 }
00449 return false;
00450 }
00455 function alias2Real($colAliasName){
00456 if($this->colAliasExists($colAliasName)){
00457 return $this->lex['reverseAlias'][$colAliasName];
00458 }
00459 return false;
00460 }
00483 function addColCriteria($colRealName, $value, $op, $cnj, $quote){
00484 if(!$this->colExist($colRealName)){
00485 perror("Invalid Column Name");
00486 return false;
00487 }
00488 $whereObj = new stdClass();
00489 $whereObj->colName = $colRealName;
00490 $whereObj->op = $op;
00491 $whereObj->value = $value;
00492 $whereObj->cnj = $cnj;
00493 $whereObj->quote = $quote;
00494 $this->lex['whereLex'][] = $whereObj;
00495 return true;
00496 }
00519 function addAliasCriteria($colAliasName, $value, $op, $cnj, $quote){
00520 if(!$this->colAliasExists($colAliasName)){
00521 perror("Invalid Column Alias Name");
00522 return false;
00523 }
00524 $whereObj = new stdClass();
00525 $whereObj->colName = $this->alias2Real($colAliasName);
00526 $whereObj->op = $op;
00527 $whereObj->value = $value;
00528 $whereObj->cnj = $cnj;
00529 $whereObj->quote = $quote;
00530 $this->lex['whereLex'][] = $whereObj;
00531 return true;
00532 }
00536 function rawExport(){
00537 return $this->rawExported;
00538 }
00542 function numRows(){
00543 return $this->numRows;
00544 }
00548 function affectedRows(){
00549 return $this->affectedRows;
00550 }
00554 function getPk(){
00555 $GLOBALS['__zigmoyd__Orm_drv']->connAlias($this->connAlias);
00556 $GLOBALS['__zigmoyd__Orm_drv']->connect();
00557 $GLOBALS['__zigmoyd__Orm_drv']->pasteLex($this->lex);
00558 $GLOBALS['__zigmoyd__Orm_drv']->setCaller(&$this);
00559 $pk = $GLOBALS['__zigmoyd__Orm_drv']->getPrimaryKey();
00560 $GLOBALS['__zigmoyd__Orm_drv']->disConnect();
00561 if(!is_array($pk) || count($pk) == 0){
00562 $this->pk = false;
00563 return false;
00564 }
00565 foreach($pk as $pkObj){
00566 $this->pk[] = $pkObj;
00567 }
00568 return true;
00569 }
00573 function getFk(){
00574 global $__ZigOrm;
00575 $GLOBALS['__zigmoyd__Orm_drv']->connAlias($this->connAlias);
00576 $GLOBALS['__zigmoyd__Orm_drv']->connect();
00577 $GLOBALS['__zigmoyd__Orm_drv']->pasteLex($this->lex);
00578 $GLOBALS['__zigmoyd__Orm_drv']->setCaller(&$this);
00579 $fk = $GLOBALS['__zigmoyd__Orm_drv']->getForeignKey();
00580 $GLOBALS['__zigmoyd__Orm_drv']->disConnect();
00581 if(is_array($fk)){
00582 foreach($fk as $fkObj){
00583 if(!isset($__ZigOrm->dataDict->tables[$fkObj->ref_table])){
00584 perror('<code>zigmoyd.orm.foreignTable.attach</code><br />'."Foreign Table <code><span style=\"background-color: #FFFFFF;font-weight: normal;\"> $fkObj->ref_table</span></code> fetched for
00585 <br />local Table <code><span style=\"background-color: #FFFFFF;font-weight: normal;\"> $this->realTableName </span></code>
00586 <br />with a relation of Local Column <code><span style=\"background-color: #FFFFFF;font-weight: normal;\"> $fkObj->src_col </code></span>
00587 <br />with the Foreign Column <code><span style=\"background-color: #FFFFFF;font-weight: normal;\"> $fkObj->ref_col</span></code> For Association
00588 <br />But failed as the Foreign Table Orm Class has yet not been made.
00589 <br />You must load the Parent Orm Map before the Child Orm map");
00590 exit;
00591 }
00592 $this->fk[$fkObj->src_col] = new stdClass();
00593 $this->fk[$fkObj->src_col]->table = $fkObj->ref_table;
00594 $this->fk[$fkObj->src_col]->col = $fkObj->ref_col;
00595 $foreignOrmClassName = $__ZigOrm->dataDict->tables[$fkObj->ref_table];
00596 if(!class_exists($foreignOrmClassName)){
00597 perror('<code>zigmoyd.orm.foreignTable.attach</code><br />'."Foreign table's orm Class not exists");
00598 exit;
00599 }
00600 $this->{$__ZigOrm->dataDict->tables[$fkObj->ref_table]} =& new $foreignOrmClassName();
00601 $this->{$__ZigOrm->dataDict->tables[$fkObj->ref_table]}->isJoined = true;
00602 $this->{$__ZigOrm->dataDict->tables[$fkObj->ref_table]}->joiner = &$this;
00603 }
00604 $this->__rdbmsJoin = true;
00605 return true;
00606 }else{
00607 return false;
00608 }
00609 }
00610 }
00612 ?>