Skip to content

Commit bf01878

Browse files
committed
refactor: Refactor ontology_utils to use matrix_db_manager::exec_search
- Update `get_active_tlds` to use exec_search. - Update `delete_tld_nodes` to use exec_search. - Refactor `restore_from_bk_table` to use parameterized queries via exec_search, replacing manual escaping.
1 parent 8974bba commit bf01878

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

core/ontology_engine/class.ontology_utils.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,14 @@ public static function get_active_tlds() : array {
138138
}
139139

140140
$table = ontology_node::$table;
141-
$sql_query = "SELECT tld FROM \"{$table}\" GROUP BY tld";
142-
$result = pg_query(DBi::_getConnection(), $sql_query);
141+
$sql_query = "SELECT \"tld\" FROM \"{$table}\" GROUP BY \"tld\"";
142+
$result = matrix_db_manager::exec_search($sql_query);
143143

144144
$active_tlds = [];
145-
while($row = pg_fetch_assoc($result)) {
146-
$active_tlds[] = $row['tld'];
145+
if ($result) {
146+
while($row = pg_fetch_assoc($result)) {
147+
$active_tlds[] = $row['tld'];
148+
}
147149
}
148150

149151
self::$active_tlds_cache = $active_tlds;
@@ -203,7 +205,7 @@ public static function delete_tld_nodes( string $tld ) : bool {
203205

204206
// dd_ontology. delete terms (records)
205207
$sql_query = 'DELETE FROM "' . $table . '" WHERE "tld" = $1;';
206-
$result = pg_query_params(DBi::_getConnection(), $sql_query, [$safe_tld]);
208+
$result = matrix_db_manager::exec_search($sql_query, [$safe_tld]);
207209

208210
if ($result === false) {
209211
debug_log(__METHOD__ . " Error deleting tld records. tld: {$tld}", logger::ERROR);
@@ -297,10 +299,11 @@ public static function restore_from_bk_table( array $tlds ) : bool {
297299
}
298300

299301
$table = ontology_node::$table;
300-
$conn = DBi::_getConnection();
302+
$params = [];
301303
$where_clauses = [];
302-
foreach ($tlds as $tld) {
303-
$where_clauses[] = "tld = " . pg_escape_literal($conn, $tld);
304+
foreach ($tlds as $index => $tld) {
305+
$params[] = $tld;
306+
$where_clauses[] = "\"tld\" = $" . ($index + 1);
304307
}
305308
$where_sql = implode(' OR ', $where_clauses);
306309

@@ -309,7 +312,7 @@ public static function restore_from_bk_table( array $tlds ) : bool {
309312
SELECT * FROM \"dd_ontology_bk\" WHERE {$where_sql};
310313
";
311314

312-
$result = pg_query($conn, $sql_query);
315+
$result = matrix_db_manager::exec_search($sql_query, $params);
313316

314317
if ($result === false) {
315318
debug_log(__METHOD__ . ' Failed to restore from backup table', logger::ERROR);

0 commit comments

Comments
 (0)