Skip to content

Commit de74ee5

Browse files
feat: add ability to define the size of the newly created db
1 parent 3e73897 commit de74ee5

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/api.coffee

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,19 @@ class Database
237237
# Open a new database either by creating a new one or opening an existing one,
238238
# stored in the byte array passed in first argument
239239
# @param data [Array<Integer>] An array of bytes representing an SQLite database file
240-
constructor: (data) ->
240+
# @param initialDbSize [Integer] Size to which the database node will be expanded
241+
constructor: (data, initialDbSize) ->
241242
@filename = 'dbfile_' + (0xffffffff*Math.random()>>>0)
242243
if data? then FS.createDataFile '/', @filename, data, true, true
243244
@handleError sqlite3_open @filename, apiTemp
244245
@db = getValue(apiTemp, 'i32')
245246
RegisterExtensionFunctions(@db)
247+
if initialDbSize?
248+
@run 'VACUUM' # Called so the node will have the correct database structure
249+
parent = FS.lookupPath '/'
250+
parentNode = parent.node
251+
dbNode = FS.lookupNode parentNode, @filename
252+
MEMFS.expandFileStorage dbNode, initialDbSize
246253
@statements = {} # A list of all prepared statements of the database
247254
@functions = {} # A list of all user function of the database (created by create_function call)
248255

@@ -315,7 +322,7 @@ class Database
315322

316323
# Store the SQL string in memory. The string will be consumed, one statement
317324
# at a time, by sqlite3_prepare_v2_sqlptr.
318-
# Note that if we want to allocate as much memory as could _possibly_ be used, we can
325+
# Note that if we want to allocate as much memory as could _possibly_ be used, we can
319326
# we allocate bytes equal to 4* the number of chars in the sql string.
320327
# It would be faster, but this is probably a premature optimization
321328
nextSqlPtr = allocateUTF8OnStack(sql)
@@ -480,7 +487,7 @@ class Database
480487
sqlite3_result_error(cx,error,-1)
481488
return
482489

483-
# Return the result of the user defined function to SQLite
490+
# Return the result of the user defined function to SQLite
484491
switch typeof(result)
485492
when 'boolean' then sqlite3_result_int(cx,if result then 1 else 0)
486493
when 'number' then sqlite3_result_double(cx, result)

0 commit comments

Comments
 (0)