{"id":16294747,"url":"https://github.com/finesse/microdb","last_synced_at":"2025-04-05T19:35:15.900Z","repository":{"id":62504784,"uuid":"108220410","full_name":"Finesse/MicroDB","owner":"Finesse","description":"A simple database connector for using pure men's SQL with bindings 💪","archived":false,"fork":false,"pushed_at":"2020-02-01T05:50:11.000Z","size":48,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-11T20:16:31.207Z","etag":null,"topics":["database","library","mysql","pdo","pure-sql","sql"],"latest_commit_sha":null,"homepage":"","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/Finesse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-25T04:40:38.000Z","updated_at":"2020-02-01T05:50:14.000Z","dependencies_parsed_at":"2022-11-02T10:01:20.105Z","dependency_job_id":null,"html_url":"https://github.com/Finesse/MicroDB","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finesse%2FMicroDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finesse%2FMicroDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finesse%2FMicroDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Finesse%2FMicroDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Finesse","download_url":"https://codeload.github.com/Finesse/MicroDB/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223206141,"owners_count":17106136,"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":["database","library","mysql","pdo","pure-sql","sql"],"created_at":"2024-10-10T20:16:22.440Z","updated_at":"2024-11-05T16:38:52.365Z","avatar_url":"https://github.com/Finesse.png","language":"PHP","readme":"# MicroDB\n\n[![Latest Stable Version](https://poser.pugx.org/finesse/micro-db/v/stable)](https://packagist.org/packages/finesse/micro-db)\n[![Total Downloads](https://poser.pugx.org/finesse/micro-db/downloads)](https://packagist.org/packages/finesse/micro-db)\n![PHP from Packagist](https://img.shields.io/packagist/php-v/finesse/micro-db.svg)\n[![Test Status](https://github.com/finesse/MicroDB/workflows/Test/badge.svg)](https://github.com/Finesse/MicroDB/actions?workflow=Test)\n[![Maintainability](https://api.codeclimate.com/v1/badges/f4d3bcbd54c012ef4eaf/maintainability)](https://codeclimate.com/github/Finesse/MicroDB/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/f4d3bcbd54c012ef4eaf/test_coverage)](https://codeclimate.com/github/Finesse/MicroDB/test_coverage)\n\nLike to use pure SQL but don't like to suffer from PDO, mysqli or etc.? Try this.\n\n```php\n$database = Connection::create('mysql:host=localhost;dbname=my_database', 'user', 'pass');\n$items = $database-\u003eselect('SELECT * FROM items WHERE category_id = ?', [3]);\n```\n\nKey features:\n\n* No silly query builder, only a good old SQL.\n* Very light, no external dependencies.\n  It required only the [PDO extension](http://php.net/manual/en/book.pdo.php) which is available by default in most of servers.\n* Database object is delivered explicitly, not through a static class.\n* Exceptions on errors.\n\nYou can combine it with a third-party SQL query builder to rock the database. Examples of suitable query builders:\n[Query Scribe](https://github.com/Finesse/QueryScribe),\n[Nilportugues SQL Query Builder](https://github.com/nilportugues/php-sql-query-builder), \n[Aura.SqlQuery](https://github.com/auraphp/Aura.SqlQuery),\n[Latitude](https://github.com/shadowhand/latitude),\n[Koine Query Builder](https://github.com/koinephp/QueryBuilder),\n[Phossa2 Query](https://github.com/phossa2/query),\n[Hydrahon](https://github.com/ClanCats/Hydrahon).\n\n\n## Installation\n\n### Using [Composer](https://getcomposer.org)\n\nRun in a console\n\n```bash\ncomposer require finesse/micro-db\n```\n\n\n## Reference\n\n### Create a `Connection` instance\n\nTo create a new `Connection` instance call the `create` method passing \n[PDO constructor arguments](http://php.net/manual/en/pdo.construct.php).\n\n```php\nuse Finesse\\MicroDB\\Connection;\n\n$database = Connection::create('dsn:string', 'username', 'password, ['options']);\n```\n\nOr pass a `PDO` instance to the constructor. But be careful: `Connection` _changes_ the given `PDO` object and you \n_must not_ change the given object, otherwise something unexpected will happen.\n\n```php\nuse Finesse\\MicroDB\\Connection;\n\n$pdo = new PDO(/* ... */);\n$database = new Connection($pdo);\n```\n\n### Select\n\nSelect many rows:\n\n```php\n$rows = $database-\u003eselect('SELECT * FROM table'); // [['id' =\u003e 1, 'name' =\u003e 'Bill'], ['id' =\u003e 2, 'name' =\u003e 'John']]\n```\n\nSelect one row:\n\n```php\n$row = $database-\u003eselectFirst('SELECT * FROM table'); // ['id' =\u003e 1, 'name' =\u003e 'Bill']\n```\n\nThe cell values are returned as they are returned by PDO. They are not casted automatically because casting can cause \ndata loss.\n\n### Insert\n\nInsert and get the number of the inserted rows:\n\n```php\n$insertedCount = $database-\u003einsert('INSERT INTO table (id, price) VALUES (1, 45), (2, 98)'); // 2\n```\n\nInsert and get the identifier of the last inserted row:\n\n```php\n$id = $database-\u003einsertGetId('INSERT INTO table (weight, price) VALUES (12.3, 45)'); // 3\n```\n\n### Update\n\nUpdate rows and get the number of the updated rows:\n\n```php\n$updatedCount = $database-\u003eupdate('UPDATE table SET status = 1 WHERE price \u003c 1000');\n```\n\n### Delete\n\nDelete rows and get the number of the deleted rows:\n\n```php\n$deletedCount = $database-\u003edelete('DELETE FROM table WHERE price \u003e 1000');\n```\n\n### Other queries\n\nPerform any other statement:\n\n```php\n$database-\u003estatement('CREATE TABLE table(id INTEGER PRIMARY KEY ASC, name TEXT, price NUMERIC)');\n```\n\nIf the query contains multiple statements separated by a semicolon, only the first statement will be executed. You can\nexecute multiple statements using the other method:\n\n```php\n$database-\u003estatements(\"\n    CREATE TABLE table(id INTEGER PRIMARY KEY ASC, name TEXT, price NUMERIC);\n    INSERT INTO table (name, price) VALUES ('Donald', 1000000);\n\");\n```\n\nThe lack of this method is that it doesn't take values to bind.\n\n### Execute a file\n\nExecute the query from an SQL file:\n\n```php\n$database-\u003eimport('path/to/file.sql');\n```\n\nOr from a resource:\n\n```php\n$stream = fopen('path/to/file.sql', 'r');\n$database-\u003eimport($stream);\n```\n\n### Binding values\n\nYou should not insert values right to an SQL query because it can cause \n[SQL injections](https://en.wikipedia.org/wiki/SQL_injection). Instead use the binding:\n\n```php\n// WRONG! Don't do it or you will be fired\n$rows = $database-\u003eselect(\"SELECT * FROM table WHERE name = '$name' LIMIT $limit\");\n\n// Good\n$rows = $database-\u003eselect('SELECT * FROM table WHERE name = ? LIMIT ?', [$name, $limit]);\n```\n\nDatabase server replaces the placeholders (`?`s) safely with the given values. Almost all the above methods accepts \nthe list of the bound values as the second argument.\n\nYou can also use named parameters:\n\n```php\n$rows = $database-\u003eselect('SELECT * FROM table WHERE name = :name LIMIT :limit', [':name' =\u003e $name, ':limit' =\u003e $limit]);\n```\n\nYou can even pass named and anonymous parameters in the same array but it works only when the array of values has the \nsame order as the placeholders in the query text.\n\nAll the scalar types of values are supported: string, integer, float, boolean and null.\n\n### Error handling\n\nThe `Finesse\\MicroDB\\Exceptions\\PDOException` is thrown in case of every database query error. If an error is caused\nby an SQL query, the exception has the query text and bound values in the message. They are also available through the\nmethods:\n\n```php\n$sql = $exception-\u003egetQuery();\n$bindings = $exception-\u003egetValues();\n``` \n\nThe `Finesse\\MicroDB\\Exceptions\\InvalidArgumentException` is thrown when the method arguments have a wrong format.\n\nThe `Finesse\\MicroDB\\Exceptions\\FileException` is thrown on a file read error.\n\nAll the exceptions implement `Finesse\\MicroDB\\IException`.\n\n### Retrieve the underlying `PDO` object\n\n```php\n$pdo = $database-\u003egetPDO();\n```\n\nYou _must not_ change the retrieved object, otherwise something unexpected will happen.\n\n\n## Known problems\n\n* `insertGetId` doesn't return the inserted row identifier for SQL Server and PostgreSQL.\n* `statements` and `import` don't throw an exception if the second or a next statement of the query has an error. This \n  is [a PDO bug](https://stackoverflow.com/a/28867491/1118709).\n\nMake a pull request or an issue if you need a problem to be fixed.\n\n\n## Versions compatibility\n\nThe project follows the [Semantic Versioning](http://semver.org).\n\n\n## License\n\nMIT. See [the LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffinesse%2Fmicrodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffinesse%2Fmicrodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffinesse%2Fmicrodb/lists"}