-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapper.class.php
216 lines (182 loc) · 4.79 KB
/
Swapper.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/*
Swapper v0.8
(c) 2017 by Thielicious
Simplifies swapping rows in the database back and forth using initial array.
It creates a UDF and a SP, flexible for any project.
*/
if (!is_subclass_of("PDOshort","PDO")) {
abstract class autoBind extends PDO {
public function prep($sql, array $bind = null) {
$stmt = $this->prepare($sql);
$stmt->execute(($bind ? $bind : null));
return $stmt;
}
abstract public function qry($sql);
}
final class PDOshort extends autoBind {
public function qry($sql) {
return $this->prep($sql);
}
}
}
trait customPDO {
private
$pdo;
private function connect($host, $db, $user, $pass) {
$this->host = $host;
$this->db = $db;
$this->user = $user;
$this->pass = $pass;
try {
$this->pdo = new PDOshort(
"mysql:host=".$this->host.";dbname=".$this->db, $this->user, $this->pass,
array(
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"
)
);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
class Swapper {
use customPDO;
public
$table, $column,
$udf, $sp,
$err = [],
$getRows = [];
public function __construct(string $tbl, string $col, $host, $db, $user, $pass) {
$this->table = $tbl;
$this->column = $col;
$this->connect($host, $db, $user, $pass);
}
public function setUp(string $sp_name, string $udf_name = null) {
if (count($this->err) == 0) {
$this->createSP($sp_name);
$this->createUDF($udf_name);
$this->assignData();
} else {
$this->showErr();
exit;
}
}
private function showErr() {
if (count($this->err) != 0) {
if (count($this->err) == 1) {
echo $this->err."<br>";
} else {
foreach ($this->err as $err) {
echo $err."<br>";
}
}
}
}
private function assignData() {
try {
$assign = $this->pdo->qry("
SELECT ".$this->column."
FROM ".$this->table."
ORDER BY ".$this->column." ASC
");
$column = $this->column;
foreach ($assign as $row) {
$this->getRows[] = $row->$column;
}
} catch (PDOException $e) {
$this->err = $e->getMessage();
}
}
private function direction($chk, $direction) {
if ($direction == "up") {
foreach (array_values(array_reverse($this->getRows)) as $row) {
if ($row < $chk) {
$new = $row;
break;
} elseif ($chk == current($this->getRows)) {
$new = $chk;
break;
}
}
} elseif ($direction == "down") {
foreach (array_values($this->getRows) as $row) {
if ($row > $chk) {
$new = $row;
break;
} elseif ($chk == end($this->getRows)) {
$new = $chk;
break;
}
}
}
if (!is_null($new)) {
return $new;
} else {
$this->err = "Error, could no swap values.";
}
}
public function swap($rowNum, $where = "up" || "down") {
try {
$this->pdo->exec("
CALL ".$this->sp."(".$rowNum.",".$this->direction($rowNum, $where).");
");
} catch (PDOException $e) {
$this->err = $e->getMessage();
}
}
private function createSP($sp_name) {
$this->sp = $sp_name;
try {
$this->pdo->beginTransaction();
$this->pdo->exec("
CREATE PROCEDURE IF NOT EXISTS `".$this->sp."` (
`p_to_be_moved` INT,
`p_to_be_swapped` INT)
BEGIN
DECLARE `moved` INT;
DECLARE `swapped` INT;
SET
`moved` = `p_to_be_moved`,
`swapped` = `p_to_be_swapped`;
UPDATE `".$this->table."`
SET `".$this->column."` = CASE
WHEN (`".$this->column."` = `moved`) THEN `swapped`
WHEN (`".$this->column."` = `swapped`) THEN `moved`
END
WHERE `".$this->column."` IN(`moved`, `swapped`);
END;
");
$this->pdo->commit();
} catch (PDOException $e) {
$this->err = $e->getMessage();
$this->pdo->rollBack();
$this->pdo->exec("DROP PROCEDURE IF EXISTS `".$this->sp."`;");
}
}
private function createUDF($udf_name) {
$this->udf = $udf_name;
try {
$this->pdo->exec("
CREATE FUNCTION IF NOT EXISTS `".$this->udf."` ()
RETURNS INT(10)
BEGIN
DECLARE `getCount` INT(10);
SET `getCount` = (
SELECT COUNT(`".$this->column."`) AS cnt
FROM `".$this->table."`) + 1;
RETURN `getCount`;
END;
");
} catch (PDOException $e) {
$this->err = $e->getMessage();
}
}
public function __destruct() {
$this->showErr();
}
}
?>