Skip to content

Commit 6c17940

Browse files
author
Nathan Nguyen
committed
Change to get_recordset_sql
1 parent e0dc32a commit 6c17940

File tree

2 files changed

+76
-59
lines changed

2 files changed

+76
-59
lines changed

classes/helper.php

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ public static function find_course_field(string $table): string {
167167
* @param string $table The table to search.
168168
* @param database_column_info $column The column to search.
169169
* @param int $limit The maximum number of results to return.
170+
* @param $stream The resource to write the results to.
170171
* @return array The results of the search.
171172
* @throws \dml_exception
172173
*/
173174
public static function plain_text_search(string $search, string $table,
174-
database_column_info $column, int $limit = 0): array {
175+
database_column_info $column, bool $summary = false,
176+
$stream = null): array {
175177
global $DB;
176178

177179
$results = [];
@@ -198,9 +200,33 @@ public static function plain_text_search(string $search, string $table,
198200
}
199201

200202
if ($column->meta_type === 'X' || $column->meta_type === 'C') {
201-
$records = $DB->get_records_sql($sql, [$searchparam], 0, $limit);
202-
if ($records) {
203-
$results[$table][$column->name] = $records;
203+
$limit = $summary ? 1 : 0;
204+
$records = $DB->get_recordset_sql($sql, [$searchparam], 0, $limit);
205+
if ($records->valid()) {
206+
if (!empty($stream)) {
207+
if ($summary) {
208+
fputcsv($stream, [
209+
$table,
210+
$column->name,
211+
]);
212+
213+
// Return empty array to skip the rest of the function.
214+
return $results;
215+
}
216+
217+
foreach ($records as $record) {
218+
fputcsv($stream, [
219+
$table,
220+
$column->name,
221+
$record->courseid ?? '',
222+
$record->courseidnumber ?? '',
223+
$record->id,
224+
$record->$columnname,
225+
]);
226+
}
227+
} else {
228+
$results[$table][$column->name] = $records;
229+
}
204230
}
205231
}
206232

@@ -215,10 +241,12 @@ public static function plain_text_search(string $search, string $table,
215241
* @param string $table The table to search.
216242
* @param database_column_info $column The column to search.
217243
* @param int $limit The maximum number of results to return.
244+
* @param $stream The resource to write the results to.
218245
* @return array
219246
*/
220247
public static function regular_expression_search(string $search, string $table,
221-
database_column_info $column, int $limit = 0): array {
248+
database_column_info $column, bool $summary = false,
249+
$stream = null): array {
222250
global $DB;
223251

224252
// Check if the database supports regular expression searches.
@@ -249,10 +277,46 @@ public static function regular_expression_search(string $search, string $table,
249277
$sql = "SELECT id, $columnname FROM {".$table."} $tablealias WHERE $select";
250278
}
251279

252-
$records = $DB->get_records_sql($sql, $params, 0, $limit);
253-
254-
if ($records) {
255-
$results[$table][$column->name] = $records;
280+
$limit = $summary ? 1 : 0;
281+
$records = $DB->get_recordset_sql($sql, $params, 0, $limit);
282+
283+
if ($records->valid()) {
284+
if (!empty($stream)) {
285+
if ($summary) {
286+
fputcsv($stream, [
287+
$table,
288+
$column->name,
289+
]);
290+
291+
// Return empty array to skip the rest of the function.
292+
return $results;
293+
}
294+
295+
foreach ($records as $record) {
296+
$data = $record->$columnname;
297+
// Replace "/" with "\/", as it is used as delimiters.
298+
$pattern = str_replace('/', '\\/', $search);
299+
300+
// Perform the regular expression search.
301+
preg_match_all( "/" . $pattern . "/", $data, $matches);
302+
303+
if (!empty($matches[0])) {
304+
// Show the result foreach match.
305+
foreach ($matches[0] as $match) {
306+
fputcsv($stream, [
307+
$table,
308+
$column->name,
309+
$record->courseid ?? '',
310+
$record->courseidnumber ?? '',
311+
$record->id,
312+
$match,
313+
]);
314+
}
315+
}
316+
}
317+
} else {
318+
$results[$table][$column->name] = $records;
319+
}
256320
}
257321
}
258322
return $results;

cli/find.php

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -105,71 +105,24 @@
105105
if (!$options['summary']) {
106106
fputcsv($fp, ['Table', 'Column', 'courseid', 'idnumber', 'ID', 'Match']);
107107
} else {
108-
fputcsv($fp, ['Table', 'Column', 'courseid', 'idnumber']);
108+
fputcsv($fp, ['Table', 'Column']);
109109
}
110110

111111
// Perform the search.
112112
$searchlist = helper::build_searching_list($tables);
113113

114114
// Output the result for each table.
115115
foreach ($searchlist as $table => $columns) {
116-
// Summary mode.
117-
$limit = $options['summary'] ? 1 : 0;
118-
119116
foreach ($columns as $column) {
120117
// Show the table and column being searched.
121118
$colname = $column->name;
122119
echo "Searching in table: $table, column: $colname\n";
123120

124121
// Perform the search.
125122
if (!empty($options['regex-match'])) {
126-
$result = helper::regular_expression_search($search, $table, $column, $limit);
127-
} else {
128-
$result = helper::plain_text_search($search, $table, $column, $limit);
129-
}
130-
131-
// Notifying the user if no results were found.
132-
if (empty($result)) {
133-
echo "No results found.\n";
134-
continue;
135-
} else {
136-
echo count($result) . " results found.\n";
137-
}
138-
139-
$rows = reset($result)[$colname];
140-
if ($options['summary']) {
141-
$courseid = reset($rows)->courseid ?? '';
142-
$courseidnumber = reset($rows)->courseidnumber ?? '';
143-
fputcsv($fp, [$table, $colname, $courseid, $courseidnumber]);
123+
helper::regular_expression_search($search, $table, $column, $options['summary'], $fp);
144124
} else {
145-
foreach ($rows as $row) {
146-
// Fields to show.
147-
$courseid = $row->courseid ?? '';
148-
$courseidnumber = $row->courseidnumber ?? '';
149-
$fields = [$table, $colname, $courseid, $courseidnumber, $row->id];
150-
// Matched data.
151-
$data = $row->$colname;
152-
153-
if (!empty($options['regex-match'])) {
154-
// If the search string is a regular expression, show each matching instance.
155-
156-
// Replace "/" with "\/", as it is used as delimiters.
157-
$search = str_replace('/', '\\/', $options['regex-match']);
158-
159-
// Perform the regular expression search.
160-
preg_match_all( "/" . $search . "/", $data, $matches);
161-
162-
if (!empty($matches[0])) {
163-
// Show the result foreach match.
164-
foreach ($matches[0] as $match) {
165-
fputcsv($fp, array_merge($fields, [$match]));
166-
}
167-
}
168-
} else {
169-
// Show the result for simple plain text search.
170-
fputcsv($fp, array_merge($fields, [$data]));
171-
}
172-
}
125+
helper::plain_text_search($search, $table, $column, $options['summary'], $fp);
173126
}
174127
}
175128
}

0 commit comments

Comments
 (0)