Skip to content

Commit

Permalink
Support MySQL Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
HaitaoDeng committed Jan 22, 2022
1 parent 680d2e6 commit 30ffaec
Show file tree
Hide file tree
Showing 21 changed files with 947 additions and 2 deletions.
60 changes: 59 additions & 1 deletion lib/api/mysql/mysql_databases_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,68 @@
// under the License.
//

import 'package:mysql1/mysql1.dart';
import 'package:paas_dashboard_flutter/module/mysql/mysql_database.dart';
import 'package:paas_dashboard_flutter/module/mysql/mysql_sql_result.dart';
import 'package:paas_dashboard_flutter/module/mysql/mysql_table.dart';
import 'package:paas_dashboard_flutter/persistent/po/mysql_instance_po.dart';
import 'package:sprintf/sprintf.dart';

class MysqlDatabaseApi {
static const String SELECT_ALL = "select * from %s limit 100";

static Future<List<DatabaseResp>> getDatabaseList(String host, int port, String username, String password) async {
throw UnimplementedError();
final setting = new ConnectionSettings(host: host, port: port, user: username, password: password);
final MySqlConnection conn = await MySqlConnection.connect(setting);
var queryResult = await conn.query('show databases');
List<DatabaseResp> result = [];
for (var row in queryResult) {
result.add(new DatabaseResp(row[0]));
}
await conn.close();
return result;
}

static Future<List<TableResp>> getTableList(
String host, int port, String username, String password, String db) async {
final setting = new ConnectionSettings(host: host, port: port, user: username, password: password, db: db);
final MySqlConnection conn = await MySqlConnection.connect(setting);
var queryResult = await conn.query('show tables');
List<TableResp> result = [];
for (var row in queryResult) {
result.add(new TableResp(row[0]));
}
await conn.close();
return result;
}

static Future<MysqlSqlResult> getData(MysqlInstancePo mysqlConn, String dbname, String? tableName) async {
final setting = new ConnectionSettings(
host: mysqlConn.host, port: mysqlConn.port, user: mysqlConn.username, password: mysqlConn.password, db: dbname);
final MySqlConnection conn = await MySqlConnection.connect(setting);
var queryResult = await conn.query(sprintf(SELECT_ALL, [tableName]));
List<List<Object>> data = [];
for (var row in queryResult) {
if (row.values != null) {
data.add(row.values!.map((e) => e == null ? "" : e).toList());
}
}
MysqlSqlResult result = MysqlSqlResult.create();
result.setFieldName = queryResult.fields.map((e) => e.name!.isNotEmpty ? e.name.toString() : "").toList();
result.setData = data;
await conn.close();
return result;
}

static Future<List<String>> getUsers(String host, int port, String username, String password) async {
final setting = new ConnectionSettings(host: host, port: port, user: username, password: password, db: "mysql");
final MySqlConnection conn = await MySqlConnection.connect(setting);
var queryResult = await conn.query("select User from user");
List<String> result = [];
for (var row in queryResult) {
result.add(row[0]);
}
await conn.close();
return result;
}
}
3 changes: 3 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"consume": "consume",
"consumer": "Consumer",
"consumerList": "Consumer List",
"database": "Database",
"delete": "Delete",
"deleteNamespace": "Delete Namespace",
"deleteTenant": "Delete Tenant",
Expand Down Expand Up @@ -50,12 +51,14 @@
"subscriptionList": "Subscription list",
"subscriptionName": "Subscription Name",
"sqlQuery": "sql query",
"tables": "Tables",
"tenant": "tenant",
"tenantName": "Tenant Name",
"tenants": "Tenants",
"topicDetail": "topic detail",
"topicName": "Topic Name",
"topics": "Topics",
"unit": "unit",
"userName": "UserName",
"versionName": "Version"
}
3 changes: 3 additions & 0 deletions lib/l10n/intl_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"consume": "消费",
"consumer": "消费者",
"consumerList": "消费者列表",
"database": "数据库",
"delete": "删除",
"deleteNamespace": "删除命名空间",
"deleteTenant": "删除租户",
Expand Down Expand Up @@ -50,12 +51,14 @@
"subscriptionList": "订阅列表",
"subscriptionName": "订阅名称",
"sqlQuery": "sql 查询",
"tables": "数据库表",
"tenant": "租户",
"tenantName": "租户名称",
"tenants": "租户列表",
"topicDetail": "主题详情",
"topicName": "Topic 名称",
"topics": "Topic 列表",
"unit": "单位",
"userName": "用户名",
"versionName": "版本"
}
16 changes: 16 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import 'package:paas_dashboard_flutter/vm/mongo/mongo_database_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mongo/mongo_instance_list_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mongo/mongo_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_instance_list_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_table_data_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_table_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_list_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_namespace_view_model.dart';
Expand Down Expand Up @@ -138,6 +141,18 @@ class MyApp extends StatelessWidget {
final args = settings.arguments as MongoDatabaseViewModel;
return RouteGen.mongoDatabase(args);
}
if (settings.name == PageRouteConst.MysqlInstance) {
final args = settings.arguments as MysqlInstanceViewModel;
return RouteGen.mysqlInstance(args);
}
if (settings.name == PageRouteConst.MysqlDatabase) {
final args = settings.arguments as MysqlTablesViewModel;
return RouteGen.mysqlTables(args);
}
if (settings.name == PageRouteConst.MysqlTable) {
final args = settings.arguments as MysqlTableDetailViewModel;
return RouteGen.mysqlTableDetail(args);
}
if (settings.name == PageRouteConst.PulsarInstance) {
final args = settings.arguments as PulsarInstanceViewModel;
return RouteGen.pulsarInstance(args);
Expand Down Expand Up @@ -170,6 +185,7 @@ class MyApp extends StatelessWidget {
final args = settings.arguments as SqlViewModel;
return RouteGen.sqlExecute(args);
}

throw UnimplementedError();
},
);
Expand Down
46 changes: 46 additions & 0 deletions lib/module/mysql/mysql_sql_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

