Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix escaping function #138

Merged
merged 1 commit into from
Nov 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { validateSqliteCommand } from './sqlite/sqliteCommandValidation';
import Clipboard from './utils/clipboard';
import { Schema } from './common';
import { extractStatements } from './sqlite/queryParser';
import { keywords } from './languageserver/keywords';

export namespace Commands {
export const showOutputChannel = "sqlite.showOutputChannel";
Expand Down Expand Up @@ -281,22 +282,25 @@ function explorerRefresh(): Thenable<any> {
);
}

export function newQuerySelect(table: Schema.Table): Thenable<any> {
function sqlSafeColName(name: string) {
return name.indexOf(" ") < 0 ? name : `\`${name}\``;
const keywordsSet = new Set(keywords);

function sqlSafeName(name: string) {
if (/^[A-Za-z_]\w*$/.test(name) && !keywordsSet.has(name.toUpperCase())) {
return name
}
let contentL0 = `SELECT ${table.columns.map(c => sqlSafeColName(c.name)).join(", ")}`;
let contentL1 = `FROM \`${table.name}\`;`;
return `\`${name.replace(/`/g, '``')}\``;
}

export function newQuerySelect(table: Schema.Table): Thenable<any> {
let contentL0 = `SELECT ${table.columns.map(c => sqlSafeName(c.name)).join(", ")}`;
let contentL1 = `FROM ${sqlSafeName(table.name)};`;
let content = contentL0 + "\n" + contentL1;
let cursorPos = new Position(1, contentL1.length-1);
return newQuery(table.database, content, cursorPos);
}

function newQueryInsert(table: Schema.Table): Thenable<any> {
function sqlSafeColName(name: string) {
return name.indexOf(" ") < 0 ? name : `\`${name}\``;
}
let contentL0 = `INSERT INTO \`${table.name}\` (${table.columns.map(c => sqlSafeColName(c.name)).join(", ")})`;
let contentL0 = `INSERT INTO ${sqlSafeName(table.name)} (${table.columns.map(c => sqlSafeName(c.name)).join(", ")})`;
let contentL1 = `VALUES ();`;
let content = contentL0 + "\n" + contentL1;
// move the cursor inside the round brackets
Expand All @@ -312,7 +316,7 @@ function newQuery(dbPath?: string, content: string = "", cursorPos: Position = n
}

function runTableQuery(dbPath: string, tableName: string) {
let query = `SELECT * FROM \`${tableName}\`;`;
let query = `SELECT * FROM ${sqlSafeName(tableName)};`;
runQuery(dbPath, query, true);
}

Expand Down