Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ Define your database schema using the Query Builder.
use Rudra\Model\Schema;

Schema::create('users', function ($table) {
$table->integer('id', '', true) // auto-increment
$table->integer('id', autoincrement: true)
->string('name')
->string('email')
->text('bio', 'NULL')
->created_at()
->updated_at()
->pk('id');
->createdAt()
->updatedAt()
->primaryKey('id');
})->execute();
```
### 5. Simple File Caching
Expand Down
122 changes: 89 additions & 33 deletions docs.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
## Table of contents
- [Rudra\Model\Driver\MySQL](#rudra_model_driver_mysql)
- [Rudra\Model\Driver\PgSQL](#rudra_model_driver_pgsql)
- [Rudra\Model\Driver\SQLite](#rudra_model_driver_sqlite)
- [Rudra\Model\Drivers\MySQL](#rudra_model_drivers_mysql)
- [Rudra\Model\Drivers\PgSQL](#rudra_model_drivers_pgsql)
- [Rudra\Model\Drivers\SQLite](#rudra_model_drivers_sqlite)
- [Rudra\Model\Entity](#rudra_model_entity)
- [Rudra\Model\Interfaces\SqlDialectInterface](#rudra_model_interfaces_sqldialectinterface)
- [Rudra\Model\Model](#rudra_model_model)
- [Rudra\Model\QB](#rudra_model_qb)
- [Rudra\Model\QBFacade](#rudra_model_qbfacade)
- [Rudra\Model\Repository](#rudra_model_repository)
- [Rudra\Model\Schema](#rudra_model_schema)
- [Rudra\Model\Traits\CacheTrait](#rudra_model_traits_cachetrait)
- [Rudra\Model\Traits\CrudTrait](#rudra_model_traits_crudtrait)
- [Rudra\Model\Traits\SchemaTrait](#rudra_model_traits_schematrait)


---



<a id="rudra_model_driver_mysql"></a>
<a id="rudra_model_drivers_mysql"></a>

### Class: Rudra\Model\Driver\MySQL
### Class: Rudra\Model\Drivers\MySQL
| Visibility | Function |
|:-----------|:---------|
| public | `concat(string $fieldName, string $alias, ?string $orderBy): string`<br> |
| public | `groupConcat(string $fieldName, string $alias, ?string $orderBy): string`<br> |
| public | `close(): string`<br> |
| public | `integer(string $field, string $default, bool $autoincrement, string $null): string`<br> |
| public | `string(string $field, string $default, string $null): string`<br> |
| public | `text(string $field, string $null): string`<br> |
| public | `created_at(): string`<br> |
| public | `updated_at(): string`<br> |
| public | `pk(string $field): string`<br> |
| public | `createdAt(): string`<br> |
| public | `updatedAt(): string`<br> |
| public | `primaryKey(string $field): string`<br> |


<a id="rudra_model_driver_pgsql"></a>
<a id="rudra_model_drivers_pgsql"></a>

### Class: Rudra\Model\Driver\PgSQL
### Class: Rudra\Model\Drivers\PgSQL
| Visibility | Function |
|:-----------|:---------|
| public | `concat(string $fieldName, string $alias, ?string $orderBy): string`<br> |
| public | `groupConcat(string $fieldName, string $alias, ?string $orderBy): string`<br> |
| public | `close(): string`<br> |
| public | `integer(string $field, string $default, bool $pk, string $null): string`<br> |
| public | `integer(string $field, string $default, bool $autoincrement, string $null): string`<br> |
| public | `string(string $field, string $default, string $null): string`<br> |
| public | `text(string $field, string $null): string`<br> |
| public | `created_at(): string`<br> |
| public | `updated_at(): string`<br> |
| public | `pk(string $field): string`<br> |
| public | `createdAt(): string`<br> |
| public | `updatedAt(): string`<br> |
| public | `primaryKey(string $field): string`<br> |


<a id="rudra_model_driver_sqlite"></a>
<a id="rudra_model_drivers_sqlite"></a>

### Class: Rudra\Model\Driver\SQLite
### Class: Rudra\Model\Drivers\SQLite
| Visibility | Function |
|:-----------|:---------|
| public | `concat(string $fieldName, string $alias, ?string $orderBy): string`<br> |
| public | `groupConcat(string $fieldName, string $alias, ?string $orderBy): string`<br> |
| public | `close(): string`<br> |
| public | `integer(string $field, string $default, bool $pk, string $null): string`<br> |
| public | `integer(string $field, string $default, bool $autoincrement, string $null): string`<br> |
| public | `string(string $field, string $default, string $null): string`<br> |
| public | `text(string $field, string $null): string`<br> |
| public | `created_at(): string`<br> |
| public | `updated_at(): string`<br> |
| public | `pk(string $field): string`<br> |
| public | `createdAt(): string`<br> |
| public | `updatedAt(): string`<br> |
| public | `primaryKey(string $field): string`<br> |


<a id="rudra_model_entity"></a>
Expand All @@ -69,6 +73,21 @@
| protected static | `callMethod(string $method, array $parameters): mixed`<br>Dynamically calls a method on the corresponding Model, Repository, or parent Repository class.<br>The method first attempts to call the method on the Model class associated with the Entity.<br>If the Model does not exist, it falls back to the Repository class.<br>If the Repository does not exist, it defaults to the parent Repository class. |


<a id="rudra_model_interfaces_sqldialectinterface"></a>

### Class: Rudra\Model\Interfaces\SqlDialectInterface
| Visibility | Function |
|:-----------|:---------|
| abstract public | `groupConcat(string $fieldName, string $alias, ?string $orderBy): string`<br> |
| abstract public | `close(): string`<br> |
| abstract public | `integer(string $field, string $default, bool $autoincrement, string $null): string`<br> |
| abstract public | `string(string $field, string $default, string $null): string`<br> |
| abstract public | `text(string $field, string $null): string`<br> |
| abstract public | `createdAt(): string`<br> |
| abstract public | `updatedAt(): string`<br> |
| abstract public | `primaryKey(string $field): string`<br> |


<a id="rudra_model_model"></a>

### Class: Rudra\Model\Model
Expand All @@ -83,9 +102,9 @@
### Class: Rudra\Model\QB
| Visibility | Function |
|:-----------|:---------|
| public | `__construct(?PDO $connection)`<br>Initializes the database driver based on the provided connection or a default connection from the container.<br>If no connection is provided and none is available in the container, a LogicException is thrown.<br>The driver is selected based on the database type specified in the connection's driver attribute. |
| public | `__construct($connection)`<br>Initializes the database driver based on the provided connection or a default connection from the container.<br>If no connection is provided and none is available in the container, a LogicException is thrown.<br>The driver is selected based on the database type specified in the connection's driver attribute. |
| public | `select(string $fields): self`<br> |
| public | `concat(string $fieldName, string $alias, ?string $orderBy): self`<br> |
| public | `groupConcat(string $fieldName, string $alias, ?string $orderBy): self`<br> |
| public | `from(string $table): self`<br> |
| public | `where(string $param): self`<br> |
| public | `and(string $param): self`<br> |
Expand All @@ -102,9 +121,9 @@
| public | `integer(string $field, string $default, bool $autoincrement, string $null): self`<br> |
| public | `string(string $field, string $default, string $null): self`<br> |
| public | `text(string $field, string $null): self`<br> |
| public | `created_at(): self`<br> |
| public | `updated_at(): self`<br> |
| public | `pk(?string $field): self`<br> |
| public | `createdAt(): self`<br> |
| public | `updatedAt(): self`<br> |
| public | `primaryKey(?string $field): self`<br> |


<a id="rudra_model_qbfacade"></a>
Expand All @@ -128,21 +147,21 @@
| public | `withConnection(PDO $connection): self`<br>Creates and returns a new instance of the class with the specified connection.<br>This method allows changing the connection while preserving the current table name.<br>It is useful for creating new instances with different database connections without modifying the original object. |
| public | `qBuilder(string $queryString, array $queryParams): array`<br>Executes a custom SQL query and returns the result as an associative array.<br>The method prepares the query, executes it with optional parameters, and fetches all results. |
| public | `getAllPerPage(Rudra\Pagination $pagination, ?string $fields): array`<br> |
| public | `find(string\|int $id): array\|false`<br> |
| public | `getAll(string $sort, ?string $fields): array`<br> |
| public | `numRows(): int`<br> |
| public | `findBy(string $field, mixed $value): array\|false`<br>Finds a single record by a specified field and value.<br>The field name is validated against the actual table columns to prevent SQL injection. |
| public | `findBy(string $field, mixed $value): array\|false`<br>Finds a single record by a specified field and value.<br>WARNING: The \$field parameter is inserted directly into the SQL query.<br>It is the developer's responsibility to ensure the field name is valid<br>and sanitized to prevent SQL injection. Do not pass raw user input here. |
| public | `lastInsertId(): string`<br> |
| public | `search(string $search, string $column, ?string $fields): array`<br>Searches for records in the database based on a search term and column.<br>WARNING: The \$column parameter is inserted directly into the SQL query.<br>Ensure it is sanitized to prevent SQL injection.<br>Results are ordered by ID in descending order and limited to 10 records. |
| public | `find(string\|int $id): array\|false`<br> |
| public | `update(array $fields): void`<br> |
| public | `create(array $fields): void`<br> |
| public | `delete(string\|int $id): void`<br> |
| protected static | `updateStmtString(array $fields): string`<br>Generates a string of fields and placeholders for an SQL UPDATE statement.<br>The method takes an array of fields and constructs a comma-separated list of "key=:key" pairs.<br>This string can be directly used in the SET clause of an SQL UPDATE query. |
| protected static | `createStmtString(array $fields): array`<br>Generates two strings for an SQL INSERT statement: one for column names and one for placeholders.<br>The method takes an array of fields and constructs two comma-separated lists:<br>- A list of column names.<br>- A list of placeholders (prefixed with colons) for parameter binding.<br>These strings can be directly used in the SQL INSERT query. |
| public | `getColumns(): array`<br>Retrieves the column information for the current table based on the database driver.<br>The method executes a query specific to the database type (MySQL, PostgreSQL, or SQLite)<br>to fetch the column details of the table. |
| public | `getFields(?string $fields): array`<br>Retrieves the list of fields (columns) for the current table.<br>If no specific fields are provided, the method fetches all column names based on the database driver.<br>Otherwise, it splits the provided comma-separated string of fields into an array. |
| public | `search(string $search, string $column, ?string $fields): array`<br>Searches for records in the database based on a search term and column.<br>The column name is validated against the actual table columns to prevent SQL injection.<br>Results are ordered by ID in descending order and limited to 10 records. |
| public | `cache(array $params, ?string $cacheTime): mixed`<br>Caches the result of a method call to a JSON file for a specified duration.<br>If the cached file exists and is still valid (based on cache time), the cached data is returned.<br>Otherwise, the method executes the specified method, caches its result, and returns the data. |
| public | `clearCache(string $type, ?string $key): void`<br>Clears cached files of a specified type or all types.<br>If a cache key is provided, only that specific cache file is deleted. |
| public | `getColumns(): array`<br>Retrieves the column information for the current table based on the database driver.<br>The method executes a query specific to the database type (MySQL, PostgreSQL, or SQLite)<br>to fetch the column details of the table. |
| public | `getFields(?string $fields): array`<br>Retrieves the list of fields (columns) for the current table.<br>If no specific fields are provided, the method fetches all column names based on the database driver.<br>Otherwise, it splits the provided comma-separated string of fields into an array. |


<a id="rudra_model_schema"></a>
Expand All @@ -154,6 +173,43 @@
| public | `execute(): bool`<br>Executes the schema creation by preparing and running the SQL query.<br>The SQL query is generated using the Query Builder and executed on the database connection. |


<a id="rudra_model_traits_cachetrait"></a>

### Class: Rudra\Model\Traits\CacheTrait
| Visibility | Function |
|:-----------|:---------|
| public | `cache(array $params, ?string $cacheTime): mixed`<br>Caches the result of a method call to a JSON file for a specified duration.<br>If the cached file exists and is still valid (based on cache time), the cached data is returned.<br>Otherwise, the method executes the specified method, caches its result, and returns the data. |
| public | `clearCache(string $type, ?string $key): void`<br>Clears cached files of a specified type or all types.<br>If a cache key is provided, only that specific cache file is deleted. |


<a id="rudra_model_traits_crudtrait"></a>

### Class: Rudra\Model\Traits\CrudTrait
| Visibility | Function |
|:-----------|:---------|
| public | `getAllPerPage(Rudra\Pagination $pagination, ?string $fields): array`<br> |
| public | `getAll(string $sort, ?string $fields): array`<br> |
| public | `numRows(): int`<br> |
| public | `findBy(string $field, mixed $value): array\|false`<br>Finds a single record by a specified field and value.<br>WARNING: The \$field parameter is inserted directly into the SQL query.<br>It is the developer's responsibility to ensure the field name is valid<br>and sanitized to prevent SQL injection. Do not pass raw user input here. |
| public | `lastInsertId(): string`<br> |
| public | `search(string $search, string $column, ?string $fields): array`<br>Searches for records in the database based on a search term and column.<br>WARNING: The \$column parameter is inserted directly into the SQL query.<br>Ensure it is sanitized to prevent SQL injection.<br>Results are ordered by ID in descending order and limited to 10 records. |
| public | `find(string\|int $id): array\|false`<br> |
| public | `update(array $fields): void`<br> |
| public | `create(array $fields): void`<br> |
| public | `delete(string\|int $id): void`<br> |
| protected static | `updateStmtString(array $fields): string`<br>Generates a string of fields and placeholders for an SQL UPDATE statement.<br>The method takes an array of fields and constructs a comma-separated list of "key=:key" pairs.<br>This string can be directly used in the SET clause of an SQL UPDATE query. |
| protected static | `createStmtString(array $fields): array`<br>Generates two strings for an SQL INSERT statement: one for column names and one for placeholders.<br>The method takes an array of fields and constructs two comma-separated lists:<br>- A list of column names.<br>- A list of placeholders (prefixed with colons) for parameter binding.<br>These strings can be directly used in the SQL INSERT query. |


<a id="rudra_model_traits_schematrait"></a>

### Class: Rudra\Model\Traits\SchemaTrait
| Visibility | Function |
|:-----------|:---------|
| public | `getColumns(): array`<br>Retrieves the column information for the current table based on the database driver.<br>The method executes a query specific to the database type (MySQL, PostgreSQL, or SQLite)<br>to fetch the column details of the table. |
| public | `getFields(?string $fields): array`<br>Retrieves the list of fields (columns) for the current table.<br>If no specific fields are provided, the method fetches all column names based on the database driver.<br>Otherwise, it splits the provided comma-separated string of fields into an array. |


---

###### created with [Rudra-Documentation-Collector](https://github.com/Jagepard/Rudra-Documentation-Collector)
21 changes: 15 additions & 6 deletions src/Driver/MySQL.php → src/Drivers/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@
* @license https://mozilla.org/MPL/2.0/ MPL-2.0
*/

namespace Rudra\Model\Driver;
namespace Rudra\Model\Drivers;

class MySQL
use Rudra\Model\Interfaces\SqlDialectInterface;

class MySQL implements SqlDialectInterface
{
public function concat(string $fieldName, string $alias, ?string $orderBy): string
#[\Override]
public function groupConcat(string $fieldName, string $alias, ?string $orderBy): string
{
return ", GROUP_CONCAT($fieldName ORDER BY $orderBy SEPARATOR ';') as $alias ";
}

#[\Override]
public function close(): string
{
return ") ENGINE = InnoDB";
}

#[\Override]
public function integer(string $field, string $default = "", bool $autoincrement = false, string $null = "NOT NULL"): string
{
if ($autoincrement) {
Expand All @@ -32,27 +37,31 @@ public function integer(string $field, string $default = "", bool $autoincrement
return ", `$field` INT $null $default";
}

#[\Override]
public function string(string $field, string $default = "", string $null = "NOT NULL"): string
{
return ", `$field` VARCHAR(255) $null $default";
}

#[\Override]
public function text(string $field, string $null = "NOT NULL"): string
{
return ", `$field` text $null";
}

public function created_at(): string
#[\Override]
public function createdAt(): string
{
return ", `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
}

