Skip to content

Commit

Permalink
v1.0.7
Browse files Browse the repository at this point in the history
- Added `APIPayload.payloadFileExtension`.
- Added `ConditionEncoder`, `ConditionSQLEncoder`.
- Improved Data & Entity framework:
  - Added `SQLDatabaseAdapter` and `PostgreAdapter`.
  - Added `DataRepositorySQL`.
- Added DB Adapter for PostgreSQL.
- APIServer:
  - Better auto MIME Type resolution.
- Now API methods can return `FutureOr<APIResponse>`.
- mime: ^1.0.0
  • Loading branch information
gmpassos committed Sep 8, 2021
1 parent 669f44d commit 8a95a79
Show file tree
Hide file tree
Showing 18 changed files with 1,418 additions and 69 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 1.0.7

- Added `APIPayload.payloadFileExtension`.
- Added `ConditionEncoder`, `ConditionSQLEncoder`.
- Improved Data & Entity framework:
- Added `SQLDatabaseAdapter` and `PostgreAdapter`.
- Added `DataRepositorySQL`.
- Added DB Adapter for PostgreSQL.
- APIServer:
- Better auto MIME Type resolution.
- Now API methods can return `FutureOr<APIResponse>`.
- mime: ^1.0.0

## 1.0.6

- CLI Hot Reload fixed:
Expand Down
5 changes: 4 additions & 1 deletion lib/bones_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
library bones_api;

export 'src/bones_api_base.dart';
export 'src/bones_api_data.dart';
export 'src/bones_api_condition.dart';
export 'src/bones_api_condition_sql.dart';
export 'src/bones_api_data.dart';
export 'src/bones_api_data_adapter.dart';
export 'src/bones_api_data_sql.dart';
export 'src/bones_api_extension.dart';
export 'src/bones_api_repository.dart';
4 changes: 4 additions & 0 deletions lib/bones_api_adapter_postgre.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Bones_API DB Adapter for PostgreSQL.
library bones_api_data_adapter_postgre;

