github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/util/sql/summarize.ts (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 import _ from "lodash"; 12 13 export interface StatementSummary { 14 statement?: string; 15 table?: string; 16 error?: string; 17 } 18 19 const keywords: { [key: string]: RegExp } = { 20 update: /^update\s+(\S+)/i, 21 select: /^select.+from\s+(\S+)/i, 22 insert: /^insert\s+into\s+([^ \t(]+)/i, 23 delete: /^delete\s+from\s+(\S+)/i, 24 create: /^create\s+table\s+(\S+)/i, 25 set: /^set\s+((cluster\s+setting\s+)?\S+)/i, 26 }; 27 28 // summarize takes a string SQL statement and produces a structured summary 29 // of the query. 30 export function summarize(statement: string): StatementSummary { 31 for (const keyword in keywords) { 32 if (_.startsWith(_.toLower(statement), _.toLower(keyword))) { 33 const tablePattern = keywords[keyword]; 34 const tableMatch = tablePattern.exec(statement); 35 36 if (!tableMatch) { 37 return { 38 error: "unable to find table for " + keyword + " statement", 39 }; 40 } 41 42 let table = tableMatch[1]; 43 if (table[0] === "\"" && table[table.length - 1] === "\"") { 44 table = table.slice(1, table.length - 1); 45 } 46 47 return { 48 statement: keyword, 49 table: table, 50 }; 51 } 52 } 53 54 return { 55 error: "unimplemented", 56 }; 57 }