From bc7021b598e07c4874a5461a1d99992df427c21d Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 23 Jan 2019 17:05:58 -0500 Subject: [PATCH] enum support --- angel_orm/CHANGELOG.md | 3 ++ angel_orm/lib/src/builder.dart | 67 ++++++++++++++++++++++++++++++++++ angel_orm/pubspec.yaml | 2 +- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/angel_orm/CHANGELOG.md b/angel_orm/CHANGELOG.md index 3f5dc8d1..0472ae70 100644 --- a/angel_orm/CHANGELOG.md +++ b/angel_orm/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.0-dev.17 +* Add `EnumSqlExpressionBuilder`. + # 2.0.0-dev.16 * Add `MapSqlExpressionBuilder` for JSON/JSONB support. diff --git a/angel_orm/lib/src/builder.dart b/angel_orm/lib/src/builder.dart index 4769f384..048fafaa 100644 --- a/angel_orm/lib/src/builder.dart +++ b/angel_orm/lib/src/builder.dart @@ -141,6 +141,73 @@ class NumericSqlExpressionBuilder } } +class EnumSqlExpressionBuilder extends SqlExpressionBuilder { + final int Function(T) _getValue; + bool _hasValue = false; + String _op = '='; + String _raw; + int _value; + + EnumSqlExpressionBuilder(Query query, String columnName, this._getValue) + : super(query, columnName); + + @override + bool get hasValue => _hasValue; + + bool _change(String op, T value) { + _raw = null; + _op = op; + _value = _getValue(value); + return _hasValue = true; + } + + UnsupportedError _unsupported() => + UnsupportedError('Enums do not support this operation.'); + + @override + String compile() { + if (_raw != null) return _raw; + if (_value == null) return null; + return '$_op $_value'; + } + + void isNull() { + _hasValue = true; + _raw = 'IS NOT NULL'; + } + + void isNotNull() { + _hasValue = true; + _raw = 'IS NOT NULL'; + } + + void equals(T value) { + _change('=', value); + } + + void notEquals(T value) { + _change('!=', value); + } + + @override + void isBetween(T lower, T upper) => throw _unsupported(); + + @override + void isNotBetween(T lower, T upper) => throw _unsupported(); + + @override + void isIn(Iterable values) { + _raw = 'IN (' + values.map(_getValue).join(', ') + ')'; + _hasValue = true; + } + + @override + void isNotIn(Iterable values) { + _raw = 'NOT IN (' + values.map(_getValue).join(', ') + ')'; + _hasValue = true; + } +} + class StringSqlExpressionBuilder extends SqlExpressionBuilder { bool _hasValue = false; String _op = '=', _raw, _value; diff --git a/angel_orm/pubspec.yaml b/angel_orm/pubspec.yaml index c7814726..55e4c45c 100644 --- a/angel_orm/pubspec.yaml +++ b/angel_orm/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_orm -version: 2.0.0-dev.16 +version: 2.0.0-dev.17 description: Runtime support for Angel's ORM. Includes base classes for queries. author: Tobe O homepage: https://github.com/angel-dart/orm