All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.db.utils.RecordUtils Maven / Gradle / Ivy

There is a newer version: 1.4.7
Show newest version
package com.litongjava.db.utils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.postgresql.util.PGobject;

import com.jfinal.kit.Kv;
import com.litongjava.db.activerecord.Record;
import com.litongjava.tio.utils.json.JsonUtils;
import com.litongjava.tio.utils.name.CamelNameUtils;

public class RecordUtils {

  public static List> getListData(List records, int size) {
    List> columnValues = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
      Object[] columnValuesForRow = records.get(i).getColumnValues();
      for (int j = 0; j < columnValuesForRow.length; j++) {
        if (columnValuesForRow[j] instanceof BigInteger) {
          columnValuesForRow[j] = columnValuesForRow[j].toString();
        } else if (columnValuesForRow[j] instanceof Map) {
          columnValuesForRow[j] = JsonUtils.toJson(columnValuesForRow[j]);
        } else if (columnValuesForRow[j] instanceof List) {
          columnValuesForRow[j] = JsonUtils.toJson(columnValuesForRow[j]);
        } else if (columnValuesForRow[j] instanceof PGobject) {
          PGobject pgObject = (PGobject) columnValuesForRow[j];
          columnValuesForRow[j] = JsonUtils.toJson(pgObject.getValue());
        }
      }
      List asList = Arrays.asList(columnValuesForRow);
      columnValues.add(asList);
    }
    return columnValues;
  }

  @SuppressWarnings("unchecked")
  public static Kv underscoreToCamel(Map map) {
    Kv kv = new Kv();
    map.forEach((key, value) -> kv.put(CamelNameUtils.toCamel(key), value));
    return kv;
  }

  public static List recordsToKv(List list, boolean underscoreToCamel) {
    return list.stream().map(record -> {
      return recordToKv(record, underscoreToCamel);
    }).collect(Collectors.toList());
  }

  public static Kv recordToKv(Record record, boolean underscoreToCamel) {
    if (record == null) {
      return null;
    }
    Map map = record.toMap();
    // 将Long转为String
    for (Map.Entry entry : map.entrySet()) {
      if (entry.getValue() instanceof Long) {
        map.put(entry.getKey(), Long.toString((Long) entry.getValue()));
      }

      if (entry.getValue() instanceof BigInteger) {
        map.put(entry.getKey(), entry.getValue().toString());
      }
    }
    if (underscoreToCamel) {
      return underscoreToCamel(map);
    } else {
      return Kv.create().set(map);
    }
  }

  public static List> recordsToMap(List records) {
    return records.stream().map(record -> record.toMap()).collect(Collectors.toList());
  }
}