{"id":18576099,"url":"https://github.com/thiagodp/pdowrapper","last_synced_at":"2025-04-10T08:31:02.030Z","repository":{"id":57040947,"uuid":"44723772","full_name":"thiagodp/pdowrapper","owner":"thiagodp","description":"A useful PDO wrapper.","archived":false,"fork":false,"pushed_at":"2020-03-23T22:59:12.000Z","size":23,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T19:02:18.723Z","etag":null,"topics":["pdo","pdo-wrapper","php","phputil","wrapper"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thiagodp.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":"2015-10-22T05:19:39.000Z","updated_at":"2020-03-23T22:59:00.000Z","dependencies_parsed_at":"2022-08-23T23:30:46.356Z","dependency_job_id":null,"html_url":"https://github.com/thiagodp/pdowrapper","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fpdowrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fpdowrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fpdowrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fpdowrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thiagodp","download_url":"https://codeload.github.com/thiagodp/pdowrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248008625,"owners_count":21032556,"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":["pdo","pdo-wrapper","php","phputil","wrapper"],"created_at":"2024-11-06T23:23:33.895Z","updated_at":"2025-04-10T08:31:01.663Z","avatar_url":"https://github.com/thiagodp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PDOWrapper\n\nA useful PDO wrapper.\n\nClasses:\n\n* [phputil\\PDOWrapper](https://github.com/thiagodp/pdowrapper/blob/master/lib/PDOWrapper.php)\n* [phputil\\PDOBuilder](https://github.com/thiagodp/pdowrapper/blob/master/lib/PDOBuilder.php)\n\nThis project uses [semantic version](http://semver.org/). See our [releases](https://github.com/thiagodp/pdowrapper/releases).\n\n## Installation\n\n```command\ncomposer require phputil/pdowrapper\n```\n\n## Example 1\n\nCreating `PDO` with `PDOBuilder` and counting rows with `PDOWrapper`.\n\n```php\n\u003c?php\nrequire_once 'vendor/autoload.php';\nuse \\phputil\\PDOBuilder;\nuse \\phputil\\PDOWrapper;\n\n$pdo = PDOBuilder::with()\n\t-\u003edsn( 'mysql:dbname=mydb;host=127.0.0.1;' )\n\t-\u003eusername( 'myuser' )\n\t-\u003epassword( 'mypass' )\n\t-\u003emodeException()\n\t-\u003epersistent()\n\t-\u003emySqlUTF8()\n\t-\u003ebuild();\n\n$pdoW = new PDOWrapper( $pdo );\n\necho 'Table \"customer\" has ', $pdoW-\u003ecountRows( 'customer' ), ' rows.';\n?\u003e\n```\n### Example 2\n\nDelete by id\n\n```php\n$id = $_GET[ 'id' ];\n// ... \u003c-- validate $id here\n$deleted = $pdoW-\u003edeleteWithId( $id, 'customer' );\necho 'Deleted ', $deleted, ' rows.';\n```\n\n### Example 3\n\nPaginated query\n\n```php\n$limit = $_GET[ 'limit' ];\n$offset = $_GET[ 'offset' ];\n// ... \u003c-- validate $limit and $offset here\n// makeLimitOffset returns a SQL clause depending of the used database.\n// Currently supports MySQL, PostgreSQL, SQLite, HSQLDB, H2, Firebird, MS SQL Server,\n// or an ANSI SQL 2008 database.\n$pdoStatement = $pdo-\u003eexecute( 'SELECT name FROM customer',\n\t$pdoW-\u003emakeLimitOffset( $limit, $offset ) );\necho 'Showing customers from ', $limit, ' to ', $offset, '\u003cbr /\u003e';\nforeach ( $pdoStatement as $customer ) {\n\techo $customer[ 'name' ], '\u003cbr /\u003e';\n}\n```\n\n### Example 4\n\nQuery objects\n\n```php\nclass User {\n\tprivate $id;\n\tprivate $name;\n\n\tfunction __construct( $id = 0, $name = '' ) {\n\t\t$this-\u003eid = $id;\n\t\t$this-\u003ename = $name;\n\t}\n\n\tfunction getId() { return $this-\u003eid; }\n\tfunction getName() { return $this-\u003ename; }\n}\n\nclass UserRepositoryInRelationalDatabase {\n\n\tprivate $pdoW;\n\n\tfunction __construct( PDOWrapper $pdoW ) {\n\t\t$this-\u003epdoW = $pdoW;\n\t}\n\n\t/**\n\t * Return all the users, considering a limit and an offset.\n\t * @return array of User\n\t */\n\tfunction allUsers( $limit = 0, $offset = 0 ) { // throw\n\t\t// Paginated query\n\t\t$sql = 'SELECT * FROM user' .\n\t\t\t$this-\u003epdoW-\u003emakeLimitOffset( $limit, $offset );\n\t\t// Call rowToUser to convert each row to a User\n\t\treturn $this-\u003epdoW-\u003equeryObjects( array( $this, 'rowToUser' ), $sql );\n\t}\n\n\t/**\n\t * Converts a row into a User.\n\t * @return User\n\t */\n\tfunction rowToUser( array $row ) {\n\t\treturn new User( $row[ 'id' ], $row[ 'name' ] );\n\t}\n}\n\n$limit = $_GET[ 'limit' ];\n$offset = $_GET[ 'offset' ];\n// ... \u003c-- validate $limit and $offset here\n$repository = new UserRepositoryInRelationalDatabase( $pdoW );\n$users = $repository-\u003eallUsers( $limit, $offset );\nforeach ( $users as $u ) {\n\techo 'Name: ', $u-\u003egetName(), '\u003cbr /\u003e';\n}\n```\n\n\n## Development\n\nAfter cloning the repo, run `composer install` to install the dependencies.\n\nHow to run the test cases:\n```shell\n./vendor/bin/phpunit tests\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fpdowrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthiagodp%2Fpdowrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fpdowrapper/lists"}