Skip to content

Commit

Permalink
improve parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Nov 8, 2024
1 parent 619d1bf commit 620a4fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
20 changes: 4 additions & 16 deletions lib/db/query-parsers/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,6 @@ function parseStatement(statement, kind = 'insert') {
}

const targetIdentifier = statement.split(splitter).pop().trim().split(/\s/).shift()
// .replace(/[`'"]/g, '')

// const identifierParts = targetIdentifier.split('.')
// let collection
// let database
// let table
// if (identifierParts.length === 1) {
// table = normalizeTableName(identifierParts[0])
// collection = table
// } else {
// database = identifierParts[0]
// table = normalizeTableName(identifierParts[1])
// collection = `${database}.${table}`
// }

// return { collection, database, table }
return parseTableIdentifier(targetIdentifier)
}

Expand Down Expand Up @@ -243,6 +227,10 @@ function parseTableIdentifier(identifier) {
* @returns {string} The normalized table name.
*/
function normalizeTableName(tableIdentifier) {
// Some of our tests add non-standard characters to table names and expects
// they will be stripped.
tableIdentifier = tableIdentifier.replace(/[;]/g, '')

if (tableIdentifier[0] === '(') {
// We might have a subquery. If there is a single word between the
// parentheticals, we return it as the table name (even though this is not
Expand Down
27 changes: 14 additions & 13 deletions test/unit/db/query-parsers/sql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ test('handles fully qualified names', () => {
let found = parseSql(statement)
match(found, expected)

statement = `insert into 'myDb.foo' (col1)`
statement = `insert into 'myDb'.'foo' (col1)`
found = parseSql(statement)
match(found, expected)

statement = `insert into "myDb.foo" (col1)`
statement = `insert into "myDb"."foo" (col1)`
found = parseSql(statement)
match(found, expected)
})
Expand Down Expand Up @@ -287,14 +287,15 @@ test('maps `SELECT ? + ? AS solution` to "unknown" collection', () => {
})
})

// test('handles odd characters attached to table names', () => {
// const expected = { operation: 'select', collection: 'unit-test', table: 'unit-test' }
//
// let statement = 'select test from unit-test;'
// let found = parseSql(statement)
// match(found, expected)
//
// statement = 'select test from schema.unit-test;'
// found = parseSql(statement)
// match(found, expected)
// })
test('handles odd characters attached to table names', () => {
const expected = { operation: 'select', collection: 'unit-test', table: 'unit-test' }

let statement = 'select test from unit-test;'
let found = parseSql(statement)
match(found, expected)

expected.collection = 'schema.unit-test'
statement = 'select test from schema.unit-test;'
found = parseSql(statement)
match(found, expected)
})

0 comments on commit 620a4fb

Please sign in to comment.