query static method

String query(
  1. Transaction data
)

Implementation

static String query(Transaction data) {
  if (data.where == null) {
    return '';
  }
  String q = '';
  int count = 0;
  for (Wheres i in data.where ?? []) {
    ++count;
    if (count == 1) {
      q += 'WHERE ';
    }
    if (i.type == WhereTypes.whereHas) {
      q += (i.queryBuilder ?? '');
      break;
    }
    if (count > 1 && count <= data.where!.length) {
      if (i.type == WhereTypes.and || i.type == WhereTypes.whereIn || i.type == WhereTypes.whereBetween || i.type == WhereTypes.whereLike) {
        q += ' AND ';
      } else if (i.type == WhereTypes.or || i.type == WhereTypes.orWhereBetween || i.type == WhereTypes.orWhereIn || i.type == WhereTypes.whereLike) {
        q += ' OR ';
      }
    }

    if (i.type == WhereTypes.whereIn || i.type == WhereTypes.orWhereBetween || i.type == WhereTypes.whereBetween || i.type == WhereTypes.orWhereIn) {
      q += ' ( ';
      for (int j = 0; j < i.whereGroup!.length; j++) {
        final tryParse = int.tryParse(i.whereGroup?[j].value ?? '');
        if (i.type == WhereTypes.orWhereBetween || i.type == WhereTypes.whereBetween) {
          if (i.whereGroup!.length != 2) {
            throw ArgumentError('Where Between must have 2 length values');
          }
          if (j < 1) {
            q += (i.whereGroup?[j].columnName ?? '') + ' BETWEEN ';
          }
          if (tryParse == null) {
            final parseDate = DateTime.tryParse(i.whereGroup?[j].value ?? '');
            if (parseDate != null) {
              q += "date(${i.whereGroup?[j].value})";
            } else {
              throw ArgumentError('Where Between Date must (y-m-d)');
            }
          } else {
            q += tryParse.toString();
          }
        } else if (i.type == WhereTypes.whereIn || i.type == WhereTypes.orWhereIn) {
          q += (i.whereGroup?[j].columnName ?? '') + (i.whereGroup?[j].operator ?? '');
          if (tryParse == null) {
            q += '"' + (i.whereGroup![j].value ?? '').replaceAll("'", "|_|_|").replaceAll('"', '|-|-|') + '"';
          } else {
            q += tryParse.toString();
          }
        }

        if ((j + 1) != i.whereGroup!.length && i.whereGroup!.length > 1) {
          if (i.whereGroup?[j].type == WhereTypes.or) {
            q += ' OR ';
          } else if (i.whereGroup?[j].type == WhereTypes.and) {
            q += ' AND ';
          }
        }
      }
      q += ' ) ';
    } else {
      final tryParse = int.tryParse(i.value ?? '');
      q += (i.columnName ?? '') + ' ' + (i.operator ?? '') + ' ';
      if (tryParse == null) {
        q += '"' + (i.value ?? '').replaceAll("'", "|_|_|").replaceAll('"', '|-|-|') + '"';
      } else {
        q += tryParse.toString();
      }
    }
  }
  return q;
}