Skip to content

Commit

Permalink
Support MySQL field filter and mongo collection show
Browse files Browse the repository at this point in the history
  • Loading branch information
HaitaoDeng committed Feb 11, 2022
1 parent 585b710 commit 1c95b4f
Show file tree
Hide file tree
Showing 15 changed files with 596 additions and 35 deletions.
33 changes: 33 additions & 0 deletions lib/api/mongo/mongo_tables_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
// under the License.
//

import 'dart:collection';

import 'package:mongo_dart/mongo_dart.dart';
import 'package:paas_dashboard_flutter/module/mongo/mongo_sql_result.dart';
import 'package:paas_dashboard_flutter/module/mongo/mongo_table.dart';

class MongoTablesApi {
Expand All @@ -30,4 +33,34 @@ class MongoTablesApi {
return new TableResp(name);
}).toList();
}

static Future<MongoSqlResult> getTableData(
String addr, String username, String password, String databaseName, String tableName) async {
var db = await Db.create(addr + "/" + databaseName);
await db.open();
var collection = await db.collection(tableName);
SelectorBuilder builder = SelectorBuilder().limit(100);
// Stream<Map<String, dynamic>> data = await collection.find(builder);
// data.map((event) => event.keys.toSet()).
List<Map<String, dynamic>> data = await collection.find(builder).toList();
if (data.isEmpty) {
return MongoSqlResult.create();
}
MongoSqlResult result = MongoSqlResult.create();
LinkedHashSet<String> fieldNames = new LinkedHashSet();
List<List<Object?>> sqldata = [];
data.forEach((element) {
fieldNames.addAll(element.keys);
});
data.forEach((element) {
List<Object?> temp = [];
fieldNames.forEach((fieldName) {
temp.add(element[fieldName]);
});
sqldata.add(temp);
});
result.fieldName = fieldNames;
result.data = sqldata;
return result;
}
}
99 changes: 85 additions & 14 deletions lib/api/mysql/mysql_databases_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
// under the License.
//

import 'dart:developer';

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:paas_dashboard_flutter/ui/component/dynamic_filter_table.dart';
import 'package:sprintf/sprintf.dart';

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

static const String TABLE_COLUMN =
Expand Down Expand Up @@ -59,21 +62,31 @@ class MysqlDatabaseApi {
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());
static Future<MysqlSqlResult> getData(
MysqlInstancePo mysqlConn, String dbname, String tableName, String where) async {
MysqlSqlResult result = MysqlSqlResult.create();
try {
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, where]));
List<List<Object?>> data = [];
for (var row in queryResult) {
if (row.values != null) {
data.add(row.values!);
}
}
result.setFieldName = queryResult.fields.map((e) => e.name!.isNotEmpty ? e.name.toString() : "").toList();
result.setData = data;
await conn.close();
} catch (e) {
log('mysql getData Exception ${e.toString()}');
throw Exception('Exception: ${e.toString()}');
}
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;
}

Expand Down Expand Up @@ -108,4 +121,62 @@ class MysqlDatabaseApi {
await conn.close();
return result;
}

static String getWhere(List<DropDownButtonData>? filters) {
String where = "";
if (filters == null || filters.isEmpty) {
return where;
}
where += "where ";
for (int i = 0; i < filters.length; i++) {
String column = filters[i].column;
String op = getWhereOP(filters[i].op);
where = where + column + sprintf(op, [getOPValue(filters[i].value!, filters[i].type, filters[i].op)]);
if (i != filters.length - 1) {
where += filters[i].join ? " and " : " or ";
}
}
return where;
}

static String getOPValue(String value, TYPE type, OP op) {
if (OP.BEGIN == op || OP.END == op || OP.CONTAIN == op || OP.EXCLUDE == op || OP.INCLUDE == op) {
return value;
} else if (OP.NULL == op || OP.NOT_NULL == op) {
return "";
} else {
return type == TYPE.TEXT ? sprintf("'%s'", [value]) : value;
}
}

