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

org.yx.db.sql.TableFactory Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/**
 * Copyright (C) 2016 - 2030 youtongluan.
 *
 * Licensed 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 org.yx.db.sql;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.yx.annotation.Exclude;
import org.yx.annotation.spec.Specs;
import org.yx.annotation.spec.TableSpec;
import org.yx.log.Log;
import org.yx.log.Logs;
import org.yx.util.S;

public class TableFactory implements Consumer> {

	@Override
	public void accept(Class clz) {
		try {
			TableSpec spec = Specs.extractTable(clz);
			if (spec == null) {
				return;
			}
			List columns = this.extractColumns(clz);
			PojoMetaHolder.register(clz, spec, columns);
		} catch (Throwable e) {
			Log.printStack("sumk.sql.error", e);
		}
	}

	public List extractColumns(Class pojoClz) {
		Field[] fs = S.bean().getFields(pojoClz);
		Map map = new LinkedHashMap<>();
		for (Field f : fs) {
			if (f.isAnnotationPresent(Exclude.class)) {
				continue;
			}
			map.putIfAbsent(f.getName(), f);
		}
		Collection set = map.values();
		List list = new ArrayList<>(set.size());
		for (Field f : set) {
			f.setAccessible(true);
			list.add(new ColumnMeta(f, Specs.extractColumn(pojoClz, f)));
		}
		if (list.isEmpty()) {
			Logs.db().debug("{}'s column is empty", pojoClz.getName());
			return Collections.emptyList();
		}
		Collections.sort(list);
		return list;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy