FLEXSQLResult.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // FLEXSQLResult.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/3/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXSQLResult.h"
  9. #import "NSArray+FLEX.h"
  10. @implementation FLEXSQLResult
  11. @synthesize keyedRows = _keyedRows;
  12. + (instancetype)message:(NSString *)message {
  13. return [[self alloc] initWithmessage:message columns:nil rows:nil];
  14. }
  15. + (instancetype)error:(NSString *)message {
  16. FLEXSQLResult *result = [self message:message];
  17. result->_isError = YES;
  18. return result;
  19. }
  20. + (instancetype)columns:(NSArray<NSString *> *)columnNames rows:(NSArray<NSArray<NSString *> *> *)rowData {
  21. return [[self alloc] initWithmessage:nil columns:columnNames rows:rowData];
  22. }
  23. - (id)initWithmessage:(NSString *)message columns:(NSArray *)columns rows:(NSArray<NSArray *> *)rows {
  24. NSParameterAssert(message || (columns && rows));
  25. NSParameterAssert(columns.count == rows.firstObject.count);
  26. self = [super init];
  27. if (self) {
  28. _message = message;
  29. _columns = columns;
  30. _rows = rows;
  31. }
  32. return self;
  33. }
  34. - (NSArray<NSDictionary<NSString *,id> *> *)keyedRows {
  35. if (!_keyedRows) {
  36. _keyedRows = [self.rows flex_mapped:^id(NSArray<NSString *> *row, NSUInteger idx) {
  37. return [NSDictionary dictionaryWithObjects:row forKeys:self.columns];
  38. }];
  39. }
  40. return _keyedRows;
  41. }
  42. @end