getReg method

Future<List> getReg(
  1. dynamic obj, {
  2. List<String>? primaryKeys,
  3. Map<String, String>? whereArgs,
  4. List<String>? fields,
})

This function constructs an SQL query based on the parameters passed to it.

The function takes a dynamic object obj which is used to determine the data type.

The function also takes an optional parameter primaryKeys which is a list of primary key values in the order specified by the class.

Another optional parameter whereArgs can be used to specify additional conditions for the WHERE clause. The keys of the map are the field names and the values are the corresponding values.

The fields parameter is a list of specific fields that you want to retrieve in the query. If this parameter is not provided, the function will retrieve the entire record.

Note: If the field is a boolean, it should be represented as 0 or 1, otherwise the program will fail.

Implementation

Future<List<dynamic>> getReg(dynamic obj,
    {List<String>? primaryKeys,
    Map<String, String>? whereArgs,
    List<String>? fields}) async {
  List listRes = [];
  InstanceMirror reflect = reflector.reflect(obj);
  final db = await DBProvider.db.database;
  String sql = "SELECT";

  /* Check if specific fields are provided, otherwise select all fields. */
  if (fields != null && fields.isNotEmpty) {
    for (var x in fields) {
      if (x != fields.last) {
        sql = "$sql $x,";
      } else {
        sql = "$sql $x FROM ${obj.runtimeType.toString().toLowerCase()}s";
      }
    }
  } else {
    sql = "$sql * FROM ${obj.runtimeType.toString().toLowerCase()}s";
  }

  /* Check if filtering by primary key is required. */
  if (primaryKeys != null && primaryKeys.isNotEmpty) {
    List<String> finalPrimary =
        reflect.type.invokeGetter("primary") as List<String>;
    int cont = 0;
    for (var x in finalPrimary) {
      if (x == finalPrimary.last) {
        if (x == finalPrimary.first) {
          sql = "$sql WHERE $x = '${primaryKeys[cont]}'";
        } else {
          sql = "$sql $x = '${primaryKeys[cont]}'";
        }
      } else {
        if (x == finalPrimary.first) {
          sql = "$sql WHERE $x = '${primaryKeys[cont]}' and";
        } else {
          sql = "$sql $x = '${primaryKeys[cont]}' and";
        }
      }
      cont++;
    }
  }

  /* Construct the specific WHERE clause. */
  if (whereArgs != null && whereArgs.isNotEmpty) {
    Iterable<String> keys = whereArgs.keys;
    String body = "";

    for (var x in keys) {
      if (x != keys.last) {
        if (whereArgs[x]!.contains('SELECT')) {
          body = "$body$x = ${whereArgs[x]} and ";
        } else {
          body = "$body$x = '${whereArgs[x]}' and ";
        }
      } else {
        if (whereArgs[x]!.contains('SELECT')) {
          body = "$body$x = ${whereArgs[x]}";
        } else {
          body = "$body$x = '${whereArgs[x]}'";
        }
      }
    }
    /* Check if the WHERE clause has been created when processing the primary keys. */
    if (primaryKeys != null && primaryKeys.isNotEmpty) {
      // The WHERE clause has been created.
      sql = "$sql $body";
    } else {
      // The WHERE clause has not been created.
      sql = "$sql WHERE $body";
    }
  }
  final res = await db!.rawQuery(sql);
  for (var x in res) {
    listRes.add(reflect.type.newInstance('fromJson', [x]));
  }
  PrintHandler.warningLogger.w(
      'sqflite_simple_dao_backend: The query returned ${listRes.length} values from ${obj.runtimeType} ⌨️');
  return listRes;
}