Skip to content

Commit

Permalink
Merge branch 'simolus3:develop' into refactor-manager-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dickermoshe committed May 3, 2024
2 parents 0170918 + 6ba049e commit f4a2887
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/encryption/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ android {
applicationId "com.example.drift_encryption_sample"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
minSdkVersion flutter.minSdkVersion
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
2 changes: 1 addition & 1 deletion examples/encryption/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
11 changes: 11 additions & 0 deletions examples/encryption/lib/database.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:ffi';
import 'dart:io';

import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:sqlcipher_flutter_libs/sqlcipher_flutter_libs.dart';
import 'package:sqlite3/open.dart';

part 'database.g.dart';

Expand Down Expand Up @@ -39,6 +42,14 @@ QueryExecutor _openDatabase() {

return NativeDatabase.createInBackground(
File(p.join(path.path, 'app.db.enc')),
isolateSetup: () async {
open
..overrideFor(OperatingSystem.android, openCipherOnAndroid)
..overrideFor(OperatingSystem.linux,
() => DynamicLibrary.open('libsqlcipher.so'))
..overrideFor(OperatingSystem.windows,
() => DynamicLibrary.open('sqlcipher.dll'));
},
setup: (db) {
// Check that we're actually running with SQLCipher by quering the
// cipher_version pragma.
Expand Down
11 changes: 0 additions & 11 deletions examples/encryption/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import 'dart:ffi';

import 'package:drift/drift.dart';
import 'package:flutter/material.dart';
import 'package:sqlite3/open.dart';
import 'package:sqlcipher_flutter_libs/sqlcipher_flutter_libs.dart';

import 'database.dart';

void main() {
open
..overrideFor(OperatingSystem.android, openCipherOnAndroid)
..overrideFor(
OperatingSystem.linux, () => DynamicLibrary.open('libsqlcipher.so'))
..overrideFor(
OperatingSystem.windows, () => DynamicLibrary.open('sqlcipher.dll'));

runApp(const MyApp());
}

Expand Down
8 changes: 8 additions & 0 deletions extras/drift_postgres/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.2.3

- Fix binding `BigInt` variables.

## 1.2.2

- Fix parameter binding when NULL is provided.

## 1.2.1

- Fix `isOpen` so that it properly returns false after the underlying postgres
Expand Down
24 changes: 22 additions & 2 deletions extras/drift_postgres/lib/src/pg_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,15 @@ class _BoundArguments {
final value = args[i];
return switch (value) {
TypedValue() => value,
null => TypedValue(Type.text, null),
int() || BigInt() => TypedValue(Type.bigInteger, value),
null => TypedValue(Type.unspecified, null),
int() => TypedValue(Type.bigInteger, value),
String() => TypedValue(Type.text, value),
bool() => TypedValue(Type.boolean, value),
double() => TypedValue(Type.double, value),
List<int>() => TypedValue(Type.byteArray, value),
// Drift's BigInts are also just 64bit, we just support them to
// represent large numbers on the web.
BigInt() => TypedValue(Type.bigInteger, value.rangeCheckedToInt()),
_ => throw ArgumentError.value(value, 'value', 'Unsupported type'),
};
},
Expand Down Expand Up @@ -215,3 +218,20 @@ class _PgVersionDelegate extends DynamicVersionDelegate {
);
}
}

extension on BigInt {
static final _bigIntMinValue64 = BigInt.from(-9223372036854775808);
static final _bigIntMaxValue64 = BigInt.from(9223372036854775807);

int rangeCheckedToInt() {
if (this < _bigIntMinValue64 || this > _bigIntMaxValue64) {
throw ArgumentError.value(
this,
'this',
'Should be in signed 64bit range ($_bigIntMinValue64..=$_bigIntMaxValue64)',
);
}

return toInt();
}
}
2 changes: 1 addition & 1 deletion extras/drift_postgres/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: drift_postgres
description: Postgres implementation and APIs for the drift database package.
version: 1.2.1
version: 1.2.3
repository: https://github.com/simolus3/drift
homepage: https://drift.simonbinder.eu/docs/platforms/postgres/
issue_tracker: https://github.com/simolus3/drift/issues
Expand Down
22 changes: 22 additions & 0 deletions extras/drift_postgres/test/drift_postgres_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:drift_postgres/drift_postgres.dart';
import 'package:drift_testcases/tests.dart';
import 'package:postgres/postgres.dart' as pg;
import 'package:test/test.dart';

class PgExecutor extends TestExecutor {
@override
Expand Down Expand Up @@ -37,4 +38,25 @@ class PgExecutor extends TestExecutor {

void main() {
runAllTests(PgExecutor());

// Regression for https://github.com/simolus3/drift/issues/2981
test('bind null to nullable column', () async {
final executor = PgExecutor();
final db = Database(executor.createConnection());

await db.customStatement('''
CREATE TABLE mytable (
id INTEGER PRIMARY KEY NOT NULL,
value INTEGER
);
''');

// Provide null to a nullable column
await db.customInsert(
r'INSERT INTO mytable (id, value) VALUES (1, $1);',
variables: [Variable(null)],
);

await executor.clearDatabaseAndClose(db);
});
}
10 changes: 10 additions & 0 deletions extras/drift_postgres/test/types_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ void main() {
.isBiggerOrEqualValue(moonLanding)),
isTrue);
});

test('bigint', () async {
expect(await eval(Variable<BigInt>(BigInt.two)), BigInt.two);

await expectLater(
eval(Variable<BigInt>(BigInt.parse('9223372036854775808'))),
throwsArgumentError,
reason: 'Out of range',
);
});
}

0 comments on commit f4a2887

Please sign in to comment.