{"id":27629563,"url":"https://github.com/markjaquith/wherewithal","last_synced_at":"2025-04-23T15:26:53.414Z","repository":{"id":62524199,"uuid":"345774254","full_name":"markjaquith/Wherewithal","owner":"markjaquith","description":"Given constraints, parses a string of conditions into a valid MySQL WHERE clause","archived":false,"fork":false,"pushed_at":"2021-03-12T05:11:16.000Z","size":50,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-16T23:34:29.563Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","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/markjaquith.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}},"created_at":"2021-03-08T19:41:13.000Z","updated_at":"2021-03-12T05:11:18.000Z","dependencies_parsed_at":"2022-11-02T14:15:36.121Z","dependency_job_id":null,"html_url":"https://github.com/markjaquith/Wherewithal","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markjaquith%2FWherewithal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markjaquith%2FWherewithal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markjaquith%2FWherewithal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markjaquith%2FWherewithal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markjaquith","download_url":"https://codeload.github.com/markjaquith/Wherewithal/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250459136,"owners_count":21434027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2025-04-23T15:26:52.565Z","updated_at":"2025-04-23T15:26:53.400Z","avatar_url":"https://github.com/markjaquith.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Wherewithal\nGiven constraints, parses a string of conditions into a valid MySQL WHERE clause\n\n![Tests](https://github.com/markjaquith/Wherewithal/actions/workflows/ci.yml/badge.svg)\n\n## Installation\n\n`composer require markjaquith/wherewithal`\n\n## Usage\n\n```php\nuse MarkJaquith\\Wherewithal\\{Parser, Config};\n\n$config = (new Config)\n\t-\u003eaddOperators('\u003c', '\u003e', '\u003c=', '\u003e=', '=', '/') // Or add a subset.\n\t-\u003eaddColumn('column_name', 'column_alias1', 'column_alias2')\n\t-\u003eaddColumn('quantity')\n\t-\u003eaddColumn('price', 'cost');\n\n$parser = new Parser($config);\n\n$structure = $parser-\u003eparse('quantity \u003e 5 and (price \u003c 3.00 or column_alias2 = 10'));\n\n$structure-\u003etoString();\n/*\n  string(57) \"quantity \u003e ? and ( price \u003c ? or price / column_name = ? )\"\n*/\n\n$structure-\u003egetBindings()]);\n/*\n  array(3) {\n    [0]=\u003e\n    string(1) \"5\"\n    [1]=\u003e\n    string(4) \"3.00\"\n    [2]=\u003e\n    string(2) \"10\"\n  }\n*/\n```\n\nYou can also map simple column names to complex expressions like so:\n\n```php\n$structure-\u003emapColumns(function($col) {\n\treturn [\n\t\t'column_name' =\u003e '`some_long_table_name`.`long_column_name`',\n\t]($col) ?? $col;\n})-\u003etoString();\n/*\n\tstring(92) \"(`some_long_table_name`.`long_column_name`) \u003e ? and ( price \u003c ? or price / column_name = ? )\"\n*/\n```\n\nColumns that you don't put in the config will be assumed to be values. Values\nalways use the placeholder token `?`.\n\nYou should combine the resulting `WHERE` (or `HAVING`) clause using your database\nlayer. Here's how you'd do it in Laravel:\n\n```php\n$orders = DB::table('orders')\n\t-\u003ewhereRaw($structure-\u003etoString(), $structure-\u003egetBindings())\n  -\u003eget();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkjaquith%2Fwherewithal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkjaquith%2Fwherewithal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkjaquith%2Fwherewithal/lists"}