Make mysql example a sub-library that we won't publish

This commit is contained in:
Simon Binder 2019-07-24 18:25:59 +02:00
parent 5f8ab42189
commit b75bd4d5ec
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
11 changed files with 47 additions and 258 deletions

View File

@ -1,22 +0,0 @@
---
layout: guide
title: Custom databases
nav_order: 8
since: 2.0
permalink: /custom_backend
---
_Note_: This feature is available starting from Moor `2.0`.
# Custom databases
Moor has builtin support for Flutter using the `sqflite` package - it also supports the [web]({{"web" | absolute_url}})
and desktop apps. However, you can also use moor with a different database of your choice. In this guide, you'll learn how
to use Moor with a `mysql` database running on a server and with an encrypted database on a mobile device!
## MySQL
We'll connect to a MySQL server with the `sqljockey5` library, so let's first add that library to the `pubspec.yaml`:
```yaml
dependencies:
// you'll also need moor, of course
sqljocky5: ^2.2.0
```

View File

@ -1 +0,0 @@
Example that uses moor with an mysql server.

View File

@ -1,48 +0,0 @@
import 'package:moor/moor.dart';
import 'package:sqljocky5/connection/settings.dart';
import 'mysql.dart';
part 'database.g.dart';
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
}
@UseMoor(tables: [Users])
class Database extends _$Database {
Database._(QueryExecutor e) : super(e);
factory Database() {
final settings = ConnectionSettings(
user: 'root',
password: 'password',
host: 'localhost',
port: 3306,
db: 'example',
);
return Database._(MySqlBackend(settings));
}
@override
int get schemaVersion => 1;
@override
MigrationStrategy get migration {
return MigrationStrategy(
beforeOpen: (engine, details) async {
// we don't have migrations in mysql, so let's run them manually here!
final migrator = Migrator(this, engine.customStatement);
// will emit a "IF NOT EXISTS" statement, so its safe to run this
// on every open
await migrator.createAllTables();
},
);
}
Future<void> insertUser(String name) async {
await into(users).insert(UsersCompanion(name: Value(name)));
}
}

View File

