♻️ apply the recase option to table name

This commit is contained in:
ValentinVignal 2022-12-03 16:16:13 +08:00
parent 496635afb5
commit 8a34a41fba
No known key found for this signature in database
GPG Key ID: 040FFDADFB7EF05A
3 changed files with 25 additions and 27 deletions

View File

@ -2,6 +2,7 @@ import 'package:charcode/ascii.dart';
import 'package:drift/drift.dart' show SqlDialect;
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';
import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart'
show BasicType, ResolvedType, SchemaFromCreateTable, SqliteVersion;
import 'package:string_scanner/string_scanner.dart';
@ -406,5 +407,26 @@ enum CaseFromDartToSql {
///
/// `myColumn` -> `MYCOLUMN`.
@JsonValue('UPPERCASE')
upper,
upper;
/// Applies the correct case to the given [name].
String apply(String name) {
final reCase = ReCase(name);
switch (this) {
case CaseFromDartToSql.preserve:
return name;
case CaseFromDartToSql.camel:
return reCase.camelCase;
case CaseFromDartToSql.constant:
return reCase.constantCase;
case CaseFromDartToSql.snake:
return reCase.snakeCase;
case CaseFromDartToSql.pascal:
return reCase.pascalCase;
case CaseFromDartToSql.lower:
return name.toLowerCase();
case CaseFromDartToSql.upper:
return name.toUpperCase();
}
}
}

View File

@ -3,8 +3,6 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';
import 'package:drift/drift.dart' show DriftSqlType;
import 'package:drift_dev/src/analysis/options.dart';
import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart' show ReferenceAction;
import '../../driver/error.dart';
@ -491,25 +489,3 @@ class PendingColumnInformation {
PendingColumnInformation(this.column, {this.referencesColumnInSameTable});
}
extension on CaseFromDartToSql {
String apply(String name) {
final reCase = ReCase(name);
switch (this) {
case CaseFromDartToSql.preserve:
return name;
case CaseFromDartToSql.camel:
return reCase.camelCase;
case CaseFromDartToSql.constant:
return reCase.constantCase;
case CaseFromDartToSql.snake:
return reCase.snakeCase;
case CaseFromDartToSql.pascal:
return reCase.pascalCase;
case CaseFromDartToSql.lower:
return name.toLowerCase();
case CaseFromDartToSql.upper:
return name.toUpperCase();
}
}
}

View File

@ -2,7 +2,6 @@ import 'package:analyzer/dart/ast/ast.dart' as dart;
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/visitor.dart';
import 'package:recase/recase.dart';
import 'package:source_gen/source_gen.dart';
import 'package:sqlparser/sqlparser.dart' hide AnalysisError;
@ -217,7 +216,8 @@ class _FindDartElements extends RecursiveElementVisitor<void> {
}
String _defaultNameForTableOrView(ClassElement definingElement) {
return ReCase(definingElement.name).snakeCase;
return _discoverStep._driver.options.caseFromDartToSql
.apply(definingElement.name);
}
DartObject? _driftViewAnnotation(ClassElement view) {