Skip to content

Commit

Permalink
chore: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jul 31, 2024
1 parent bd6a4ae commit 32b1850
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 43 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"private": true,
"dependencies": {
"@prisma/client": "^5.14.0",
"prisma": "^5.14.0",
"prisma": "^5.17.0",
"vitepress": "^1.2.2"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/orm_flutter/ffigen.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Run with `dart run ffigen --config ffigen.yaml`.
name: QueryEngineBindings
description: |
Bindings for `src/engine_bridge.h`.
Bindings for `src/orm_flutter.h`.
Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
output: "lib/src/query_engine_bindings.dart"
headers:
entry-points:
- "src/engine_bridge.h"
- "src/orm_flutter.h"
include-directives:
- "src/**"
preamble: |
Expand Down
68 changes: 41 additions & 27 deletions packages/orm_flutter/lib/src/query_engine_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// ignore_for_file: type=lint
import 'dart:ffi' as ffi;

/// Bindings for `src/prisma_flutter.h`.
/// Bindings for `src/orm_flutter.h`.
///
/// Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
///
Expand All @@ -28,21 +28,21 @@ class QueryEngineBindings {
/// Create a new [QueryEngine]
///
/// Returns a [Status] code.
int create(
Status create(
ConstructorOptions options,
ffi.Pointer<ffi.Pointer<QueryEngine>> qePtr,
ffi.Pointer<ffi.Pointer<ffi.Char>> errorStringPtr,
) {
return _create(
return Status.fromValue(_create(
options,
qePtr,
errorStringPtr,
);
));
}

late final _createPtr = _lookup<
ffi.NativeFunction<
ffi.Int Function(
ffi.UnsignedInt Function(
ConstructorOptions,
ffi.Pointer<ffi.Pointer<QueryEngine>>,
ffi.Pointer<ffi.Pointer<ffi.Char>>)>>('create');
Expand All @@ -51,75 +51,79 @@ class QueryEngineBindings {
ffi.Pointer<ffi.Pointer<ffi.Char>>)>();

/// Destroy a [QueryEngine]
int destroy(
Status destroy(
ffi.Pointer<QueryEngine> qe,
) {
return _destroy(
return Status.fromValue(_destroy(
qe,
);
));
}

late final _destroyPtr =
_lookup<ffi.NativeFunction<ffi.Int Function(ffi.Pointer<QueryEngine>)>>(
'destroy');
late final _destroyPtr = _lookup<
ffi.NativeFunction<
ffi.UnsignedInt Function(ffi.Pointer<QueryEngine>)>>('destroy');
late final _destroy =
_destroyPtr.asFunction<int Function(ffi.Pointer<QueryEngine>)>();

/// Start a [QueryEngine]
int start(
Status start(
ffi.Pointer<QueryEngine> qe,
ffi.Pointer<ffi.Char> trace,
ffi.Pointer<ffi.Pointer<ffi.Char>> errorStringPtr,
) {
return _start(
return Status.fromValue(_start(
qe,
trace,
errorStringPtr,
);
));
}

late final _startPtr = _lookup<
ffi.NativeFunction<
ffi.Int Function(ffi.Pointer<QueryEngine>, ffi.Pointer<ffi.Char>,
ffi.UnsignedInt Function(
ffi.Pointer<QueryEngine>,
ffi.Pointer<ffi.Char>,
ffi.Pointer<ffi.Pointer<ffi.Char>>)>>('start');
late final _start = _startPtr.asFunction<
int Function(ffi.Pointer<QueryEngine>, ffi.Pointer<ffi.Char>,
ffi.Pointer<ffi.Pointer<ffi.Char>>)>();

/// Stop a [QueryEngine]
int stop(
Status stop(
ffi.Pointer<QueryEngine> qe,
ffi.Pointer<ffi.Char> headerStr,
) {
return _stop(
return Status.fromValue(_stop(
qe,
headerStr,
);
));
}

late final _stopPtr = _lookup<
ffi.NativeFunction<
ffi.Int Function(
ffi.UnsignedInt Function(
ffi.Pointer<QueryEngine>, ffi.Pointer<ffi.Char>)>>('stop');
late final _stop = _stopPtr.asFunction<
int Function(ffi.Pointer<QueryEngine>, ffi.Pointer<ffi.Char>)>();

/// Apply migrations
int applyMigrations(
Status applyMigrations(
ffi.Pointer<QueryEngine> qe,
ffi.Pointer<ffi.Char> migrationsPath,
ffi.Pointer<ffi.Pointer<ffi.Char>> errorStringPtr,
) {
return _applyMigrations(
return Status.fromValue(_applyMigrations(
qe,
migrationsPath,
errorStringPtr,
);
));
}

late final _applyMigrationsPtr = _lookup<
ffi.NativeFunction<
ffi.Int Function(ffi.Pointer<QueryEngine>, ffi.Pointer<ffi.Char>,
ffi.UnsignedInt Function(
ffi.Pointer<QueryEngine>,
ffi.Pointer<ffi.Char>,
ffi.Pointer<ffi.Pointer<ffi.Char>>)>>('applyMigrations');
late final _applyMigrations = _applyMigrationsPtr.asFunction<
int Function(ffi.Pointer<QueryEngine>, ffi.Pointer<ffi.Char>,
Expand Down Expand Up @@ -272,13 +276,23 @@ final class ConstructorOptions extends ffi.Struct {
/// - [QueryEngineBindings.start]
/// - [QueryEngineBindings.stop]
/// - [QueryEngineBindings.applyMigrations]
abstract class Status {
enum Status {
/// Success
static const int ok = 0;
ok(0),

/// Error
static const int err = 1;
err(1),

/// Missing pointer, only create returns.
static const int miss = 2;
miss(2);

final int value;
const Status(this.value);

static Status fromValue(int value) => switch (value) {
0 => ok,
1 => err,
2 => miss,
_ => throw ArgumentError("Unknown value for Status: $value"),
};
}
2 changes: 1 addition & 1 deletion packages/orm_flutter/prisma-engines-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e9771e62de70f79a5e1c604a2d7c8e2a0a874b48
a6977e518975c6a4e6ae77bf7faf8afc08b97e7d
2 changes: 1 addition & 1 deletion packages/orm_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies:
webfetch: ^0.0.17

dev_dependencies:
ffigen: ^12.0.0
ffigen: ^13.0.0
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
Expand Down
10 changes: 5 additions & 5 deletions packages/orm_flutter/src/orm_flutter.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Returns a [Status] code.
*/
FFI_PLUGIN_EXPORT int create(struct ConstructorOptions options,
FFI_PLUGIN_EXPORT enum Status create(struct ConstructorOptions options,
struct QueryEngine **qePtr,
char **errorStringPtr)
{
Expand All @@ -16,25 +16,25 @@ FFI_PLUGIN_EXPORT int create(struct ConstructorOptions options,
/**
Destroy a [QueryEngine]
*/
FFI_PLUGIN_EXPORT int destroy(struct QueryEngine *qe) {
FFI_PLUGIN_EXPORT enum Status destroy(struct QueryEngine *qe) {
return prisma_destroy(qe);
}

/** Start a [QueryEngine] */
FFI_PLUGIN_EXPORT int start(struct QueryEngine *qe,
FFI_PLUGIN_EXPORT enum Status start(struct QueryEngine *qe,
const char *trace,
char **errorStringPtr) {
return prisma_connect(qe, trace, errorStringPtr);
}

/** Stop a [QueryEngine] */
FFI_PLUGIN_EXPORT int stop(struct QueryEngine *qe,
FFI_PLUGIN_EXPORT enum Status stop(struct QueryEngine *qe,
const char *headerStr) {
return prisma_disconnect(qe, headerStr);
}

/** Apply migrations */
FFI_PLUGIN_EXPORT int applyMigrations(struct QueryEngine *qe,
FFI_PLUGIN_EXPORT enum Status applyMigrations(struct QueryEngine *qe,
const char *migrationsPath,
char **errorStringPtr) {
return prisma_apply_pending_migrations(qe, migrationsPath, errorStringPtr);
Expand Down
10 changes: 5 additions & 5 deletions packages/orm_flutter/src/orm_flutter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ FFI_PLUGIN_EXPORT enum Status {
Returns a [Status] code.
*/
FFI_PLUGIN_EXPORT int create(struct ConstructorOptions options,
FFI_PLUGIN_EXPORT enum Status create(struct ConstructorOptions options,
struct QueryEngine **qePtr,
char **errorStringPtr);

/**
Destroy a [QueryEngine]
*/
FFI_PLUGIN_EXPORT int destroy(struct QueryEngine *qe);
FFI_PLUGIN_EXPORT enum Status destroy(struct QueryEngine *qe);

/** Start a [QueryEngine] */
FFI_PLUGIN_EXPORT int start(struct QueryEngine *qe,
FFI_PLUGIN_EXPORT enum Status start(struct QueryEngine *qe,
const char *trace,
char **errorStringPtr);

/** Stop a [QueryEngine] */
FFI_PLUGIN_EXPORT int stop(struct QueryEngine *qe,
FFI_PLUGIN_EXPORT enum Status stop(struct QueryEngine *qe,
const char *headerStr);

/** Apply migrations */
FFI_PLUGIN_EXPORT int applyMigrations(struct QueryEngine *qe,
FFI_PLUGIN_EXPORT enum Status applyMigrations(struct QueryEngine *qe,
const char *migrationsPath,
char **errorStringPtr);

Expand Down

0 comments on commit 32b1850

Please sign in to comment.