Skip to content

Commit 52630a9

Browse files
committed
was created a trait with sql expressions and insert class
1 parent ac01291 commit 52630a9

File tree

5 files changed

+350
-129
lines changed

5 files changed

+350
-129
lines changed

src/sql/Insert.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Manipulator\SQL;
4+
5+
use \Manipulator\SQL\Traits\Expressions;
6+
7+
class Insert extends Query
8+
{
9+
use Expressions;
10+
11+
/**
12+
* Contains the table name
13+
* which we set into our model
14+
* class
15+
*
16+
* @var string
17+
*/
18+
private $table;
19+
20+
/**
21+
* Contains all SQL statements
22+
* like array elements
23+
*
24+
* @var array
25+
*/
26+
protected $sql = [];
27+
28+
/**
29+
* Columns names which we was
30+
* passed into our "into" method
31+
*
32+
* @var string
33+
*/
34+
private $columns;
35+
36+
/**
37+
* Values which will be
38+
* bind with question marks
39+
* in prepared SQL statement
40+
*
41+
* @var array
42+
*/
43+
protected $values;
44+
45+
/**
46+
* @param string $table
47+
*/
48+
public function __construct(string $table)
49+
{
50+
$this->table = $table;
51+
52+
parent::__construct();
53+
}
54+
55+
/**
56+
* Prepare columns for question marks
57+
* generator
58+
*
59+
* @param string $columns
60+
*
61+
* @return \Manipulator\SQL\Insert
62+
*/
63+
public function into(string $columns)
64+
{
65+
$this->columns = $columns;
66+
67+
$this->sql[] = "INSERT INTO {$this->table} ({$this->columns})";
68+
return $this;
69+
}
70+
71+
/**
72+
* Prepare passed values for
73+
* binding with question marks
74+
*
75+
* @param array $values
76+
*
77+
* @return \Manipulator\SQL\Insert
78+
* */
79+
public function values(array $values)
80+
{
81+
$this->values = $values;
82+
83+
$columns = $this->convertColumnsToArray($this->columns);
84+
$this->sql[] = " VALUES ({$this->questionMarksGenerator($columns)})";
85+
return $this;
86+
}
87+
88+
}

src/sql/Query.php

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ class Query extends Connector
1717
*/
1818
protected $statement;
1919

20+
/**
21+
* Array with questions marks for
22+
* prepared statement SQL
23+
*
24+
* @var array
25+
*/
26+
protected $questionMarks = [];
27+
2028
public function __construct()
2129
{
2230
parent::__construct();
@@ -40,14 +48,34 @@ public function get()
4048
)->fetchAll($this->statement);
4149
}
4250

51+
/**
52+
* Prepared SQL statement
53+
*
54+
* Prepare SQL statement and bind
55+
* values with question marks.
56+
* Can be used with Insert and
57+
* Delete classes.
58+
*
59+
* @return bool
60+
*/
61+
public function push()
62+
{
63+
$processing = $this->prepare(
64+
$this->convertSQLToString($this->sql)
65+
)->bind($this->statement, $this->values);
66+
67+
return $processing;
68+
69+
}
70+
4371
/**
4472
* This is abstaraction of fetchAll PDO method
4573
*
4674
* @param \PDOStatement $statement
4775
*
4876
* @return array
4977
*/
50-
public function fetchAll(\PDOStatement $statement)
78+
protected function fetchAll(\PDOStatement $statement)
5179
{
5280
return $statement->fetchAll();
5381
}
@@ -59,7 +87,7 @@ public function fetchAll(\PDOStatement $statement)
5987
*
6088
* @return string
6189
* */
62-
public function fetchColumn(\PDOStatement $statement)
90+
protected function fetchColumn(\PDOStatement $statement)
6391
{
6492
return $statement->fetchColumn();
6593
}
@@ -71,12 +99,41 @@ public function fetchColumn(\PDOStatement $statement)
7199
*
72100
* @return \Manipulator\SQL\Select
73101
*/
74-
public function query(string $query)
102+
protected function query(string $query)
75103
{
76104
$this->statement = $this->connection()->query($query);
77105
return $this;
78106
}
79107

108+
/**
109+
* Prepare SQL statement
110+
*
111+
* Assign statement property \PDOStatement object
112+
* and return reference on this class
113+
*
114+
* @param string $query
115+
*
116+
* @return \Manipulator\SQL\Query
117+
*/
118+
protected function prepare(string $query)
119+
{
120+
$this->statement = $this->connection->prepare($query);
121+
return $this;
122+
}
123+
124+
/**
125+
* Bind values with question marks into SQL statement
126+
*
127+
* @param \PDOStatement $statement
128+
* @param array $values
129+
*
130+
* @return bool
131+
*/
132+
protected function bind(\PDOStatement $statement, array $values)
133+
{
134+
return $statement->execute($values);
135+
}
136+
80137
/**
81138
* Convert passed SQL statement like array
82139
* in to string
@@ -85,9 +142,39 @@ public function query(string $query)
85142
*
86143
* @return string
87144
*/
88-
public function convertSQLToString(array $sql)
145+
protected function convertSQLToString(array $sql)
89146
{
90147
return $this->parser->convertToString($sql, ' ');
91148
}
92149

150+
/**
151+
* Convert columns from sql to
152+
* array
153+
*
154+
* @param string $columns
155+
*
156+
* @return array
157+
*/
158+
protected function convertColumnsToArray(string $values)
159+
{
160+
return $this->parser->convertToArray($values, ',');
161+
}
162+
163+
/**
164+
* Generat question marks for prepared statement SQL
165+
*
166+
* @param array $columns
167+
*
168+
* @return array;
169+
*/
170+
public function questionMarksGenerator(array $columns)
171+
{
172+
foreach($columns as $column)
173+
{
174+
$this->questionMarks[] = $column = '?';
175+
}
176+
177+
return $this->parser->convertToString($this->questionMarks, ',');
178+
}
179+
93180
}

src/sql/SQLManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ public function select()
1414
{
1515
return new Select($this->table);
1616
}
17+
18+
/**
19+
* Insert object creating
20+
*
21+
* @return \Manipulator\SQL\Insert
22+
*/
23+
public function insert()
24+
{
25+
return new Insert($this->table);
26+
}
1727
}

0 commit comments

Comments
 (0)