getQueryStringFromJsonSingleList method

String getQueryStringFromJsonSingleList({
  1. required dynamic model,
  2. required dynamic dataList,
  3. String specificKeyInList = "",
  4. Function? onProgress,
  5. bool needAllTimeDataCountOnProgress = false,
})

Implementation

String getQueryStringFromJsonSingleList({required dynamic model, required dynamic dataList, String specificKeyInList = "", Function? onProgress ,bool needAllTimeDataCountOnProgress = false}) {

 StringBuffer query = StringBuffer();
 query.write("INSERT INTO " + model.runtimeType.toString() + " (");

 Map<String, String> fieldNameList = model.getDataTypeMap();

 int i = 0;
 fieldNameList.forEach((field, type) {
   if (i == fieldNameList.length - 1) {
     query.write("$field ) VALUES ");
   } else {
     query.write("$field , ");
   }
   i++;
 });

 StringBuffer totalValue = StringBuffer();
 int l = 0;
 // Loop for All Object
 int dataCount = 0;
 for (int j = 0; j < dataList!.length; j++) {
   StringBuffer singleModelTotalValue = StringBuffer();

   Map<String, dynamic>? data ;

   if(specificKeyInList != ""){
     if(dataList[j][specificKeyInList] != null) {
       data = dataList[j][specificKeyInList];
     }
   }else{
     if(dataList[j] != null) {
       data = dataList[j];
     }
   }
   String fieldValue = "";
   int k = 0;

   if(data != null  ) {
     if (data.length > 0) {
       if (l == 0) {
         singleModelTotalValue.write("(");
         l++;
       } else {
         singleModelTotalValue.write(",(");
         l++;
       }
       //Map for Single Object
       fieldNameList.forEach((fieldName, type) {
         fieldValue = "";

         try {
           //Map for Single Field
           data!.forEach((key, value) {
             if (fieldName == key) {
               fieldValue = value.toString();
               throw 'Break Map'; // Stop iteration when the key is Matched
             }
           });
         } catch (e) {

         }


         fieldValue = fieldValue.replaceAll("'", "''");
         if (k == fieldNameList.length - 1) {
           singleModelTotalValue.write("'$fieldValue')");
         } else {
           singleModelTotalValue.write("'$fieldValue',");
         }
         k++;
       });

       if(needAllTimeDataCountOnProgress) {
         if (j % 10 == 0) {
           if (onProgress != null) {
             onProgress(j + 1);
           }
         }
       }

       totalValue.write("$singleModelTotalValue");
     }
   }

   dataCount = j+1;

 }

 query.write(totalValue);

 if (onProgress != null) {
   onProgress(dataCount);
 }

 return query.toString();
 }