renderClientFunc method

String renderClientFunc(
  1. parseTable pt
)

Implementation

String renderClientFunc(parseTable pt) {
  return '''
Future<int>delete(int id) async {
  return await clientSet.db.rawDelete("delete from \$table where $IDField = ?", [id]);
}

Future<${formatType(pt.table)}?>first(int idx) async {
  var rows = await query().where(Eq("$IDField", idx)).query();

  if(rows.isEmpty) {
    return null;
  }

  return rows[0];
}

Future<${formatType(pt.table)}>firstOrNew(int idx) async {
  var rows = await query().where(Eq("$IDField", idx)).query();

  if(rows.isEmpty) {
    var item = ${formatType(pt.table)}();
    if(idx > 0) {
      item.id = idx;
    }
    return wrapType(item);
  }

  return rows[0];
}

${formatType(pt.table)} wrapType(${formatType(pt.table)} typ) {
  typ.clientSet = clientSet;
  return typ;
}

Future<List<${formatType(pt.table)}>>all() async {
  return await query().query();
}

QueryBuild<${formatType(pt.table)}> query() {
  var qb = QueryBuild<${formatType(pt.table)}>()..table(Table.from(table));
  qb.queryFunc = (String q) async {
    return (await clientSet.db.rawQuery(q))
      .map((e) => newTypeByRow(e))
      .toList();
  };
  return qb;
}

Future<int>insert(${formatType(pt.table)} obj) async {
  return await clientSet.db.insert(table, obj.toDB());
}

Future<int>update(${formatType(pt.table)} obj) async {
  return await clientSet.db.update(table, obj.toDB(), where: "$IDField = ?", whereArgs: [obj.$IDField!]);
}

${formatType(pt.table)} newType() {
  return wrapType(${formatType(pt.table)}());
}

${formatType(pt.table)} newTypeByRow(Map row) {
  return wrapType(${formatType(pt.table)}.DB(row));
}
''';
}