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

Commit

Permalink
Browse files Browse the repository at this point in the history
* Fixed null safety issue
* Added an example
* Doc update
  • Loading branch information
Milvintsiss committed Jul 2, 2021
1 parent 9c6b27c commit 4e04641
Show file tree
Hide file tree
Showing 20 changed files with 619 additions and 83 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pubspec.lock

# Miscellaneous
*.class
*.log
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [2.1.0] - 2021/06/27

* Fixed null safety issue
* Added an example
* Doc update

## [2.0.0] - 2021/03/07

* Migrate to null safety
Expand Down
62 changes: 41 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
[![pub package](https://img.shields.io/pub/v/sqlite3_library_linux)](https://pub.dev/packages/camera)
# SQLite3 library for linux

This package help you bundle SQLite3 library to your apps.

He was originally developed to use with moor but you can use it for others use cases that
need SQLite3.
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)).

## How to use with Moor

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

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

import 'dart:ffi';
import 'dart:io';
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3/open.dart';
import 'package:sqlite3_library_linux/sqlite3_library_linux.dart';
void main() {
open.overrideFor(OperatingSystem.linux, openSQLiteOnLinux);
```dart
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3/open.dart';
import 'package:sqlite3_library_linux/sqlite3_library_linux.dart';
late final Database db;
void main() {
open.overrideFor(OperatingSystem.linux, openSQLiteOnLinux);
final db = sqlite3.openInMemory();
db.dispose();
runApp(MyApp());
}
// For database file creation and more please see the example
db = sqlite3.open([YOUR_DB_FILE]);
runApp(MyApp());
}
```

And... that's it! No need to provide your own sqlite3.so file 🙂

## How to use with Moor

Be sure to follow all the steps to migrate from moor_flutter to moor ffi ([docs](https://moor.simonbinder.eu/docs/other-engines/vm/)).

Then add an override for linux and give it the `openSQLiteOnLinux` function provided the package:

```dart
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3/open.dart';
import 'package:sqlite3_library_linux/sqlite3_library_linux.dart';
void main() {
open.overrideFor(OperatingSystem.linux, openSQLiteOnLinux);
final db = sqlite3.openInMemory();
db.dispose();
runApp(MyApp());
}
```

And... that's it! No need to provide your own libsqlite3.so file🙂
>For the moment the package does not provide support for arm64 architectures, if you need it feel free to do a pull request!
>For the moment the package does not provide support for arm64 architectures, if you need it feel
> free to do a pull request!
>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.
47 changes: 47 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/
pubspec.lock

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
10 changes: 10 additions & 0 deletions example/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: d79295af24c3ed621c33713ecda14ad196fd9c31
channel: stable

project_type: app
1 change: 1 addition & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# sqlite3_library_linux example
119 changes: 119 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/// This example show you how to use the openSQLiteOnWindows function
///
/// It also show you how to create your database file and how to execute simple
/// SQL functions.
///
/// Here the table used have only one column and we use only one row to store
/// a count value. But you can use all SQLite possibilities and build complex
/// tables and queries, the only limitations are on SQLite side.
/// If you have a more relevant example in mind like TodoList app or other feel
/// free to do a pull request :)
import 'dart:io' show File;
import 'package:path_provider/path_provider.dart'
show getApplicationSupportDirectory;

import 'package:flutter/material.dart';

import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3/open.dart';
import 'package:sqlite3_library_linux/sqlite3_library_linux.dart';

late final Database db;

Future<void> main() async {
// Override the sqlite3 load config when OS is Windows
open.overrideFor(OperatingSystem.linux, openSQLiteOnLinux);

// Folder where the database will be stored
final dbFolder = await getApplicationSupportDirectory();

// If the database file doesn't exist, create it.
File dbFile =
await File(dbFolder.path + "/sqlite3_library_linux_example/db")
.create(recursive: true);

// Open the database file
db = sqlite3.open(dbFile.path);

// Create 'count' table if the table doesn't already exist
db.execute('CREATE TABLE IF NOT EXISTS '
'count (count_value INTEGER NOT NULL);');

runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() => _counter++);
_updateCounterInDatabase();
}

void _getCounterFromDatabase() {
var values = db.select('SELECT count_value FROM count;');
if (values.isNotEmpty) _counter = values.first['count_value'];
}

void _updateCounterInDatabase() {
db.execute('DELETE FROM count;');
db.execute('INSERT INTO count (count_value) VALUES ($_counter);');
}

@override
void initState() {
super.initState();
_getCounterFromDatabase();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
1 change: 1 addition & 0 deletions example/linux/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flutter/ephemeral
106 changes: 106 additions & 0 deletions example/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
cmake_minimum_required(VERSION 3.10)
project(runner LANGUAGES CXX)

set(BINARY_NAME "example")
set(APPLICATION_ID "com.example.example")

cmake_policy(SET CMP0063 NEW)

set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")

# Configure build options.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "Debug" CACHE
STRING "Flutter build mode" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Profile" "Release")
endif()

# Compilation settings that should be applied to most targets.
function(APPLY_STANDARD_SETTINGS TARGET)
target_compile_features(${TARGET} PUBLIC cxx_std_14)
target_compile_options(${TARGET} PRIVATE -Wall -Werror)
target_compile_options(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O3>")
target_compile_definitions(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:NDEBUG>")
endfunction()

set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")

# Flutter library and tool build rules.
add_subdirectory(${FLUTTER_MANAGED_DIR})

# System-level dependencies.
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)

add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}")

# Application build
add_executable(${BINARY_NAME}
"main.cc"
"my_application.cc"
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
)
apply_standard_settings(${BINARY_NAME})
target_link_libraries(${BINARY_NAME} PRIVATE flutter)
target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK)
add_dependencies(${BINARY_NAME} flutter_assemble)
# Only the install-generated bundle's copy of the executable will launch
# correctly, since the resources must in the right relative locations. To avoid
# people trying to run the unbundled copy, put it in a subdirectory instead of
# the default top-level location.
set_target_properties(${BINARY_NAME}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run"
)

# Generated plugin build rules, which manage building the plugins and adding
# them to the application.
include(flutter/generated_plugins.cmake)


# === Installation ===
# By default, "installing" just makes a relocatable bundle in the build
# directory.
set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle")
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
endif()

# Start with a clean build bundle directory every time.
install(CODE "
file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\")
" COMPONENT Runtime)

set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")

install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
COMPONENT Runtime)

install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
COMPONENT Runtime)

install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)

if(PLUGIN_BUNDLED_LIBRARIES)
install(FILES "${PLUGIN_BUNDLED_LIBRARIES}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
endif()

# Fully re-copy the assets directory on each build to avoid having stale files
# from a previous install.
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
install(CODE "
file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
" COMPONENT Runtime)
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)

# Install the AOT library on non-Debug builds only.
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
endif()
Loading

0 comments on commit 4e04641

Please sign in to comment.