2 class DatabaseConnector
{
3 private $database_name="";
4 private $database_type="mysql";
5 private $host="localhost";
9 private $lastQueryName="default";
10 private $results=array();
11 private $queries=array();
12 public function __construct(){
13 $this->host
=CiteConfig
::getValue("database_host");
14 $this->user
=CiteConfig
::getValue("database_user");
15 $this->password
=CiteConfig
::getValue("database_password");
16 $this->database_name
=CiteConfig
::getValue("database_name");
18 final private static function getInstance()
20 static $instance = null;
21 if (null === $instance) {
22 $instance = new self();
26 private function setter($variable,$value){
27 $inst=self
::getInstance();
28 $inst->$variable=$value;
31 public function database($db_name){return self
::setter("database_name",$db_name);}
32 public function user($user){return self
::setter("user",$user);}
33 public function password($password){return self
::setter("password",$password);}
34 public function connect(){
35 $inst=self
::getInstance();
36 $query="{$inst->database_type}:host={$inst->host};dbname={$inst->database_name}";
38 $inst->db
=new PDO($query,$inst->user
,$inst->password
,array(PDO
::MYSQL_ATTR_INIT_COMMAND
=> "SET NAMES utf8"));
40 catch(PDOException
$e){CiteOutput
::factory("Nastala chyba při připojování k databázi: ".$e->getMessage())->linebreak()->italics($query)->out();}
43 public function query($query_name="default"){
44 $inst=self
::getInstance();
45 $inst->queries
[$query_name]=new Query($inst);
46 $inst->lastQueryName
=$query_name;
47 return $inst->queries
[$query_name];
50 public function rawQuery($query){
51 $inst=self
::getInstance();
52 return $inst->db
->query($query);
54 public function prepare($query){
55 $inst=self
::getInstance();
56 return $inst->db
->prepare($query);
58 public function errorInfo(){
59 $inst=self
::getInstance();
60 return $inst->db
->errorInfo();
62 public function run($query=null){
63 $inst=self
::getInstance();
64 $this->db
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_WARNING
);
65 $query=$query?
$query:$inst->lastQueryName
;
66 $this->results
[$query]= $this->db
->query($this->queries
[$query]->buildQuery());
67 echo $this->queries
[$query]->buildQuery()."<br />\n";
68 //$res=$inst->db->query($this->queries[$query]->buildQuery());
69 foreach($this->results
[$query] as $row){
77 private $parentClass=null;
79 private $tables=array();
81 private $alias=array();
82 private $primary=array();
83 private $foreign=array();
84 private $links=array();
85 private $aliasCount=1;
86 private $where=array();
87 public function __construct(DatabaseConnector
$parent=null){
88 $this->parentClass
=$parent;
90 public function end(){
91 return $this->parentClass
;
93 public function table($table_name){
94 $this->tables
[$table_name]=array();
95 $this->lastTable
=$table_name;
98 public function alias($alias){
99 $this->alias
[$this->lastTable
]=$alias;
103 public function column($column_name){
104 $this->tables
[$this->lastTable
][]=$column_name;
107 public function primary($column_name){
108 $this->primary
[$this->lastTable
]=$column_name;
111 public function foreign($column_name,$table=""){
112 $this->foreign
[$this->lastTable
][$table]=$column_name;
115 public function link($primary,$foreign){
116 if(array_key_exists($primary,$this->primary
) AND array_key_exists($foreign,$this->foreign
) AND array_key_exists($primary,$this->foreign
[$foreign])){
117 $this->links
[]=$this->getAlias($primary).".".$this->primary
[$primary]."=".$this->getAlias($foreign).".".$this->foreign
[$foreign][$primary];
121 public function where($where){
122 $this->where
[]=$where;
125 private function getAlias($table){
126 if(array_key_exists($table,$this->alias
))return $this->alias
[$table];
127 else $this->alias
[$table]="t{$this->aliasCount}";
129 return $this->getAlias($table);
131 private function buildSelect($select_part){
132 return is_array($select_part)?
"SELECT ".implode(", ",$select_part):"";
134 private function buildFrom($table_part){
135 return is_array($table_part)?
" FROM ".implode(", ",$table_part):"";
137 private function buildWhere($where_part){
138 return is_array($where_part)?
" WHERE ".implode(" AND ",$where_part):"";
141 public function buildQuery(){
142 $select_part=array();
144 foreach($this->tables
as $table=>$columns){
145 $alias=$this->getAlias($table);
146 foreach($columns as $col){
147 $select_part[]="$alias.$col";
149 $table_part[]="$table AS $alias";
151 $this->query
=$this->buildSelect($select_part).$this->buildFrom($table_part).$this->buildWhere($this->links
);