class MysqlSqlResult {
List<String> fieldName;

List<List<Object>> data;

MysqlSqlResult(this.fieldName, this.data);

List<String> get getFieldName {
return fieldName;
}

set setFieldName(List<String> fieldName) {
this.fieldName = fieldName;
}

List<List<Object>> get getData {
return data;
}

set setData(List<List<Object>> data) {
this.data = data;
}

factory MysqlSqlResult.create() {
return MysqlSqlResult([], []);
}
}
1 change: 1 addition & 0 deletions lib/route/page_route_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class PageRouteConst {
static const String Mysql = '/mysql';
static const String MysqlInstance = '/mysql/instance';
static const String MysqlDatabase = '/mysql/instance/database';
static const String MysqlTable = '/mysql/instance/table';
static const String Pulsar = '/pulsar';
static const String PulsarInstance = '/pulsar/instance';
static const String PulsarTenant = '/pulsar/instance/tenant';
Expand Down
30 changes: 30 additions & 0 deletions lib/route/route_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/ui/code/screen/code_execute_screen.dart';
import 'package:paas_dashboard_flutter/ui/mongo/mongo_instance.dart';
import 'package:paas_dashboard_flutter/ui/mongo/screen/mongo_database.dart';
import 'package:paas_dashboard_flutter/ui/mysql/mysql_instance.dart';
import 'package:paas_dashboard_flutter/ui/mysql/widget/mysql_table_data.dart';
import 'package:paas_dashboard_flutter/ui/mysql/widget/mysql_tables.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/pulsar_instance.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_namespace.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_partitioned_topic.dart';
Expand All @@ -32,6 +35,9 @@ import 'package:paas_dashboard_flutter/ui/sql/screen/sql_execute_screen.dart';
import 'package:paas_dashboard_flutter/vm/code/code_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mongo/mongo_database_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mongo/mongo_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_table_data_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_table_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_namespace_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_partitioned_topic_view_model.dart';
Expand Down Expand Up @@ -70,6 +76,30 @@ class RouteGen {
));
}

static Route mysqlInstance(MysqlInstanceViewModel viewModel) {
return MaterialPageRoute(
builder: (context) => ChangeNotifierProvider(
create: (context) => viewModel,
child: MysqlInstanceScreen(),
));
}

static Route mysqlTables(MysqlTablesViewModel viewModel) {
return MaterialPageRoute(
builder: (context) => ChangeNotifierProvider(
create: (context) => viewModel,
child: MysqlTablesWidget(),
));
}

static Route mysqlTableDetail(MysqlTableDetailViewModel viewModel) {
return MaterialPageRoute(
builder: (context) => ChangeNotifierProvider(
create: (context) => viewModel,
child: MysqlTableDataWidget(),
));
}

static Route pulsarInstance(PulsarInstanceViewModel viewModel) {
// deep copy view model
return MaterialPageRoute(
Expand Down
1 change: 1 addition & 0 deletions lib/ui/general/author_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AuthorScreen extends StatelessWidget {
List<Author> authors = [
Author('fu_turer', 'Tian Luo', "futurer@outlook.com"),
Author('goflutterjava', 'KeLe He', "goflutterjava@gmail.com"),
Author('Jaime', 'HaiTao Deng', "denghaitao001@gmail.com"),
Author('lovehzj', 'TingTing Wang', "1922919664@qq.com"),
Author('shoothzj', 'ZhangJian He', 'shoothzj@gmail.com'),
Author('zxJin-x', 'ZhiXin Jin', 'jinzhixin096@gmail.com'),
Expand Down
70 changes: 70 additions & 0 deletions lib/ui/mysql/mysql_instance.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/generated/l10n.dart';
import 'package:paas_dashboard_flutter/ui/mysql/widget/mysql_database.dart';
import 'package:paas_dashboard_flutter/ui/mysql/widget/mysql_user.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_database_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_instance_view_model.dart';
import 'package:provider/provider.dart';

/// MySQL instance window
class MysqlInstanceScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new _MysqlInstanceState();
}
}

class _MysqlInstanceState extends State<MysqlInstanceScreen> {
_MysqlInstanceState();

@override
Widget build(BuildContext context) {
MysqlInstanceViewModel vm = Provider.of<MysqlInstanceViewModel>(context);

return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('Mysql ${vm.name} db Dashboard'),
bottom: TabBar(tabs: [
Tab(
text: S.of(context).database,
),
Tab(text: S.of(context).detail),
]),
),
body: TabBarView(
children: [
ChangeNotifierProvider(
create: (context) => MysqlDatabaseViewModel(vm.mysqlInstancePo.deepCopy()),
child: MysqlDatabaseWidget(),
).build(context),
ChangeNotifierProvider(
create: (context) => vm.deepCopy(),
child: MysqlUserWidget(),
).build(context)
],
),
),
);
}
}
1 change: 1 addition & 0 deletions lib/ui/mysql/mysql_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:paas_dashboard_flutter/ui/util/form_util.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_instance_list_view_model.dart';
import 'package:provider/provider.dart';

/// MySQL Instance List Windows
class MysqlPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
Expand Down
Loading

0 comments on commit 30ffaec

Please sign in to comment.