Skip to content

Commit 60dfa8f

Browse files
committed
Successfully reading tables from spatialite database
1 parent 002d84c commit 60dfa8f

1 file changed

Lines changed: 48 additions & 66 deletions

File tree

src/plugins/aequilibrae/AequilibraEReader.vue

Lines changed: 48 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ import AequilibraEFileSystem from '@/plugins/aequilibrae/AequilibraEFileSystem'
3232
3333
import { FileSystemConfig, UI_FONT, BG_COLOR_DASHBOARD } from '@/Globals'
3434
35-
import initSqlJs from 'sql.js'
36-
import type { Database } from 'sql.js'
35+
import SPL from 'spl.js'
3736
import YAML from 'yaml'
3837
3938
const MyComponent = defineComponent({
@@ -55,8 +54,8 @@ const MyComponent = defineComponent({
5554
loadingText: '',
5655
id: `id-${Math.floor(1e12 * Math.random())}` as any,
5756
aeqFileSystem: null as any,
58-
SQL: null as any,
59-
db: null as Database | null,
57+
spl: null as any,
58+
db: null as any,
6059
tables: [] as Array<{name: string, type: string, rowCount: number, columns: any[]}>,
6160
searchTerm: '',
6261
isLoaded: false,
@@ -90,32 +89,39 @@ const MyComponent = defineComponent({
9089
// only continue if we are on a real page and not the file browser
9190
if (this.thumbnail) return
9291
93-
// Initialize sql.js
94-
if (!this.SQL) {
95-
this.SQL = await initSqlJs({
96-
locateFile: (file: string) => `https://sql.js.org/dist/${file}`,
97-
})
92+
// Initialize spl.js with spatialite support
93+
if (!this.spl) {
94+
this.loadingText = 'Loading SQL engine with spatialite...'
95+
this.spl = await SPL()
96+
console.log('SPL engine initialized with spatialite')
9897
}
9998
10099
// Load the database
101100
this.loadingText = 'Loading database...'
101+
console.log(this.loadingText)
102102
const dbPath = this.vizDetails.database
103-
this.db = await this.loadDatabase(dbPath)
104-
103+
await this.loadDatabase(dbPath)
104+
105105
// Get table information
106106
this.loadingText = 'Reading tables...'
107-
const tableNames = this.getTableNames(this.db)
107+
console.log(this.loadingText)
108+
const tableNames = await this.getTableNames(this.db)
108109
109110
for (const tableName of tableNames) {
110-
const schema = this.getTableSchema(this.db, tableName)
111-
const rowCount = this.getRowCount(this.db, tableName)
112-
113-
this.tables.push({
114-
name: tableName,
115-
type: 'table',
116-
rowCount,
117-
columns: schema,
118-
})
111+
const allowedTables = ['nodes', 'links', 'zones']
112+
if (allowedTables.includes(tableName.toLowerCase())) {
113+
console.log(tableName)
114+
115+
const schema = await this.getTableSchema(this.db, tableName)
116+
const rowCount = await this.getRowCount(this.db, tableName)
117+
118+
this.tables.push({
119+
name: tableName,
120+
type: 'table',
121+
rowCount,
122+
columns: schema,
123+
})
124+
}
119125
}
120126
121127
this.isLoaded = true
@@ -175,66 +181,42 @@ const MyComponent = defineComponent({
175181
}
176182
},
177183
178-
async loadDatabase(filepath: string): Promise<Database> {
184+
async loadDatabase(filepath: string): Promise<void> {
179185
console.log('Loading database from:', filepath)
180186
181-
// Fetch database file as blob
182187
const blob = await this.aeqFileSystem.getFileBlob(filepath)
183-
console.log('Blob size:', blob.size, 'type:', blob.type)
184-
185188
const arrayBuffer = await blob.arrayBuffer()
186-
console.log('ArrayBuffer size:', arrayBuffer.byteLength)
187-
188-
const uint8Array = new Uint8Array(arrayBuffer)
189-
190-
// Check SQLite magic header (should be "SQLite format 3\0")
191-
const header = new TextDecoder().decode(uint8Array.slice(0, 16))
192-
console.log('File header:', header)
193-
194-
if (!header.startsWith('SQLite format 3')) {
195-
throw new Error(`Not a valid SQLite file. Header: ${header}`)
196-
}
197-
198-
// Create database instance
199-
const db = new this.SQL!.Database(uint8Array)
200-
return db
201-
},
202189
203-
getTableNames(db: Database): string[] {
204-
if (!db) throw new Error('Database not loaded')
190+
const spl = await SPL();
191+
const db = spl.db(arrayBuffer)
205192
206-
const res = db.exec(`
207-
SELECT name FROM sqlite_master
208-
WHERE type='table'
209-
AND name NOT LIKE 'sqlite_%'
210-
AND name NOT LIKE 'geometry_columns%'
211-
AND name NOT LIKE 'spatial_ref_sys%'
212-
AND name NOT LIKE 'idx_%'
213-
ORDER BY name
214-
`)
215-
216-
return res.length > 0 ? res[0].values.flat() as string[] : []
193+
this.db = db
217194
},
218195
219-
getTableSchema(db: Database, tableName: string): { name: string; type: string; nullable: boolean }[] {
196+
async getTableNames(db: any): Promise<string[]> {
197+
if (!db) throw new Error('Database not loaded')
198+
const result = await db.exec(`SELECT name FROM sqlite_master;`).get.objs
199+
200+
return result.map((row: any) => row.name)
201+
},
202+
203+
async getTableSchema(db: any, tableName: string): Promise<{ name: string; type: string; nullable: boolean }[]> {
220204
if (!db) throw new Error('Database not loaded')
221205
222-
const res = db.exec(`PRAGMA table_info("${tableName}");`)
206+
const result = await db.exec(`PRAGMA table_info("${tableName}");`).get.objs
223207
224-
return res.length > 0
225-
? res[0].values.map((row: any[]) => ({
226-
name: row[1],
227-
type: row[2],
228-
nullable: row[3] === 0,
229-
}))
230-
: []
208+
return result.map((row: any) => ({
209+
name: row.name,
210+
type: row.type,
211+
nullable: row.notnull === 0,
212+
}))
231213
},
232214
233-
getRowCount(db: Database, tableName: string): number {
215+
async getRowCount(db: any, tableName: string): Promise<number> {
234216
if (!db) throw new Error('Database not loaded')
235217
236-
const res = db.exec(`SELECT COUNT(*) FROM "${tableName}";`)
237-
return res.length > 0 ? res[0].values[0][0] as number : 0
218+
const result = await db.exec(`SELECT COUNT(*) as count FROM "${tableName}";`).get.objs
219+
return result.length > 0 ? result[0].count : 0
238220
},
239221
240222

0 commit comments

Comments
 (0)