Skip to content

Commit

Permalink
Change to get_recordset_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Sep 19, 2024
1 parent e0dc32a commit 6c17940
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 59 deletions.
82 changes: 73 additions & 9 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ public static function find_course_field(string $table): string {
* @param string $table The table to search.
* @param database_column_info $column The column to search.
* @param int $limit The maximum number of results to return.
* @param $stream The resource to write the results to.
* @return array The results of the search.
* @throws \dml_exception
*/
public static function plain_text_search(string $search, string $table,
database_column_info $column, int $limit = 0): array {
database_column_info $column, bool $summary = false,
$stream = null): array {
global $DB;

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

if ($column->meta_type === 'X' || $column->meta_type === 'C') {
$records = $DB->get_records_sql($sql, [$searchparam], 0, $limit);
if ($records) {
$results[$table][$column->name] = $records;
$limit = $summary ? 1 : 0;
$records = $DB->get_recordset_sql($sql, [$searchparam], 0, $limit);
if ($records->valid()) {
if (!empty($stream)) {
if ($summary) {
fputcsv($stream, [
$table,
$column->name,
]);

// Return empty array to skip the rest of the function.
return $results;
}

foreach ($records as $record) {
fputcsv($stream, [
$table,
$column->name,
$record->courseid ?? '',
$record->courseidnumber ?? '',
$record->id,
$record->$columnname,
]);
}
} else {
$results[$table][$column->name] = $records;
}
}
}

Expand All @@ -215,10 +241,12 @@ public static function plain_text_search(string $search, string $table,
* @param string $table The table to search.
* @param database_column_info $column The column to search.
* @param int $limit The maximum number of results to return.
* @param $stream The resource to write the results to.
* @return array
*/
public static function regular_expression_search(string $search, string $table,
database_column_info $column, int $limit = 0): array {
database_column_info $column, bool $summary = false,
$stream = null): array {
global $DB;

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

$records = $DB->get_records_sql($sql, $params, 0, $limit);

if ($records) {
$results[$table][$column->name] = $records;
$limit = $summary ? 1 : 0;
$records = $DB->get_recordset_sql($sql, $params, 0, $limit);

if ($records->valid()) {
if (!empty($stream)) {
if ($summary) {
fputcsv($stream, [
$table,
$column->name,
]);

// Return empty array to skip the rest of the function.
return $results;
}

foreach ($records as $record) {
$data = $record->$columnname;
// Replace "/" with "\/", as it is used as delimiters.
$pattern = str_replace('/', '\\/', $search);

// Perform the regular expression search.
preg_match_all( "/" . $pattern . "/", $data, $matches);

if (!empty($matches[0])) {
// Show the result foreach match.
foreach ($matches[0] as $match) {
fputcsv($stream, [
$table,
$column->name,
$record->courseid ?? '',
$record->courseidnumber ?? '',
$record->id,
$match,
]);
}
}
}
} else {
$results[$table][$column->name] = $records;
}
}
}
return $results;
Expand Down
53 changes: 3 additions & 50 deletions cli/find.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,71 +105,24 @@
if (!$options['summary']) {
fputcsv($fp, ['Table', 'Column', 'courseid', 'idnumber', 'ID', 'Match']);
} else {
fputcsv($fp, ['Table', 'Column', 'courseid', 'idnumber']);
fputcsv($fp, ['Table', 'Column']);
}

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

// Output the result for each table.
foreach ($searchlist as $table => $columns) {
// Summary mode.
$limit = $options['summary'] ? 1 : 0;

foreach ($columns as $column) {
// Show the table and column being searched.
$colname = $column->name;
echo "Searching in table: $table, column: $colname\n";

// Perform the search.
if (!empty($options['regex-match'])) {
$result = helper::regular_expression_search($search, $table, $column, $limit);
} else {
$result = helper::plain_text_search($search, $table, $column, $limit);
}

// Notifying the user if no results were found.
if (empty($result)) {
echo "No results found.\n";
continue;
} else {
echo count($result) . " results found.\n";
}

$rows = reset($result)[$colname];
if ($options['summary']) {
$courseid = reset($rows)->courseid ?? '';
$courseidnumber = reset($rows)->courseidnumber ?? '';
fputcsv($fp, [$table, $colname, $courseid, $courseidnumber]);
helper::regular_expression_search($search, $table, $column, $options['summary'], $fp);
} else {
foreach ($rows as $row) {
// Fields to show.
$courseid = $row->courseid ?? '';
$courseidnumber = $row->courseidnumber ?? '';
$fields = [$table, $colname, $courseid, $courseidnumber, $row->id];
// Matched data.
$data = $row->$colname;

if (!empty($options['regex-match'])) {
// If the search string is a regular expression, show each matching instance.

// Replace "/" with "\/", as it is used as delimiters.
$search = str_replace('/', '\\/', $options['regex-match']);

// Perform the regular expression search.
preg_match_all( "/" . $search . "/", $data, $matches);

if (!empty($matches[0])) {
// Show the result foreach match.
foreach ($matches[0] as $match) {
fputcsv($fp, array_merge($fields, [$match]));
}
}
} else {
// Show the result for simple plain text search.
fputcsv($fp, array_merge($fields, [$data]));
}
}
helper::plain_text_search($search, $table, $column, $options['summary'], $fp);
}
}
}
Expand Down

0 comments on commit 6c17940

Please sign in to comment.