mirror of
https://github.com/dart-lang/sdk
synced 2024-10-06 15:50:01 +00:00
[vm/ffi] Add copyright headers to sqlite example and incorporate Michael's feedback.
Change-Id: I83555a81bb6394a8c2a740a5127175be85aabf3d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98007 Commit-Queue: Samir Jindel <sjindel@google.com> Reviewed-by: Michael Thomsen <mit@google.com> Auto-Submit: Samir Jindel <sjindel@google.com>
This commit is contained in:
parent
7a15b02935
commit
991c9da720
|
@ -6,7 +6,7 @@ This is an illustrative sample for how to use `dart:ffi`.
|
|||
## Building and Running this Sample
|
||||
|
||||
Building and running this sample is done through pub.
|
||||
Running `pub get` and `pub run test` should produce the following output.
|
||||
Running `pub get` and `pub run example/main` should produce the following output.
|
||||
|
||||
```sh
|
||||
$ pub get
|
||||
|
@ -24,8 +24,7 @@ Precompiled test:test.
|
|||
```
|
||||
|
||||
```
|
||||
$ pub run test
|
||||
00:01 +0: test/sqlite_test.dart: sqlite integration test
|
||||
$ pub run example/main
|
||||
1 Chocolade chip cookie Chocolade cookie foo
|
||||
2 Ginger cookie null 42
|
||||
3 Cinnamon roll null null
|
||||
|
@ -33,7 +32,6 @@ $ pub run test
|
|||
2 Ginger cookie null 42
|
||||
expected exception on accessing result data after close: The result has already been closed.
|
||||
expected this query to fail: no such column: non_existing_column (Code 1: SQL logic error)
|
||||
00:02 +3: All tests passed!
|
||||
```
|
||||
|
||||
## Tutorial
|
||||
|
|
84
samples/ffi/sqlite/example/main.dart
Normal file
84
samples/ffi/sqlite/example/main.dart
Normal file
|
@ -0,0 +1,84 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:test/test.dart";
|
||||
|
||||
import "../lib/sqlite.dart";
|
||||
|
||||
void main() {
|
||||
Database d = Database("test.db");
|
||||
d.execute("drop table if exists Cookies;");
|
||||
d.execute("""
|
||||
create table Cookies (
|
||||
id integer primary key,
|
||||
name text not null,
|
||||
alternative_name text
|
||||
);""");
|
||||
d.execute("""
|
||||
insert into Cookies (id, name, alternative_name)
|
||||
values
|
||||
(1,'Chocolade chip cookie', 'Chocolade cookie'),
|
||||
(2,'Ginger cookie', null),
|
||||
(3,'Cinnamon roll', null)
|
||||
;""");
|
||||
Result result = d.query("""
|
||||
select
|
||||
id,
|
||||
name,
|
||||
alternative_name,
|
||||
case
|
||||
when id=1 then 'foo'
|
||||
when id=2 then 42
|
||||
when id=3 then null
|
||||
end as multi_typed_column
|
||||
from Cookies
|
||||
;""");
|
||||
for (Row r in result) {
|
||||
int id = r.readColumnAsInt("id");
|
||||
String name = r.readColumnByIndex(1);
|
||||
String alternativeName = r.readColumn("alternative_name");
|
||||
dynamic multiTypedValue = r.readColumn("multi_typed_column");
|
||||
print("$id $name $alternativeName $multiTypedValue");
|
||||
}
|
||||
result = d.query("""
|
||||
select
|
||||
id,
|
||||
name,
|
||||
alternative_name,
|
||||
case
|
||||
when id=1 then 'foo'
|
||||
when id=2 then 42
|
||||
when id=3 then null
|
||||
end as multi_typed_column
|
||||
from Cookies
|
||||
;""");
|
||||
for (Row r in result) {
|
||||
int id = r.readColumnAsInt("id");
|
||||
String name = r.readColumnByIndex(1);
|
||||
String alternativeName = r.readColumn("alternative_name");
|
||||
dynamic multiTypedValue = r.readColumn("multi_typed_column");
|
||||
print("$id $name $alternativeName $multiTypedValue");
|
||||
if (id == 2) {
|
||||
result.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
try {
|
||||
result.iterator.moveNext();
|
||||
} on SQLiteException catch (e) {
|
||||
print("expected exception on accessing result data after close: $e");
|
||||
}
|
||||
try {
|
||||
d.query("""
|
||||
select
|
||||
id,
|
||||
non_existing_column
|
||||
from Cookies
|
||||
;""");
|
||||
} on SQLiteException catch (e) {
|
||||
print("expected this query to fail: $e");
|
||||
}
|
||||
d.execute("drop table Cookies;");
|
||||
d.close();
|
||||
}
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// A synchronous SQLite wrapper.
|
||||
///
|
||||
/// Written using dart:ffi.
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "dart:ffi";
|
||||
|
||||
import "../ffi/cstring.dart";
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// Result Codes
|
||||
///
|
||||
/// Many SQLite functions return an integer result code from the set shown
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "dart:ffi";
|
||||
|
||||
import "../ffi/cstring.dart";
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "dart:ffi";
|
||||
|
||||
import "../ffi/cstring.dart";
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
/// This iterator should be [close]d after use.
|
||||
///
|
||||
/// [ClosableIterator]s often use resources which should be freed after use.
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "dart:collection";
|
||||
import "dart:ffi";
|
||||
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "dart:async";
|
||||
import "dart:ffi";
|
||||
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "dart:convert";
|
||||
import "dart:ffi";
|
||||
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// VMOptions=--optimization-counter-threshold=5
|
||||
|
||||
import "package:test/test.dart";
|
||||
|
|
Loading…
Reference in a new issue