-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlHints.ts
More file actions
63 lines (55 loc) · 1.89 KB
/
sqlHints.ts
File metadata and controls
63 lines (55 loc) · 1.89 KB
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
/**
* Interface for SQL hints configuration
*/
export interface SqlHints {
/** SQL hints for SELECT queries */
select?: string[];
/** SQL hints for INSERT queries */
insert?: string[];
/** SQL hints for UPDATE queries */
update?: string[];
/** SQL hints for DELETE queries */
delete?: string[];
}
/**
* Detects the type of SQL query and injects appropriate hints
* @param query - The SQL query to analyze
* @param hints - The hints configuration
* @returns The modified query with injected hints
*/
export function injectSqlHints(query: string, hints?: SqlHints): string {
if (!hints) {
return query;
}
// Normalize the query for easier matching
const normalizedQuery = query.trim().toUpperCase();
// Get the appropriate hints based on query type
let queryHints: string[] | undefined;
if (normalizedQuery.startsWith("SELECT")) {
queryHints = hints.select;
} else if (normalizedQuery.startsWith("INSERT")) {
queryHints = hints.insert;
} else if (normalizedQuery.startsWith("UPDATE")) {
queryHints = hints.update;
} else if (normalizedQuery.startsWith("DELETE")) {
queryHints = hints.delete;
}
// If no hints for this query type, return original query
if (!queryHints || queryHints.length === 0) {
return query;
}
// Join all hints with spaces
const hintsString = queryHints.join(" ");
// Inject hints into the query
if (normalizedQuery.startsWith("SELECT")) {
return `SELECT /*+ ${hintsString} */ ${query.substring(6)}`;
} else if (normalizedQuery.startsWith("INSERT")) {
return `INSERT /*+ ${hintsString} */ ${query.substring(6)}`;
} else if (normalizedQuery.startsWith("UPDATE")) {
return `UPDATE /*+ ${hintsString} */ ${query.substring(6)}`;
} else if (normalizedQuery.startsWith("DELETE")) {
return `DELETE /*+ ${hintsString} */ ${query.substring(6)}`;
}
// If no match found, return original query
return query;
}