Skip to content

Commit 2dbc038

Browse files
committed
changes
1 parent b275062 commit 2dbc038

File tree

4 files changed

+117
-33
lines changed

4 files changed

+117
-33
lines changed

Join.php

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
11
<?php
2+
namespace QueryBuilder;
23

3-
/*
4-
* To change this license header, choose License Headers in Project Properties.
5-
* To change this template file, choose Tools | Templates
6-
* and open the template in the editor.
7-
*/
8-
4+
class Join
5+
{
6+
public $join_type, $primary_field, $table, $alias, $foreign_field, $fields = array();
7+
8+
public function __construct($join_type, $primary_field, $table, $alias, $foreign_field)
9+
{
10+
$this->join_type = trim($join_type);
11+
$this->primary_field = trim($primary_field);
12+
$this->table = trim($table);
13+
$this->alias = trim($alias);
14+
$this->foreign_field = trim($foreign_field);
15+
}
16+
17+
public static function init($join_type, $primary_field, $table, $alias, $foreign_field)
18+
{
19+
return new Join($join_type, $primary_field, $table, $alias, $foreign_field);
20+
}
21+
22+
public function field($field, $alias = "")
23+
{
24+
if (!$alias)
25+
{
26+
$alias = $field;
27+
}
28+
29+
$this->fields[$alias] = $field;
30+
return $this;
31+
}
32+
33+
public function getFields()
34+
{
35+
$fields = array();
36+
37+
$table_alias = $this->alias ? $this->alias : $this->table;
38+
foreach($this->fields as $ailas => $field)
39+
{
40+
$fields[] = $table_alias . "." . $field . " AS " . $table_alias . "__" . $ailas;
41+
}
42+
43+
if (!$fields)
44+
{
45+
$fields[] = "$table_alias.*";
46+
}
47+
48+
return $fields;
49+
}
50+
}

Query.php

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class QuerySelect
55
{
6-
private $table, $alias, $fields = array(), $orders = array(), $where = null;
6+
private $table, $alias, $fields = array(), $orders = array(), $where = null, $joins = array();
77

88
public function __construct($table, $alias = NULL)
99
{
@@ -41,25 +41,32 @@ public function get()
4141
$table_alias = $this->alias ? $this->alias : $this->table;
4242
foreach($this->fields as $ailas => $field)
4343
{
44-
$fields[] = $table_alias . "." . $field . " AS " . $table_alias . "." . $ailas;
44+
$fields[] = $table_alias . "." . $field . " AS " . $table_alias . "__" . $ailas;
4545
}
4646

47-
if ($fields)
47+
if (!$fields)
4848
{
49-
$fields = implode(", ", $fields);
50-
}
51-
else
52-
{
53-
$fields = "*";
49+
$fields[] = "$table_alias.*";
50+
5451
}
5552

56-
if ($this->alias)
53+
foreach($this->joins as $join)
5754
{
58-
$q = "SELECT $fields FROM " . $this->table . " AS " . $table_alias;
55+
$fields = array_merge($fields, $join["join"]->getFields());
5956
}
60-
else
57+
58+
$fields = implode(", ", $fields);
59+
60+
$q = "SELECT $fields FROM " . $this->table . " AS " . $table_alias;
61+
62+
foreach($this->joins as $join)
6163
{
62-
$q = "SELECT $fields FROM " . $this->table;
64+
$str = $this->getJoin($join["join"], $join["where"]);
65+
66+
if ($str)
67+
{
68+
$q .= " " . $str;
69+
}
6370
}
6471

6572
$wh = "";
@@ -82,4 +89,31 @@ public function get()
8289

8390
return $q . $wh . $order . ";";
8491
}
92+
93+
public function join(Join $join, Where $wh = null)
94+
{
95+
$this->joins[] = array(
96+
"join" => $join,
97+
"where" => $wh
98+
);
99+
100+
return $this;
101+
}
102+
103+
public function getJoin(Join $join, Where $wh = null)
104+
{
105+
if (is_null($wh))
106+
{
107+
$wh = new Where("AND");
108+
}
109+
110+
$table_alias = $this->alias ? $this->alias : $this->table;
111+
$other_table_alias = $join->alias ? $join->alias : $join->table;
112+
113+
$wh->add($table_alias . "." . $join->primary_field, $other_table_alias . "." . $join->foreign_field , "=", "");
114+
115+
$q = $join->join_type . " " . $join->table . " AS " . $other_table_alias . " ON " . $wh->get();
116+
117+
return $q;
118+
}
85119
}

Where.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function add($field, $value, $operator = "=", $value_type = "string")
3535
break;
3636

3737
case "date":
38+
$value = date("Y-m-d", strtotime($value));
39+
$value = "'" . $value . "'";
40+
break;
41+
3842
case "datetime":
3943
$value = date("Y-m-d H:i:s", strtotime($value));
4044
$value = "'" . $value . "'";
@@ -54,6 +58,7 @@ public function add($field, $value, $operator = "=", $value_type = "string")
5458

5559
case "array":
5660
$value = "(" . implode(",", $value) . ")";
61+
$operator = "IN";
5762
break;
5863

5964
case "NULL":
@@ -96,19 +101,22 @@ public function get($table = "")
96101

97102
foreach($this->fields as $field)
98103
{
99-
$key = $field["field"];
100-
101-
if ($table)
102-
{
103-
$key = $table . "." . $key;
104-
}
105-
106-
if ($field["op"])
104+
if (strlen($field["value"]) > 0)
107105
{
108-
$key = $key . " " . $field["op"];
106+
$key = $field["field"];
107+
108+
if ($table)
109+
{
110+
$key = $table . "." . $key;
111+
}
112+
113+
if ($field["op"])
114+
{
115+
$key = $key . " " . $field["op"];
116+
}
117+
118+
$list[$key] = $field["value"];
109119
}
110-
111-
$list[$key] = $field["value"];
112120
}
113121

114122
$list = $this->_listToStr($list);

test.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
require_once './Query.php';
33
require_once './Where.php';
4+
require_once './Join.php';
45

56
$qb = new \QueryBuilder\QuerySelect("orders");
67

7-
$wh = \QueryBuilder\Where::init("AND")
8-
->add("product_id", "1")
9-
->addWhere(\QueryBuilder\Where::init("OR")->add("is_deliverd", "1")->add("is_ship", "1"));
8+
$qb->field("id");
9+
$qb->join(\QueryBuilder\Join::init("INNER JOIN", "id", "order_details", "OD", "order_id")->field("product_id")->field);
1010

11-
echo $qb->setWhere($wh)->order("id")->get();
11+
echo $qb->get();

0 commit comments

Comments
 (0)