Skip to content

Commit

Permalink
Add Database.exec #12
Browse files Browse the repository at this point in the history
  • Loading branch information
gionatamettifogo committed Nov 8, 2023
1 parent 2dc81fe commit 9f926d8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ export class Database {
return this
}

/**
* Runs all SQL queries in the supplied string. No result rows are retrieved.
* The function returns the Database object to allow for function chaining.
* If a query fails, no subsequent statements will be executed (wrap it in a
* transaction if you want all or none to be executed). When all statements
* have been executed successfully, or when an error occurs, the callback
* function is called, with the first parameter being either null or an error
* object. When no callback is provided and an error occurs, an error event
* will be emitted on the database object.
*/
public exec(sql: string, callback?: ErrorCallback): this {
void this.getConnection()
.sendCommands(sql)
.then(() => {
callback?.call(this, null)
})
.catch(error => {
callback?.call(this, error)
})
return this
}

/** Close database connections then callback */
public close(callback?: ErrorCallback): void {
const closingPromises = this._connections.map(connection =>
Expand Down
34 changes: 34 additions & 0 deletions test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,38 @@ describe('Database', () => {
})
})
})

describe('exec', () => {
it('execute set client key statement', done => {
const db = new Database(testConfig, null, () => {
db.exec('SET CLIENT KEY COMPRESSION TO 1;', error => {
expect(error).toBeNull()

db.close(error => {
expect(error).toBeNull()
done()
})
})
})
})

it('execute statement with errors', done => {
const db = new Database(testConfig, null, () => {
db.exec('SET BOGUS STATEMENT TO 1;', error => {
expect(error).toMatchObject({
errorCode: '10002',
externalErrorCode: '0',
name: 'SQLiteCloudError',
offsetCode: -1,
message: 'Unable to find command SET BOGUS STATEMENT TO 1;'
})

db.close(error => {
expect(error).toBeNull()
done()
})
})
})
})
})
})

0 comments on commit 9f926d8

Please sign in to comment.