Skip to content
This repository was archived by the owner on Aug 13, 2023. It is now read-only.

Commit 7aa7d6c

Browse files
committed
+ update
+ insert + delete
1 parent 958b216 commit 7aa7d6c

12 files changed

Lines changed: 207 additions & 12 deletions

File tree

demo/delete.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
include_once dirname(__DIR__) . '/vendor/autoload.php';
4+
5+
$mySQL = new \Deimos\QueryBuilder\Adapter\MySQL();
6+
$qb = new Deimos\QueryBuilder\QueryBuilder($mySQL);
7+
8+
$query = $qb->delete()
9+
->from('from')
10+
->where('id', 5)
11+
->whereXor('id', 5)
12+
->where([
13+
'OR' => [
14+
['id', 9],
15+
['id', 6],
16+
]
17+
])
18+
->whereOr('id', 5)
19+
->where('id', 5);
20+
21+
var_dump((string)$query); // build Query
22+
23+
var_dump($query);
24+
var_dump($query->attributes());

demo/insert.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
include_once dirname(__DIR__) . '/vendor/autoload.php';
4+
5+
$mySQL = new \Deimos\QueryBuilder\Adapter\MySQL();
6+
$qb = new Deimos\QueryBuilder\QueryBuilder($mySQL);
7+
8+
$query = $qb->create()
9+
->from('from')
10+
->value('hello', 'world')
11+
->value(
12+
'id',
13+
$qb->query()->select(['id' => $qb->raw('max(id)')])->from('select')
14+
)->value(
15+
'index',
16+
$qb->raw(5)
17+
);
18+
19+
var_dump((string)$query); // build Query
20+
21+
var_dump($query);
22+
var_dump($query->attributes());

demo/update.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
include_once dirname(__DIR__) . '/vendor/autoload.php';
4+
5+
$mySQL = new \Deimos\QueryBuilder\Adapter\MySQL();
6+
$qb = new Deimos\QueryBuilder\QueryBuilder($mySQL);
7+
8+
$query = $qb->update()->from('from')
9+
->set('hello', 'world')
10+
->set('id', 5);
11+
12+
var_dump((string)$query); // build Query
13+
14+
var_dump($query);
15+
var_dump($query->attributes());

src/QueryBuilder/Instruction.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ abstract class Instruction
1515
*/
1616
protected $builder;
1717

18+
/**
19+
* @var bool
20+
*/
21+
protected $alias = true;
22+
1823
/**
1924
* Instruction constructor.
2025
*
@@ -105,7 +110,10 @@ protected function build($name, $storage)
105110
}
106111

107112
$result[] = $value .
108-
(!is_int($key) ? ' AS ' . $this->builder->adapter()->quote($key) : '');
113+
(!is_int($key) && $this->alias ?
114+
' AS ' . $this->builder->adapter()->quote($key)
115+
: ''
116+
);
109117
}
110118

111119
return implode(', ', $result);
@@ -134,7 +142,7 @@ public function __toString()
134142
}
135143
}
136144

137-
return implode("\n", $sql);
145+
return implode(' ', $sql);
138146
}
139147

140148
/**

src/QueryBuilder/Instruction/Delete.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class Delete extends Instruction
1010

1111
use Operator\From;
1212
use Operator\Where;
13-
use Operator\Where;
13+
use Operator\Limit;
14+
15+
protected $alias = false;
1416

1517
/**
1618
* @return array

src/QueryBuilder/Instruction/Insert.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ class Insert extends Instruction
99
{
1010

1111
use Operator\From;
12-
use Operator\Set;
12+
use Operator\Values;
13+
14+
protected $alias = false;
1315

1416
/**
1517
* @return array
1618
*/
1719
protected function operators()
1820
{
1921
return [
20-
'from' => 'INSERT',
21-
'set' => 'SET',
22-
'where' => 'WHERE',
23-
'orderBy' => 'ORDER BY',
24-
'limit' => 'LIMIT'
22+
'from' => 'INSERT INTO',
23+
'values' => '',
2524
];
2625
}
2726

src/QueryBuilder/Instruction/Update.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Update extends Instruction
1414
use Operator\OrderBy;
1515
use Operator\Limit;
1616

17+
protected $alias = false;
18+
1719
/**
1820
* @return array
1921
*/

src/QueryBuilder/Operator/Having.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Deimos\QueryBuilder\Operator;
44

5+
use Deimos\QueryBuilder\RawQuery;
6+
57
/**
68
* Class Having
79
*
@@ -74,4 +76,23 @@ protected function storageHaving()
7476
return $this->storageHaving;
7577
}
7678

79+
/**
80+
* @param $storage
81+
*
82+
* @return string
83+
*/
84+
protected function buildHaving($storage)
85+
{
86+
/**
87+
* @var array $where
88+
*/
89+
$having = $this->buildWhereOperator($storage);
90+
91+
$sql = '';
92+
$this->allowOperator = false;
93+
$this->buildIf2String($having, $sql);
94+
95+
return $sql;
96+
}
97+
7798
}

src/QueryBuilder/Operator/Select.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Deimos\QueryBuilder\Operator;
44

5+
use Deimos\QueryBuilder\Exceptions\NotFound;
6+
57
trait Select
68
{
79

@@ -25,6 +27,11 @@ public function select(...$fields)
2527
continue;
2628
}
2729

30+
if (is_object($field))
31+
{
32+
throw new NotFound('Alias for parameter `' . $field . '` not found');
33+
}
34+
2835
$key = key($field);
2936
$item = current($field);
3037

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Deimos\QueryBuilder\Operator;
4+
5+
use Deimos\QueryBuilder\Instruction\Select;
6+
use Deimos\QueryBuilder\RawQuery;
7+
8+
trait Values
9+
{
10+
11+
/**
12+
* @var array
13+
*/
14+
private $storageValues = [];
15+
16+
/**
17+
* @param string $field
18+
* @param mixed $value
19+
*
20+
* @return static
21+
*/
22+
public function value($field, $value)
23+
{
24+
$this->storageValues[$field] = $value;
25+
26+
return $this;
27+
}
28+
29+
/**
30+
* @param array $storage
31+
*
32+
* @return static
33+
*/
34+
public function values(array $storage)
35+
{
36+
$this->storageValues = $storage;
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* @return array
43+
*/
44+
protected function storageValues()
45+
{
46+
return $this->storageValues;
47+
}
48+
49+
/**
50+
* @param array $data
51+
*
52+
* @return string
53+
*/
54+
protected function buildValues(array $data)
55+
{
56+
$values = [];
57+
58+
foreach ($data as $field => $value)
59+
{
60+
$_value = $value;
61+
62+
$field = $this->builder->adapter()->quote($field);
63+
64+
$values[$field] = '?';
65+
66+
if ($value instanceof Select || $value instanceof RawQuery)
67+
{
68+
$values[$field] = '(' . $value . ')';
69+
$this->push($value->attributes());
70+
}
71+
else
72+
{
73+
$this->push([$_value]);
74+
}
75+
}
76+
77+
return '(' .
78+
implode(', ', array_keys($values)) .
79+
') VALUES (' .
80+
implode(', ', $values) .
81+
')';
82+
}
83+
84+
}

0 commit comments

Comments
 (0)