{"id":39175536,"url":"https://github.com/xbsoftware/querysql","last_synced_at":"2026-01-17T22:28:05.421Z","repository":{"id":42126103,"uuid":"254156018","full_name":"xbsoftware/querysql","owner":"xbsoftware","description":"Generate SQL query from json configuration","archived":false,"fork":false,"pushed_at":"2025-08-04T09:45:38.000Z","size":51,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-06T16:16:55.156Z","etag":null,"topics":["webix-query"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xbsoftware.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-04-08T17:28:57.000Z","updated_at":"2025-08-04T09:45:42.000Z","dependencies_parsed_at":"2025-08-04T11:25:12.217Z","dependency_job_id":"4c9de999-ef48-48ac-b6b1-894779e801e7","html_url":"https://github.com/xbsoftware/querysql","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/xbsoftware/querysql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xbsoftware%2Fquerysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xbsoftware%2Fquerysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xbsoftware%2Fquerysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xbsoftware%2Fquerysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xbsoftware","download_url":"https://codeload.github.com/xbsoftware/querysql/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xbsoftware%2Fquerysql/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28520503,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T22:11:28.393Z","status":"ssl_error","status_checked_at":"2026-01-17T22:11:27.841Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["webix-query"],"created_at":"2026-01-17T22:28:05.304Z","updated_at":"2026-01-17T22:28:05.414Z","avatar_url":"https://github.com/xbsoftware.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON to SQL Query\n\nThis library converts a JSON object into a SQL WHERE clause.\n\n## Features\n\n*   Converts JSON to SQL.\n*   Supports nested conditions.\n*   Supports multiple database dialects (MySQL and PostgreSQL).\n\n## Installation\n\n```sh\ngo get github.com/xbsoftware/querysql@v2.0.0\n```\n\n## V2 API\n\n### `GetSQL`\n\n```go\nfunc GetSQL(data Filter, config *SQLConfig, dbArr ...DBDriver) (string, []interface{}, error)\n```\n\n-   `data`: The `Filter` object, which can be created from JSON using `FromJSON`.\n-   `config`: An optional `SQLConfig` for advanced configuration.\n-   `dbArr`: An optional `DBDriver` for database-specific SQL generation. Defaults to `MySQL{}`.\n\n### `Filter` Struct\n\nThe `Filter` struct is the main data structure for building queries.\n\n```go\ntype Filter struct {\n    Glue      string        `json:\"glue\"`\n    Field     string        `json:\"field\"`\n    Type      string        `json:\"type\"`\n    Predicate string        `json:\"predicate\"`\n    Filter    string        `json:\"filter\"`\n    Value     interface{}   `json:\"value\"`\n    Includes  []interface{} `json:\"includes\"`\n    Rules     []Filter      `json:\"rules\"`\n}\n```\n\n### `SQLConfig`\n\nThe `SQLConfig` struct allows you to customize the behavior of `GetSQL`.\n\n```go\ntype SQLConfig struct {\n    WhitelistFunc CheckFunction\n    Whitelist     map[string]bool\n    Operations    map[string]CustomOperation\n    Predicates    map[string]CustomPredicate\n}\n```\n\n-   `Whitelist` and `WhitelistFunc`: Restrict which fields can be used in the query.\n-   `Operations`: Define custom operations.\n-   `Predicates`: Define custom predicates.\n\n### `CustomPredicate`\n\nA `CustomPredicate` allows you to modify the field name in the SQL query, for example, by wrapping it in a function call.\n\n**Example:**\n\nYou can define a custom predicate to extract the year from a date field.\n\n```go\n\tjsonString := `{\n\t\t\"field\": \"created_at\",\n\t\t\"predicate\": \"year\",\n\t\t\"filter\": \"equal\",\n\t\t\"value\": 2024\n\t}`\n\n\tfilter, _ := querysql.FromJSON([]byte(jsonString))\n\n\tconfig := \u0026querysql.SQLConfig{\n\t\tPredicates: map[string]querysql.CustomPredicate{\n\t\t\t\"year\": func(n string, p string) (string, error) {\n\t\t\t\treturn fmt.Sprintf(\"YEAR(%s)\", n), nil\n\t\t\t},\n\t\t},\n\t}\n\n\tsql, values, _ := querysql.GetSQL(filter, config)\n```\n\n### `CustomOperation`\n\nA `CustomOperation` allows you to define a new, custom filter operation.\n\n**Example:**\n\nYou can define a custom `is_empty` operation to check for empty strings.\n\n```go\n\tjsonString := `{\n\t\t\"field\": \"name\",\n\t\t\"filter\": \"is_empty\"\n\t}`\n\n\tfilter, _ := querysql.FromJSON([]byte(jsonString))\n\n\tconfig := \u0026querysql.SQLConfig{\n\t\tOperations: map[string]querysql.CustomOperation{\n\t\t\t\"is_empty\": func(field string, filter string, values []interface{}) (string, []interface{}, error) {\n\t\t\t\treturn fmt.Sprintf(\"%s = ''\", field), []interface{}{}, nil\n\t\t\t},\n\t\t},\n\t}\n\n\tsql, values, _ := querysql.GetSQL(filter, config)\n```\n\n## Usage\n\nHere is a basic example of how to use the library:\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/xbsoftware/querysql\"\n)\n\nfunc main() {\n    jsonString := `{\n        \"glue\": \"and\",\n        \"rules\": [{\n            \"field\": \"age\",\n            \"filter\": \"less\",\n            \"value\": 42\n        }, {\n            \"field\": \"region\",\n            \"includes\": [1, 2, 6]\n        }]\n    }`\n\n    filter, err := querysql.FromJSON([]byte(jsonString))\n    if err != nil {\n        panic(err)\n    }\n\n    // Using the default MySQL driver\n    sql, values, err := querysql.GetSQL(filter, nil)\n    if err != nil {\n        panic(err)\n    }\n    fmt.Println(sql)    // ( age \u003c ? AND region IN(?,?,?) )\n    fmt.Println(values) // [42 1 2 6]\n\n    // Using the PostgreSQL driver\n    sql, values, err = querysql.GetSQL(filter, nil, \u0026querysql.PostgreSQL{})\n    if err != nil {\n        panic(err)\n    }\n    fmt.Println(sql)    // ( age \u003c $1 AND region IN($2,$3,$4) )\n    fmt.Println(values) // [42 1 2 6]\n}\n```\n\n## Migrating from V1 to V2\n\nThe V2 release introduces several breaking changes. Here's how to migrate your code.\n\n### `Filter` Struct Changes\n\nThe `Filter` struct has been redesigned.\n\n**V1:**\n\n```go\ntype Filter struct {\n    Glue      string        `json:\"glue\"`\n    Field     string        `json:\"field\"`\n    Condition Condition     `json:\"condition\"`\n    Includes  []interface{} `json:\"includes\"`\n    Kids      []Filter      `json:\"rules\"`\n}\n```\n\n**V2:**\n\n```go\ntype Filter struct {\n    Glue      string        `json:\"glue\"`\n    Field     string        `json:\"field\"`\n    Type      string        `json:\"type\"`\n    Predicate string        `json:\"predicate\"`\n    Filter    string        `json:\"filter\"`\n    Value     interface{}   `json:\"value\"`\n    Includes  []interface{} `json:\"includes\"`\n    Rules     []Filter      `json:\"rules\"`\n}\n```\n\n-   The `Condition` field has been removed. Its functionality is now handled by the `Type`, `Predicate`, `Filter`, and `Value` fields.\n-   The `Kids` field has been renamed to `Rules`.\n\n### `CustomPredicate` Signature Change\n\nThe `CustomPredicate` function signature has been simplified.\n\n**V1:**\n\n```go\ntype CustomPredicate func(fieldName string, predicateName string, values []interface{}) (string, []interface{}, error)\n```\n\n**V2:**\n\n```go\ntype CustomPredicate func(fieldName string, predicateName string) (string, error)\n```\n\nThe `values` parameter and return value have been removed.\n\n### `SQLConfig` Changes\n\nThe `SQLConfig` struct now includes a `Predicates` map to support custom predicates.\n\n**V1:**\n\n```go\ntype SQLConfig struct {\n    WhitelistFunc CheckFunction\n    Whitelist     map[string]bool\n    Operations    map[string]CustomOperation\n}\n```\n\n**V2:**\n\n```go\ntype SQLConfig struct {\n    WhitelistFunc CheckFunction\n    Whitelist     map[string]bool\n    Operations    map[string]CustomOperation\n    Predicates    map[string]CustomPredicate\n}\n```\n\n### Supported operations\n\n- equal\n- notEqual\n- contains\n- notContains\n- lessOrEqual\n- greaterOrEqual\n- less\n- notBetween\n- between\n- greater\n- beginsWith\n- notBeginsWith\n- endsWith\n- notEndsWith\n\n### Nesting\n\nBlocks can be nested as follows:\n\n```json\n{\n  \"glue\": \"and\",\n  \"rules\": [\n    ruleA,\n    {\n      \"glue\": \"or\",\n      \"rules\": [\n        ruleC,\n        ruleD\n      ]\n    }\n  ]\n}\n```\n\n### `between` / `notBetween`\n\nFor these operations, both `start` and `end` values can be provided.\n\n```json\n{\n    \"field\": \"age\",\n    \"filter\": \"between\",\n    \"value\": { \"start\": 10, \"end\": 99 }\n}\n```\n\nIf only `start` or `end` is provided, the operation will change to `less` or `greater` automatically.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxbsoftware%2Fquerysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxbsoftware%2Fquerysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxbsoftware%2Fquerysql/lists"}