Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/jao/lib/src/db/adapters/sqlite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ library;
import 'dart:async';
import 'dart:io';
import 'package:sqlite3/sqlite3.dart' as sqlite;
import '../../migrations/operations.dart' show sqlDefault;
import '../../migrations/schema.dart' show ForeignKeyDefinition, OnDeleteAction;
import '../connection.dart';

Expand Down Expand Up @@ -703,7 +704,7 @@ List<String> generateTableRecreationSql({
buffer.write(' NOT NULL');
}
if (defaultValue != null) {
buffer.write(' DEFAULT $defaultValue');
buffer.write(' DEFAULT ${sqlDefault(defaultValue)}');
}
}

Expand Down
4 changes: 0 additions & 4 deletions packages/jao/lib/src/fields/field_def.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,6 @@ class EnumField<T extends Enum> extends Field {
});
}

// === Primary Key ===

/// Auto-incrementing primary key
@immutable
class AutoField extends Field {
Expand All @@ -331,8 +329,6 @@ class UuidPrimaryKey extends UuidField {
const UuidPrimaryKey({super.column}) : super(autoGenerate: true, nullable: false, unique: true);
}

// === Relationships ===

/// Foreign key relationship
@immutable
class ForeignKey extends Field {
Expand Down
10 changes: 0 additions & 10 deletions packages/jao/lib/src/fields/field_ref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ abstract class FieldRef<T> {
/// Get the column reference expression
ColumnRef get col => ColumnRef(name, table: table ?? '');

// === Common lookups available on all fields ===

/// Exact match: field = value
Q eq(T value) => Q(Comparison(col, ComparisonOp.eq, Value(value)));

Expand All @@ -41,16 +39,12 @@ abstract class FieldRef<T> {
/// In list: field IN (values)
Q inList(List<T> values) => Q(Comparison(col, ComparisonOp.inList, Value(values)));

// === Ordering ===

/// Order ascending
OrderBy asc() => OrderBy(col, ascending: true);

/// Order descending
OrderBy desc() => OrderBy(col, ascending: false);

// === F-expression comparison ===

/// Compare with another field
Q eqField(FieldRef<T> other) => Q(Comparison(col, ComparisonOp.eq, other.col));

Expand Down Expand Up @@ -125,8 +119,6 @@ abstract class NumericFieldRef<T extends num> extends ComparableFieldRef<T> {
};
}

// === Concrete Field Reference Types ===

/// String field reference with text-specific lookups
@immutable
class StringFieldRef extends FieldRef<String> {
Expand Down Expand Up @@ -205,8 +197,6 @@ class DurationFieldRef extends ComparableFieldRef<Duration> {
const DurationFieldRef(super.name, {super.table});
}

// === Related Field References (for ForeignKey, etc.) ===

/// Reference to a related model for traversing relationships
@immutable
class RelatedFieldRef<T> extends FieldRef<T> {
Expand Down
35 changes: 19 additions & 16 deletions packages/jao/lib/src/migrations/operations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import 'package:meta/meta.dart';
import '../db/connection.dart';
import 'schema.dart';

/// Quote a default value for SQL if needed.
/// Numbers, booleans, and SQL keywords (CURRENT_TIMESTAMP) are emitted as-is.
/// Everything else is wrapped in single quotes.
String sqlDefault(String value) {
if (num.tryParse(value) != null) return value;
final lower = value.toLowerCase();
if (lower == 'true' || lower == 'false') return value;
if (lower == 'current_timestamp' || lower == 'null') return value;
if (value.startsWith("'") && value.endsWith("'")) return value;
final escaped = value.replaceAll("'", "''");
return "'$escaped'";
}

/// Base class for all migration operations.
@immutable
abstract class MigrationOperation {
Expand All @@ -22,8 +35,6 @@ abstract class MigrationOperation {
bool get isReversible;
}

// === Table Operations ===

/// Create a new table.
@immutable
class CreateTable extends MigrationOperation {
Expand Down Expand Up @@ -92,8 +103,8 @@ class CreateTable extends MigrationOperation {
}

// Default value
if (col.defaultValue != null) {
buffer.write(' DEFAULT ${col.defaultValue}');
if (col.defaultValue case final defaultValue?) {
buffer.write(' DEFAULT ${sqlDefault(defaultValue)}');
}

// Check constraint
Expand Down Expand Up @@ -191,8 +202,6 @@ class RenameTable extends MigrationOperation {
bool get isReversible => true;
}

// === Column Operations ===

/// Add a column to an existing table.
@immutable
class AddColumn extends MigrationOperation {
Expand All @@ -219,8 +228,8 @@ class AddColumn extends MigrationOperation {
buffer.write(' NOT NULL');
}

if (column.defaultValue != null) {
buffer.write(' DEFAULT ${column.defaultValue}');
if (column.defaultValue case final defaultValue?) {
buffer.write(' DEFAULT ${sqlDefault(defaultValue)}');
}

return buffer.toString();
Expand Down Expand Up @@ -308,8 +317,8 @@ class AlterColumn extends MigrationOperation {
}

// Change default
if (modification.defaultValue != null) {
statements.add('ALTER TABLE $table ALTER COLUMN $column SET DEFAULT ${modification.defaultValue}');
if (modification.defaultValue case final defaultValue?) {
statements.add('ALTER TABLE $table ALTER COLUMN $column SET DEFAULT ${sqlDefault(defaultValue)}');
} else if (modification.dropDefault) {
statements.add('ALTER TABLE $table ALTER COLUMN $column DROP DEFAULT');
}
Expand All @@ -329,8 +338,6 @@ class AlterColumn extends MigrationOperation {
bool get isReversible => false;
}

// === Index Operations ===

/// Create an index.
@immutable
class CreateIndex extends MigrationOperation {
Expand Down Expand Up @@ -385,8 +392,6 @@ class DropIndex extends MigrationOperation {
bool get isReversible => false;
}

// === Constraint Operations ===

/// Add a foreign key constraint.
@immutable
class AddForeignKey extends MigrationOperation {
Expand Down Expand Up @@ -442,8 +447,6 @@ class DropConstraint extends MigrationOperation {
bool get isReversible => false;
}

// === Raw SQL Operations ===

/// Execute raw SQL.
@immutable
class RawSql extends MigrationOperation {
Expand Down
6 changes: 0 additions & 6 deletions packages/jao/lib/src/query/aggregates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ class Least extends FunctionCall {
Least(List<Expression> exprs) : super('LEAST', exprs);
}

// === String Functions ===

/// String length
@immutable
class Length extends FunctionCall {
Expand Down Expand Up @@ -140,8 +138,6 @@ class Replace extends FunctionCall {
Replace(Expression expr, String from, String to) : super('REPLACE', [expr, Value(from), Value(to)]);
}

// === Date/Time Functions ===

/// Current date
@immutable
class CurrentDate extends FunctionCall {
Expand Down Expand Up @@ -173,8 +169,6 @@ class DateTrunc extends FunctionCall {
DateTrunc(String precision, Expression expr) : super('DATE_TRUNC', [Value(precision), expr]);
}

// === Math Functions ===

/// Absolute value
@immutable
class Abs extends FunctionCall {
Expand Down
Loading