Skip to content

Commit

Permalink
Feature: Add support for ByteArrays/Blobs
Browse files Browse the repository at this point in the history
This connects the dart type Uint8List to the SQLite data type BLOB and adjusts some tests for that.
Fixes pinchbv#229.
  • Loading branch information
mqus committed Feb 22, 2020
1 parent 0acf397 commit 061a886
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
8 changes: 5 additions & 3 deletions floor/test/integration/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions floor/test/integration/database_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

import 'package:floor/floor.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:matcher/matcher.dart';
Expand Down Expand Up @@ -212,15 +214,15 @@ void main() {

group('foreign key', () {
test('foreign key constraint failed exception', () {
final dog = Dog(null, 'Peter', 'Pete', 2);
final dog = Dog(null, 'Peter', 'Pete', 2, Uint8List(8));

expect(() => dogDao.insertDog(dog), _throwsDatabaseException);
});

test('find dog for person', () async {
final person = Person(1, 'Simon');
await personDao.insertPerson(person);
final dog = Dog(2, 'Peter', 'Pete', person.id);
final dog = Dog(2, 'Peter', 'Pete', person.id, Uint8List(8));
await dogDao.insertDog(dog);

final actual = await dogDao.findDogForPersonId(person.id);
Expand All @@ -231,7 +233,7 @@ void main() {
test('cascade delete dog on deletion of person', () async {
final person = Person(1, 'Simon');
await personDao.insertPerson(person);
final dog = Dog(2, 'Peter', 'Pete', person.id);
final dog = Dog(2, 'Peter', 'Pete', person.id, Uint8List(8));
await dogDao.insertDog(dog);

await personDao.deletePerson(person);
Expand Down
10 changes: 8 additions & 2 deletions floor/test/integration/model/dog.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:typed_data';

import 'package:floor/floor.dart';

import '../../test_util/list_helper.dart';
import 'person.dart';

@Entity(
Expand All @@ -25,7 +28,9 @@ class Dog {
@ColumnInfo(name: 'owner_id')
final int ownerId;

Dog(this.id, this.name, this.nickName, this.ownerId);
final Uint8List picture;

Dog(this.id, this.name, this.nickName, this.ownerId, this.picture);

@override
bool operator ==(Object other) =>
Expand All @@ -35,6 +40,7 @@ class Dog {
id == other.id &&
name == other.name &&
nickName == other.nickName &&
ListHelper.deepEquals(picture, other.picture) &&
ownerId == other.ownerId;

@override
Expand All @@ -43,6 +49,6 @@ class Dog {

@override
String toString() {
return 'Dog{id: $id, name: $name, nickName: $nickName, ownerId: $ownerId}';
return 'Dog{id: $id, name: $name, nickName: $nickName, ownerId: $ownerId, picture: $picture}';
}
}
22 changes: 22 additions & 0 deletions floor/test/test_util/list_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dart:typed_data';

class ListHelper{
static bool deepEquals(final Uint8List l1,final Uint8List l2){
if(identical(l1,l2)){
return true;
}
if(l1 == null || l2 == null){
return false;
}
if(l1.length!=l2.length) {
return false;
}
for(int i=0; i < l1.length ; ++i) {
if (l1.elementAt(i) != l2.elementAt(i)) {
return false;
}
}
return true;
}
}

1 change: 1 addition & 0 deletions floor_generator/lib/misc/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class SqlType {
static const INTEGER = 'INTEGER';
static const TEXT = 'TEXT';
static const REAL = 'REAL';
static const BLOB = 'BLOB';
}

abstract class OnConflictStrategy {
Expand Down
2 changes: 2 additions & 0 deletions floor_generator/lib/processor/entity_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ class EntityProcessor extends Processor<Entity> {
return '$parameterValue as String';
} else if (parameterType.isDartCoreInt) {
return '$parameterValue as int';
} else if (parameterType.getDisplayString() == 'Uint8List') {
return '$parameterValue';
} else {
return '$parameterValue as double'; // must be double
}
Expand Down
2 changes: 2 additions & 0 deletions floor_generator/lib/processor/field_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class FieldProcessor extends Processor<Field> {
return SqlType.INTEGER;
} else if (type.isDartCoreDouble) {
return SqlType.REAL;
} else if (type.getDisplayString() == 'Uint8List') {
return SqlType.BLOB;
}
throw InvalidGenerationSourceError(
'Column type is not supported for $type.',
Expand Down

0 comments on commit 061a886

Please sign in to comment.