-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from pinax-network/fix/create-table
improve SQL create table handling
- Loading branch information
Showing
3 changed files
with
97 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,52 @@ | ||
export function parseSchema(sql: string) { | ||
const tables = new Map<string, string[]>(); // <table, columns> | ||
const statements = sql.split(";"); | ||
const statements = sql.split(";") | ||
|
||
// should return `block_meta` as table and `id, at, number, hash, parent_hash, timestamp` as columns | ||
for (const statement of statements) { | ||
const match = statement.match(/CREATE TABLE (\w+)/); | ||
if (match) { | ||
const table = match[1]; | ||
const columns = new Set<string>([]); // use Set to avoid duplicates | ||
const columnMatches = statement.match(/\(([\w\s,]+)\)/); | ||
if (columnMatches) { | ||
const columnNames = columnMatches[1].split(","); | ||
for (const columnName of columnNames) { | ||
columns.add(columnName.trim().split(/[ ]+/)[0]); | ||
} | ||
} | ||
tables.set(table, Array.from(columns)); | ||
const lines = statement.trim().split("\n"); | ||
const table = parseCreateTable(lines[0]); | ||
// console.log(table, lines); | ||
if ( !table ) continue; | ||
const columns = new Set<string>(); | ||
for ( const line of lines) { | ||
const column = parseColumn(line); | ||
if (column) columns.add(column); | ||
} | ||
tables.set(table, Array.from(columns)); | ||
} | ||
return tables; | ||
} | ||
|
||
// must match the following statements: | ||
// CREATE TABLE block_meta | ||
// CREATE TABLE block_meta ( | ||
// create table block meta | ||
// CREATE TABLE IF NOT EXISTS block_meta | ||
export function parseCreateTable(statement: string) { | ||
const match = statement.match(/^CREATE TABLE/i); | ||
if (match) { | ||
statement = statement.replace("(", "").trim(); | ||
return statement.split(" ").reverse()[0].trim(); | ||
} | ||
return ''; | ||
} | ||
|
||
// must match the following statements: | ||
// id INTEGER PRIMARY KEY, | ||
// parent_hash TEXT, | ||
// timestamp INTEGER | ||
export function parseColumn(statement: string) { | ||
statement = statement.trim().replace(/[,;]/g, ''); // remove trailing comma or semicolon | ||
statement = statement.replace(/[\"\']/g, ''); // remove quotes | ||
if ( statement.match(/^CREATE TABLE/i) ) return '' // ignore table name | ||
if ( statement.match(/^PRIMARY KEY/i) ) return '' // ignore primary key as valid column | ||
if ( statement.match(/^\)/) ) return '' // ignore closing parenthesis | ||
if ( statement.match(/^\s*$/) ) return '' // ignore empty lines | ||
if ( statement.match(/^CONSTRAINT/i) ) return '' // ignore constraints | ||
const words = statement.split(" "); | ||
if ( words.length > 1) { | ||
return words[0].trim(); | ||
} | ||
return ''; | ||
} |