MCPcopy Create free account
hub / github.com/apecloud/myduckserver / convertQuery

Method convertQuery

pgserver/connection_handler.go:1235–1298  ·  view source on GitHub ↗

convertQuery takes the given Postgres query, and converts it as a list of ast.ConvertedStatement that will work with the handler.

(query string, modifiers ...QueryModifier)

Source from the content-addressed store, hash-verified

1233
1234// convertQuery takes the given Postgres query, and converts it as a list of ast.ConvertedStatement that will work with the handler.
1235func (h *ConnectionHandler) convertQuery(query string, modifiers ...QueryModifier) ([]ConvertedStatement, error) {
1236 for _, modifier := range modifiers {
1237 query = modifier(query)
1238 }
1239
1240 // Check if the query is a subscription query, and if so, parse it as a subscription query.
1241 subscriptionConfig, err := parseSubscriptionSQL(query)
1242 if subscriptionConfig != nil && err == nil {
1243 return []ConvertedStatement{{
1244 String: query,
1245 PgParsable: true,
1246 SubscriptionConfig: subscriptionConfig,
1247 }}, nil
1248 }
1249
1250 // Check if the query is a backup/restore query, and if so, parse it as a backup/restore query.
1251 backupConfig, err := parseBackupSQL(query)
1252 if backupConfig != nil && err == nil {
1253 return []ConvertedStatement{{
1254 String: query,
1255 PgParsable: true,
1256 BackupConfig: backupConfig,
1257 }}, nil
1258 }
1259 restoreConfig, err := parseRestoreSQL(query)
1260 if restoreConfig != nil && err == nil {
1261 return []ConvertedStatement{{
1262 String: query,
1263 PgParsable: true,
1264 RestoreConfig: restoreConfig,
1265 }}, nil
1266 }
1267
1268 stmts, err := parser.Parse(query)
1269 if err != nil {
1270 // DuckDB syntax is not fully compatible with PostgreSQL, so we need to handle some queries differently.
1271 stmts, _ = parser.Parse("SELECT 'SQL syntax is incompatible with PostgreSQL' AS error")
1272 return []ConvertedStatement{{
1273 String: query,
1274 AST: stmts[0].AST,
1275 Tag: GuessStatementTag(query),
1276 PgParsable: false,
1277 }}, nil
1278 }
1279
1280 if len(stmts) == 0 {
1281 return []ConvertedStatement{{String: query}}, nil
1282 }
1283
1284 convertedStmts := make([]ConvertedStatement, len(stmts))
1285 for i, stmt := range stmts {
1286 // Check if the query is a full match query, and if so, handle it as a full match query.
1287 fullMatchQuery := handleFullMatchQuery(stmt.SQL)
1288 if fullMatchQuery != "" {
1289 convertedStmts[i].String = fullMatchQuery
1290 } else {
1291 convertedStmts[i].String = stmt.SQL
1292 }

Callers 3

handleQueryMethod · 0.95
handleParseMethod · 0.95
handledPSQLCommandsMethod · 0.95

Calls 5

parseSubscriptionSQLFunction · 0.85
parseBackupSQLFunction · 0.85
parseRestoreSQLFunction · 0.85
GuessStatementTagFunction · 0.85
handleFullMatchQueryFunction · 0.85

Tested by

no test coverage detected