Reformat samples/ with 3.8 style.

Change-Id: I13ecd6339afd62cb5ebeb29c29d80470bcc22671
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425146
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
This commit is contained in:
Robert Nystrom
2025-04-29 07:45:07 -07:00
committed by Commit Queue
parent 361cf93984
commit 237ba8de01
5 changed files with 137 additions and 91 deletions
+3 -4
View File
@@ -54,10 +54,9 @@ Stream<int> produceIntStreamWithController(int count, int delayMs) {
}
Future<int> sumIntStream(int count, int delayMs, bool useAsyncStar) async {
final stream =
useAsyncStar
? produceIntStreamWithAsyncStar(count, delayMs)
: produceIntStreamWithController(count, delayMs);
final stream = useAsyncStar
? produceIntStreamWithAsyncStar(count, delayMs)
: produceIntStreamWithController(count, delayMs);
var sum = 0;
await for (var value in stream) {
sum += value;
@@ -28,16 +28,18 @@ class _SQLiteBindings {
/// an English language description of the error following a failure of any
/// of the sqlite3_open() routines.
late int Function(
Pointer<Utf8> filename,
Pointer<Pointer<Database>> databaseOut,
int flags,
Pointer<Utf8> vfs) sqlite3_open_v2;
Pointer<Utf8> filename,
Pointer<Pointer<Database>> databaseOut,
int flags,
Pointer<Utf8> vfs,
)
sqlite3_open_v2;
late int Function(Pointer<Database> database) sqlite3_close_v2;
late Pointer<NativeFunction<sqlite3_close_v2_native_t>>
sqlite3_close_v2_native;
sqlite3_close_v2_native;
late Pointer<NativeFunction<Void Function(Pointer<Database> database)>>
sqlite3_close_v2_native_return_void;
sqlite3_close_v2_native_return_void;
/// Compiling An SQL Statement
///
@@ -87,11 +89,13 @@ class _SQLiteBindings {
/// original SQL text. This causes the [sqlite3_step] interface to
/// behave differently in three ways:
late int Function(
Pointer<Database> database,
Pointer<Utf8> query,
int nbytes,
Pointer<Pointer<Statement>> statementOut,
Pointer<Pointer<Utf8>> tail) sqlite3_prepare_v2;
Pointer<Database> database,
Pointer<Utf8> query,
int nbytes,
Pointer<Pointer<Statement>> statementOut,
Pointer<Pointer<Utf8>> tail,
)
sqlite3_prepare_v2;
/// Evaluate An SQL Statement
///
@@ -219,9 +223,9 @@ class _SQLiteBindings {
/// undesirable behavior such as segfaults and heap corruption.
late int Function(Pointer<Statement> statement) sqlite3_finalize;
late Pointer<NativeFunction<sqlite3_finalize_native_t>>
sqlite3_finalize_native;
sqlite3_finalize_native;
late Pointer<NativeFunction<Void Function(Pointer<Statement> statement)>>
sqlite3_finalize_native_return_void;
sqlite3_finalize_native_return_void;
/// Number Of Columns In A Result Set
///
@@ -254,7 +258,7 @@ class _SQLiteBindings {
/// that column, if there is an AS clause. If there is no AS clause
/// then the name of the column is unspecified and may change from
late Pointer<Utf8> Function(Pointer<Statement> statement, int columnIndex)
sqlite3_column_name;
sqlite3_column_name;
/// CAPI3REF: Declared Datatype Of A Query Result
///
@@ -284,22 +288,22 @@ class _SQLiteBindings {
/// is associated with individual values, not with the containers
/// used to hold those values.
late Pointer<Utf8> Function(Pointer<Statement> statement, int columnIndex)
sqlite3_column_decltype;
sqlite3_column_decltype;
late int Function(Pointer<Statement> statement, int columnIndex)
sqlite3_column_type;
sqlite3_column_type;
late Pointer<Value> Function(Pointer<Statement> statement, int columnIndex)
sqlite3_column_value;
sqlite3_column_value;
late double Function(Pointer<Statement> statement, int columnIndex)
sqlite3_column_double;
sqlite3_column_double;
late int Function(Pointer<Statement> statement, int columnIndex)
sqlite3_column_int;
sqlite3_column_int;
late Pointer<Utf8> Function(Pointer<Statement> statement, int columnIndex)
sqlite3_column_text;
sqlite3_column_text;
/// The sqlite3_errstr() interface returns the English-language text that
/// describes the result code, as UTF-8. Memory to hold the error message
@@ -350,7 +354,8 @@ class _SQLiteBindings {
sqlite3_close_v2 = sqlite3_close_v2_native.asFunction();
sqlite3_prepare_v2 = sqlite
.lookup<NativeFunction<sqlite3_prepare_v2_native_t>>(
"sqlite3_prepare_v2")
"sqlite3_prepare_v2",
)
.asFunction();
sqlite3_step = sqlite
.lookup<NativeFunction<sqlite3_step_native_t>>("sqlite3_step")
@@ -370,35 +375,43 @@ class _SQLiteBindings {
.asFunction();
sqlite3_column_count = sqlite
.lookup<NativeFunction<sqlite3_column_count_native_t>>(
"sqlite3_column_count")
"sqlite3_column_count",
)
.asFunction();
sqlite3_column_name = sqlite
.lookup<NativeFunction<sqlite3_column_name_native_t>>(
"sqlite3_column_name")
"sqlite3_column_name",
)
.asFunction();
sqlite3_column_decltype = sqlite
.lookup<NativeFunction<sqlite3_column_decltype_native_t>>(
"sqlite3_column_decltype")
"sqlite3_column_decltype",
)
.asFunction();
sqlite3_column_type = sqlite
.lookup<NativeFunction<sqlite3_column_type_native_t>>(
"sqlite3_column_type")
"sqlite3_column_type",
)
.asFunction();
sqlite3_column_value = sqlite
.lookup<NativeFunction<sqlite3_column_value_native_t>>(
"sqlite3_column_value")
"sqlite3_column_value",
)
.asFunction();
sqlite3_column_double = sqlite
.lookup<NativeFunction<sqlite3_column_double_native_t>>(
"sqlite3_column_double")
"sqlite3_column_double",
)
.asFunction();
sqlite3_column_int = sqlite
.lookup<NativeFunction<sqlite3_column_int_native_t>>(
"sqlite3_column_int")
"sqlite3_column_int",
)
.asFunction();
sqlite3_column_text = sqlite
.lookup<NativeFunction<sqlite3_column_text_native_t>>(
"sqlite3_column_text")
"sqlite3_column_text",
)
.asFunction();
}
}
@@ -8,50 +8,57 @@ import "package:ffi/ffi.dart";
import "types.dart";
typedef sqlite3_open_v2_native_t = Int32 Function(Pointer<Utf8> filename,
Pointer<Pointer<Database>> ppDb, Int32 flags, Pointer<Utf8> vfs);
typedef sqlite3_open_v2_native_t =
Int32 Function(
Pointer<Utf8> filename,
Pointer<Pointer<Database>> ppDb,
Int32 flags,
Pointer<Utf8> vfs,
);
typedef sqlite3_close_v2_native_t = Int32 Function(Pointer<Database> database);
typedef sqlite3_prepare_v2_native_t = Int32 Function(
Pointer<Database> database,
Pointer<Utf8> query,
Int32 nbytes,
Pointer<Pointer<Statement>> statementOut,
Pointer<Pointer<Utf8>> tail);
typedef sqlite3_prepare_v2_native_t =
Int32 Function(
Pointer<Database> database,
Pointer<Utf8> query,
Int32 nbytes,
Pointer<Pointer<Statement>> statementOut,
Pointer<Pointer<Utf8>> tail,
);
typedef sqlite3_step_native_t = Int32 Function(Pointer<Statement> statement);
typedef sqlite3_reset_native_t = Int32 Function(Pointer<Statement> statement);
typedef sqlite3_finalize_native_t = Int32 Function(
Pointer<Statement> statement);
typedef sqlite3_finalize_native_t =
Int32 Function(Pointer<Statement> statement);
typedef sqlite3_errstr_native_t = Pointer<Utf8> Function(Int32 error);
typedef sqlite3_errmsg_native_t = Pointer<Utf8> Function(
Pointer<Database> database);
typedef sqlite3_errmsg_native_t =
Pointer<Utf8> Function(Pointer<Database> database);
typedef sqlite3_column_count_native_t = Int32 Function(
Pointer<Statement> statement);
typedef sqlite3_column_count_native_t =
Int32 Function(Pointer<Statement> statement);
typedef sqlite3_column_name_native_t = Pointer<Utf8> Function(
Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_name_native_t =
Pointer<Utf8> Function(Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_decltype_native_t = Pointer<Utf8> Function(
Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_decltype_native_t =
Pointer<Utf8> Function(Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_type_native_t = Int32 Function(
Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_type_native_t =
Int32 Function(Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_value_native_t = Pointer<Value> Function(
Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_value_native_t =
Pointer<Value> Function(Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_double_native_t = Double Function(
Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_double_native_t =
Double Function(Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_int_native_t = Int32 Function(
Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_int_native_t =
Int32 Function(Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_text_native_t = Pointer<Utf8> Function(
Pointer<Statement> statement, Int32 columnIndex);
typedef sqlite3_column_text_native_t =
Pointer<Utf8> Function(Pointer<Statement> statement, Int32 columnIndex);
+47 -22
View File
@@ -25,12 +25,18 @@ class Database {
bool _open = false;
/// Open a database located at the file [path].
Database(String path,
[int flags = Flags.SQLITE_OPEN_READWRITE | Flags.SQLITE_OPEN_CREATE]) {
Database(
String path, [
int flags = Flags.SQLITE_OPEN_READWRITE | Flags.SQLITE_OPEN_CREATE,
]) {
Pointer<Pointer<types.Database>> dbOut = calloc();
final pathC = Utf8Resource(path.toNativeUtf8());
final int resultCode =
bindings.sqlite3_open_v2(pathC.unsafe(), dbOut, flags, nullptr);
final int resultCode = bindings.sqlite3_open_v2(
pathC.unsafe(),
dbOut,
flags,
nullptr,
);
_database = DatabaseResource(dbOut.value);
calloc.free(dbOut);
pathC.free();
@@ -108,10 +114,12 @@ class Database {
if (errorCode == null) {
return SQLiteException(errorMessage);
}
String errorCodeExplanation =
bindings.sqlite3_errstr(errorCode).toDartString();
String errorCodeExplanation = bindings
.sqlite3_errstr(errorCode)
.toDartString();
return SQLiteException(
"$errorMessage (Code $errorCode: $errorCodeExplanation)");
"$errorMessage (Code $errorCode: $errorCodeExplanation)",
);
}
}
@@ -185,8 +193,10 @@ class Row {
/// By default it returns a dynamically typed value. If [convert] is set to
/// [Convert.StaticType] the value is converted to the static type computed
/// for the column by the query compiler.
dynamic readColumn(String columnName,
{Convert convert = Convert.DynamicType}) {
dynamic readColumn(
String columnName, {
Convert convert = Convert.DynamicType,
}) {
return readColumnByIndex(_columnIndices[columnName]!, convert: convert);
}
@@ -195,16 +205,19 @@ class Row {
/// By default it returns a dynamically typed value. If [convert] is set to
/// [Convert.StaticType] the value is converted to the static type computed
/// for the column by the query compiler.
dynamic readColumnByIndex(int columnIndex,
{Convert convert = Convert.DynamicType}) {
dynamic readColumnByIndex(
int columnIndex, {
Convert convert = Convert.DynamicType,
}) {
_checkIsCurrentRow();
Type dynamicType;
if (convert == Convert.DynamicType) {
dynamicType = _typeFromCode(_statement.columnType(columnIndex));
} else {
dynamicType =
_typeFromText(_statement.columnDecltype(columnIndex).toDartString());
dynamicType = _typeFromText(
_statement.columnDecltype(columnIndex).toDartString(),
);
}
switch (dynamicType) {
@@ -245,8 +258,9 @@ class Row {
void _checkIsCurrentRow() {
if (!_isCurrentRow) {
throw Exception(
"This row is not the current row, reading data from the non-current"
" row is not supported by sqlite.");
"This row is not the current row, reading data from the non-current"
" row is not supported by sqlite.",
);
}
}
@@ -256,8 +270,9 @@ class Row {
}
class DatabaseResource implements Finalizable {
static final NativeFinalizer _finalizer =
NativeFinalizer(bindings.sqlite3_close_v2_native_return_void.cast());
static final NativeFinalizer _finalizer = NativeFinalizer(
bindings.sqlite3_close_v2_native_return_void.cast(),
);
/// [_statement] must never escape [StatementResource], otherwise the
/// [_finalizer] will run prematurely.
@@ -272,10 +287,19 @@ class DatabaseResource implements Finalizable {
return bindings.sqlite3_close_v2(_database);
}
int prepare(Utf8Resource query, int nbytes,
Pointer<Pointer<Statement>> statementOut, Pointer<Pointer<Utf8>> tail) {
int prepare(
Utf8Resource query,
int nbytes,
Pointer<Pointer<Statement>> statementOut,
Pointer<Pointer<Utf8>> tail,
) {
int result = bindings.sqlite3_prepare_v2(
_database, query.unsafe(), nbytes, statementOut, tail);
_database,
query.unsafe(),
nbytes,
statementOut,
tail,
);
return result;
}
@@ -283,8 +307,9 @@ class DatabaseResource implements Finalizable {
}
class StatementResource implements Finalizable {
static final NativeFinalizer _finalizer =
NativeFinalizer(bindings.sqlite3_finalize_native_return_void.cast());
static final NativeFinalizer _finalizer = NativeFinalizer(
bindings.sqlite3_finalize_native_return_void.cast(),
);
/// [_statement] must never escape [StatementResource], otherwise the
/// [_finalizer] will run prematurely.
+10 -8
View File
@@ -47,10 +47,11 @@ void main() {
final alternativeName = r.readColumn("alternative_name") as String?;
dynamic multiTypedValue = r.readColumn("multi_typed_column");
expect(
true,
multiTypedValue == 42 ||
multiTypedValue == 'foo' ||
multiTypedValue == null);
true,
multiTypedValue == 42 ||
multiTypedValue == 'foo' ||
multiTypedValue == null,
);
print("$id $name $alternativeName $multiTypedValue");
}
result = d.query("""
@@ -73,10 +74,11 @@ void main() {
final alternativeName = r.readColumn("alternative_name") as String?;
dynamic multiTypedValue = r.readColumn("multi_typed_column");
expect(
true,
multiTypedValue == 42 ||
multiTypedValue == 'foo' ||
multiTypedValue == null);
true,
multiTypedValue == 42 ||
multiTypedValue == 'foo' ||
multiTypedValue == null,
);
print("$id $name $alternativeName $multiTypedValue");
if (id == 2) {
result.close();