VaishnavDatir / sql_conn

A sql_conn is a plugin for connecting Flutter Android application to SQL Server.
https://pub.dev/packages/sql_conn
Apache License 2.0
10 stars 13 forks source link

Automatic converting #6

Open ertugrulcakici opened 2 years ago

ertugrulcakici commented 2 years ago

Whenever I run a query to select a table, the String fields automatically turn into integers if there is no String literal in it. Is there a solution for this ?

VaishnavDatir commented 2 years ago

Hello ertugrulcakici, if the string contains digits then it will be considered as integer. For now, from plugin side there is no solution for this. From your side, in flutter code you can convert that to string.

ertugrulcakici commented 2 years ago

I found a way to do that on the dart side.

class Datatable {
  List<Map<String, dynamic>> _data = [];
  final List<DataTypes> dataTypes;

  bool get isEmpty => _data.isEmpty;
  bool get isNotEmpty => _data.isNotEmpty;
  List<String> get columns => _data.first.keys.toList();
  List<Map<String, dynamic>> get rows => _data;
  List<List<dynamic>> get rowsAsList =>
      _data.map((e) => e.values.toList()).toList();

  Map<String, dynamic> get first => _data.first;

  Datatable(dynamic data, this.dataTypes) {
    if (data is String) {
      // find the text between { and }
      Iterable<Match> matches = RegExp(r"{(.*?)}").allMatches(data);
      for (var row in matches) {
        Map<String, dynamic> innerRow = {};
        int i = 0;
        row.group(1)!.split(', ').forEach((e) {
          List keyValue = e.split(":");
          if (keyValue[1] == "null") {
            innerRow[keyValue[0]] = null;
          } else {
            switch (dataTypes[i++]) {
              case DataTypes.INT:
                innerRow[keyValue[0]] = int.parse(keyValue[1]);
                break;
              case DataTypes.TEXT:
                innerRow[keyValue[0]] = keyValue[1];
                break;
              case DataTypes.REAL:
                innerRow[keyValue[0]] = double.parse(keyValue[1]);
                break;
              case DataTypes.BOOL:
                innerRow[keyValue[0]] =
                    (keyValue[1] == "0" || keyValue[1] == "false")
                        ? false
                        : true;
                break;
              default:
            }
          }
        });
        _data.add(innerRow);
      }
    } else {
      _data = data;
    }
  }

  void debugPrint() {
    if (_data.isEmpty) {
      log("Datatable is empty");
    } else {
      for (var element in _data) {
        log(element.toString());
      }
      log("Row count: ${_data.length}");
      log("Column count: ${_data[0].length}");
    }
  }

  void orderBy(field, {descending = false}) {
    if (descending) {
      _data.sort((a, b) => b[field].compareTo(a[field]));
    } else {
      _data.sort((a, b) => a[field].compareTo(b[field]));
    }
  }

  void excludeFields(List<String> fields) {
    for (var element in _data) {
      for (var field in fields) {
        element.remove(field);
      }
    }
  }

  getByValue(String field, dynamic value) =>
      _data.firstWhere((element) => element[field] == value);
  getIndexByValue(String field, dynamic value) =>
      _data.indexWhere((element) => element[field] == value);

  Datatable copy() => Datatable(_data, dataTypes);
}