ArcGIS API for JavaScript 图层服务查询where条件构造器

本文介绍了一个用于创建SQL where条件的类,支持eq, ne, gt, ge, lt, le, like, in, notIn等操作,适用于前端和后端开发中的高效查询构建。测试用例展示了如何灵活组合条件进行数据库筛选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * SQL where 条件构建器
 */
export default class CreateSqlWhere {
    /**
     * where 条件 sql
     */
    whereSql: string;

    getSql() {
        return this.whereSql;
    }

    eq(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} = '${value}'`;
        return this;
    }

    ne(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} != '${value}'`;
        return this;
    }

    gt(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} > '${value}'`;
        return this;
    }

    ge(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} >= '${value}'`;
        return this;
    }

    lt(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} < '${value}'`;
        return this;
    }

    le(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} <= '${value}'`;
        return this;
    }

    like(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} like '%${value}'%`;
        return this;
    }

    likeLeft(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} like '%${value}'`;
        return this;
    }

    likeRight(field?: string, value?: string) {
        this.defConnect();
        this.whereSql += `${field} like '${value}'%`;
        return this;
    }

    in(field?: string, values?: Array<any>) {
        if (values.length > 0) {
            this.defConnect();
            let sql = `${field} in (`;
            for (let i = 0; i < values.length; i++) {
                if (i === values.length - 1) {
                    sql += `'${values[i]}'`;
                } else {
                    sql += `'${values[i]}', `;
                }
            }
            sql += ')';
            this.whereSql += sql;
        }
        return this;
    }

    notIn(field?: string, values?: Array<any>) {
        if (values.length > 0) {
            this.defConnect();
            let sql = `${field} not in (`;
            for (let i = 0; i < values.length; i++) {
                if (i === values.length - 1) {
                    sql += `'${values[i]}'`;
                } else {
                    sql += `'${values[i]}', `;
                }
            }
            sql += ')';
            this.whereSql += sql;
        }
        return this;
    }

    or() {
        if (this.whereSql) {
            this.whereSql += ' or ';
        } else {
            this.whereSql = '';
        }
        return this;
    }

    private defConnect() {
        if (this.whereSql) {
            if (!this.whereSql.endsWith('and ') && !this.whereSql.endsWith('or ')) {
                this.whereSql += ' and ';
            }
        } else {
            this.whereSql = '';
        }
    }
}

测试用例

const where = new CreateSqlWhere().eq('test', '001')
            .in('test1', [1001, 1002, 1003])
            .eq('test2', '001')
            .or()
            .in('test3', [1001, 1002, 1003])
            .getSql();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值