{"id":15003377,"url":"https://github.com/joomla-framework/database","last_synced_at":"2025-04-08T06:35:21.178Z","repository":{"id":7094466,"uuid":"8385748","full_name":"joomla-framework/database","owner":"joomla-framework","description":"Joomla Framework Database Package","archived":false,"fork":false,"pushed_at":"2025-03-30T19:09:22.000Z","size":2888,"stargazers_count":30,"open_issues_count":23,"forks_count":37,"subscribers_count":21,"default_branch":"3.x-dev","last_synced_at":"2025-04-01T04:52:56.166Z","etag":null,"topics":["database","joomla","joomla-framework","mysql","php","postgresql","sql-server","sqlite"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joomla-framework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"joomla","custom":"https://community.joomla.org/sponsorship-campaigns.html"}},"created_at":"2013-02-24T03:17:45.000Z","updated_at":"2025-03-30T14:43:41.000Z","dependencies_parsed_at":"2024-06-18T12:38:33.074Z","dependency_job_id":"600b4065-6252-42a7-b220-e3cb17829b6f","html_url":"https://github.com/joomla-framework/database","commit_stats":{"total_commits":962,"total_committers":42,"mean_commits":"22.904761904761905","dds":0.7016632016632016,"last_synced_commit":"c270ee17789a1b0f48dfc9aabac904d446d3f675"},"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Fdatabase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Fdatabase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Fdatabase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Fdatabase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joomla-framework","download_url":"https://codeload.github.com/joomla-framework/database/tar.gz/refs/heads/3.x-dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247792856,"owners_count":20996890,"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","joomla","joomla-framework","mysql","php","postgresql","sql-server","sqlite"],"created_at":"2024-09-24T18:58:11.194Z","updated_at":"2025-04-08T06:35:21.150Z","avatar_url":"https://github.com/joomla-framework.png","language":"PHP","readme":"# The Database Package [![Build Status](https://ci.joomla.org/api/badges/joomla-framework/database/status.svg?ref=refs/heads/3.x-dev)](https://ci.joomla.org/joomla-framework/database) [![Build status](https://ci.appveyor.com/api/projects/status/m2eh75a1g9k9y59u?svg=true)](https://ci.appveyor.com/project/joomla/database)\n\n[![Latest Stable Version](https://poser.pugx.org/joomla/database/v/stable)](https://packagist.org/packages/joomla/database)\n[![Total Downloads](https://poser.pugx.org/joomla/database/downloads)](https://packagist.org/packages/joomla/database)\n[![Latest Unstable Version](https://poser.pugx.org/joomla/database/v/unstable)](https://packagist.org/packages/joomla/database)\n[![License](https://poser.pugx.org/joomla/database/license)](https://packagist.org/packages/joomla/database)\n\n## Introduction\n\nThe *Database* package is designed to manage the operations of data management through the use of a generic database engine.\n\n```php\n// Example for initialising a database driver in a custom application class.\n\nuse Joomla\\Application\\AbstractApplication;\nuse Joomla\\Database;\n\nclass MyApplication extends AbstractApplication\n{\n\t/**\n\t * Database driver.\n\t *\n\t * @var    Database\\DatabaseDriver\n\t * @since  1.0\n\t */\n\tprotected $db;\n\n\tprotected function doExecute()\n\t{\n\t\t// Do stuff\n\t}\n\n\tprotected function initialise()\n\t{\n\t\t// Make the database driver.\n\t\t$dbFactory = new Database\\DatabaseFactory;\n\n\t\t$this-\u003edb = $dbFactory-\u003egetDriver(\n\t\t\t$this-\u003eget('database.driver'),\n\t\t\tarray(\n\t\t\t\t'host' =\u003e $this-\u003eget('database.host'),\n\t\t\t\t'user' =\u003e $this-\u003eget('database.user'),\n\t\t\t\t'password' =\u003e $this-\u003eget('database.password'),\n\t\t\t\t'port' =\u003e $this-\u003eget('database.port'),\n\t\t\t\t'socket' =\u003e $this-\u003eget('database.socket'),\n\t\t\t\t'database' =\u003e $this-\u003eget('database.name'),\n\t\t\t)\n\t\t);\n\t}\n}\n```\n\n## Escaping Strings and Input\n\nStrings must be escaped before using them in queries (never trust any variable input, even if it comes from a previous database query from your own data source). This can be done using the `escape` and the `quote` method.\n\nThe `escape` method will generally backslash unsafe characters (unually quote characters but it depends on the database engine). It also allows for optional escaping of additional characters (such as the underscore or percent when used in conjunction with a `LIKE` clause).\n\nThe `quote` method will escape a string and wrap it in quotes, however, the escaping can be turned off which is desirable in some situations. The `quote` method will also accept an array of strings and return an array quoted and escaped (unless turned off) string.\n\n```php\nfunction search($title)\n{\n\t// Get the database driver from the factory, or by some other suitable means.\n\t$db = DatabaseDriver::getInstance($options);\n\n\t// Search for an exact match of the title, correctly sanitising the untrusted input.\n\t$sql1 = 'SELECT * FROM #__content WHERE title = ' . $db-\u003equote($title);\n\n\t// Special treatment for a LIKE clause.\n\t$search = $db-\u003equote($db-\u003eescape($title, true) . '%', false);\n\t$sql2 = 'SELECT * FROM #__content WHERE title LIKE ' . $search;\n\n\tif (is_array($title))\n\t{\n\t\t$sql3 = 'SELECT * FROM #__content WHERE title IN ('\n\t\t\t. implode(',', $db-\u003equote($title)) . ')';\n\t}\n\n\t// Do the database calls.\n}\n```\n\nIn the first case, the title variable is simply escaped and quoted. Any quote characters in the title string will be prepended with a backslash and the whole string will be wrapped in quotes.\n\nIn the second case, the example shows how to treat a search string that will be used in a `LIKE` clause. In this case, the title variable is manually escaped using `escape` with a second argument of `true`. This will force other special characters to be escaped (otherwise you could set youself up for serious performance problems if the user includes too many wildcards). Then, the result is passed to the `quote` method but escaping is turned off (because it has already been done manually).\n\nIn the third case, the title variable is an array so the whole array can be passed to the `quote` method (this saves using a closure and a )\n\nShorthand versions are  available the these methods:\n\n* `q` can be used instead of `quote`\n* `qn` can be used instead of `quoteName`\n* `e` can be used instead of `escape`\n\nThese shorthand versions are also available when using the `Database\\DatabaseQuery` class.\n\n## Iterating Over Results\n\nThe `Database\\DatabaseIterator` class allows iteration over database results\n\n```php\n$db = DatabaseDriver::getInstance($options);\n$iterator = $db-\u003esetQuery(\n\t$db-\u003egetQuery(true)-\u003eselect('*')-\u003efrom('#__content')\n)-\u003egetIterator();\n\nforeach ($iterator as $row)\n{\n    // Deal with $row\n}\n```\n\nIt allows also to count the results.\n\n```php\n$count = count($iterator);\n```\n## Logging\n\n`Database\\DatabaseDriver` implements the `Psr\\Log\\LoggerAwareInterface` so is ready for intergrating with a logging package that supports that standard.\n\nDrivers log all errors with a log level of `LogLevel::ERROR`.\n\nIf debugging is enabled (using `setDebug(true)`), all queries are logged with a log level of `LogLevel::DEBUG`. The context of the log include:\n\n* **sql** : The query that was executed.\n* **category** : A value of \"databasequery\" is used.\n\n### An example to log error by Monolog\n\nAdd this to `composer.json`\n\n``` json\n{\n\t\"require\" : {\n\t\t\"monolog/monolog\" : \"1.*\"\n\t}\n}\n```\n\nThen we push Monolog into Database instance.\n\n``` php\nuse Monolog\\Logger;\nuse Monolog\\Handler\\StreamHandler;\nuse Monolog\\Processor\\PsrLogMessageProcessor;\n\n// Create logger object\n$logger = new Logger('sql');\n\n// Push logger handler, use DEBUG level that we can log all information\n$logger-\u003epushHandler(new StreamHandler('path/to/log/sql.log', Logger::DEBUG));\n\n// Use PSR-3 logger processor that we can replace {sql} with context like array('sql' =\u003e 'XXX')\n$logger-\u003epushProcessor(new PsrLogMessageProcessor);\n\n// Push into DB\n$db-\u003esetLogger($logger);\n$db-\u003esetDebug(true);\n\n// Do something\n$db-\u003esetQuery('A WRONG QUERY')-\u003eexecute();\n```\n\nThis is the log file:\n\n```\n[2014-07-29 07:25:22] sql.DEBUG: A WRONG QUERY {\"sql\":\"A WRONG QUERY\",\"category\":\"databasequery\",\"trace\":[...]} []\n[2014-07-29 07:36:01] sql.ERROR: Database query failed (error #42000): SQL: 42000, 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'A WRONG QUERY' at line 1 {\"code\":42000,\"message\":\"SQL: 42000, 1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'A WRONG QUERY' at line 1\"} []\n```\n\n\n## Installation via Composer\n\nAdd `\"joomla/database\": \"~3.0\"` to the require block in your composer.json and then run `composer install`.\n\n```json\n{\n\t\"require\": {\n\t\t\"joomla/database\": \"~3.0\"\n\t}\n}\n```\n\nAlternatively, you can simply run the following from the command line:\n\n```sh\ncomposer require joomla/database \"~3.0\"\n```\n\nIf you want to include the test sources, use\n\n```sh\ncomposer require --prefer-source joomla/database \"~3.0\"\n```\n","funding_links":["https://github.com/sponsors/joomla","https://community.joomla.org/sponsorship-campaigns.html"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Fdatabase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoomla-framework%2Fdatabase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Fdatabase/lists"}