Last active
January 18, 2019 16:57
-
-
Save je8n/d67264be688da8b34af66edcf18d7860 to your computer and use it in GitHub Desktop.
DatabaseModel.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
. | |
$dbmodel=new DatabaseModel; | |
$this->db=$dbmodel->connect(); | |
insert | |
$result=$this->db->insert("etkinlik")->add("baslik","baslik")->add("icerik","icerik")->add("seo","seo-adresi")->exec(); | |
update | |
$result=$this->db->update("etkinlik")->set("baslik","baslik3")->set("icerik","icerik4")->where("seo='seo-adresi'")->exec(); | |
delete | |
$result=$this->db->delete("etkinlik")->where("seo='seo-adresi'")->exec(); | |
select | |
$query=$this->db->select("etkinlik")->choose("baslik,seo")->where("id > 5")->exec(); | |
. | |
*/ | |
class DatabaseModel{ | |
private $dbc,$dsn,$lastinsertid=null,$status=false,$prefix="",$lastprocess; | |
private $lastdata,$lasttables,$lastcontent; | |
public function connect(){ | |
global $user_settings; | |
try{ | |
$this->dbc= new PDO("mysql:host=".$user_settings["db"]["host"].";charset=utf8;dbname=".$user_settings["db"]["name"], $user_settings["db"]["username"],$user_settings["db"]["password"]); | |
$this->status=true; | |
$this->setprefix($user_settings["db"]["prefix"]); | |
} | |
catch(PDOException $e){ | |
$this->status=false; | |
DBErr::err(403,"","connect()",$e); | |
} | |
return $this->dbc; | |
} | |
public function setprefix($prefix){ | |
$this->prefix=$prefix; | |
return $this; | |
} | |
public function status(){ | |
return $this->status; | |
} | |
public function disconnect(){ | |
$this->dbc=null; | |
} | |
private function clear(){ | |
$arr["table"]=""; | |
$arr["query"]=""; | |
$arr["where"]=""; | |
$arr["choose"]=""; | |
$arr["bind"]=[]; | |
$this->lasttables=[]; | |
$this->lastcontent=[]; | |
$this->lastdata=$arr; | |
return true; | |
} | |
public function insert($tabloadi){ | |
if(!$this->clear())return DBErr::err(500,"clear() not completed","insert()"); //temizlemeyez ise çalıştırma | |
$this->lastprocess=1; | |
$this->lastdata["table"]=$this->prefix.$tabloadi; | |
$this->lastdata["start"]="INSERT INTO "; | |
return $this; | |
} | |
public function update($tabloadi){ | |
if(!$this->clear())return DBErr::err(500,"clear() not completed","update()"); //temizlemeyez ise çalıştırma | |
$this->lastprocess=2; | |
$this->lastdata["table"]=$this->prefix.$tabloadi; | |
$this->lastdata["start"]="UPDATE "; | |
return $this; | |
} | |
public function delete($tabloadi){ | |
if(!$this->clear())return DBErr::err(500,"clear() not completed","delete()"); //temizlemeyez ise çalıştırma | |
$this->lastprocess=3; | |
$this->lastdata["table"]=$this->prefix.$tabloadi; | |
$this->lastdata["start"]="DELETE FROM "; | |
return $this; | |
} | |
public function select($tabloadi){ | |
if(!$this->clear()) return DBErr::err(500,"clear() not completed","select()"); //temizlemeyez ise çalıştırma | |
$this->lastprocess=4; | |
$this->lastdata["table"]=$this->prefix.$tabloadi; | |
$this->lastdata["start"]="SELECT "; | |
return $this; | |
} | |
public function where($str){ | |
$this->lastdata["where"]=" WHERE ".$str; | |
return $this; | |
} | |
public function add($column,$value){ | |
if($this->lastprocess==1){ | |
$this->lasttables[]=$column; | |
$this->lastcontent[]=$value; | |
return $this; | |
} | |
} | |
public function set($column,$value){ | |
if($this->lastprocess==2){ | |
$this->lasttables[]=$column; | |
$this->lastcontent[]=$value; | |
return $this; | |
} | |
} | |
public function bind($value){ | |
if($this->lastprocess==4){ | |
$this->lastcontent[]=$value; | |
return $this; | |
} | |
} | |
public function choose($str){ | |
$this->lastdata["choose"]=$str; | |
return $this; | |
} | |
public function exec($default=""){ | |
switch ($this->lastprocess) { | |
case 1: // EXAMPLE : insert into table set column=?,column2=? | |
$tables=implode("=?,",$this->lasttables)."=?"; | |
$query=$this->lastdata["start"].$this->lastdata["table"]." SET ".$tables." ".$this->lastdata["where"]; | |
$query=str_replace("{prefix}", $this->prefix, $query); | |
$command=$this->dbc->prepare($query); | |
$result=$command->execute($this->lastcontent); | |
$this->lastindertid=$this->dbc->lastInsertId(); | |
return $result; | |
break; | |
case 2: // EXAMPLE : update table set column=?,column2=? | |
$tables=implode("=?,",$this->lasttables)."=?"; | |
$query=$this->lastdata["start"].$this->lastdata["table"]." SET ".$tables." ".$this->lastdata["where"]; | |
$query=str_replace("{prefix}", $this->prefix, $query); | |
$command=$this->dbc->prepare($query); | |
$result=$command->execute($this->lastcontent); | |
return $result; | |
break; | |
case 3: // EXAMPLE : delete from table set column=?,column2=? | |
$query=$this->lastdata["start"].$this->lastdata["table"]." ".$this->lastdata["where"]; | |
$query=str_replace("{prefix}", $this->prefix, $query); | |
$command=$this->dbc->prepare($query); | |
$result=$command->execute($this->lastcontent); | |
return $result; | |
break; | |
case 4: // EXAMPLE : select choose from table WHERE | |
$query=$this->lastdata["start"]." ".$this->lastdata["choose"]." FROM ".$this->lastdata["table"]." ".$this->lastdata["where"]; | |
$query=str_replace("{prefix}", $this->prefix, $query); | |
$command=$this->dbc->prepare($query); | |
$result=$command->execute($this->lastcontent); | |
return $command->fetchAll(); | |
break; | |
default: | |
DBErr::err(500,$query,"prepare()"); | |
break; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment