Created
September 11, 2018 18:01
-
-
Save MatheusPimentel/739a5bd0d5eb2ca4a92fd9f3f7f696ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {SQLite} from "expo"; | |
import SQL from "./SQL"; | |
export default class Connection { | |
constructor() { | |
this._db = SQLite.openDatabase(SQL.NOME_BANCO); | |
} | |
static getInstance() { | |
let connection; | |
if (this.db === null) { | |
connection = new Connection() | |
} | |
return connection; | |
} | |
get db() { | |
return this._db; | |
} | |
} | |
// let db = null; | |
// | |
// function connectionDB () { | |
// if (db === null) { | |
// | |
// } | |
// } | |
// export default connectionDB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SQL from "../../database/SQL"; | |
import GenericDao from "./GenericDao"; | |
export default class EscolaDao extends GenericDao { | |
static save = async (escola) => { | |
if (escola !== null && escola._id === null){ | |
//salva a escola | |
//this.executeSql(SQL.SAVE_ESCOLA, [escola._inep, escola._nome, escola._nomeAbreviado]); | |
debugger; | |
this.getDb().transaction(tx => { | |
debugger; | |
tx.executeSql(SQL.SAVE_ESCOLA, [escola._inep, escola._nome, escola._nomeAbreviado], function (transaction, resultSet){ | |
debugger; | |
console.log(resultSet); | |
}, function (transaction, error){ | |
console.log(error); | |
debugger; | |
}); | |
}); | |
// database.close | |
} | |
} | |
static success(transaction, resultSet) { | |
if (resultSet.insertId !== null) { | |
console.log("Result SET", resultSet); | |
return resultSet; | |
} | |
} | |
static error(transaction, error) { | |
console.log("ERRO: ", error); | |
return error; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Connection from '../../database/Connection' | |
import {SQLite} from "expo"; | |
import SQL from "../../database/SQL"; | |
const db = SQLite.openDatabase(SQL.NOME_BANCO); | |
export default class GenericDao { | |
static executeSql = async (sql, params = []) => { | |
return new Promise((resolve, reject) => this.getDb().transaction(tx => { | |
tx.executeSql(sql, params, (_, {rows}) => resolve(rows._array), reject) | |
//todo melhorar este método com retorno de chamadas | |
})); | |
}; | |
static getDb() { | |
return db | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment