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

org.noear.wood.dialect.DbSQLServerDialect Maven / Gradle / Ivy

There is a newer version: 1.3.14
Show newest version
package org.noear.wood.dialect;

import org.noear.wood.DbContext;
import org.noear.wood.SQLBuilder;

/**
 * SQLServer数据库方言处理(BETWEEN AND :: >= + <=)
 *
 * @author noear
 * @since 3.2
 * */
public class DbSQLServerDialect extends DbDialectBase{
    @Override
    public boolean excludeFormat(String str) {
        return str.startsWith("\"") || str.startsWith("[") || str.indexOf(".") > 0;
    }

    @Override
    public String tableFormat(String tb) {
        return "[" + tb + "]";
    }

    @Override
    public String columnFormat(String col) {
        return "[" + col + "]";
    }

    @Override
    public void buildSelectRangeCode(DbContext ctx, String table1, SQLBuilder sqlB, StringBuilder orderBy, int start, int size) {
        StringBuilder sb = new StringBuilder();
        if (orderBy == null) {
            String tb = table1.split(" ")[0].replace("$.","").trim();
            String pk = ctx.getMetaData().getTablePk1(tb);

            if(pk == null){
                throw new RuntimeException("Please add orderBy");
            }

            sb.append("SELECT ROW_NUMBER() OVER(ORDER BY ").append(pk).append(") AS _ROW_NUM, ");
        } else {
            sb.append("SELECT ROW_NUMBER() OVER(").append(orderBy).append(") AS _ROW_NUM, ");
        }

        sqlB.insert(0, sb);

        //
        // _ROW_NUM 是从1开始的
        //
        StringBuilder sb2 = new StringBuilder();
        sb2.append("SELECT _x.* FROM (").append(sqlB.builder).append(") _x ");
        sb2.append(" WHERE _x._ROW_NUM BETWEEN ")
                .append(start + 1).append(" AND ")
                .append(start + size);

        sqlB.builder = sb2;
    }

    @Override
    public void buildSelectTopCode(DbContext ctx, String table1, SQLBuilder sqlB, StringBuilder orderBy, int size) {
        sqlB.insert(0, "SELECT TOP " + size);
        if (orderBy != null) {
            sqlB.append(orderBy);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy