com.wkit.lost.mybatis.sql.SqlTemplate Maven / Gradle / Ivy
The newest version!
package com.wkit.lost.mybatis.sql;
import com.wkit.lost.mybatis.core.metadata.TableWrapper;
import com.wkit.lost.mybatis.utils.StringUtil;
/**
* SQL模板
* @author wvkity
*/
public enum SqlTemplate {
/**
* 查询
*
* SELECT %s FROM TABLE_NAME alias %s
*
*/
SELECT {
@Override
public String toSqlString( TableWrapper table, String alias ) {
return "SELECT %s FROM " + SqlTemplate.parseTableName( table ) +
( StringUtil.isBlank( alias ) ? " " : " " + alias + " " ) + "%s";
}
},
/**
* CRITERIA查询
*
* SELECT %s FROM TABLE_NAME alias %s
*
*/
CRITERIA_SELECT {
@Override
public String toSqlString( TableWrapper table, String alias ) {
return "SELECT FROM ${criteria.tableName} %s";
}
},
/**
* 添加
*
* INSERT INTO TABLE_NAME %s VALUES %s
*
*/
INSERT {
@Override
public String toSqlString( TableWrapper table, String alias ) {
return "INSERT INTO " + SqlTemplate.parseTableName( table ) + " %s VALUES %s";
}
},
/**
* 修改
*
* UPDATE TABLE_NAME %s%s
*
*/
UPDATE {
@Override
public String toSqlString( TableWrapper table, String alias ) {
return "UPDATE " + SqlTemplate.parseTableName( table ) + "%s%s";
}
},
/**
* 删除
*
* DELETE FROM TABLE_NAME %s%s
*
*/
DELETE {
@Override
public String toSqlString( TableWrapper table, String alias ) {
return "DELETE FROM " + SqlTemplate.parseTableName( table ) + "%s%s";
}
};
/**
* 转SQL语句
* @param table 表映射信息
* @param alias 别名
* @return SQL字符串
*/
public abstract String toSqlString( TableWrapper table, String alias );
/**
* 解析表名
* @param table 表映射对象
* @return 表名
*/
private static String parseTableName( final TableWrapper table ) {
String catalog = table.getCatalog();
String schema = table.getSchema();
String tableName = table.getName();
if ( StringUtil.hasText( schema ) ) {
return schema + "." + tableName;
} else if ( StringUtil.hasText( catalog ) ) {
return catalog + "." + tableName;
}
return tableName;
}
}