1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2025-12-08 02:58:54 +08:00

Pivot implementation and redesign (#69)

- Pivot support implementation 
- Rename queries into inquiries
- Rename editor into workspace
- Change result set format
- New JSON format for inquiries
- Redesign panels
This commit is contained in:
lana-k
2021-08-04 22:20:51 +02:00
committed by GitHub
parent 8d0bc6affe
commit 5017b55944
105 changed files with 4659 additions and 2021 deletions

View File

@@ -34,10 +34,11 @@ describe('_sql.js', () => {
sql.open(data)
const result = sql.exec('SELECT * from test')
expect(result).to.have.lengthOf(1)
expect(result[0].columns).to.eql(['id', 'name', 'faculty'])
expect(result[0].values).to.have.lengthOf(2)
expect(result[0].values[0]).to.eql([1, 'Harry Potter', 'Griffindor'])
expect(result[0].values[1]).to.eql([2, 'Draco Malfoy', 'Slytherin'])
expect(result[0]).to.eql({
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: ['Griffindor', 'Slytherin']
})
})
it('throws an error if query is empty', async () => {
@@ -63,26 +64,21 @@ describe('_sql.js', () => {
it('imports', async () => {
const data = {
columns: ['id', 'name'],
values: [
[1, 'Harry Potter'],
[2, 'Draco Malfoy'],
[3, 'Hermione Granger'],
[4, 'Ron Weasley']
id: [1, 2, 3, 4],
name: [
'Harry Potter',
'Draco Malfoy',
'Hermione Granger',
'Ron Weasley'
]
}
const progressCallback = sinon.stub()
const progressCounterId = 1
const sql = await Sql.build()
sql.import('foo', data.columns, data.values, progressCounterId, progressCallback, 2)
sql.import('foo', data, progressCounterId, progressCallback, 2)
const result = sql.exec('SELECT * from foo')
expect(result).to.have.lengthOf(1)
expect(result[0].columns).to.eql(['id', 'name'])
expect(result[0].values).to.have.lengthOf(4)
expect(result[0].values[0]).to.eql([1, 'Harry Potter'])
expect(result[0].values[1]).to.eql([2, 'Draco Malfoy'])
expect(result[0].values[2]).to.eql([3, 'Hermione Granger'])
expect(result[0].values[3]).to.eql([4, 'Ron Weasley'])
expect(result[0]).to.eql(data)
expect(progressCallback.calledThrice).to.equal(true)
expect(progressCallback.getCall(0).args[0]).to.eql({ progress: 0, id: 1 })
@@ -108,10 +104,11 @@ describe('_sql.js', () => {
anotherSql.open(data)
const result = anotherSql.exec('SELECT * from test')
expect(result).to.have.lengthOf(1)
expect(result[0].columns).to.eql(['id', 'name', 'faculty'])
expect(result[0].values).to.have.lengthOf(2)
expect(result[0].values[0]).to.eql([1, 'Harry Potter', 'Griffindor'])
expect(result[0].values[1]).to.eql([2, 'Draco Malfoy', 'Slytherin'])
expect(result[0]).to.eql({
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: ['Griffindor', 'Slytherin']
})
})
it('closes', async () => {
@@ -149,22 +146,28 @@ describe('_sql.js', () => {
`)
let result = sql.exec('SELECT * from test')
expect(result[0].values).to.have.lengthOf(2)
expect(result[0]).to.eql({
id: [1, 2],
name: ['foo', 'bar']
})
const data = {
columns: ['id', 'name'],
values: [
[1, 'Harry Potter'],
[2, 'Draco Malfoy'],
[3, 'Hermione Granger'],
[4, 'Ron Weasley']
id: [1, 2, 3, 4],
name: [
'Harry Potter',
'Draco Malfoy',
'Hermione Granger',
'Ron Weasley'
]
}
// import adds table
sql.import('foo', data.columns, data.values, 1, sinon.stub(), 2)
sql.import('foo', data, 1, sinon.stub(), 2)
result = sql.exec('SELECT * from foo')
expect(result[0].values).to.have.lengthOf(4)
expect(result[0]).to.eql(data)
result = sql.exec('SELECT * from test')
expect(result[0].values).to.have.lengthOf(2)
expect(result[0]).to.eql({
id: [1, 2],
name: ['foo', 'bar']
})
})
})

View File

@@ -3,16 +3,18 @@ import stmts from '@/lib/database/_statements'
describe('_statements.js', () => {
it('generateChunks', () => {
const arr = ['1', '2', '3', '4', '5']
const source = {
id: ['1', '2', '3', '4', '5']
}
const size = 2
const chunks = stmts.generateChunks(arr, size)
const chunks = stmts.generateChunks(source, size)
const output = []
for (const chunk of chunks) {
output.push(chunk)
}
expect(output[0]).to.eql(['1', '2'])
expect(output[1]).to.eql(['3', '4'])
expect(output[2]).to.eql(['5'])
expect(output[0]).to.eql([['1'], ['2']])
expect(output[1]).to.eql([['3'], ['4']])
expect(output[2]).to.eql([['5']])
})
it('getInsertStmt', () => {
@@ -22,12 +24,14 @@ describe('_statements.js', () => {
})
it('getCreateStatement', () => {
const columns = ['id', 'name', 'isAdmin', 'startDate']
const values = [
[1, 'foo', true, new Date()],
[2, 'bar', false, new Date()]
]
expect(stmts.getCreateStatement('foo', columns, values)).to.equal(
const data = {
id: [1, 2],
name: ['foo', 'bar'],
isAdmin: [true, false],
startDate: [new Date(), new Date()]
}
expect(stmts.getCreateStatement('foo', data)).to.equal(
'CREATE table "foo"("id" REAL, "name" TEXT, "isAdmin" INTEGER, "startDate" TEXT);'
)
})

View File

@@ -25,10 +25,7 @@ describe('database.js', () => {
it('creates schema', async () => {
const SQL = await getSQL
const tempDb = new SQL.Database()
tempDb.run(`CREATE TABLE test (
col1,
col2 integer
)`)
tempDb.run('CREATE TABLE test (col1, col2 integer)')
const data = tempDb.export()
const buffer = new Blob([data])
@@ -88,11 +85,11 @@ describe('database.js', () => {
await db.loadDb(buffer)
const result = await db.execute('SELECT * from test limit 1; SELECT * from test;')
expect(result.columns).to.have.lengthOf(3)
expect(result.columns).to.eql(['id', 'name', 'faculty'])
expect(result.values).to.have.lengthOf(2)
expect(result.values[0]).to.eql([1, 'Harry Potter', 'Griffindor'])
expect(result.values[1]).to.eql([2, 'Draco Malfoy', 'Slytherin'])
expect(result).to.eql({
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: ['Griffindor', 'Slytherin']
})
})
it('returns an error', async () => {
@@ -119,11 +116,9 @@ describe('database.js', () => {
it('adds table from csv', async () => {
const data = {
columns: ['id', 'name', 'faculty'],
values: [
[1, 'Harry Potter', 'Griffindor'],
[2, 'Draco Malfoy', 'Slytherin']
]
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: ['Griffindor', 'Slytherin']
}
const progressHandler = sinon.spy()
const progressCounterId = db.createProgressCounter(progressHandler)
@@ -140,8 +135,7 @@ describe('database.js', () => {
expect(db.schema[0].columns[2]).to.eql({ name: 'faculty', type: 'text' })
const result = await db.execute('SELECT * from foo')
expect(result.columns).to.eql(data.columns)
expect(result.values).to.eql(data.values)
expect(result).to.eql(data)
expect(progressHandler.calledTwice).to.equal(true)
expect(progressHandler.firstCall.calledWith(0)).to.equal(true)
@@ -150,16 +144,13 @@ describe('database.js', () => {
it('addTableFromCsv throws errors', async () => {
const data = {
columns: ['id', 'name'],
values: [
[1, 'Harry Potter', 'Griffindor'],
[2, 'Draco Malfoy', 'Slytherin']
]
id: [1, 2],
name: ['Harry Potter', 'Draco Malfoy'],
faculty: null
}
const progressHandler = sinon.stub()
const progressCounterId = db.createProgressCounter(progressHandler)
await expect(db.addTableFromCsv('foo', data, progressCounterId))
.to.be.rejectedWith('column index out of range')
await expect(db.addTableFromCsv('foo', data, progressCounterId)).to.be.rejected
})
it('progressCounters', () => {
@@ -222,9 +213,10 @@ describe('database.js', () => {
// check that new db works and has the same table and data
result = await anotherDb.execute('SELECT * from foo')
expect(result.columns).to.eql(['id', 'name'])
expect(result.values).to.have.lengthOf(1)
expect(result.values[0]).to.eql([1, 'Harry Potter'])
expect(result).to.eql({
id: [1],
name: ['Harry Potter']
})
})
it('sanitizeTableName', () => {

View File

@@ -36,7 +36,27 @@ describe('SQLite extensions', function () {
abs(pi() - radians(180)) < 0.000001,
abs(pi() / 2 - atan2(1, 0)) < 0.000001
`)
expect(actual.values).to.eql([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
expect(actual).to.eql({
'abs(3.1415926 - pi()) < 0.000001': [1],
'abs(1 - cos(2 * pi())) < 0.000001': [1],
'abs(0 - sin(pi())) < 0.000001': [1],
'abs(0 - tan(0)) < 0.000001': [1],
'abs(0 - cot(pi() / 2)) < 0.000001': [1],
'abs(1 - acos(cos(1))) < 0.000001': [1],
'abs(1 - asin(sin(1))) < 0.000001': [1],
'abs(1 - atan(tan(1))) < 0.000001': [1],
'abs(1 - cosh(0)) < 0.000001': [1],
'abs(0 - sinh(0)) < 0.000001': [1],
'abs(tanh(1) + tanh(-1)) < 0.000001': [1],
'abs(coth(1) + coth(-1)) < 0.000001': [1],
'abs(1 - acosh(cosh(1))) < 0.000001': [1],
'abs(1 - asinh(sinh(1))) < 0.000001': [1],
'abs(1 - atanh(tanh(1))) < 0.000001': [1],
'abs(180 - degrees(pi())) < 0.000001': [1],
'abs(pi() - radians(180)) < 0.000001': [1],
'abs(pi() / 2 - atan2(1, 0)) < 0.000001': [1]
})
})
it('supports contrib math functions', async function () {
@@ -51,7 +71,17 @@ describe('SQLite extensions', function () {
ceil(-1.95) + ceil(1.95),
floor(-1.95) + floor(1.95)
`)
expect(actual.values).to.eql([[1, 1, 4, 8, 0, 16, 1, -1]])
expect(actual).to.eql({
'exp(0)': [1],
'log(exp(1))': [1],
'log10(10000)': [4],
'power(2, 3)': [8],
'sign(-10) + sign(20)': [0],
'sqrt(square(16))': [16],
'ceil(-1.95) + ceil(1.95)': [1],
'floor(-1.95) + floor(1.95)': [-1]
})
})
it('supports contrib string functions', async function () {
@@ -69,9 +99,19 @@ describe('SQLite extensions', function () {
padc('foo', 5),
strfilter('abcba', 'bc')
`)
expect(actual.values).to.eql([
['abababab', 7, 0, 'fo', 'ar', 'raboof', 'Foobar', ' foo', 'foo ', ' foo ', 'bcb']
])
expect(actual).to.eql({
"replicate('ab', 4)": ['abababab'],
"charindex('ab', 'foobarabbarfoo')": [7],
"charindex('ab', 'foobarabbarfoo', 8)": [0],
"leftstr('foobar', 2)": ['fo'],
"rightstr('foobar', 2)": ['ar'],
"reverse('foobar')": ['raboof'],
"proper('fooBar')": ['Foobar'],
"padl('foo', 5)": [' foo'],
"padr('foo', 5)": ['foo '],
"padc('foo', 5)": [' foo '],
"strfilter('abcba', 'bc')": ['bcb']
})
})
it('supports contrib aggregate functions', async function () {
@@ -97,7 +137,14 @@ describe('SQLite extensions', function () {
VALUES (1)
)
`)
expect(actual.values).to.eql([[1, 1, 1, 6, 3, 9]])
expect(actual).to.eql({
'abs( 3.77406806 - stdev(x)) < 0.000001': [1],
'abs(14.24358974 - variance(x)) < 0.000001': [1],
'mode(x)': [1],
'median(x)': [6],
'lower_quartile(x)': [3],
'upper_quartile(x)': [9]
})
})
it('supports generate_series', async function () {
@@ -105,7 +152,9 @@ describe('SQLite extensions', function () {
SELECT value
FROM generate_series(5, 20, 5)
`)
expect(actual.values).to.eql([[5], [10], [15], [20]])
expect(actual).to.eql({
value: [5, 10, 15, 20]
})
})
it('supports transitive_closure', async function () {
@@ -145,33 +194,42 @@ describe('SQLite extensions', function () {
WHERE nc.root = 2 AND nc.depth = 2
);
`)
expect(actual.values).to.eql([
['_sql.spec.js'],
['_statements.spec.js'],
['database.spec.js'],
['sqliteExtensions.spec.js'],
['fileIo.spec.js'],
['time.spec.js']
])
expect(actual).to.eql({
name: [
'_sql.spec.js',
'_statements.spec.js',
'database.spec.js',
'sqliteExtensions.spec.js',
'fileIo.spec.js',
'time.spec.js'
]
})
})
it('supports UUID functions', async function () {
const actual = await db.execute(`
SELECT
length(uuid()),
uuid_str(uuid_blob('26a8349c8a7f4cbeb519bf792c3d7ac6'))
length(uuid()) as length,
uuid_str(uuid_blob('26a8349c8a7f4cbeb519bf792c3d7ac6')) as uid
`)
expect(actual.values).to.eql([[36, '26a8349c-8a7f-4cbe-b519-bf792c3d7ac6']])
expect(actual).to.eql({
length: [36],
uid: ['26a8349c-8a7f-4cbe-b519-bf792c3d7ac6']
})
})
it('supports regexp', async function () {
const actual = await db.execute(`
SELECT
regexp('=\\s?\\d+', 'const foo = 123; const bar = "bar"'),
regexpi('=\\s?\\d+', 'const foo = 123; const bar = "bar"'),
'const foo = 123; const bar = "bar"' REGEXP '=\\s?\\d+'
regexp('=\\s?\\d+', 'const foo = 123; const bar = "bar"') as one,
regexpi('=\\s?\\d+', 'const foo = 123; const bar = "bar"') as two,
'const foo = 123; const bar = "bar"' REGEXP '=\\s?\\d+' as three
`)
expect(actual.values).to.eql([[1, 1, 1]])
expect(actual).to.eql({
one: [1],
two: [1],
three: [1]
})
})
it('supports pivot virtual table', async function () {
@@ -202,12 +260,13 @@ describe('SQLite extensions', function () {
ALTER TABLE surface DROP COLUMN rownum;
SELECT * FROM surface;
`)
expect(actual.columns).to.eql(['x', 'y', '5.0', '10.0', '15.0'])
expect(actual.values).to.eql([
[5, 3, 3.2, 4, 4.8],
[10, 6, 4.3, 3.8, 4],
[15, 9, 5.4, 3.6, 3.5]
])
expect(actual).to.eql({
x: [5, 10, 15],
y: [3, 6, 9],
'5.0': [3.2, 4.3, 5.4],
'10.0': [4, 3.8, 3.6],
'15.0': [4.8, 4, 3.5]
})
})
it('supports FTS5', async function () {
@@ -233,6 +292,8 @@ describe('SQLite extensions', function () {
WHERE body MATCH '"full-text" NOT document'
ORDER BY rank;
`)
expect(actual.values).to.eql([['bar@localhost']])
expect(actual).to.eql({
sender: ['bar@localhost']
})
})
})