Skip to content

Commit eb0f233

Browse files
committed
was created classes which update and delete records from database
1 parent 52630a9 commit eb0f233

File tree

4 files changed

+151
-1
lines changed

4 files changed

+151
-1
lines changed

src/sql/Delete.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Manipulator\SQL;
4+
5+
use Manipulator\SQL\Traits\Expressions;
6+
7+
class Delete extends Query
8+
{
9+
use Expressions;
10+
11+
/**
12+
* Table name where will be
13+
* happen manipualtions
14+
*
15+
* @var string
16+
*/
17+
protected $table;
18+
19+
/**
20+
* Array with SQL statements
21+
*
22+
* @var array
23+
*/
24+
protected $sql = [];
25+
26+
public function __construct(string $table)
27+
{
28+
$this->table = $table;
29+
30+
parent::__construct();
31+
32+
$this->dropRecord();
33+
}
34+
35+
/**
36+
* Set sql statement and return reference
37+
* at this class
38+
*
39+
* @return \Manipulator\SQL\Delete
40+
*/
41+
protected function dropRecord()
42+
{
43+
$this->sql[] = "DELETE FROM {$this->table}";
44+
return $this;
45+
}
46+
47+
}

src/sql/Query.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function get()
5454
* Prepare SQL statement and bind
5555
* values with question marks.
5656
* Can be used with Insert and
57-
* Delete classes.
57+
* Update classes.
5858
*
5959
* @return bool
6060
*/
@@ -68,6 +68,18 @@ public function push()
6868

6969
}
7070

71+
/**
72+
* This method can be used with Delete class
73+
* when we need just to execute SQL statement
74+
* and get number of effected rows
75+
*
76+
* @return int
77+
*/
78+
public function drop()
79+
{
80+
return $this->exec($this->convertSQLToString($this->sql));
81+
}
82+
7183
/**
7284
* This is abstaraction of fetchAll PDO method
7385
*
@@ -92,6 +104,19 @@ protected function fetchColumn(\PDOStatement $statement)
92104
return $statement->fetchColumn();
93105
}
94106

107+
/**
108+
* Execute SQL statement and return a number
109+
* of executed rows
110+
*
111+
* @param string $query
112+
*
113+
* @return int
114+
*/
115+
protected function exec(string $query)
116+
{
117+
return $this->connection()->exec($query);
118+
}
119+
95120
/**
96121
* Pass needed query into query PDO method
97122
*

src/sql/SQLManager.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,24 @@ public function insert()
2424
{
2525
return new Insert($this->table);
2626
}
27+
28+
/**
29+
* Update object creating
30+
*
31+
* @return \Manipulator\SQL\Update
32+
*/
33+
public function update()
34+
{
35+
return new Update($this->table);
36+
}
37+
38+
/**
39+
* Delete object creating
40+
*
41+
* @return \Manipulator\SQL\Delete
42+
*/
43+
public function delete()
44+
{
45+
return new Delete($this->table);
46+
}
2747
}

src/sql/Update.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Manipulator\SQL;
4+
5+
use Manipulator\SQL\Traits\Expressions;
6+
7+
class Update extends Query
8+
{
9+
use Expressions;
10+
11+
/**
12+
* Table name where we will be
13+
* maniulate data
14+
*
15+
* @var string
16+
*/
17+
protected $table;
18+
19+
/**
20+
* Values which will be bind
21+
* in prepared statement with
22+
* question marks
23+
*
24+
* @var array
25+
*/
26+
protected $values;
27+
28+
/**
29+
* SQL statement
30+
*
31+
* @var array
32+
*/
33+
protected $sql = [];
34+
35+
public function __construct(string $table)
36+
{
37+
$this->table = $table;
38+
39+
parent::__construct();
40+
}
41+
42+
/**
43+
* Set SQL statement which update record into
44+
* database
45+
*
46+
* @param string $columns
47+
* @param array $values
48+
*
49+
* @return \Manipulator\SQL\Update
50+
*/
51+
public function set(string $columns, array $values)
52+
{
53+
$this->values = $values;
54+
55+
$this->sql[] = "UPDATE {$this->table} SET {$columns} ";
56+
return $this;
57+
}
58+
}

0 commit comments

Comments
 (0)