@ -1,160 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'database.dart';
// **************************************************************************
// MoorGenerator
// **************************************************************************
// ignore_for_file: unnecessary_brace_in_string_interps
class User extends DataClass implements Insertable<User> {
final int id;
final String name;
User({@required this.id, @required this.name});
factory User.fromData(Map<String, dynamic> data, GeneratedDatabase db,
{String prefix}) {
final effectivePrefix = prefix ?? '';
final intType = db.typeSystem.forDartType<int>();
final stringType = db.typeSystem.forDartType<String>();
return User(
id: intType.mapFromDatabaseResponse(data['${effectivePrefix}id']),
name: stringType.mapFromDatabaseResponse(data['${effectivePrefix}name']),
);
}
factory User.fromJson(Map<String, dynamic> json,
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
return User(
id: serializer.fromJson<int>(json['id']),
name: serializer.fromJson<String>(json['name']),
);
}
@override
Map<String, dynamic> toJson(
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
return {
'id': serializer.toJson<int>(id),
'name': serializer.toJson<String>(name),
};
}
@override
T createCompanion<T extends UpdateCompanion<User>>(bool nullToAbsent) {
return UsersCompanion(
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
name: name == null && nullToAbsent ? const Value.absent() : Value(name),
) as T;
}
User copyWith({int id, String name}) => User(
id: id ?? this.id,
name: name ?? this.name,
);
@override
String toString() {
return (StringBuffer('User(')
..write('id: $id, ')
..write('name: $name')
..write(')'))
.toString();
}
@override
int get hashCode => $mrjf($mrjc($mrjc(0, id.hashCode), name.hashCode));
@override
bool operator ==(other) =>
identical(this, other) ||
(other is User && other.id == id && other.name == name);
}
class UsersCompanion extends UpdateCompanion<User> {
final Value<int> id;
final Value<String> name;
const UsersCompanion({
this.id = const Value.absent(),
this.name = const Value.absent(),
});
}
class $UsersTable extends Users with TableInfo<$UsersTable, User> {
final GeneratedDatabase _db;
final String _alias;
$UsersTable(this._db, [this._alias]);
final VerificationMeta _idMeta = const VerificationMeta('id');
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id => _id ??= _constructId();
GeneratedIntColumn _constructId() {
return GeneratedIntColumn('id', $tableName, false, hasAutoIncrement: true);
}
final VerificationMeta _nameMeta = const VerificationMeta('name');
GeneratedTextColumn _name;
@override
GeneratedTextColumn get name => _name ??= _constructName();
GeneratedTextColumn _constructName() {
return GeneratedTextColumn(
'name',
$tableName,
false,
);
}
@override
List<GeneratedColumn> get $columns => [id, name];
@override
$UsersTable get asDslTable => this;
@override
String get $tableName => _alias ?? 'users';
@override
final String actualTableName = 'users';
@override
VerificationContext validateIntegrity(UsersCompanion d,
{bool isInserting = false}) {
final context = VerificationContext();
if (d.id.present) {
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
} else if (id.isRequired && isInserting) {
context.missing(_idMeta);
}
if (d.name.present) {
context.handle(
_nameMeta, name.isAcceptableValue(d.name.value, _nameMeta));
} else if (name.isRequired && isInserting) {
context.missing(_nameMeta);
}
return context;
}
@override
Set<GeneratedColumn> get $primaryKey => {id};
@override
User map(Map<String, dynamic> data, {String tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null;
return User.fromData(data, _db, prefix: effectivePrefix);
}
@override
Map<String, Variable> entityToSql(UsersCompanion d) {
final map = <String, Variable>{};
if (d.id.present) {
map['id'] = Variable<int, IntType>(d.id.value);
}
if (d.name.present) {
map['name'] = Variable<String, StringType>(d.name.value);
}
return map;
}
@override
$UsersTable createAlias(String alias) {
return $UsersTable(_db, alias);
}
}
abstract class _$Database extends GeneratedDatabase {
_$Database(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e);
$UsersTable _users;
$UsersTable get users => _users ??= $UsersTable(this);
@override
List<TableInfo> get allTables => [users];
}

View File

@ -1,6 +0,0 @@
import 'lib/database.dart';
void main() async {
final database = Database();
await database.insertUser('MySql test');
}

View File

@ -1,21 +0,0 @@
name: mysql
description: A sample command-line application.
# version: 1.0.0
# homepage: https://www.example.com
# author: Simon Binder <oss@simonbinder.eu>
environment:
sdk: '>=2.4.0 <3.0.0'
dependencies:
moor: ^1.6.0
sqljocky5: ^2.2.0
dev_dependencies:
test: ^1.6.0
moor_generator: ^1.6.0
build_runner:
dependency_overrides:
moor:
path: ../../moor

26
extras/mysql/README.md Normal file
View File

@ -0,0 +1,26 @@
Experimental support for using Moor with an MySQL server.
## Using this
For general notes on using moor, see [this guide](https://moor.simonbinder.eu/getting-started/).
To use the MySQL backend, also add this to your pubspec (you don't need to depend on
`moor_flutter`).
```yaml
dependencies:
moor: "$latest version"
moor_mysql:
git:
url: https://github.com/simolus3/moor.git
path: extras/mysql
```
Then, instead of using a `FlutterQueryExecutor`, use a `MySqlBackend` with the
right [`ConnectionSettings`](https://pub.dev/documentation/sqljocky5/latest/connection_settings/ConnectionSettings-class.html).
You'll need to import `package:moor_mysql/moor_mysql.dart`.
## Limitations
We're currently experimenting with other database engines - Moor was mainly designed for
sqlite and supporting advanced features of MySQL is not a priority right now.
- No migrations
- Some statements don't work
- Compiled custom queries don't work - we can only parse sqlite. Of course, runtime custom
queries with `customSelect` and `customUpdate` will work as expected.

View File

@ -1,8 +1,13 @@
/// Experimental support to run moor on a mysql backend.
library moor_mysql;
import 'package:moor/backends.dart';
import 'package:moor/moor.dart';
import 'package:sqljocky5/connection/connection.dart';
import 'package:sqljocky5/sqljocky.dart';
export 'package:sqljocky5/sqljocky.dart' show ConnectionSettings;
class MySqlBackend extends DelegatedDatabase {
MySqlBackend(ConnectionSettings settings)
: super(_MySqlDelegate(settings), logStatements: true);

16
extras/mysql/pubspec.yaml Normal file
View File

@ -0,0 +1,16 @@
name: moor_mysql
description: MySQL support for moor
environment:
sdk: '>=2.4.0 <3.0.0'
dependencies:
moor: ^1.6.0
sqljocky5: ^2.2.0
dev_dependencies:
test: ^1.6.0
dependency_overrides:
moor:
path: ../../moor