Please wait. This can take some minutes ...
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.
com.litongjava.db.activerecord.dialect.SqlServerDialect Maven / Gradle / Ivy
package com.litongjava.db.activerecord.dialect;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.litongjava.db.activerecord.CPI;
import com.litongjava.db.activerecord.Record;
import com.litongjava.db.activerecord.Table;
import com.litongjava.db.activerecord.builder.TimestampProcessedModelBuilder;
import com.litongjava.db.activerecord.builder.TimestampProcessedRecordBuilder;
import com.litongjava.tio.utils.json.Json;
/**
* SqlServerDialect 为OSC 网友战五渣贡献代码:http://www.oschina.net/question/2333909_234198
*/
public class SqlServerDialect extends Dialect {
public SqlServerDialect() {
this.modelBuilder = TimestampProcessedModelBuilder.me;
this.recordBuilder = TimestampProcessedRecordBuilder.me;
}
public String forTableBuilderDoBuild(String tableName) {
return "select * from " + tableName + " where 1 = 2";
}
public void forModelSave(Table table, Map attrs, StringBuilder sql, List paras) {
sql.append("insert into ").append(table.getName()).append('(');
StringBuilder temp = new StringBuilder(") values(");
for (Entry e : attrs.entrySet()) {
String colName = e.getKey();
if (table.hasColumnLabel(colName)) {
if (paras.size() > 0) {
sql.append(", ");
temp.append(", ");
}
sql.append(colName);
temp.append('?');
paras.add(e.getValue());
}
}
sql.append(temp.toString()).append(')');
}
public String forModelDeleteById(Table table) {
String[] pKeys = table.getPrimaryKey();
StringBuilder sql = new StringBuilder(45);
sql.append("delete from ");
sql.append(table.getName());
sql.append(" where ");
for (int i = 0; i < pKeys.length; i++) {
if (i > 0) {
sql.append(" and ");
}
sql.append(pKeys[i]).append(" = ?");
}
return sql.toString();
}
public void forModelUpdate(Table table, Map attrs, Set modifyFlag, StringBuilder sql,
List paras) {
sql.append("update ").append(table.getName()).append(" set ");
String[] pKeys = table.getPrimaryKey();
for (Entry e : attrs.entrySet()) {
String colName = e.getKey();
if (modifyFlag.contains(colName) && !isPrimaryKey(colName, pKeys) && table.hasColumnLabel(colName)) {
if (paras.size() > 0) {
sql.append(", ");
}
sql.append(colName).append(" = ? ");
paras.add(e.getValue());
}
}
sql.append(" where ");
for (int i = 0; i < pKeys.length; i++) {
if (i > 0) {
sql.append(" and ");
}
sql.append(pKeys[i]).append(" = ?");
paras.add(attrs.get(pKeys[i]));
}
}
public String forModelFindById(Table table, String columns) {
StringBuilder sql = new StringBuilder("select ").append(columns).append(" from ");
sql.append(table.getName());
sql.append(" where ");
String[] pKeys = table.getPrimaryKey();
for (int i = 0; i < pKeys.length; i++) {
if (i > 0) {
sql.append(" and ");
}
sql.append(pKeys[i]).append(" = ?");
}
return sql.toString();
}
public String forDbFindById(String tableName, String[] pKeys) {
tableName = tableName.trim();
trimPrimaryKeys(pKeys);
StringBuilder sql = new StringBuilder("select * from ").append(tableName).append(" where ");
for (int i = 0; i < pKeys.length; i++) {
if (i > 0) {
sql.append(" and ");
}
sql.append(pKeys[i]).append(" = ?");
}
return sql.toString();
}
public String forDbDeleteById(String tableName, String[] pKeys) {
tableName = tableName.trim();
trimPrimaryKeys(pKeys);
StringBuilder sql = new StringBuilder("delete from ").append(tableName).append(" where ");
for (int i = 0; i < pKeys.length; i++) {
if (i > 0) {
sql.append(" and ");
}
sql.append(pKeys[i]).append(" = ?");
}
return sql.toString();
}
public void forDbSave(String tableName, String[] pKeys, Record record, StringBuilder sql, List paras) {
tableName = tableName.trim();
trimPrimaryKeys(pKeys);
sql.append("insert into ");
sql.append(tableName).append('(');
StringBuilder temp = new StringBuilder();
temp.append(") values(");
for (Entry e : record.getColumns().entrySet()) {
if (paras.size() > 0) {
sql.append(", ");
temp.append(", ");
}
sql.append(e.getKey());
temp.append('?');
paras.add(e.getValue());
}
sql.append(temp.toString()).append(')');
}
public void forDbUpdate(String tableName, String[] pKeys, Object[] ids, Record record, StringBuilder sql,
List paras) {
tableName = tableName.trim();
trimPrimaryKeys(pKeys);
// Record 新增支持 modifyFlag
Set modifyFlag = CPI.getModifyFlag(record);
sql.append("update ").append(tableName).append(" set ");
for (Entry e : record.getColumns().entrySet()) {
String colName = e.getKey();
if (modifyFlag.contains(colName) && !isPrimaryKey(colName, pKeys)) {
if (paras.size() > 0) {
sql.append(", ");
}
sql.append(colName).append(" = ? ");
paras.add(e.getValue());
}
}
sql.append(" where ");
for (int i = 0; i < pKeys.length; i++) {
if (i > 0) {
sql.append(" and ");
}
sql.append(pKeys[i]).append(" = ?");
paras.add(ids[i]);
}
}
/**
* sql.replaceFirst("(?i)select", "") 正则中带有 "(?i)" 前缀,指定在匹配时不区分大小写
*/
public String forPaginate(int pageNumber, int pageSize, StringBuilder findSql) {
int end = pageNumber * pageSize;
if (end <= 0) {
end = pageSize;
}
int begin = (pageNumber - 1) * pageSize;
if (begin < 0) {
begin = 0;
}
StringBuilder ret = new StringBuilder();
ret.append("SELECT * FROM ( SELECT row_number() over (order by tempcolumn) temprownumber, * FROM ");
ret.append(" ( SELECT TOP ").append(end).append(" tempcolumn=0,");
ret.append(findSql.toString().replaceFirst("(?i)select", ""));
ret.append(")vip)mvp where temprownumber>").append(begin);
return ret.toString();
}
public void fillStatement(PreparedStatement pst, List paras) throws SQLException {
fillStatementHandleDateType(pst, paras);
}
public void fillStatement(PreparedStatement pst, Object... paras) throws SQLException {
fillStatementHandleDateType(pst, paras);
}
@Override
public String forDbFindColumnsById(String tableName, String columns, String[] pKeys) {
return DialectUtils.forDbFindColumnsById(tableName, columns, pKeys);
}
@Override
public String forDbFindColumns(String tableName, String columns) {
return DialectUtils.forDbFindColumns(tableName, columns);
}
@Override
public void forDbDelete(String tableName, String[] pKeys, Record record, StringBuilder sql, List paras) {
DialectUtils.forDbDelete(tableName, pKeys, record, sql, paras);
}
@Override
public String forExistsByFields(String tableName, String fields) {
return DialectUtils.forExistsByFields(tableName, fields);
}
@Override
public void forDbUpdate(String tableName, String[] pKeys, Object[] ids, Record record, StringBuilder sql,
List paras, String[] jsonFields) {
if (jsonFields != null) {
for (String f : jsonFields) {
record.set(f, Json.getJson().toJson(record.get(f)));
}
}
forDbUpdate(tableName, pKeys, ids, record, sql, paras);
}
@Override
public void forDbSave(String tableName, String[] pKeys, Record record, StringBuilder sql, List paras,
String[] jsonFields) {
if (jsonFields != null) {
for (String f : jsonFields) {
Object object = record.get(f);
if (object != null) {
String value = Json.getJson().toJson(object);
record.set(f, value);
}
}
}
this.forDbSave(tableName, pKeys, record, sql, paras);
}
}