{"id":24540691,"url":"https://github.com/mistralys/application-dbhelper","last_synced_at":"2025-03-16T05:43:31.072Z","repository":{"id":57017596,"uuid":"218358463","full_name":"Mistralys/application-dbhelper","owner":"Mistralys","description":"PHP Database abstraction layer","archived":false,"fork":false,"pushed_at":"2019-11-08T12:04:57.000Z","size":151,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T18:14:37.925Z","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":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mistralys.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":"2019-10-29T18:42:53.000Z","updated_at":"2019-11-08T12:04:59.000Z","dependencies_parsed_at":"2022-08-22T11:31:35.684Z","dependency_job_id":null,"html_url":"https://github.com/Mistralys/application-dbhelper","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fapplication-dbhelper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fapplication-dbhelper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fapplication-dbhelper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mistralys%2Fapplication-dbhelper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mistralys","download_url":"https://codeload.github.com/Mistralys/application-dbhelper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243830914,"owners_count":20354850,"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-01-22T18:14:42.919Z","updated_at":"2025-03-16T05:43:31.054Z","avatar_url":"https://github.com/Mistralys.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.com/Mistralys/application-dbhelper.svg?branch=master)](https://travis-ci.com/Mistralys/application-dbhelper)\n\n# DBHelper\n\nPHP Database abstraction layer.\n\n## Installation\n\nSimply require the package via composer:\n\n```\n\"require\": {\n   \"mistralys/application-dbhelper\": \"dev-master\"\n}\n```\n\n## Configuration\n\nTo get started, at least one database connection has to be added:\n\n```php\n$database = DBHelper::addDatabase('identifier', 'database_name')\n-\u003esetHost('somehost') // default is localhost\n-\u003esetCredentials('username', 'password')\n-\u003esetPort(1234); // omit to use default port\n```\nOnce all databases and optional event handlers have been added, the helper has to be initialized manually once:\n\n```php\nDBHelper::init()\n```\n\n### Database init command\n\nA database connection can be configured further with an init command, which is run when\nthe connection is established. Typically, this is used to set the encoding of the connection.\n\n```php\n$database-\u003esetInitCommand('SET NAMES latin1');\n```\n\n## Methods overview\n\n### Fetching single records\n\n- **fetch**: Gets a record's data from a custom SQL statement.\n- **fetchData**: Gets a record's data, building the SQL statement dynamically.\n- **fetchKey**: Gets a record's data, and returns the specified column value.\n\n### Fetching multiple records\n\n- **fetchAll**: Gets all entries from a custom SQL statement.\n- **fetchAllKey**: Gets an indexed array with a single column's values from a custom SQL statement.\n\n### Deleting records\n\n- **delete**: Deletes records using a custom SQL statement.\n- **deleteRecords**: Deletes records, building the SQL statement dynamically.\n\n### Transactions\n\n- **startTransaction**: Starts a transaction.\n- **commitTransaction**: Commits an active transaction.\n- **rollbackTransaction**: Rolls back an active transaction.\n\n### Table-related\n\n- **columnExists**: Check if a specific column exists in a table.\n- **fetchTableNames**: Retrieves the names of all tables in the database.\n- **dropTables**: Drops all tables in the database.\n- **tableExists**: Checks whether the specified table exists in the database.\n- **isAutoincrementColumn**: Checks whether a column is an auto increment column.\n\n### Debugging and logging\n\n- **countQueries**: Counts the amount of queries executed up to this point (requires query tracking).\n- **countSelectQueries**: Counts the amount of SELECT queries executed up to this point (requires query tracking).\n- **countWriteQueries**: Counts the amount of database write operations executed up to this point (requires query tracking).\n- **enableDebugging**: Enables query debugging, which will echo all SQL statements after this call.\n- **enableQueryTracking**: Enables saving all queries to memory to be able to access them later.\n- **disableDebugging**: Disable debugging again after enabling it.\n- **disableQueryTracking**: Disables query tracking again after enabling it.\n- **getSelectQueries**: Retrieves all SELECT SQL statements executed up to this point (requires query tracking).\n- **getWriteQueries**: Retrieves all write operation SQL statements executed up to this point (requires query tracking).\n- **getQueryCount**: Returns the total amount of queries executed up to this point.\n- **getQueries**: Retrieves all SQL statements executed up to this point (requires query tracking).\n- **setLogCallback**: Sets a callback to call for handling log messages.\n\n## Event handling\n\nThere are currently two events that listeners can be added to: \n\n1. `Init`: Called when initialization is complete, a connection to the database was successful, and queries can be run.\n2. `OnBeforeWriteOperation`: Called whenever a write operation is about to be executed. Allows cancelling the operation.\n\nBoth events have their own method to add callback functions or methods as event listeners.\n\n```php\nDBHelper::onInit('handle_initDatabase');\nDBHelper::onBeforeWriteOperation('handle_beforeWriteOperation');\n```\n\nThe callback function always gets the event object as first parameter.\nAdditional arguments can optionally be specified in the arguments parameter:\n\n```php\n DBHelper::onInit(\n     'handle_initDatabase', \n     array(\n         'foo', \n         'bar'\n     )\n);\n \nfunction handle_initDatabase(\\AppDB\\DBHelper_Event $event, $param1, $param2)\n{\n    // $param1 = 'foo'\n    // $param2 = 'bar'\n}\n```\n\n## Origin\n\nHistorically, these classes were integrated in several legacy applications. This repository aims to centralize the code and to make it easier to test and maintain them.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistralys%2Fapplication-dbhelper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmistralys%2Fapplication-dbhelper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistralys%2Fapplication-dbhelper/lists"}