getRawQuery method
String
getRawQuery(
{ - required bool distinct,
- required dynamic column,
- required dynamic from,
- required dynamic join,
- required dynamic where,
- required bool isWhereArgs,
- required dynamic orderBy,
- required dynamic limit,
- required dynamic offset,
- required dynamic groupBy,
- required dynamic having,
})
Implementation
String getRawQuery({
required bool distinct,
required dynamic column,
required dynamic from,
required dynamic join,
required dynamic where,
required bool isWhereArgs,
required dynamic orderBy,
required dynamic limit,
required dynamic offset,
required dynamic groupBy,
required dynamic having,
}) {
final select = distinct ? 'SELECT DISTINCT' : 'SELECT';
List<String> columnList = [];
if (column is Map) {
column.forEach((key, value) {
final origin = value['origin'];
if (origin == null) return;
columnList.add("$origin as '${key.toString().snakeCase}'");
});
}
final List joins = [];
if (join is String) {
joins.add(join);
} else if (join is List) {
joins.addAll(join);
}
final whereQuery = isWhereArgs
? where ?? ''
: where != null
? 'WHERE $where'
: '';
final orderByQuery = orderBy != null ? 'ORDER BY $orderBy' : '';
final limitQuery = limit != null ? 'LIMIT $limit' : '';
final offsetQuery = offset != null ? 'OFFSET $offset' : '';
final groupByQuery = groupBy != null ? 'GROUP BY $groupBy' : '';
final havingQuery = having != null ? 'HAVING $having' : '';
final int whereArgsLength = RegExp(r'\?').allMatches(whereQuery).length;
String rawQuery =
'$select ${columnList.join(', ')} FROM ${from.toString().snakeCase} ${joins.join(' ')} ${isWhereArgs && whereArgsLength > 0 ? '\$where' : whereQuery} $orderByQuery $groupByQuery $limitQuery $offsetQuery $havingQuery;'
.replaceAll(RegExp(r'\s+'), ' ');
return rawQuery;
}