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

Fixes to tablename being parsed from query #19

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
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
21 changes: 16 additions & 5 deletions src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,32 @@ export async function query(

// note: statement[0] is the query **without** the semicolon, this lets us split on `_` and still
// get the table id if the table name is the last word in the statement
const tablename =
statement[0].match(/\b(?:FROM|JOIN|UPDATE|INTO)\s+(\S+(?:.\s)*)/i) ?? []; // Find table name
const tablenameMatches =
statement[0].match(
/\b(?:FROM|JOIN|UPDATE|INTO)\s+(\S+(?=\()|\S+(?:.\s)*)/i
) ?? []; // Find table name

if (!(tablename && tablename[1])) {
let tablename = tablenameMatches[1] ?? "";

if (tablename[0] === '"') {
tablename = tablename.slice(1);
}
if (tablename[tablename.length - 1] === '"') {
tablename = tablename.slice(0, -1);
}

if (!tablename) {
// If ID isn't a postive interger, throw error.
throw new Error(
"No table name identifier found in query. Tableland does not support sql statements that do not" +
" include a specific table name identifier."
);
}

const tablenameArray = tablename[1].split("_"); // Split tablename into chunks divided by _
const tablenameArray = tablename.split("_"); // Split tablename into chunks divided by _
const tableId = tablenameArray[tablenameArray.length - 1]; // The find the last chunk, which should be ID

if (!isPositiveInteger(tableId) && tablename[1] !== "system_table") {
if (!isPositiveInteger(tableId) && tablename !== "system_table") {
// If ID isn't a postive interger, throw error.
throw Error(
"No ID found in query. Remember to add the table's ID after it's name. Ex; TableName_0000"
Expand Down
24 changes: 24 additions & 0 deletions test/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ describe("query method", function () {
await expect(res2).toEqual({columns: ["colname"], rows: ["val1"]});
});

test("parses tablename regardless of whitespace", async function () {
fetch.mockResponseOnce(FetchSelectQuerySuccess);

const res1 = await connection.query("INSERT INTO test_1(colname) Values ('val6');");
await expect(res1).toEqual({columns: ["colname"], rows: ["val1"]});

fetch.mockResponseOnce(FetchSelectQuerySuccess);

const res2 = await connection.query("sELEct * frOM test_1;");
await expect(res2).toEqual({columns: ["colname"], rows: ["val1"]});
});

test("parses tablename when inside double-quotes", async function () {
fetch.mockResponseOnce(FetchSelectQuerySuccess);

const res1 = await connection.query("INSERT INTO \"test_1\" (colname) Values ('val6');");
await expect(res1).toEqual({columns: ["colname"], rows: ["val1"]});

fetch.mockResponseOnce(FetchSelectQuerySuccess);

const res2 = await connection.query("sELEct * frOM test_1;");
await expect(res2).toEqual({columns: ["colname"], rows: ["val1"]});
});

test("throws error when query tablename is invalid", async function () {
fetch.mockResponseOnce(FetchRunQueryError);

Expand Down