SHARE
TWEET

Untitled

a guest Sep 11th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:async';
  2.  
  3. import 'package:path/path.dart';
  4. import 'package:sqflite/sqflite.dart';
  5.  
  6. final String transacaoTable = "transacaoTable";
  7. final String idColumn = "idColumn";
  8. final String valorColumn = "valorColumn";
  9. final String dataColumn = "dataColumn";
  10. final String descricaoColumn = "descricaoColumn";
  11. final String tipoColumn = "tipoColumn";
  12.  
  13. class TransacaoHelper {
  14.  
  15.   static final TransacaoHelper _instance = TransacaoHelper.internal();
  16.  
  17.   factory TransacaoHelper() => _instance;
  18.  
  19.   TransacaoHelper.internal();
  20.  
  21.   Database _db;
  22.  
  23.   Future<Database> get db async {
  24.     if(_db != null){
  25.       return _db;
  26.     } else {
  27.       _db = await initDb();
  28.       return _db;
  29.     }
  30.   }
  31.  
  32.   Future<Database> initDb() async {
  33.     final databasesPath = await getDatabasesPath();
  34.     final path = join(databasesPath, "transacaos.db");
  35.  
  36.     return await openDatabase(path, version: 1, onCreate: (Database db, int newerVersion) async {
  37.       await db.execute(
  38.         "CREATE TABLE $transacaoTable($idColumn INTEGER PRIMARY KEY, $valorColumn REAL, $descricaoColumn TEXT,"
  39.             "$tipoColumn TEXT, $dataColumn TEXT)"
  40.       );
  41.     });
  42.   }
  43.  
  44.   Future<Transacao> savetransacao(Transacao transacao) async {
  45.     Database dbTransacao = await db;
  46.     transacao.id = await dbTransacao.insert(transacaoTable, transacao.toMap());
  47.     return transacao;
  48.   }
  49.  
  50.   Future<Transacao> gettransacao(int id) async {
  51.     Database dbTransacao = await db;
  52.     List<Map> maps = await dbTransacao.query(transacaoTable,
  53.       columns: [idColumn, valorColumn, descricaoColumn, tipoColumn, dataColumn],
  54.       where: "$idColumn = ?",
  55.       whereArgs: [id]);
  56.     if(maps.length > 0){
  57.       return Transacao.fromMap(maps.first);
  58.     } else {
  59.       return null;
  60.     }
  61.   }
  62.  
  63.   Future<int> deletetransacao(int id) async {
  64.     Database dbTransacao = await db;
  65.     return await dbTransacao.delete(transacaoTable, where: "$idColumn = ?", whereArgs: [id]);
  66.   }
  67.  
  68.   Future<int> updatetransacao(Transacao transacao) async {
  69.     Database dbTransacao = await db;
  70.     return await dbTransacao.update(transacaoTable,
  71.         transacao.toMap(),
  72.         where: "$idColumn = ?",
  73.         whereArgs: [transacao.id]);
  74.   }
  75.  
  76.   Future<List> getAlltransacaos() async {
  77.     Database dbTransacao = await db;
  78.     List listMap = await dbTransacao.rawQuery("SELECT * FROM $transacaoTable");
  79.     List<Transacao> listtransacao = List();
  80.     for(Map m in listMap){
  81.       listtransacao.add(Transacao.fromMap(m));
  82.     }
  83.     return listtransacao;
  84.   }
  85.  
  86.   Future<int> getNumber() async {
  87.     Database dbTransacao = await db;
  88.     return Sqflite.firstIntValue(await dbTransacao.rawQuery("SELECT COUNT(*) FROM $transacaoTable"));
  89.   }
  90.  
  91.   Future close() async {
  92.     Database dbTransacao = await db;
  93.     dbTransacao.close();
  94.   }
  95.  
  96. }
  97.  
  98. class Transacao {
  99.  
  100.   int id;
  101.   String valor;
  102.   String data;
  103.   String tipo;
  104.   String descricao;
  105.  
  106.   Transacao();
  107.  
  108.   Transacao.fromMap(Map map){
  109.     id = map[idColumn];
  110.     valor = map[valorColumn];
  111.     data = map[dataColumn];
  112.     descricao = map[descricaoColumn];
  113.     tipo = map[tipoColumn];
  114.   }
  115.  
  116.   Map toMap() {
  117.     Map<String, dynamic> map = {
  118.       valorColumn: valor,
  119.       dataColumn: data,
  120.       descricaoColumn: descricao,
  121.       tipoColumn: tipo
  122.     };
  123.     if(id != null){
  124.       map[idColumn] = id;
  125.     }
  126.     return map;
  127.   }
  128.  
  129.   @override
  130.   String toString() {
  131.     return "transacao(id: $id, valor: $valor, data: $data, descricao: $descricao, tipo: $tipo)";
  132.   }
  133.  
  134. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top