static String getWhereOP(OP op) {
switch (op) {
case OP.EQ:
return " = %s ";
case OP.NOT_EQ:
return " != %s ";
case OP.LT:
return " < %s ";
case OP.GT:
return " > %s ";
case OP.LT_EQ:
return " <= %s ";
case OP.GT_EQ:
return " >= %s ";
case OP.NULL:
return " is null ";
case OP.NOT_NULL:
return " is not null ";
case OP.INCLUDE:
return " in (%s) ";
case OP.EXCLUDE:
return " not in (%s) ";
case OP.BEGIN:
return " like '%s%%' ";
case OP.END:
return " like '%%%s' ";
case OP.CONTAIN:
return " like '%%%s%%' ";
}
}
}
1 change: 1 addition & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"email": "email",
"entryId": "entryId",
"execute": "execute",
"filter": "filter",
"forceDelete": "forceDelete",
"isLeader": "Is Leader",
"languageSettings": "Language Settings",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/intl_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"email": "邮箱",
"entryId": "编号",
"execute": "执行",
"filter": "过滤",
"forceDelete": "强制删除",
"isLeader": "是否是主节点",
"languageSettings": "语言设置",
Expand Down
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import 'package:paas_dashboard_flutter/vm/kubernetes/k8s_instance_list_view_mode
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/mongo/mongo_table_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_sql_query_view_model.dart';
Expand Down Expand Up @@ -144,6 +145,10 @@ class MyApp extends StatelessWidget {
final args = settings.arguments as MongoDatabaseViewModel;
return RouteGen.mongoDatabase(args);
}
if (settings.name == PageRouteConst.MongoTable) {
final args = settings.arguments as MongoTableViewModel;
return RouteGen.mongoTableData(args);
}
if (settings.name == PageRouteConst.MysqlInstance) {
final args = settings.arguments as MysqlInstanceViewModel;
return RouteGen.mysqlInstance(args);
Expand Down
50 changes: 50 additions & 0 deletions lib/module/mongo/mongo_sql_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// 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 'dart:collection';

class MongoSqlResult {
Set<String> fieldName;

List<List<Object?>> data;

MongoSqlResult(this.fieldName, this.data);

Set<String> get getFieldName {
return fieldName;
}

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

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

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

factory MongoSqlResult.create() {
return MongoSqlResult(HashSet.identity(), [
['']
]);
}
}
6 changes: 3 additions & 3 deletions lib/module/mysql/mysql_sql_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class MysqlSqlResult {
List<String> fieldName;

List<List<Object>> data;
List<List<Object?>> data;

MysqlSqlResult(this.fieldName, this.data);

Expand All @@ -32,11 +32,11 @@ class MysqlSqlResult {
this.fieldName = fieldName;
}

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

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

Expand Down
1 change: 1 addition & 0 deletions lib/route/page_route_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PageRouteConst {
static const String Mongo = '/mongo';
static const String MongoInstance = '/mongo/instance';
static const String MongoDatabase = '/mongo/instance/database';
static const String MongoTable = '/mongo/instance/database/table';
static const String Mysql = '/mysql';
static const String MysqlInstance = '/mysql/instance';
static const String MysqlDatabase = '/mysql/instance/database';
Expand Down
11 changes: 11 additions & 0 deletions lib/route/route_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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/mongo/widget/mongo_table_data.dart';
import 'package:paas_dashboard_flutter/ui/mysql/mysql_instance.dart';
import 'package:paas_dashboard_flutter/ui/mysql/widget/mysql_sql_query.dart';
import 'package:paas_dashboard_flutter/ui/mysql/widget/mysql_table_column.dart';
Expand All @@ -38,6 +39,7 @@ 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/mongo/mongo_table_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_sql_query_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mysql/mysql_table_column_view_model.dart';
Expand Down Expand Up @@ -82,6 +84,15 @@ class RouteGen {
));
}

static Route mongoTableData(MongoTableViewModel viewModel) {
// deep copy view model
return MaterialPageRoute(
builder: (context) => ChangeNotifierProvider(
create: (context) => viewModel,
child: MongoTableDataWidget(),
));
}

static Route mysqlInstance(MysqlInstanceViewModel viewModel) {
return MaterialPageRoute(
builder: (context) => ChangeNotifierProvider(
Expand Down
Loading

0 comments on commit 1c95b4f

Please sign in to comment.