# Database

The `Database.php` file in the `Drivers/Database/` directory of the <kbd>PHPFast</kbd> framework provides methods to interact with the database. Each specific database driver (e.g., MySQL, PostgreSQL) will implement these methods. Below is a detailed explanation of each method along.

PHPFast is a lightweight and efficient PHP framework designed to streamline web application development while providing flexibility and scalability. One of its key features is the built-in support for multiple database drivers, making it easy to integrate and work with different database systems. PHPFast includes drivers for [**MongoDB**](https://www.mongodb.com/), [**MySQL**](https://www.mysql.com/), and [**PostgreSQL**](https://www.postgresql.org/), allowing developers to choose the best database solution for their application needs.

* [**MongodbDriver**](https://docs.cmsfullform.com/documents/drivers/database/mongodbdriver): For applications that require NoSQL databases, PHPFast offers seamless integration with MongoDB, a high-performance, flexible database ideal for handling unstructured data.
* [**MysqlDriver**](https://docs.cmsfullform.com/documents/drivers/database/mysqldriver): PHPFast supports MySQL, one of the most widely used relational databases, providing robust and efficient data management for traditional applications.
* [**PostgresqlDriver**](https://docs.cmsfullform.com/documents/drivers/database/postgresqldriver): With built-in support for PostgreSQL, PHPFast enables the use of an advanced, open-source relational database known for its reliability, performance, and scalability.

## Constructor (`__construct`)

```php
abstract public function __construct($config);
```

Initializes the database connection with the provided configuration.\\

* `$config`: An array containing the database configuration settings.

## `query()`

```php
abstract public function query($query, $params = []);
```

Executes an arbitrary SQL query with optional parameters.

* `$query`: The SQL query string.
* `$params`: An array of values corresponding to the parameters in the SQL query.

## `lastInsertId()`

```php
abstract public function lastInsertId();
```

Retrieves the ID of the last inserted record.

The ID of the last inserted record.

## `count()`

```php
abstract public function count($table, $where = '', $params = []);
```

Counts the number of records in a table.

* `$table`: The name of the table.
* `$where`: The WHERE clause to filter records (optional).
* `$params`: An array of values corresponding to the parameters in the WHERE clause (optional).

## `fetchAll()`

```php
abstract public function fetchAll($table, $where = '', $params = [], $orderBy = '', $page = 1, $limit = null);
```

Executes a SELECT query to retrieve multiple rows.

* `$table`: The name of the table.
* `$where`: The WHERE clause as a string (optional).
* `$params`: An array of values corresponding to the parameters in the WHERE clause (optional).
* `$orderBy`: The ORDER BY clause (optional).
* `$page`: The current page number (optional).
* `$limit`: The number of results to limit (optional).

## `fetchPagination()`

```php
abstract public function fetchPagination($table, $where = '', $params = [], $orderBy = '', $page = 1, $limit = null);
```

Executes a SELECT query to retrieve multiple rows with pagination support.

* `$table`: The name of the table.
* `$where`: The WHERE clause as a string (optional).
* `$params`: An array of values corresponding to the parameters in the WHERE clause (optional).
* `$orderBy`: The ORDER BY clause (optional).
* `$page`: The current page number (optional).
* `$limit`: The number of results per page (optional).

## `fetchRow()`

```php
abstract public function fetchRow($table, $where = '', $params = [], $orderBy = '', $page = 1);
```

Executes a SELECT query to retrieve a single row.

* `$table`: The name of the table.
* `$where`: The WHERE clause as a string.
* `$params`: An array of values corresponding to the parameters in the WHERE clause.
* `$orderBy`: The ORDER BY clause (optional).
* `$page`: The current page number (optional).

## `insert()`

```php
abstract public function insert($table, $data);
```

Executes an INSERT query to add a new record.

* `$table`: The name of the table.
* `$data`: An array of data to insert (in the form of 'column' => 'value').

## `update()`

```php
abstract public function update($table, $data, $where = '', $params = []);
```

Executes an UPDATE query to modify existing records.

* `$table`: The name of the table.
* `$data`: An array of data to update (in the form of 'column' => 'value').
* `$where`: The WHERE clause to filter records.
* `$params`: An array of values corresponding to the parameters in the WHERE clause.

## `delete()`

```php
abstract public function delete($table, $where = '', $params = []);
```

Executes a DELETE query to remove records.

* `$table`: The name of the table.
* `$where`: The WHERE clause to filter records.
* `$params`: An array of values corresponding to the parameters in the WHERE clause.
