Skip to content

Commit 7b011cc

Browse files
committed
init mysql
1 parent 345dc08 commit 7b011cc

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class ConnectionInfo {
2+
ConnectionInfo({
3+
required this.host,
4+
required this.port,
5+
required this.userName,
6+
required this.password,
7+
required this.databaseName,
8+
this.secure,
9+
this.collation,
10+
});
11+
12+
final String host;
13+
final int port;
14+
final String userName;
15+
final String password;
16+
final String databaseName;
17+
final bool? secure;
18+
final String? collation;
19+
}

lib/store/mysql/const.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const kVersion = 1;

lib/store/mysql/migration.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:mysql_client/mysql_client.dart';
2+
3+
class Migration {
4+
Migration(
5+
this.originVersion,
6+
this.targetVersion,
7+
this.execute,
8+
);
9+
10+
final int originVersion;
11+
final int targetVersion;
12+
final Function(MySQLConnection) execute;
13+
}

lib/store/mysql/mysql.dart

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import 'package:mysql_client/mysql_client.dart';
2+
3+
import 'connection_info.dart';
4+
import 'migration.dart';
5+
6+
class FeMySql {
7+
// 工厂构造函数,用于创建单例实例
8+
factory FeMySql({ConnectionInfo? connectionInfo}) {
9+
return _instance.._init(connectionInfo);
10+
}
11+
// 私有构造方法,确保只能在类内部创建实例
12+
FeMySql._();
13+
14+
// 单例实例
15+
static final FeMySql _instance = FeMySql._();
16+
17+
late MySQLConnection _conn;
18+
List<Migration>? migrations;
19+
20+
// 内部初始化方法
21+
Future<void> _init(ConnectionInfo? connectionInfo) async {
22+
_conn = await MySQLConnection.createConnection(
23+
host: connectionInfo!.host,
24+
port: connectionInfo.port,
25+
userName: connectionInfo.userName,
26+
password: connectionInfo.password,
27+
databaseName: connectionInfo.databaseName,
28+
);
29+
await _conn.connect();
30+
}
31+
32+
FeMySql addMigrations(List<Migration> migrations) {
33+
this.migrations = migrations;
34+
return this;
35+
}
36+
37+
Future<void> migrate() async {
38+
if (migrations == null) {
39+
return;
40+
}
41+
42+
final version = await getVersion() ?? 0;
43+
final lastVersion = migrations!.last.targetVersion;
44+
45+
if (version == lastVersion) {
46+
return;
47+
}
48+
49+
for (final migration in migrations!) {
50+
if (version < migration.targetVersion) {
51+
await migration.execute(_conn);
52+
await setVersion(migration.targetVersion);
53+
}
54+
}
55+
}
56+
57+
Future<int?> getVersion() async {
58+
final result = await _conn.execute('SELECT version FROM version');
59+
if (result.isEmpty) {
60+
return null;
61+
}
62+
63+
return result.first as int;
64+
}
65+
66+
Future<void> setVersion(int targetVersion) async {
67+
await _conn.execute('UPDATE version SET version = $targetVersion');
68+
}
69+
}

pubspec.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ packages:
146146
url: "https://pub.flutter-io.cn"
147147
source: hosted
148148
version: "2.1.1"
149+
buffer:
150+
dependency: transitive
151+
description:
152+
name: buffer
153+
sha256: "94f60815065a8f0fd4f05be51faf86cf86519327e039d5c2aac72e1d1cc1dad4"
154+
url: "https://pub.flutter-io.cn"
155+
source: hosted
156+
version: "1.2.2"
149157
build:
150158
dependency: transitive
151159
description:
@@ -1461,6 +1469,14 @@ packages:
14611469
url: "https://pub.flutter-io.cn"
14621470
source: hosted
14631471
version: "2.1.2"
1472+
mysql_client:
1473+
dependency: "direct main"
1474+
description:
1475+
name: mysql_client
1476+
sha256: "6a0fdcbe3e0721c637f97ad24649be2f70dbce2b21ede8f962910e640f753fc2"
1477+
url: "https://pub.flutter-io.cn"
1478+
source: hosted
1479+
version: "0.0.27"
14641480
native_dio_adapter:
14651481
dependency: "direct main"
14661482
description:
@@ -2142,6 +2158,14 @@ packages:
21422158
url: "https://pub.flutter-io.cn"
21432159
source: hosted
21442160
version: "1.0.1"
2161+
tuple:
2162+
dependency: transitive
2163+
description:
2164+
name: tuple
2165+
sha256: a97ce2013f240b2f3807bcbaf218765b6f301c3eff91092bcfa23a039e7dd151
2166+
url: "https://pub.flutter-io.cn"
2167+
source: hosted
2168+
version: "2.0.2"
21452169
typed_data:
21462170
dependency: transitive
21472171
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ dependencies:
196196
flutter_windowmanager: ^0.2.0
197197
integral_isolates: ^0.4.1
198198
cupertino_battery_indicator: ^1.1.0
199+
mysql_client: ^0.0.27
199200

200201
dev_dependencies:
201202
# flutter pub run build_runner build --delete-conflicting-outputs

0 commit comments

Comments
 (0)