Advertisement
joaopaulofcc

[SC] [DBAPI] databaseClient.dart

Nov 25th, 2020
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.71 KB | None | 0 0
  1. import 'dart:async';
  2. import 'package:path/path.dart';
  3. import 'package:sqflite/sqflite.dart';
  4. import '../models/dog.dart';
  5.  
  6. class DatabaseClient {
  7.   Database _db;
  8.  
  9.   Future open() async {
  10.     try {
  11.       String dbPath = join(await getDatabasesPath(), 'database.db');
  12.       _db = await openDatabase(dbPath, version: 1, onCreate: this._create);
  13.     }
  14.     catch (error) {
  15.       throw Exception('Falha ao abrir o BD!');
  16.     }
  17.   }
  18.  
  19.   Future _create(Database db, int version) async {
  20.     try {
  21.       await db.execute("""
  22.            CREATE TABLE dogs (
  23.              id TEXT PRIMARY KEY,
  24.              name TEXT,
  25.              age INTEGER
  26.            )""");
  27.     }
  28.     catch (error) {
  29.       throw Exception('Falha ao criar tabela no BD!');
  30.     }
  31.   }
  32.  
  33.   Future insertDogs(List<Dog> dogs) async {
  34.     try {
  35.       await Future.forEach(dogs, (dog) async {
  36.         await _db.insert('dogs', dog.toMap(),
  37.             conflictAlgorithm: ConflictAlgorithm.replace);
  38.       });
  39.     }
  40.     catch (error) {
  41.       throw Exception('Falha ao inserir lista de dados no BD!');
  42.     }
  43.   }
  44.  
  45.   Future insertDog(Dog dog) async {
  46.     try {
  47.       await _db.insert('dogs', dog.toMap(),
  48.           conflictAlgorithm: ConflictAlgorithm.replace);
  49.     }
  50.     catch (error) {
  51.       throw Exception('Falha ao inserir dado no BD!');
  52.     }
  53.   }
  54.  
  55.   Future<Dog> getDog(String id) async {
  56.     try {
  57.       final List<Map<String, dynamic>> maps =
  58.           await _db.query('dogs', where: "id = ?", whereArgs: [id], limit: 1);
  59.  
  60.       if (maps.length > 0) {
  61.         return Dog.fromMap(maps[0]);
  62.       }
  63.       else {
  64.         return null;
  65.       }
  66.     }
  67.     catch (error) {
  68.       throw Exception('Falha ao obter dado no BD!');
  69.     }
  70.   }
  71.  
  72.   Future<List<Dog>> getDogs() async {
  73.     try {
  74.       final List<Map<String, dynamic>> maps = await _db.query('dogs');
  75.       return List.generate(maps.length, (i) {
  76.         return Dog.fromMap(maps[i]);
  77.       });
  78.     }
  79.     catch (error) {
  80.       throw Exception('Falha ao obter lista de dados no BD!');
  81.     }
  82.   }
  83.  
  84.   Future updateDog(Dog dog) async {
  85.     try {
  86.       _db.update(
  87.         'dogs',
  88.         dog.toMap(),
  89.         where: "id = ?",
  90.         whereArgs: [dog.id],
  91.       );
  92.     }
  93.     catch (error) {
  94.       throw Exception('Falha ao atualizar dado no BD!');
  95.     }
  96.   }
  97.  
  98.   Future deleteDog(String id) async {
  99.     try {
  100.       await _db.delete('dogs', where: "id = ?", whereArgs: [id]);
  101.     }
  102.     catch (error) {
  103.       throw Exception('Falha ao deletar dado no BD!');
  104.     }
  105.   }
  106.  
  107.   Future deleteAllDogs() async {
  108.     try {
  109.       await _db.execute("""DELETE FROM dogs""");
  110.     }
  111.     catch (error) {
  112.       throw Exception('Falha ao deletar todos os dados do BD!');
  113.     }
  114.   }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement