com.litongjava.table.services.ApiTable Maven / Gradle / Ivy
package com.litongjava.table.services;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import org.postgresql.util.PGobject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.litongjava.db.TableInput;
import com.litongjava.db.TableResult;
import com.litongjava.db.activerecord.Db;
import com.litongjava.db.activerecord.DbPro;
import com.litongjava.db.activerecord.Record;
import com.litongjava.db.activerecord.dialect.PostgreSqlDialect;
import com.litongjava.db.utils.PgVectorUtils;
import com.litongjava.model.page.Page;
import com.litongjava.table.config.DbDataConfig;
import com.litongjava.table.model.DataPageRequest;
import com.litongjava.table.model.DataQueryRequest;
import com.litongjava.table.model.DbTableStruct;
import com.litongjava.table.model.Sql;
import com.litongjava.table.utils.TableInputUtils;
import com.litongjava.tio.utils.UUIDUtils;
import com.litongjava.tio.utils.snowflake.SnowflakeIdGenerator;
public class ApiTable {
private static DbSqlService dbSqlService = new DbSqlService();
private static PrimaryKeyService primaryKeyService = new PrimaryKeyService();
private static TableColumnService tableColumnService = new TableColumnService();
private static DbTableService dbTableService = new DbTableService();
private static DbService dbService = new DbService();
private static Function embeddingFun;
public static void setEmbeddingFun(Function embeddingFun) {
ApiTable.embeddingFun = embeddingFun;
}
public static Function getEmbeddingFun() {
return embeddingFun;
}
public static TableResult saveOrUpdate(String tableName, TableInput kv) {
String[] jsonFields = TableInputUtils.getJsonFields(kv);
return saveOrUpdate(tableName, kv, jsonFields);
}
public static TableResult save(String tableName, TableInput kv) {
String[] jsonFields = TableInputUtils.getJsonFields(kv);
return save(tableName, kv, jsonFields);
}
/**
* @param tableName
* @param kv
* @param jsonFields
* @return
*/
@SuppressWarnings("unchecked")
public static TableResult save(String tableName, TableInput kv, String[] jsonFields) {
TableInputUtils.removeEmptyValue(kv);
TableInputUtils.true21(kv);
Record record = new Record();
record.setColumns(kv);
String primaryKeyName = primaryKeyService.getPrimaryKeyName(tableName);
if (kv.get(primaryKeyName) == null) {
// 如果主键是varchar类型,插入uuid类型
String primaryKeyColumnType = primaryKeyService.getPrimaryKeyColumnType(tableName);
if (!StrKit.isBlank(primaryKeyColumnType)) {
if (primaryKeyColumnType.startsWith("varchar")) {
String id = UUIDUtils.random();
record.set(primaryKeyName, id);
} else if (primaryKeyColumnType.startsWith("bigint") || primaryKeyColumnType.startsWith("long")) {
// 如果主键是bigint (20)类型,插入雪花Id
long threadId = Thread.currentThread().getId();
if (threadId > 31) {
threadId = threadId % 31;
}
if (threadId < 0) {
threadId = 0;
}
long id = new SnowflakeIdGenerator(threadId, 0).generateId();
record.set(primaryKeyName, id);
}
}
}
boolean save = Db.save(tableName, primaryKeyName, record, jsonFields);
if (save) {
Kv by = Kv.by(primaryKeyName, record.getObject(primaryKeyName));
return new TableResult<>(by);
} else {
return TableResult.fail("save fail");
}
}
@SuppressWarnings("unchecked")
public static TableResult saveOrUpdate(String tableName, TableInput kv, String[] jsonFields) {
// KvUtils.removeEmptyValue(kv);
TableInputUtils.true21(kv);
Map embeddingMap = TableInputUtils.getEmbeddingMap(kv);
Record record = new Record();
record.setColumns(kv);
Set> set = embeddingMap.entrySet();
if (embeddingFun != null) {
for (Entry e : set) {
String key = e.getKey();
String textValue = kv.getStr(key);
if (textValue != null && Db.use().getConfig().getDialect() instanceof PostgreSqlDialect) {
String embeddingArrayString = embeddingFun.apply(textValue);
PGobject pgVector = PgVectorUtils.getPgVector(embeddingArrayString);
record.set(e.getValue(), pgVector);
}
}
}
String primaryKeyName = primaryKeyService.getPrimaryKeyName(tableName);
if (kv.containsKey(primaryKeyName)) { // update
String idValue = record.getStr(primaryKeyName);
if (!StrKit.isBlank(idValue)) {
boolean update = update(tableName, primaryKeyName, idValue, record, jsonFields);
if (update) {
return new TableResult<>();
} else {
return TableResult.fail();
}
} else {
Object id = getIdValueByType(tableName);
if (id != null) {
record.set(primaryKeyName, id);
}
boolean save = Db.save(tableName, record, jsonFields);
if (save) {
Kv by = Kv.by(primaryKeyName, record.getObject(primaryKeyName));
return new TableResult<>(by);
} else {
return TableResult.fail("save fail");
}
}
} else { // 保存
boolean save = save(tableName, jsonFields, primaryKeyName, record);
if (save) {
Kv by = Kv.by(primaryKeyName, record.getObject(primaryKeyName));
return new TableResult<>(by);
} else {
return TableResult.fail("save fail");
}
}
}
public static boolean save(String tableName, String[] jsonFields, String primaryKeyName, Record record) {
// 如果主键是varchar类型,插入uuid类型 不处理uuid类型.如果是uuid类型,让数据库自动生成
Object id = getIdValueByType(tableName);
if (id != null) {
record.set(primaryKeyName, id);
}
return Db.save(tableName, record, jsonFields);
}
public static Object getIdValueByType(String tableName) {
String primaryKeyColumnType = primaryKeyService.getPrimaryKeyColumnType(tableName).toLowerCase();
if (!StrKit.isBlank(primaryKeyColumnType)) {
if (primaryKeyColumnType.startsWith("varchar") || primaryKeyColumnType.startsWith("text")) {
return UUIDUtils.random();
} else if (primaryKeyColumnType.startsWith("bigint") || primaryKeyColumnType.startsWith("long")) {
// 如果主键是bigint (20)类型,插入雪花Id
long threadId = Thread.currentThread().getId();
if (threadId > 31) {
threadId = threadId % 31;
}
if (threadId < 0) {
threadId = 0;
}
return new SnowflakeIdGenerator(threadId, 0).generateId();
}
}
return null;
}
public static boolean update(String tableName, String primaryKeyName, String idValue, Record record, String[] jsonFields) {
String primaryKeyColumnType = primaryKeyService.getPrimaryKeyColumnType(tableName);
boolean update = false;
if ("uuid".equals(primaryKeyColumnType)) {
UUID idUUID = UUID.fromString(idValue);
record.set(primaryKeyName, idUUID);
update = Db.update(tableName, primaryKeyName, record, jsonFields);
} else {
update = Db.update(tableName, primaryKeyName, record, jsonFields);
}
return update;
}
public static boolean update(String tableName, String primaryKeyName, Object idValue, Record record) {
String primaryKeyColumnType = primaryKeyService.getPrimaryKeyColumnType(tableName);
boolean update = false;
if ("uuid".equals(primaryKeyColumnType)) {
UUID idUUID = UUID.fromString((String) idValue);
record.set(primaryKeyName, idUUID);
update = Db.update(tableName, primaryKeyName, record);
} else {
record.set(primaryKeyName, idValue);
update = Db.update(tableName, primaryKeyName, record);
}
return update;
}
public static TableResult batchUpdateByIds(String f, TableInput kv) {
DbPro dbPro = Db.use();
return batchUpdateByIds(dbPro, f, kv);
}
public static TableResult batchUpdateByIds(DbPro dbPro, String tableName, TableInput kv) {
Object[] ids = kv.getAs("ids", new Object[0]);
kv.remove("ids");
DbTableStruct primaryKey = primaryKeyService.getPrimaryKey(dbPro, tableName);
String primaryKeyName = primaryKey.getField();
String type = primaryKey.getType();
boolean isUuid = false;
if (type != null && type.startsWith("uuid")) {
isUuid = true;
}
List lists = new ArrayList<>();
for (Object id : ids) {
Record record = new Record();
record.setColumns(kv.toMap());
if (isUuid) {
record.set(primaryKeyName, UUID.fromString((String) id));
} else {
record.set(primaryKeyName, id);
}
lists.add(record);
}
int[] results = dbPro.batchUpdate(tableName, lists, lists.size());
return new TableResult<>(Kv.by("data", results));
}
public static TableResult saveOrUpdate(TableInput kv) {
String tableName = (String) kv.remove("table_name");
return saveOrUpdate(tableName, kv);
}
/**
* @param tableName
* @return
*/
public static TableResult> listAll(String tableName) {
DbPro dbPro = Db.use();
return listAll(dbPro, tableName);
}
public static TableResult> listAll(String f, TableInput kv) {
DbPro dbPro = Db.use();
return listAll(dbPro, f, kv);
}
/**
* 无任何条件过滤,包含所有数据
*
* @param dbPro
* @param tableName
* @return
*/
public static TableResult> listAll(DbPro dbPro, String tableName) {
List records = dbPro.find("select * from " + tableName);
if (records.size() < 1) {
List columns = dbService.getTableStruct(dbPro, tableName);
Record record = new Record();
for (DbTableStruct struct : columns) {
record.set(struct.getField(), null);
}
records.add(record);
}
return new TableResult>(records);
}
public static TableResult> listAll(DbPro dbPro, String tableName, TableInput kv) {
String[] jsonFields = TableInputUtils.getJsonFields(kv);
String sql = "select * from " + tableName;
List records = null;
if (jsonFields != null) {
records = dbPro.findWithJsonField(sql, jsonFields);
} else {
records = dbPro.find(sql);
}
if (records.size() < 1) {
List columns = dbService.getTableStruct(dbPro, tableName);
Record record = new Record();
for (DbTableStruct struct : columns) {
record.set(struct.getField(), null);
}
records.add(record);
}
return new TableResult>(records);
}
/**
* @param tableName
* @param queryParam
* @return
*/
public static TableResult> list(String tableName, TableInput kvBean) {
return list(null, tableName, kvBean);
}
/**
* @param dbPro
* @param tableName
* @param queryParam
* @return
*/
public static TableResult> list(DbPro dbPro, String tableName, TableInput kvBean) {
if (dbPro == null) {
dbPro = Db.use();
}
DataQueryRequest queryRequest = new DataQueryRequest(kvBean);
String[] jsonFields = TableInputUtils.getJsonFields(kvBean);
// 添加其他查询条件
Sql sql = dbSqlService.getWhereClause(queryRequest, kvBean);
sql.setColumns(queryRequest.getColumns());
sql.setTableName(tableName);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy