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

org.yx.db.sql.AbstractSqlBuilder 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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.yx.db.visit.SumkDbVisitor;
import org.yx.exception.SumkException;
import org.yx.util.Asserts;
import org.yx.util.CollectionUtil;

public abstract class AbstractSqlBuilder implements SqlBuilder {

	protected SumkDbVisitor visitor;

	protected Class tableClass;

	protected PojoMeta pojoMeta;

	protected List> in;

	protected boolean failIfPropertyNotMapped;

	protected String sub;

	protected void sub(String sub) {
		if (this.sub != null) {
			SumkException.throwException(14323543, "sub已经设置了,这个属性只允许调用一次");
		}
		if (sub == null || sub.isEmpty()) {
			return;
		}
		this.sub = sub;
		if (this.pojoMeta != null) {
			this.pojoMeta = PojoMetaHolder.getPojoMeta(this.pojoMeta, sub);
		}
	}

	protected void checkMap(Map map, PojoMeta pm) {
		if (!this.failIfPropertyNotMapped) {
			return;
		}
		Set keys = map.keySet();
		MAPKEY: for (String key : keys) {
			for (ColumnMeta p : pm.fieldMetas) {
				if (p.getFieldName().equals(key)) {
					continue MAPKEY;
				}
			}
			SumkException.throwException(743257, key + " is not a field in " + pm.pojoClz);
		}
	}

	protected void checkIn() {
		Asserts.isTrue(CollectionUtil.isNotEmpty(this.in), "no conditions");
	}

	protected void _addIn(Object src) {
		this._addIn(src, Map.class.isInstance(src));
	}

	@SuppressWarnings("unchecked")
	protected void _addIn(Object src, boolean addNullFieldToIn) {
		if (src == null) {
			return;
		}
		if (this.in == null) {
			this.in = new ArrayList<>();
		}
		if (Map.class.isInstance(src)) {
			Map map = (Map) src;
			if (!map.isEmpty()) {
				this.in.add(new HashMap<>(map));
			}
			return;
		}

		if (this.pojoMeta == null) {
			this.pojoMeta = PojoMetaHolder.getPojoMeta(src.getClass(), sub);
			if (this.pojoMeta == null) {
				SumkException.throwException(3654, src.getClass() + " does not config as a table");
			}
		}
		try {
			this.in.add(this.pojoMeta.populate(src, addNullFieldToIn));
		} catch (Exception e) {
			SumkException.throwException(534254, e.getMessage(), e);
		}
	}

	private PojoMeta parsePojoMeta() {
		if (this.tableClass != null) {
			return PojoMetaHolder.getPojoMeta(tableClass, sub);
		}

		return pojoMeta;
	}

	public PojoMeta parsePojoMeta(boolean failIfNull) {
		PojoMeta pm = this.parsePojoMeta();
		if (pm == null && failIfNull) {
			SumkException.throwException(7325435, "please call tableClass(XX.class) first");
		}
		return pm;
	}

	public AbstractSqlBuilder(SumkDbVisitor visitor) {
		this.visitor = visitor;
		this.failIfPropertyNotMapped = DBSettings.failIfPropertyNotMapped();
	}

	protected T accept(SumkDbVisitor visitor) {
		try {
			return visitor.visit(this);
		} catch (Exception e) {
			if (SumkException.class.isInstance(e)) {
				throw (SumkException) e;
			}
			throw new SumkException(-53172189, e.getMessage(), e);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy