Skip to content

Commit d02913b

Browse files
committed
Add support for saving tests. Also switch save & update over top use prepared statements.
1 parent f1143cd commit d02913b

File tree

4 files changed

+103
-29
lines changed

4 files changed

+103
-29
lines changed

PCRE-TEST-DB-UPDATES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add new "mode" and "tests" columns
2+
3+
ALTER TABLE `patterns` ADD `mode` ENUM('text','tests') NOT NULL DEFAULT 'text' AFTER `numNegativeVotes`;
4+
ALTER TABLE `patterns` ADD `tests` MEDIUMTEXT NULL DEFAULT NULL AFTER `mode`;

server/actions/patterns/save.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function savePattern($userProfile) {
4444
$parentId = convertFromURL($this->getValue('parentId'));
4545
$access = $this->getValue('access');
4646
$access = empty($access)?null:$access;
47+
$mode = $this->getValue('mode');
48+
$tests = $this->getValue('tests');
4749

4850
$patternId = convertFromURL($id);
4951

@@ -61,23 +63,46 @@ function savePattern($userProfile) {
6163
} else {
6264
$accessUpdate = '';
6365
if (!is_null($access)) {
64-
$accessUpdate = ",visibility='{$access}'";
66+
$accessUpdate = ",visibility=?";
6567
}
68+
6669
$sql = "UPDATE patterns SET
67-
name='{$name}',
68-
content='{$content}',
69-
`pattern`='{$pattern}',
70-
author='{$author}',
71-
keywords='{$keywords}',
72-
description='{$description}',
73-
state='{$tool}',
74-
flavor='{$flavor}'
70+
`name`=?,
71+
content=?,
72+
`pattern`=?,
73+
author=?,
74+
keywords=?,
75+
`description`=?,
76+
`state`=?,
77+
flavor=?,
78+
mode=?,
79+
tests=?
7580
$accessUpdate
76-
WHERE id='{$patternId}'";
77-
$this->db->query($sql);
81+
WHERE id=?";
82+
83+
$sqlParams = [
84+
["s", $name],
85+
["s", $content],
86+
["s", $pattern],
87+
["s", $author],
88+
["s", $keywords],
89+
["s", $description],
90+
["s", $tool],
91+
["s", $flavor],
92+
["s", $mode],
93+
["s", $tests],
94+
];
95+
96+
if (!is_null($access)) {
97+
array_push($sqlParams, ["s", $access]);
98+
}
99+
100+
array_push($sqlParams, ["i", $patternId]);
101+
102+
$this->db->execute($sql, $sqlParams);
78103
}
79104
} else if (is_null($parentId)) { // Not a fork
80-
$patternId = savePattern($this->db, $name, $content, $pattern, $author, $description, $keywords, $tool, $flavor, $userProfile->userId, $access);
105+
$patternId = savePattern($this->db, $name, $content, $pattern, $author, $description, $keywords, $tool, $flavor, $userProfile->userId, $access, $mode, $tests);
81106
} else { // Fork
82107
$existingPattern = $this->db->query("SELECT visibility, owner FROM patterns WHERE id='{$patternId}'", true);
83108
if (!is_null($existingPattern)) {
@@ -119,13 +144,15 @@ function getVisibilityValues() {
119144
public function getSchema() {
120145
return array(
121146
"id" => array("type"=>self::STRING, "required"=>false),
147+
"mode" => array("type"=>self::ENUM, "values"=>["text", "tests"], "required"=>false, "default" => "text"),
122148
"keywords" => array("type"=>self::STRING, "required"=>false, "length"=>2056),
123149
"name" => array("type"=>self::STRING, "required"=>false, "length"=>30),
124150
"expression" => array("type"=>self::STRING, "required"=>false, "length"=>2048),
125151
"text" => array("type"=>self::STRING, "required"=>false),
126152
"description" => array("type"=>self::STRING, "required"=>false, "length"=>250),
127153
"token" => array("type"=>self::STRING, "required"=>false),
128154
"tool" => array("type"=>self::STRING, "required"=>false, "length"=>1024),
155+
"tests" => array("type"=>self::STRING, "required"=>false),
129156
"flavor" => array("type"=>self::ENUM, "values"=>$this->getTypeValues(), "required"=>false),
130157
"parentId" => array("type"=>self::STRING, "required"=>false),
131158
"access" => array("type"=>self::ENUM, "values"=>$this->getVisibilityValues(), "required"=>false)

server/core/DB.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ function connect($host, $username, $password, $dbName, $dbPort = null, $dbSock =
4444
}
4545
}
4646

47+
public function execute($sql, $params) {
48+
$stmt = $this->mysqli->prepare($sql);
49+
50+
if ($stmt == true) {
51+
$types = "";
52+
$values = [];
53+
54+
foreach ($params as $i => $value) {
55+
$types .= $value[0];
56+
$values[] = &$value[1];
57+
}
58+
59+
$stmt->bind_param($types, ...$values);
60+
61+
if (!$stmt->execute()) {
62+
throw new \core\APIError(\core\ErrorCodes::MYSQL_QUERY_ERR, $stmt->error);
63+
}
64+
65+
$stmt->close();
66+
} else {
67+
throw new \core\APIError(\core\ErrorCodes::MYSQL_QUERY_ERR, $this->mysqli->error);
68+
}
69+
}
70+
4771
public function isConnected() {
4872
return $this->_isConnected;
4973
}

server/utils.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ function convertToURL($id) {
102102
}
103103

104104
function convertFromURL($id) {
105-
if (!empty($id)) {
106-
return base_convert($id, 32, 10) / 3 - 1000000;
107-
}
108-
return null;
105+
if (!empty($id)) {
106+
return base_convert($id, 32, 10) / 3 - 1000000;
107+
}
108+
return null;
109109
}
110110

111111
function createPatternNode($row) {
@@ -151,6 +151,8 @@ function createPatternNode($row) {
151151
'userRating' => idx($row, 'userRating') ?? '0',
152152
'favorite' => !is_null(idx($row, 'favorite')),
153153
'access' => idx($row, 'visibility'),
154+
'mode' => idx($row, 'mode'),
155+
'tests' => idx($row, 'tests')
154156
);
155157

156158
return $result;
@@ -171,7 +173,7 @@ function createPatternSet($result, $total = -1, $startIndex = 0, $limit = 100) {
171173
}
172174

173175

174-
function savePattern($db, $name, $content, $pattern, $author, $description, $keywords, $state, $type, $userId, $visibility=null) {
176+
function savePattern($db, $name, $content, $pattern, $author, $description, $keywords, $state, $type, $userId, $visibility=null, $mode = "text", $tests = null) {
175177
if (is_null($visibility)) {
176178
$visibility = \core\PatternVisibility::PROTECTED;
177179
}
@@ -188,24 +190,41 @@ function savePattern($db, $name, $content, $pattern, $author, $description, $key
188190
state,
189191
flavor,
190192
owner,
191-
visibility
193+
visibility,
194+
mode,
195+
tests
192196
)
193197
VALUES
194198
(
195-
'{$name}',
196-
'{$content}',
197-
'{$pattern}',
198-
'{$author}',
199+
?,
200+
?,
201+
?,
202+
?,
199203
NOW(),
200-
'{$description}',
201-
'{$keywords}',
202-
'{$state}',
203-
'{$type}',
204-
'{$userId}',
205-
'{$visibility}'
204+
?,
205+
?,
206+
?,
207+
?,
208+
?,
209+
?,
210+
?,
211+
?
206212
)";
207213

208-
$db->query($sql);
214+
$db->execute($sql, [
215+
["s", $name],
216+
["s", $content],
217+
["s", $pattern],
218+
["s", $author],
219+
["s", $description],
220+
["s", $keywords],
221+
["s", $state],
222+
["s", $type],
223+
["s", $userId],
224+
["s", $visibility],
225+
["s", $mode],
226+
["s", $tests]
227+
]);
209228

210229
return $db->getLastId();
211230
}

0 commit comments

Comments
 (0)