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

com.webapp.mybatis.helper.Sqls Maven / Gradle / Ivy

The newest version!
package com.webapp.mybatis.helper;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.webapp.mybatis.helper.SqlHelper.ColAndVal;

public final class Sqls {

	private static final Logger logger = LoggerFactory.getLogger(DaoAdapterHelper.class);

	private String crud;

	public Sqls(String sql) {
		this.crud = sql;
	}
	public static Sqls of(String sql){
		return new Sqls(sql);
	}
	public static  String insert(T model) {
		ColAndVal table = SqlHelper.handleInsColsAndVals(model);
		return String.format("INSERT INTO %1$s%2$s VALUES %3$s", table.getTable(), table.getInsCols(), table.getInsVals());
	}

	public static  String insert(List models) {
		ColAndVal table = SqlHelper.handleInsColsAndVals(models);
		return String.format("INSERT INTO %1$s%2$s VALUES %3$s", table.getTable(), table.getInsCols(), table.getInsVals());
	}

	public static Sqls delete(String table) {
		return Sqls.of(String.format("DELETE FROM %1$s", SqlHelper.spacer(table)));
	}

	public static  Sqls update(String table, T sets) {
		return Sqls.of(String.format("UPDATE %1$s SET %2$s", SqlHelper.spacer(table), SqlHelper.handleUpdSets(sets)));
	}

	public static Sqls select(String table, String... selCols) {
		return Sqls.of(String.format("SELECT %2$s FROM %1$s", SqlHelper.spacer(table), SqlHelper.handleSelCols(selCols)));
	}

	public static Sqls count(String table) {
		return Sqls.of(String.format("SELECT COUNT(1) FROM %1$s", SqlHelper.spacer(table)));
	}

	public String where(Cnds where){
		String cnds = where.toSql();
		if(StringUtils.isNotBlank(cnds))
			crud = String.format("%1$s %2$s", crud, cnds);
		return crud;
	}
	public String where(String where){
		if(StringUtils.isNotBlank(where))
			crud = String.format("%1$s WHERE %2$s", crud, where);
		return crud;
	}
	public String toSql() {
		return crud;
	}
	public String toString() {
		return toSql();
	}


	/**
	 * The following is used for BaseDao
	 */
	protected static Sqls deleteInner(){
		return delete(SqlHelper.TABLE);
	}
	protected static  Sqls updateInner(T sets){
		return update(SqlHelper.TABLE, sets);
	}
	protected static Sqls updateInner(Map param){
		return Sqls.of(String.format("UPDATE %1$s SET %2$s", SqlHelper.spacer(SqlHelper.TABLE), SqlHelper.handleUpdSets(param.get(SqlHelper.PVAL), SqlHelper.PVAL)));
	}
	protected static Sqls selectInner(String selCols){
		return Sqls.of(String.format("SELECT %2$s FROM %1$s", SqlHelper.spacer(SqlHelper.TABLE), selCols));
	}
	protected static Sqls countInner(){
		return count(SqlHelper.TABLE);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy