Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 4e04641

Browse files
committed
* Fixed null safety issue * Added an example * Doc update
1 parent 9c6b27c commit 4e04641

20 files changed

Lines changed: 619 additions & 83 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pubspec.lock
2+
13
# Miscellaneous
24
*.class
35
*.log

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [2.1.0] - 2021/06/27
2+
3+
* Fixed null safety issue
4+
* Added an example
5+
* Doc update
6+
17
## [2.0.0] - 2021/03/07
28

39
* Migrate to null safety

README.md

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,53 @@
1+
[![pub package](https://img.shields.io/pub/v/sqlite3_library_linux)](https://pub.dev/packages/camera)
12
# SQLite3 library for linux
23

34
This package help you bundle SQLite3 library to your apps.
45

5-
He was originally developed to use with moor but you can use it for others use cases that
6-
need SQLite3.
6+
It can be used with packages like Moor to make the SQLite opening process easier (See: [How to use with Moor](#how-to-use-with-moor)).
77

8-
## How to use with Moor
98

10-
Be sure to follow all the steps to migrate from moor_flutter to moor ffi
11-
([doc](https://moor.simonbinder.eu/docs/other-engines/vm/)).
9+
## How to use
1210

1311
Add an override for linux and give it the `openSQLiteOnLinux` function provided by the package:
1412

15-
import 'dart:ffi';
16-
import 'dart:io';
17-
import 'package:sqlite3/sqlite3.dart';
18-
import 'package:sqlite3/open.dart';
19-
import 'package:sqlite3_library_linux/sqlite3_library_linux.dart';
20-
21-
void main() {
22-
open.overrideFor(OperatingSystem.linux, openSQLiteOnLinux);
13+
```dart
14+
import 'package:sqlite3/sqlite3.dart';
15+
import 'package:sqlite3/open.dart';
16+
import 'package:sqlite3_library_linux/sqlite3_library_linux.dart';
17+
18+
late final Database db;
19+
void main() {
20+
open.overrideFor(OperatingSystem.linux, openSQLiteOnLinux);
2321
24-
final db = sqlite3.openInMemory();
25-
db.dispose();
26-
27-
runApp(MyApp());
28-
}
22+
// For database file creation and more please see the example
23+
db = sqlite3.open([YOUR_DB_FILE]);
24+
25+
runApp(MyApp());
26+
}
27+
```
28+
29+
And... that's it! No need to provide your own sqlite3.so file 🙂
30+
31+
## How to use with Moor
32+
33+
Be sure to follow all the steps to migrate from moor_flutter to moor ffi ([docs](https://moor.simonbinder.eu/docs/other-engines/vm/)).
34+
35+
Then add an override for linux and give it the `openSQLiteOnLinux` function provided the package:
36+
37+
```dart
38+
import 'package:sqlite3/sqlite3.dart';
39+
import 'package:sqlite3/open.dart';
40+
import 'package:sqlite3_library_linux/sqlite3_library_linux.dart';
41+
42+
void main() {
43+
open.overrideFor(OperatingSystem.linux, openSQLiteOnLinux);
44+
final db = sqlite3.openInMemory();
45+
db.dispose();
46+
47+
runApp(MyApp());
48+
}
49+
```
2950

30-
And... that's it! No need to provide your own libsqlite3.so file🙂
51+
>For the moment the package does not provide support for arm64 architectures, if you need it feel free to do a pull request!
3152
32-
>For the moment the package does not provide support for arm64 architectures, if you need it feel
33-
> free to do a pull request!
53+
>The package as been tested for Ubuntu 20.04 and Debian 10, it should work on others distributions too but if you have any issue using the package please [report](https://github.com/Milvintsiss/sqlite3_library_linux/issues) it.

example/.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
**/ios/Flutter/.last_build_id
26+
.dart_tool/
27+
.flutter-plugins
28+
.flutter-plugins-dependencies
29+
.packages
30+
.pub-cache/
31+
.pub/
32+
/build/
33+
pubspec.lock
34+
35+
# Web related
36+
lib/generated_plugin_registrant.dart
37+
38+
# Symbolication related
39+
app.*.symbols
40+
41+
# Obfuscation related
42+
app.*.map.json
43+
44+
# Android Studio will place build artifacts here
45+
/android/app/debug
46+
/android/app/profile
47+
/android/app/release

example/.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: d79295af24c3ed621c33713ecda14ad196fd9c31
8+
channel: stable
9+
10+
project_type: app

example/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# sqlite3_library_linux example

example/lib/main.dart

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/// This example show you how to use the openSQLiteOnWindows function
2+
///
3+
/// It also show you how to create your database file and how to execute simple
4+
/// SQL functions.
5+
///
6+
/// Here the table used have only one column and we use only one row to store
7+
/// a count value. But you can use all SQLite possibilities and build complex
8+
/// tables and queries, the only limitations are on SQLite side.
9+
/// If you have a more relevant example in mind like TodoList app or other feel
10+
/// free to do a pull request :)
11+
12+
import 'dart:io' show File;
13+
import 'package:path_provider/path_provider.dart'
14+
show getApplicationSupportDirectory;
15+
16+
import 'package:flutter/material.dart';
17+
18+
import 'package:sqlite3/sqlite3.dart';
19+
import 'package:sqlite3/open.dart';
20+
import 'package:sqlite3_library_linux/sqlite3_library_linux.dart';
21+
22+
late final Database db;
23+
24+
Future<void> main() async {
25+
// Override the sqlite3 load config when OS is Windows
26+
open.overrideFor(OperatingSystem.linux, openSQLiteOnLinux);
27+
28+
// Folder where the database will be stored
29+
final dbFolder = await getApplicationSupportDirectory();
30+
31+
// If the database file doesn't exist, create it.
32+
File dbFile =
33+
await File(dbFolder.path + "/sqlite3_library_linux_example/db")
34+
.create(recursive: true);
35+
36+
// Open the database file
37+
db = sqlite3.open(dbFile.path);
38+
39+
// Create 'count' table if the table doesn't already exist
40+
db.execute('CREATE TABLE IF NOT EXISTS '
41+
'count (count_value INTEGER NOT NULL);');
42+
43+
runApp(MyApp());
44+
}
45+
46+
class MyApp extends StatelessWidget {
47+
@override
48+
Widget build(BuildContext context) {
49+
return MaterialApp(
50+
title: 'Flutter Demo',
51+
theme: ThemeData(
52+
primarySwatch: Colors.blue,
53+
),
54+
home: MyHomePage(title: 'Flutter Demo Home Page'),
55+
);
56+
}
57+
}
58+
59+
class MyHomePage extends StatefulWidget {
60+
MyHomePage({Key? key, required this.title}) : super(key: key);
61+
62+
final String title;
63+
64+
@override
65+
_MyHomePageState createState() => _MyHomePageState();
66+
}
67+
68+
class _MyHomePageState extends State<MyHomePage> {
69+
int _counter = 0;
70+
71+
void _incrementCounter() {
72+
setState(() => _counter++);
73+
_updateCounterInDatabase();
74+
}
75+
76+
void _getCounterFromDatabase() {
77+
var values = db.select('SELECT count_value FROM count;');
78+
if (values.isNotEmpty) _counter = values.first['count_value'];
79+
}
80+
81+
void _updateCounterInDatabase() {
82+
db.execute('DELETE FROM count;');
83+
db.execute('INSERT INTO count (count_value) VALUES ($_counter);');
84+
}
85+
86+
@override
87+
void initState() {
88+
super.initState();
89+
_getCounterFromDatabase();
90+
}
91+
92+
@override
93+
Widget build(BuildContext context) {
94+
return Scaffold(
95+
appBar: AppBar(
96+
title: Text(widget.title),
97+
),
98+
body: Center(
99+
child: Column(
100+
mainAxisAlignment: MainAxisAlignment.center,
101+
children: <Widget>[
102+
Text(
103+
'You have pushed the button this many times:',
104+
),
105+
Text(
106+
'$_counter',
107+
style: Theme.of(context).textTheme.headline4,
108+
),
109+
],
110+
),
111+
),
112+
floatingActionButton: FloatingActionButton(
113+
onPressed: _incrementCounter,
114+
tooltip: 'Increment',
115+
child: Icon(Icons.add),
116+
),
117+
);
118+
}
119+
}

example/linux/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flutter/ephemeral

example/linux/CMakeLists.txt

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(runner LANGUAGES CXX)
3+
4+
set(BINARY_NAME "example")
5+
set(APPLICATION_ID "com.example.example")
6+
7+
cmake_policy(SET CMP0063 NEW)
8+
9+
set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")
10+
11+
# Configure build options.
12+
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
13+
set(CMAKE_BUILD_TYPE "Debug" CACHE
14+
STRING "Flutter build mode" FORCE)
15+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
16+
"Debug" "Profile" "Release")
17+
endif()
18+
19+
# Compilation settings that should be applied to most targets.
20+
function(APPLY_STANDARD_SETTINGS TARGET)
21+
target_compile_features(${TARGET} PUBLIC cxx_std_14)
22+
target_compile_options(${TARGET} PRIVATE -Wall -Werror)
23+
target_compile_options(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O3>")
24+
target_compile_definitions(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:NDEBUG>")
25+
endfunction()
26+
27+
set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")
28+
29+
# Flutter library and tool build rules.
30+
add_subdirectory(${FLUTTER_MANAGED_DIR})
31+
32+
# System-level dependencies.
33+
find_package(PkgConfig REQUIRED)
34+
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
35+
36+
add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}")
37+
38+
# Application build
39+
add_executable(${BINARY_NAME}
40+
"main.cc"
41+
"my_application.cc"
42+
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
43+
)
44+
apply_standard_settings(${BINARY_NAME})
45+
target_link_libraries(${BINARY_NAME} PRIVATE flutter)
46+
target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK)
47+
add_dependencies(${BINARY_NAME} flutter_assemble)
48+
# Only the install-generated bundle's copy of the executable will launch
49+
# correctly, since the resources must in the right relative locations. To avoid
50+
# people trying to run the unbundled copy, put it in a subdirectory instead of
51+
# the default top-level location.
52+
set_target_properties(${BINARY_NAME}
53+
PROPERTIES
54+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run"
55+
)
56+
57+
# Generated plugin build rules, which manage building the plugins and adding
58+
# them to the application.
59+
include(flutter/generated_plugins.cmake)
60+
61+
62+
# === Installation ===
63+
# By default, "installing" just makes a relocatable bundle in the build
64+
# directory.
65+
set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle")
66+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
67+
set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
68+
endif()
69+
70+
# Start with a clean build bundle directory every time.
71+
install(CODE "
72+
file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\")
73+
" COMPONENT Runtime)
74+
75+
set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
76+
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
77+
78+
install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
79+
COMPONENT Runtime)
80+
81+
install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
82+
COMPONENT Runtime)
83+
84+
install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
85+
COMPONENT Runtime)
86+
87+
if(PLUGIN_BUNDLED_LIBRARIES)
88+
install(FILES "${PLUGIN_BUNDLED_LIBRARIES}"
89+
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
90+
COMPONENT Runtime)
91+
endif()
92+
93+
# Fully re-copy the assets directory on each build to avoid having stale files
94+
# from a previous install.
95+
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
96+
install(CODE "
97+
file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
98+
" COMPONENT Runtime)
99+
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
100+
DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)
101+
102+
# Install the AOT library on non-Debug builds only.
103+
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
104+
install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
105+
COMPONENT Runtime)
106+
endif()

0 commit comments

Comments
 (0)