PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/nacridan/backup/script/DBObject.inc.php

https://gitlab.com/nacridan/Nacridan
PHP | 221 lines | 192 code | 16 blank | 13 comment | 57 complexity | 87f84b3e67e5e034acc35dc27d1480ae MD5 | raw file
  1. require_once(HOMEPATH."/lib/utils.inc.php"); DEFINE
  2. ("FOREIGN_KEY_SYNTAX","/id_([^_\$]*)[_]?([^\$]*)(.*)/"); DEFINE
  3. ("TABLE_SYNTAX","([^_]*)(.*)"); // VIEW DEFINE("SIZEAREA",5); /* MAP */
  4. DEFINE ("MAPHEIGHT",400); DEFINE ("MAPWIDTH",640); /* DICE TYPE */
  5. DEFINE ("DICE_D3",0); DEFINE ("DICE_D6",1); DEFINE ("DICE_D8",2); DEFINE
  6. ("DICE_D10",3); DEFINE ("DICE_D12",4); DEFINE ("DICE_D20",5); DEFINE
  7. ("DICE_D100",6); DEFINE ("DICE_ADD",7); DEFINE ("DICE_MULT",8); DEFINE
  8. ("DICE_CHARAC",9); DEFINE ("DICE_NB",7); DEFINE("WIN_MONEY","1");
  9. DEFINE("WIN_XP","2"); DEFINE("WIN_EQUIP","3"); DEFINE("ADD_PLAYER","4");
  10. DEFINE("DEL_PLAYER","5"); DEFINE("WIN_PLAYER","6");
  11. DEFINE("FREE_PLAYER","7"); class DBObject{ public $m_attr; public
  12. $isnull; public $m_object; public $m_linkdbobject; public $m_realDBName;
  13. public $m_realAPIName; public $m_tableName; public $m_className; public
  14. $m_transaction; public $m_errorno; public $m_errormsg; function
  15. DBObject() { $this->m_attr=array(); $this->isnull=array();
  16. $this->m_realDBName=array(); $this->m_object=array();
  17. $this->m_transaction=array(); $this->m_errorno=0;
  18. $this->m_linkdbobject=array(); } function init(){ foreach($this->m_attr
  19. as $key => $val) { if(preg_match("/id_(.*)/",$key,$class)) {
  20. if($class[1]!="") { $this->m_object[$class[1]]="";
  21. $this->m_linkdbobject[$class[1]]=false; } } } } function
  22. getASRenamer($from,$to){ $str=""; foreach($this->m_attr as $key => $val)
  23. { if(isset($this->m_realDBName[$key])) {
  24. $realname=$this->m_realDBName[$key]; } else { $realname=$key; }
  25. $str.=",".$from.".".$realname." AS ".$to.$realname; } return
  26. substr($str, 1); } function initSheet($id,$tablename,$db) {
  27. $attachName=$this->getAttach(); $this->setAttach($tablename);
  28. $this->load($id,$db); $this->set("id","");
  29. $this->setAttach($attachName); } function errorNoDB(){ return
  30. $this->m_errorno; } function errorMsgDB(){ return $this->m_errormsg; }
  31. protected function attachObj($nameparam) { $name="id_".$nameparam;
  32. if(isset($this->m_realDBName[$name])) {
  33. if(preg_match(FOREIGN_KEY_SYNTAX,$this->m_realDBName[$name],$tab)) {
  34. $this->m_object[$tab[1]]->setAttach($tab[1]."_".$tab[2]); } else {
  35. trigger_error ("Bad FOREIGN_KEY_SYNTAX attachObj()
  36. (".$this->m_className.")", E_USER_ERROR); } } else {
  37. $this->m_object[$nameparam]->setAttach($nameparam); } } function
  38. setAttach($attachName) { $this->m_tableName=$attachName; } function
  39. getAttach() { if(isset($this->m_tableName))
  40. $tableName=$this->m_tableName; else { if(isset($this->extraName)) {
  41. return -1; } $tableName=$this->m_className; } return $tableName; }
  42. private function errorAttachment() { trigger_error ("The
  43. ".$this->m_className." object must have a valid table attachment i.e.
  44. \"Modifier_?\" use setAttach() to define a valid \"?\" target",
  45. E_USER_ERROR); } private function getAttachStopIfError() {
  46. $tableName=$this->getAttach(); if($tableName==-1) {
  47. $this->errorAttachment(); } return $tableName; } function get($varname)
  48. { if($this->isExist($varname)) return $this->m_attr[$varname]; else
  49. return null; } function set($varname,$value) {
  50. if($this->isExist($varname)) { $this->m_attr[$varname]=$value;
  51. unset($this->isnull[$varname]); } else return -1; } function
  52. setNull($varname) { if($this->isExist($varname)) {
  53. $this->isnull[$varname]=1; } else return -1; } function
  54. getSub($name,$varname) { if(isset($this->m_object[$name])) { return
  55. $this->m_object[$name]->get($varname); } } function
  56. setSub($name,$varname,$value) { if(isset($this->m_object[$name])) {
  57. return $this->m_object[$name]->set($varname,$value); } else { return -1;
  58. } } function isExist($varname){ return isset($this->m_attr[$varname]); }
  59. function isExistObj($varname){ return
  60. isset($this->m_attr["id_".$varname]); } function
  61. isInstantiateObj($varname){ if(!isset($this->m_attr[$varname])) return
  62. 0; return is_object($this->m_object[$varname]); } private function
  63. newSingleObj($name,$linkDB=false) { //echo $this->m_className." :
  64. ".$name." \n"; if($this->isExistObj($name)) {
  65. if(!$this->isInstantiateObj($name)) { $result=explode("\$",$name);
  66. $class=$result[0]; $this->m_object[$name]=new $class();
  67. $this->attachObj($name); } if($linkDB)
  68. $this->m_linkdbobject[$name]=true; return $this->m_object[$name]; } else
  69. { return null; } } function externDBObj($names){
  70. $this->externObj($names,true); } function resetExternDBObj() { foreach
  71. ($this->m_object as $key => $fkobj) { if($this->isInstantiateObj($key))
  72. { $this->m_object[$key]->resetExternDBObj(); }
  73. $this->m_linkdbobject[$key]=false; } } function unlinkExternDBObj($name)
  74. { if(is_object($this->m_object[$name])) {
  75. $this->m_linkdbobject[$name]=false; } } function
  76. externObj($names,$linkDB=false){ foreach (explode(",",$names) as
  77. $fullname) { $obj=$this; foreach (explode("::",$fullname) as $name) {
  78. if(!$obj->isInstantiateObj($name)) {
  79. $obj=$obj->newSingleObj($name,$linkDB); } else {
  80. $obj->m_linkdbobject[$name]=$linkDB; $obj=$obj->getObj($name); } } }
  81. return 0; } function loadExternDBObj($db){ foreach ($this->m_object as
  82. $key => $fkobj) { if($this->m_linkdbobject[$key]==true) {
  83. $fkkey="id_".$key; $fkid=$this->get($fkkey);
  84. $this->m_object[$key]->load($fkid,$db); } } } function
  85. setObj($name,$obj,$linkDB=false){ if($this->isExistObj($name)) {
  86. $result=explode("\$",$name); $class=$result[0];
  87. $this->m_object[$name]=$obj; $this->m_linkdbobject[$name]=$linkDB;
  88. $this->attachObj($name); return 0; } else { return -1; } } function
  89. &getObj($name) { return $this->m_object[$name]; } function &getAllObj()
  90. { return $this->m_object; } function &getAllAttr() { return
  91. $this->m_attr; } function &getRealAttr() { $arr=array();
  92. foreach($this->m_attr as $key => $val) {
  93. if(isset($this->m_realDBName[$key])) { $key=$this->m_realDBName[$key]; }
  94. $arr[$key]=$val; } return $arr; } private function rollback($db) {
  95. foreach ($this->m_transaction as $key => $id) { $query=sprintf("DELETE
  96. FROM %s WHERE id=%s;",$key,$id); $db->Execute($query); } } function
  97. addDBr($db) { $this->m_transaction=array(); unset($this->m_attr["id"]);
  98. foreach ($this->m_object as $key => $fkobj) {
  99. if($this->m_linkdbobject[$key]) {
  100. $lastid=$this->m_object[$key]->addDBr($db); //printf("Key : %s id:%d
  101. <br />
  102. ",$key,$lastid); if($lastid!=-1) {
  103. $this->m_transaction[$this->m_object[$key]->getAttach()]=$lastid; } else
  104. { $this->m_errorno=$this->m_object[$key]->errorNoDB();
  105. $this->m_errormsg=$this->m_object[$key]->errorMsgDB();
  106. $this->rollback($db); if($lastid==-2) { $this->errorAttachment(); }
  107. return -1; } $this->m_attr["id_".$key]=$lastid; } }
  108. $lastid=$this->addDB($db); if($lastid==-1) { $this->rollback($db); }
  109. $this->m_transaction=array(); return $lastid; } function addDB($db) {
  110. if(isset($db)) { $tableName=$this->getAttach(); if($tableName==-1)
  111. return -2; $fields="("; $values="("; foreach ($this->m_attr as $key =>
  112. $value) { if(isset($this->m_realDBName[$key])) {
  113. $key=$this->m_realDBName[$key]; } if($value!="") { if($fields!="(") {
  114. $fields.=","; $values.=","; } $fields.=$key;
  115. $values.="\"".addslashes($value)."\""; } } $fields.=")"; $values.=")";
  116. $query=sprintf("INSERT INTO %s %s VALUES
  117. %s;",$tableName,$fields,$values); if($this->execQuery($query,$db)!=0)
  118. return -1; $records=$db->Execute("SELECT @@IDENTITY AS NewID");
  119. $this->m_attr["id"]=$records->Fields("NewID"); $records->Close(); return
  120. $this->m_attr["id"]; } else { $name=$this->m_className; trigger_error
  121. ("l'object DB de la classe $name = NULL", E_USER_ERROR); return -1; } }
  122. private function execQuery($query,$db,&$record=null) { //echo $query;
  123. try { $record=$db->Execute($query); } catch (exception $e) { }
  124. $this->m_errorno=$db->ErrorNo(); //dbLogMessage($query);
  125. if($db->ErrorNo()) { $this->m_errormsg=$db->ErrorMsg();
  126. dbLogMessage($query); dbLogMessage($this->m_errormsg); return -1; }
  127. return 0; } function updateDBr($db) { if($this->m_attr["id"]=="") {
  128. return $this->addDBr($db); } else { foreach ($this->m_object as $key =>
  129. $fkobj) { if($this->m_linkdbobject[$key]) {
  130. $lastid=$this->m_object[$key]->updateDBr($db);
  131. $this->m_attr["id_".$key]=$lastid; } } return $this->updateDB($db); } }
  132. function updateDB($db) { if($this->m_attr["id"]=="") { return
  133. $this->addDB($db); } else { if(isset($db)) {
  134. $tableName=$this->getAttachStopIfError(); $query=""; $where="WHERE ";
  135. foreach ($this->m_attr as $key => $value) {
  136. if(isset($this->m_realDBName[$key])) { $key=$this->m_realDBName[$key]; }
  137. if($key!=="id") { if($query!="") { $query.=","; } $query.=$key;
  138. if(isset($this->isnull[$key])) { $query.="=NULL"; } else {
  139. $query.="=\"".addslashes($value)."\""; } } else { $where.=$key;
  140. $where.="=\"".addslashes($value)."\""; } } $query=sprintf("UPDATE %s SET
  141. %s %s;",$tableName,$query,$where); if($this->execQuery($query,$db)!=0)
  142. return -1; return $this->m_attr["id"]; } else { trigger_error ("l'object
  143. DB de la classe $this->m_className = NULL", E_USER_ERROR); return -1; }
  144. } } function deleteDBr($db) { foreach ($this->m_object as $key =>
  145. $fkobj) { if($this->m_linkdbobject[$key]) { $fkkey="id_".$key;
  146. $fkid=$this->get($fkkey); $this->m_object[$key]->set("id",$fkid);
  147. $this->m_object[$key]->deleteDBr($db); } } $this->deleteDB($db); }
  148. function deleteDB($db) { if(isset($db)) { $this->m_errorno=0;
  149. $tableName=$this->getAttachStopIfError(); if($tableName==-1)
  150. trigger_error ("The ".$this->m_className." object must have a valid
  151. table attachment i.e. \"Modifier_?\" use setAttach() to define a valid
  152. \"?\" target", E_USER_ERROR); $where="WHERE ";
  153. $value=$this->m_attr["id"]; $where.="id"; $where.="=\"".$value."\"";
  154. $query=sprintf("DELETE FROM %s %s;",$tableName,$where);
  155. if($this->execQuery($query,$db)!=0) return -1; return 0; } else {
  156. trigger_error ("l'object DB de la classe $this->m_className = NULL",
  157. E_USER_ERROR); return -1; } } function load($id,$db) {
  158. $tableName=$this->getAttach(); if($tableName==-1) trigger_error ("The
  159. ".$this->m_className." object must have a valid table attachment i.e.
  160. \"Modifier_?\" use setAttach() to define a valid \"?\" target",
  161. E_USER_ERROR); if($id!=0) { if(isset($db)){ $query=sprintf("SELECT *
  162. FROM %s WHERE id=%s",$tableName,$id);
  163. $this->execQuery($query,$db,$records); if($records->RecordCount()==0)
  164. return -1; //$records=$db->Execute($query); $this->DBLoad($records); }
  165. else { $name=$this->m_className; trigger_error ("l'object DB de la
  166. classe $name = NULL", E_USER_ERROR); } foreach($this->m_object as $key
  167. => $fkobj) { if($this->m_linkdbobject[$key]==true) { $fkkey="id_".$key;
  168. /* if(isset($this->m_realDBName[$fkkey])) {
  169. $fkkey=$this->m_realDBName[$fkkey]; } */ $fkid=$this->get($fkkey);
  170. if(isset($fkid)) { $this->m_object[$key]->load($fkid,$db); } } } } else
  171. { return -1; //trigger_error ("id:\"".$id."\" ou nom de la
  172. table:\"".$tableName."\" non valide", E_USER_ERROR); } } function
  173. reload($db) { if(isset($this->m_attr["id"]) && $this->m_attr["id"]!=0) {
  174. $this->load($this->m_attr["id"],$db); } } function getClassName() {
  175. return $this->m_className; } function getPrefix() { return
  176. $this->m_objprefix; } function DBLoad(&$records,$extra="") { /*
  177. $nbField=$records->FieldCount(); print_r($records);
  178. for($i=0;$i<$nbField;$i++) { echo $key."\n";
  179. $field=&$records->FetchField($i); $key=$field->name;
  180. if(isset($this->m_realAPIName[$key])) { $key=$this->m_realAPIName[$key];
  181. } if(isset($this->m_attr[$key]))
  182. $this->m_attr[$key]=$records->fields[$field->name]; } */ foreach
  183. ($this->m_attr as $key => $val) { if(isset($this->m_realDBName[$key])) {
  184. $realname=$this->m_realDBName[$key]; } else { $realname=$key; }
  185. if(isset($records->fields[$extra.$realname])) {
  186. $this->m_attr[$key]=$records->fields[$extra.$realname]; } else {
  187. $this->isnull[$key]=1; } } } function show() { foreach ($this->m_attr as
  188. $key => $val) { echo $key.":".$this->m_attr[$key]."\n"; }
  189. foreach($this->m_object as $key => $fkobj) { echo "****** ".$key."\n";
  190. if($this->isInstantiateObj($key)) $fkobj->show(); } } /* function
  191. initVars($filename) { if (file_exists($filename)) { include($filename);
  192. } foreach($this->m_attr as $key => $data) {
  193. if(!preg_match(FOREIGN_KEY_SYNTAX,$key,$tab)) { if(isset($cond[$key])) {
  194. $this->m_varConds[$key]=$cond[$key]; } else {
  195. $this->m_varConds[$key]["type"]="text"; }
  196. $this->m_varConds[$key]["name"]=$key; if($this->m_attr[$key]!="") {
  197. $this->m_varConds[$key]["value"]=$this->m_attr[$key]; } } } } */ }