Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2017, ZhuKaipeng 朱开鹏 ([email protected]).
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kim.zkp.quick.orm.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import kim.zkp.quick.orm.cache.ClassCache;
import kim.zkp.quick.orm.common.Constants;
import kim.zkp.quick.orm.exception.SqlBuilderException;
import kim.zkp.quick.orm.model.ConditionConstants;
import kim.zkp.quick.orm.model.Model;
import kim.zkp.quick.orm.model.Schema;
import kim.zkp.quick.orm.sharding.Sharding;
import kim.zkp.quick.orm.sql.convert.FieldConvertProcessor;
@SuppressWarnings("unchecked")
public class ObjectSqlBuilderUtils {
private static final List CONDITION_PARAM = new ArrayList<>(Arrays.asList("lt", "gt", "le", "ge", "eq", "neq", "like"));
public static String getTableName(Object o) {
if (o instanceof Sharding) {
Method m = ClassCache.getStrategyMethod(o.getClass());
try {
Object tableName = m.invoke(o);
if (tableName == null) {
throw new SqlBuilderException("Strategy is wrong,Ungenerated tableName.");
}
return (String) tableName;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new SqlBuilderException("Strategy is wrong,Ungenerated tableName.",e);
}
}
if (o instanceof Model) {
Field f = ClassCache.getField(o.getClass(), "tableName");
Object v = FieldConvertProcessor.toDB(f, o);
if (v != null) {
return (String) v;
}
}
return null;
}
public static String getSelect(Object o) {
if (o instanceof Model) {
try {
Class> clzz = o.getClass();
Field f = ClassCache.getField(clzz, "select");
try {
f.setAccessible(true);
Object v = f.get(o);
if (v == null) {
return null;
}
String[] selectAll = (String[]) v;
StringBuilder sb = new StringBuilder();
String alias = ClassCache.getAliasPoint(clzz);
String aliasUnderline = ClassCache.getAliasUnderline(clzz);
for (Object key : selectAll) {
sb.append(alias).append(key);
sb.append(Constants.AS).append(aliasUnderline).append(key).append(Constants.COMMA);
}
sb.deleteCharAt(sb.lastIndexOf(Constants.COMMA));
return sb.toString();
} catch (IllegalArgumentException e) {
throw new SqlBuilderException("通过反射获取属性值出错!",e);
} catch (IllegalAccessException e) {
throw new SqlBuilderException("通过反射获取属性值出错!",e);
}
} catch (Exception e) {
throw new SqlBuilderException("Get select list error", e);
}
}
if (o instanceof Schema) {
return "*";
}
return null;
}
public static String getCondition(Object o, List