public function updated_at(): string
#[\Override]
public function updatedAt(): string
{
return ", `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
}

public function pk(string $field): string
public function primaryKey(string $field): string
{
return ", PRIMARY KEY (`$field`)";
}
Expand Down
26 changes: 18 additions & 8 deletions src/Driver/PgSQL.php → src/Drivers/PgSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,60 @@
* @license https://mozilla.org/MPL/2.0/ MPL-2.0
*/

namespace Rudra\Model\Driver;
namespace Rudra\Model\Drivers;

class PgSQL
use Rudra\Model\Interfaces\SqlDialectInterface;

class PgSQL implements SqlDialectInterface
{
public function concat(string $fieldName, string $alias, ?string $orderBy): string
#[\Override]
public function groupConcat(string $fieldName, string $alias, ?string $orderBy): string
{
return ", array_to_string(array_agg($fieldName ORDER BY $orderBy), ';') $alias ";
}

#[\Override]
public function close(): string
{
return ");";
}

public function integer(string $field, string $default = "", bool $pk = false, string $null = "NOT NULL"): string
#[\Override]
public function integer(string $field, string $default = "", bool $autoincrement = false, string $null = "NOT NULL"): string
{
if ($pk) {
if ($autoincrement) {
return "$field SERIAL PRIMARY KEY";
}

return ", $field INTEGER $null $default";
}

#[\Override]
public function string(string $field, string $default = "", string $null = "NOT NULL"): string
{
return ", $field VARCHAR(255) $null $default";
}

#[\Override]
public function text(string $field, string $null = "NOT NULL"): string
{
return ", $field TEXT $null";
}

public function created_at(): string
#[\Override]
public function createdAt(): string
{
return ", created_at TIMESTAMP without time zone";
}

public function updated_at(): string
#[\Override]
public function updatedAt(): string
{
return ", updated_at TIMESTAMP without time zone";
}

public function pk(string $field): string
#[\Override]
public function primaryKey(string $field): string
{
return "";
}
Expand Down
Loading
Loading