export 'src/bones_api_data_adapter_postgre.dart';
15 changes: 14 additions & 1 deletion lib/src/bones_api_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ abstract class APIPayload {
/// The payload MIME Type.
String? get payloadMimeType;

/// The payload usual file name extension.
String? get payloadFileExtension;

/// Returns `true` if [payload] is not `null`.
bool get hasPayload => payload != null;

Expand Down Expand Up @@ -469,13 +472,18 @@ class APIRequest extends APIPayload {
@override
String? payloadMimeType;

/// The payload usual file name extension.
@override
String? payloadFileExtension;

late final List<String> _pathParts;

APIRequest(this.method, this.path,
{Map<String, dynamic>? parameters,
Map<String, dynamic>? headers,
this.payload,
this.payloadMimeType})
this.payloadMimeType,
this.payloadFileExtension})
: parameters = parameters ?? <String, dynamic>{},
headers = headers ?? <String, dynamic>{},
_pathParts = _buildPathParts(path);
Expand Down Expand Up @@ -768,13 +776,18 @@ class APIResponse<T> extends APIPayload {
@override
String? payloadMimeType;

/// The payload usual file name extension.
@override
String? payloadFileExtension;

/// The response error.
final dynamic error;

APIResponse(this.status,
{this.headers = const <String, dynamic>{},
this.payload,
this.payloadMimeType,
this.payloadFileExtension,
this.error});

/// Creates a response of status `OK`.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bones_api_condition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ abstract class KeyCondition<O> extends Condition<O> {
Object? value;

if (key is ConditionKeyField) {
var objDataHandler = dataHandler?.getDataHandler(obj);
var objDataHandler = dataHandler?.getDataHandler(obj: obj);
value =
fieldAccessor.getField(obj, key.name, dataHandler: objDataHandler);
} else if (key is ConditionKeyIndex) {
Expand Down
220 changes: 220 additions & 0 deletions lib/src/bones_api_condition_encoder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import 'bones_api_condition.dart';

abstract class ConditionEncoder {
ConditionEncoder();

String encode(
Condition condition, Map<String, dynamic> parametersPlaceholders,
{Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters}) {
var s = StringBuffer();

var rootIsGroup = condition is GroupCondition;

if (!rootIsGroup) {
s.write(groupOpener);
}

encodeCondition(condition, parametersPlaceholders, s, parameters,
positionalParameters, namedParameters);

if (!rootIsGroup) {
s.write(groupCloser);
}

return s.toString();
}

StringBuffer encodeCondition(
Condition c,
Map<String, dynamic> p,
StringBuffer? s,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters) {
s ??= StringBuffer();

if (c is KeyCondition) {
return encodeKeyCondition(
c, p, s, parameters, positionalParameters, namedParameters);
} else if (c is GroupCondition) {
return encodeGroupCondition(
c, p, s, parameters, positionalParameters, namedParameters);
} else if (c is IDCondition) {
return encodeIDCondition(
c, p, s, parameters, positionalParameters, namedParameters);
} else {
throw ConditionEncodingError("$c");
}
}

StringBuffer encodeIDCondition(
IDCondition c,
Map<String, dynamic> p,
StringBuffer? s,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters);

StringBuffer encodeGroupCondition(
GroupCondition c,
Map<String, dynamic> p,
StringBuffer? s,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters) {
s ??= StringBuffer();

if (c is GroupConditionAND) {
return encodeGroupConditionAND(
c, p, s, parameters, positionalParameters, namedParameters);
} else if (c is GroupConditionOR) {
return encodeGroupConditionOR(
c, p, s, parameters, positionalParameters, namedParameters);
} else {
throw ConditionEncodingError("$c");
}
}

String get groupOpener;

String get groupCloser;

String get groupOperatorAND;

String get groupOperatorOR;

StringBuffer encodeGroupConditionAND(
GroupConditionAND c,
Map<String, dynamic> p,
StringBuffer? s,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters,
) {
s ??= StringBuffer();

var conditions = c.conditions;
var length = conditions.length;

if (length == 0) {
return s;
}

s.write(groupOpener);
encodeCondition(conditions.first, p, s, parameters, positionalParameters,
namedParameters);

for (var i = 1; i < length; ++i) {
s.write(groupOperatorAND);

var c2 = conditions[i];
encodeCondition(
c2, p, s, parameters, positionalParameters, namedParameters);
}

s.write(groupCloser);

return s;
}

StringBuffer encodeGroupConditionOR(
GroupConditionOR c,
Map<String, dynamic> p,
StringBuffer? s,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters) {
s ??= StringBuffer();

var conditions = c.conditions;
var length = conditions.length;

if (length == 0) {
return s;
}

s.write(groupOpener);

encodeCondition(conditions.first, p, s, parameters, positionalParameters,
namedParameters);

for (var i = 1; i < length; ++i) {
s.write(groupOperatorOR);

var c2 = conditions[i];
encodeCondition(
c2, p, s, parameters, positionalParameters, namedParameters);
}

s.write(groupCloser);

return s;
}

StringBuffer encodeKeyCondition(
KeyCondition c,
Map<String, dynamic> p,
StringBuffer? s,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters) {
s ??= StringBuffer();

if (c is KeyConditionEQ) {
return encodeKeyConditionEQ(
c, p, s, parameters, positionalParameters, namedParameters);
} else if (c is KeyConditionNotEQ) {
return encodeKeyConditionNotEQ(
c, p, s, parameters, positionalParameters, namedParameters);
} else {
throw ConditionEncodingError("$c");
}
}

StringBuffer encodeKeyConditionEQ(
KeyConditionEQ c,
Map<String, dynamic> p,
StringBuffer? s,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters);

StringBuffer encodeKeyConditionNotEQ(
KeyConditionNotEQ c,
Map<String, dynamic> p,
StringBuffer? s,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters);

String resolveParameterSQL(
String valueKey,
ConditionParameter value,
Map<String, dynamic> p,
Object? parameters,
List? positionalParameters,
Map<String, Object?>? namedParameters) {
p.putIfAbsent(
valueKey,
() => value.getValue(
parameters: parameters,
positionalParameters: positionalParameters,
namedParameters: namedParameters));

var placeholder = parameterPlaceholder(valueKey);
return placeholder;
}

String parameterPlaceholder(String parameterKey);
}

class ConditionEncodingError extends Error {
final String message;

ConditionEncodingError(this.message);

@override
String toString() => "Encoding error: $message";
}
Loading

0 comments on commit 8a95a79

Please sign in to comment.