{"id":4506,"date":"2021-01-27T15:43:26","date_gmt":"2021-01-27T14:43:26","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4506"},"modified":"2021-02-08T14:40:42","modified_gmt":"2021-02-08T13:40:42","slug":"filterbuilder","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/filterbuilder\/","title":{"rendered":"FilterBuilder"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(() => {\n    const getEq = (a, b) => `[${a}] = '${b}'`;\n    const getOr = (...args) => `(${Array.from(args).join(\" OR \")})`;\n    const getAnd = (...args) => `(${Array.from(args).join(\" AND \")})`;\n    const getWhere = (...args) => `WHERE ${Array.from(args).join(\" AND \")}`;\n\n    const eq = (a, b) => getEq(a, b);\n    const and = (...args) => getAnd(...args);\n    const or = (...args) => getOr(...args);\n    const where = (...args) => getWhere(...args);\n\n    const result = where(eq(\"Name\", \"x\"), or(eq(\"Age\", \"12\"), and(eq(\"Name\", \"empty\"), eq(\"Name\", \"e\"))));\n    console.log(result);\n    \/\/> WHERE [Name] = 'x' AND ([Age] = '12' OR ([Name] = 'empty' AND [Name] = 'e'))\n})();<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export interface IVariableResult {\n    getValue(): any;\n}\n\nexport interface IBooleanResult {\n    evaluate(): boolean;\n    asString(): string;\n}\n\nexport interface IBooleanProvider {\n    field(name: string): IVariableResult;\n    var(value: string): IVariableResult;\n    val(value: string): IVariableResult;\n\n    eq(a: IVariableResult, b: IVariableResult): IBooleanResult;\n    notEq(a: IVariableResult, b: IVariableResult): IBooleanResult;\n    not(a: IBooleanResult): IBooleanResult;\n\n    greaterThen(a: IVariableResult, b: IVariableResult): IBooleanResult;\n    greaterThenEquals(a: IVariableResult, b: IVariableResult): IBooleanResult;\n    lessThen(a: IVariableResult, b: IVariableResult): IBooleanResult;\n    lessThenEquals(a: IVariableResult, b: IVariableResult): IBooleanResult;\n\n    contains(a: IVariableResult, b: Array&lt;IVariableResult>): IBooleanResult;\n    notContains(a: IVariableResult, b: Array&lt;IVariableResult>): IBooleanResult;\n\n    and(...args: Array&lt;IBooleanResult>): IBooleanResult;\n    or(...args: Array&lt;IBooleanResult>): IBooleanResult;\n    where(...args: Array&lt;IBooleanResult>): IBooleanResult;\n}\n\nexport class VariableResult implements IVariableResult {\n    s: string;\n    constructor(s: string) {\n        this.s = s;\n    }\n    getValue() {\n        return this.s;\n    }\n}\n\nexport class BooleanResult implements IBooleanResult {\n    s: string;\n    constructor(s: string) {\n        this.s = s;\n    }\n    evaluate(): boolean {\n        throw new Error(\"Method not implemented.\");\n    }\n    asString(): string {\n        return this.s;\n    }\n}\n\nexport class BooleanProvider implements IBooleanProvider {\n    field(name: string): IVariableResult {\n        return new VariableResult(`[${name}]`);\n    }\n    var(name: string): IVariableResult {\n        return new VariableResult(`:${name}]`);\n    }\n    val(value: any): IVariableResult {\n        if (typeof value === null) {\n            return new VariableResult('NULL');\n        }\n        if (typeof value === 'number') {\n            return new VariableResult(`'${value}'`);\n        }\n        return new VariableResult(`'${String(value).replace(\/'\/g, \"''\")}'`);\n    }\n\n    eq(a: IVariableResult, b: IVariableResult): IBooleanResult {\n        return new BooleanResult(`${a.getValue()} = ${b.getValue()}`);\n    }\n    notEq(a: IVariableResult, b: IVariableResult): IBooleanResult {\n        return new BooleanResult(`${a.getValue()} != ${b.getValue()}`);\n    }\n    not(a: IBooleanResult): IBooleanResult {\n        return new BooleanResult(`!(${a.asString()})`);\n    }\n    greaterThen(a: IVariableResult, b: IVariableResult): IBooleanResult {\n        return new BooleanResult(`${a.getValue()} > ${b.getValue()}`);\n    }\n    greaterThenEquals(a: IVariableResult, b: IVariableResult): IBooleanResult {\n        return new BooleanResult(`${a.getValue()} >= ${b.getValue()}`);\n    }\n    lessThen(a: IVariableResult, b: IVariableResult): IBooleanResult {\n        return new BooleanResult(`${a.getValue()} &lt; ${b.getValue()}`);\n    }\n    lessThenEquals(a: IVariableResult, b: IVariableResult): IBooleanResult {\n        return new BooleanResult(`${a.getValue()} &lt;= ${b.getValue()}`);\n    }\n    contains(a: IVariableResult, b: IVariableResult[]): IBooleanResult {\n        return new BooleanResult(`${a.getValue()} IN (${b.map(x => x.getValue()).join(\",\")})`);\n    }\n    notContains(a: IVariableResult, b: IVariableResult[]): IBooleanResult {\n        return new BooleanResult(`${a.getValue()} NOT IN (${b.map(x => x.getValue()).join(\",\")})`);\n    }\n    and(...args: IBooleanResult[]): IBooleanResult {\n        return new BooleanResult(`(${Array.from(args).map(x => x.asString()).join(\" AND \")})`);\n    }\n    or(...args: IBooleanResult[]): IBooleanResult {\n        return new BooleanResult(`(${Array.from(args).map(x => x.asString()).join(\" OR \")})`);\n    }\n    where(...args: IBooleanResult[]): IBooleanResult {\n        return new BooleanResult(`WHERE ${Array.from(args).map(x => x.asString()).join(\" AND \")}`);\n    }\n}\n\nvar p = new BooleanProvider();\n\nconst field = p.field.bind(p);\nconst val = p.val.bind(p);\n\nconst eq = p.eq.bind(p);\nconst and = p.and.bind(p);\nconst or = p.or.bind(p);\nconst where = p.where.bind(p);\n\nconst result = where(\n    eq(field(\"Name\"), val(\"x\")),\n    or(\n        eq(field(\"Age\"), val(12)),\n        and(\n            eq(field(\"Name\"), val(\"I like to 'move' it\")),\n            eq(field(\"Name\"), val(\"e\")))\n    )\n);\n\nconsole.log(\"=====================\");\nconsole.log(result.asString());\nconsole.log(\"=====================\");<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4506","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4506"}],"version-history":[{"count":7,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4506\/revisions"}],"predecessor-version":[{"id":4601,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4506\/revisions\/4601"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}