All files SQLite.ts

84.38% Statements 27/32
62.5% Branches 5/8
77.78% Functions 7/9
86.67% Lines 26/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105          1x                   5x       1x       5x 5x                           1x             1x 1x     1x     1x         1x     1x 1x     1x       1x       1x     1x     1x 1x     1x 2x     2x           2x       1x     1x 1x        
import sqlite from 'react-native-sqlite-storage'
 
import SQLiteTransaction from './SQLiteTransaction'
import SQLiteResultSet from './SQLiteResultSet'
 
sqlite.enablePromise(true)
 
interface TransactionCallback {
	(tx: SQLiteTransaction): void
}
 
export default class SQLite {
	protected static db: sqlite.SQLiteDatabase
 
	static get databaseInstance() {
		return SQLite.db
	}
 
	static async openDatabase(params: sqlite.DatabaseParams) {
		SQLite.db = await sqlite.openDatabase(params)
	}
 
	static async query(statement: string, params?: any[]) {
		const [result] = await SQLite.db.executeSql(statement.trim(), params)
		return new SQLiteResultSet(result)
	}
 
	/**
	 * TODO: Terminar essa função
	 * @param callback
	 */
	static transaction(callback: TransactionCallback) {
		return SQLite.db.transaction(tx => {
			callback(new SQLiteTransaction(tx))
		})
	}
 
	static truncateTable(table: string) {
		return SQLite.query(
			`DELETE FROM ${table}; DELETE FROM sqlite_sequence where name='${table}';`
		)
	}
 
	static insert(table: string, item: object) {
		// Faz o join
		let columns = Object.keys(item).join(',')
		let values = Object.values(item).join("','")
 
		// Gera a query
		let query = `INSERT INTO ${table} (${columns}) VALUES ('${values}');`
 
		// Executa a query
		return SQLite.query(query)
	}
 
	static insertOrReplace(table: string, item: object) {
		// @ts-ignore
		const { id, ...rest } = item
 
		// Faz o join
		let columns = Object.keys(rest).join(',')
		let values = Object.values(rest).join("','")
 
		// Gera a query
		let query = `REPLACE INTO ${table} (id, ${columns}) VALUES (${id ||
			'null'},'${values}');`
 
		// Executa a query
		return SQLite.query(query)
	}
 
	static async insertMany(table: string, items: object[]) {
		Iif (!items) return
 
		// Faz o join
		let columns = Object.keys(items[0]).join(',')
 
		// Inicia a query de insert
		let queries = []
		let query = `INSERT INTO ${table} (${columns}) VALUES `
 
		// Itera os registros
		for (let i = 0; i < items.length; i++) {
			const item = items[i]
 
			// Se já tiver 500 registros, vai pra próxima query
			Iif (i && i % 500 === 0) {
				queries.push(query.slice(0, -1))
				query = `INSERT INTO ${table} (${columns}) VALUES `
			}
 
			// Insere um por um no banco
			query += "('" + Object.values(item).join("','") + "'),"
		}
 
		// Concatena a última query
		queries.push(query.slice(0, -1))
 
		// Executa as queries
		for (const stmt of queries) {
			SQLite.query(stmt)
		}
	}
}