generateDataSqlJson function

String generateDataSqlJson({
  1. required Map data,
})

Implementation

String generateDataSqlJson({
  required Map data,
}) {
  try {
    if (data["is_test"] is bool == false) {
      data["is_test"] = false;
    }
  } catch (e) {}
  String script_data = "";
  data.forEach((key, value) {
    if (RegExp(r"^(@)", caseSensitive: false).hasMatch(key)) {
      return;
    }
    if (key == "id") {
      return;
    }
    String type_data = "";
    String default_data = "default";
    if (value is num) {
      type_data = "bigint";
      // type_data += " 0";
      default_data += " 0";
    } else if (value is Map || value is List) {
      // type_data = "json";
      // new
      type_data = "jsonb";
      default_data += (" '${json.encode(value)}'::json");
    } else if (value is String) {
      type_data = "text";
      default_data += " format(''::text)";
    } else if (value is bool) {
      type_data = "boolean";
      default_data += " false";
    } else {
      throw "not found type";
    }
    String script_data_loop = """
${key} ${type_data} null ${default_data},
"""
        .trim();
    script_data += "  ${script_data_loop}";
    script_data += "\n";
  });
  return script_data.replaceAll(RegExp(r"(,|,\n)$", caseSensitive: false), "");
}