Skip to content

Commit 6927b68

Browse files
committed
refactor(mysql_utils): optimize code format and structure
- adjust code indentation and line breaks to improve readability - update description information in pubspec.yaml
1 parent d9b918b commit 6927b68

File tree

2 files changed

+87
-26
lines changed

2 files changed

+87
-26
lines changed

lib/src/mysql_utils.dart

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class MysqlUtils {
5454
}
5555

5656
///create single connection
57-
Future<MySQLConnection> createConnectionSingle(MysqlUtilsSettings settings) async {
57+
Future<MySQLConnection> createConnectionSingle(
58+
MysqlUtilsSettings settings) async {
5859
final conn = await MySQLConnection.createConnection(
5960
host: settings.host,
6061
port: settings.port,
@@ -71,7 +72,8 @@ class MysqlUtils {
7172
}
7273

7374
///create pool connection
74-
Future<MySQLConnectionPool> createConnectionPool(MysqlUtilsSettings settings) async {
75+
Future<MySQLConnectionPool> createConnectionPool(
76+
MysqlUtilsSettings settings) async {
7577
return MySQLConnectionPool(
7678
host: settings.host,
7779
port: settings.port,
@@ -90,7 +92,8 @@ class MysqlUtils {
9092
///isConnectionAlive
9193
Future<bool> isConnectionAlive() async {
9294
try {
93-
await query('select 1').timeout(Duration(milliseconds: 500), onTimeout: () {
95+
await query('select 1').timeout(Duration(milliseconds: 500),
96+
onTimeout: () {
9497
throw TimeoutException('test isConnectionAlive timeout.');
9598
});
9699
return true;
@@ -144,7 +147,11 @@ class MysqlUtils {
144147
}) async {
145148
table = _tableParse(table);
146149
List _whereAndValues = _whereParse(where);
147-
ResultFormat results = await query('DELETE FROM $table ${_whereAndValues.first} ', debug: debug, whereValues: _whereAndValues.last, isStmt: true);
150+
ResultFormat results = await query(
151+
'DELETE FROM $table ${_whereAndValues.first} ',
152+
debug: debug,
153+
whereValues: _whereAndValues.last,
154+
isStmt: true);
148155
return results.affectedRows;
149156
}
150157

@@ -183,7 +190,8 @@ class MysqlUtils {
183190

184191
String _setValue = _setkeys.join(',');
185192
String _sql = 'UPDATE $table SET $_setValue ${_whereAndValues.first}';
186-
ResultFormat results = await query(_sql, whereValues: values, debug: debug, isStmt: true);
193+
ResultFormat results =
194+
await query(_sql, whereValues: values, debug: debug, isStmt: true);
187195
return results.affectedRows;
188196
}
189197

@@ -225,7 +233,8 @@ class MysqlUtils {
225233
final placeholders = List.filled(fields.length, '?').join(',');
226234
final fieldsString = fields.map((k) => '`$k`').join(',');
227235

228-
final valuesPlaceholder = List.filled(insertData.length, '($placeholders)').join(',');
236+
final valuesPlaceholder =
237+
List.filled(insertData.length, '($placeholders)').join(',');
229238

230239
final values = <dynamic>[];
231240
for (final row in insertData) {
@@ -234,9 +243,11 @@ class MysqlUtils {
234243
}
235244
}
236245

237-
final sql = '${replace ? 'REPLACE' : 'INSERT'} INTO $table ($fieldsString) VALUES $valuesPlaceholder';
246+
final sql =
247+
'${replace ? 'REPLACE' : 'INSERT'} INTO $table ($fieldsString) VALUES $valuesPlaceholder';
238248

239-
final result = await query(sql, whereValues: values, debug: debug, isStmt: true);
249+
final result =
250+
await query(sql, whereValues: values, debug: debug, isStmt: true);
240251
return result.affectedRows;
241252
}
242253

@@ -271,7 +282,8 @@ class MysqlUtils {
271282
final sql = '${replace ? 'REPLACE' : 'INSERT'} INTO $table '
272283
'(${fields.join(',')}) VALUES (${placeholders.join(',')})';
273284

274-
final result = await query(sql, whereValues: values, debug: debug, isStmt: true);
285+
final result =
286+
await query(sql, whereValues: values, debug: debug, isStmt: true);
275287
return result.lastInsertID;
276288
}
277289

@@ -295,7 +307,15 @@ class MysqlUtils {
295307
String having = '',
296308
bool debug = false,
297309
}) async {
298-
double res = await _keyMaxMinAvgCount(table: table, fields: fields, where: where, group: group, having: having, debug: debug, sqlKey: 'COUNT', sqlValue: '_count');
310+
double res = await _keyMaxMinAvgCount(
311+
table: table,
312+
fields: fields,
313+
where: where,
314+
group: group,
315+
having: having,
316+
debug: debug,
317+
sqlKey: 'COUNT',
318+
sqlValue: '_count');
299319
return res.toInt();
300320
}
301321

@@ -319,7 +339,15 @@ class MysqlUtils {
319339
String having = '',
320340
bool debug = false,
321341
}) async {
322-
return _keyMaxMinAvgCount(table: table, fields: fields, where: where, group: group, having: having, debug: debug, sqlKey: 'AVG', sqlValue: '_avg');
342+
return _keyMaxMinAvgCount(
343+
table: table,
344+
fields: fields,
345+
where: where,
346+
group: group,
347+
having: having,
348+
debug: debug,
349+
sqlKey: 'AVG',
350+
sqlValue: '_avg');
323351
}
324352

325353
///```
@@ -342,7 +370,15 @@ class MysqlUtils {
342370
String having = '',
343371
bool debug = false,
344372
}) async {
345-
return _keyMaxMinAvgCount(table: table, fields: fields, where: where, group: group, having: having, debug: debug, sqlKey: 'MAX', sqlValue: '_max');
373+
return _keyMaxMinAvgCount(
374+
table: table,
375+
fields: fields,
376+
where: where,
377+
group: group,
378+
having: having,
379+
debug: debug,
380+
sqlKey: 'MAX',
381+
sqlValue: '_max');
346382
}
347383

348384
///```
@@ -365,7 +401,15 @@ class MysqlUtils {
365401
String having = '',
366402
bool debug = false,
367403
}) async {
368-
return _keyMaxMinAvgCount(table: table, fields: fields, where: where, group: group, having: having, debug: debug, sqlKey: 'MIN', sqlValue: '_min');
404+
return _keyMaxMinAvgCount(
405+
table: table,
406+
fields: fields,
407+
where: where,
408+
group: group,
409+
having: having,
410+
debug: debug,
411+
sqlKey: 'MIN',
412+
sqlValue: '_min');
369413
}
370414

371415
Future<double> _keyMaxMinAvgCount({
@@ -388,8 +432,10 @@ class MysqlUtils {
388432
final whereValues = whereResult.last;
389433
final groupClause = group.isNotEmpty ? ' GROUP BY $group' : '';
390434
final havingClause = having.isNotEmpty ? ' HAVING $having' : '';
391-
final sql = 'SELECT $sqlKey($fields) AS $sqlValue FROM $table$whereClause$groupClause$havingClause';
392-
final result = await query(sql, whereValues: whereValues, debug: debug, isStmt: true);
435+
final sql =
436+
'SELECT $sqlKey($fields) AS $sqlValue FROM $table$whereClause$groupClause$havingClause';
437+
final result =
438+
await query(sql, whereValues: whereValues, debug: debug, isStmt: true);
393439
final rawValue = result.rows.first[sqlValue];
394440
if (rawValue == null) return 0.0;
395441
if (rawValue is num) return rawValue.toDouble();
@@ -436,9 +482,14 @@ class MysqlUtils {
436482
table = _tableParse(table);
437483
limit = _limitParse(limit);
438484

439-
String _sql = 'SELECT $fields FROM $table ${_whereAndValues.first} $group $having $order $limit';
485+
String _sql =
486+
'SELECT $fields FROM $table ${_whereAndValues.first} $group $having $order $limit';
440487

441-
ResultFormat results = await query(_sql, debug: debug, whereValues: _whereAndValues.last, isStmt: true, excludeFields: excludeFields);
488+
ResultFormat results = await query(_sql,
489+
debug: debug,
490+
whereValues: _whereAndValues.last,
491+
isStmt: true,
492+
excludeFields: excludeFields);
442493

443494
if (results.numOfRows > 0) {
444495
return results.rows;
@@ -556,19 +607,22 @@ class MysqlUtils {
556607
if (value.length < 2 || value[1] is! List) break;
557608
final list = value[1] as List;
558609
final placeholders = List.filled(list.length, '?').join(', ');
559-
conditions.add('`$key` ${op == 'in' ? 'IN' : 'NOT IN'} ($placeholders)');
610+
conditions.add(
611+
'`$key` ${op == 'in' ? 'IN' : 'NOT IN'} ($placeholders)');
560612
values.addAll(list.map(sqlEscapeString));
561613
break;
562614

563615
case 'between':
564616
case 'notbetween':
565617
if (value.length == 3) {
566-
conditions.add('`$key` ${op == 'between' ? 'BETWEEN' : 'NOT BETWEEN'} ? AND ?');
618+
conditions.add(
619+
'`$key` ${op == 'between' ? 'BETWEEN' : 'NOT BETWEEN'} ? AND ?');
567620
values.addAll([value[1], value[2]]);
568621
} else if (value.length == 2 && value[1] is String) {
569622
final parts = (value[1] as String).split(',');
570623
if (parts.length == 2) {
571-
conditions.add('`$key` ${op == 'between' ? 'BETWEEN' : 'NOT BETWEEN'} ? AND ?');
624+
conditions.add(
625+
'`$key` ${op == 'between' ? 'BETWEEN' : 'NOT BETWEEN'} ? AND ?');
572626
values.addAll(parts);
573627
}
574628
}
@@ -642,19 +696,24 @@ class MysqlUtils {
642696
final isPool = _settings.pool;
643697
late IResultSet resultSet;
644698
if (isStmt) {
645-
final stmt = isPool ? await (connection as MySQLConnectionPool).prepare(sql) : await (connection as MySQLConnection).prepare(sql);
699+
final stmt = isPool
700+
? await (connection as MySQLConnectionPool).prepare(sql)
701+
: await (connection as MySQLConnection).prepare(sql);
646702
try {
647703
resultSet = await stmt.execute(whereValues);
648704
} finally {
649705
// await stmt.deallocate();
650706
}
651707
} else {
652-
resultSet = isPool ? await (connection as MySQLConnectionPool).execute(sql) : await (connection as MySQLConnection).execute(sql);
708+
resultSet = isPool
709+
? await (connection as MySQLConnectionPool).execute(sql)
710+
: await (connection as MySQLConnection).execute(sql);
653711
}
654712
return ResultFormat.from(resultSet, excludeFields: excludeFields);
655713
} catch (e, stackTrace) {
656714
// Enhanced error handling
657-
final errorMsg = 'Query failed: $sql\nError: $e\nStack trace: $stackTrace';
715+
final errorMsg =
716+
'Query failed: $sql\nError: $e\nStack trace: $stackTrace';
658717
_errorLog(errorMsg);
659718

660719
// Attempt to rollback if in transaction
@@ -668,7 +727,8 @@ class MysqlUtils {
668727
}
669728

670729
///query multi
671-
Future<List<int>> queryMulti(String sql, Iterable<List<Object?>> values, {debug = false}) async {
730+
Future<List<int>> queryMulti(String sql, Iterable<List<Object?>> values,
731+
{debug = false}) async {
672732
var queryStr = '$sql $values';
673733
queryTimes++;
674734
if (debug || _settings.debug) _sqlLog(queryStr);
@@ -755,7 +815,8 @@ class ResultFormat {
755815
}
756816
if (results.cols.isNotEmpty) {
757817
if (_excludeFields.isEmpty) {
758-
results.cols.forEach((e) => _cols.add({'name': e.name, 'type': e.type}));
818+
results.cols
819+
.forEach((e) => _cols.add({'name': e.name, 'type': e.type}));
759820
} else {
760821
results.cols.forEach((e) {
761822
if (!_excludeFields.contains(e.name)) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: mysql_utils
2-
description: This is [mysql_client] help library,This makes mysql easier to use and simple.
2+
description: This is [mysql_client_plus] help library,This makes mysql easier to use and simple.
33
version: 2.1.9
44
homepage: https://github.com/biner88/mysql_utils
55
repository: https://github.com/biner88/mysql_utils

0 commit comments

Comments
 (0)