Download the PHP package gp/dbms without Composer
On this page you can find all versions of the php package gp/dbms. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package dbms
Short Description Simple PHP Database tool with QueryParam class supports mysqli and pdo
License MIT
Informations about the package dbms
GP DBMS Library
The GP DBMS Library is a database management system written in PHP. It provides tools for interacting with databases, managing models, querying data, and handling relationships. The library is designed to make database interaction easier and more efficient.
Table of Contents
- Requirements
- Installation
- Getting Started
- Features
- Classes
- DBQuery
- Database
- Model
- Events
- Usage
- Frame Queries
- Select Query
- Insert Query
- Update Query
- Delete Query
- Running Queries
- Transaction Management
- Creating custom DB Driver
- Creating an ORM Model
- Performing CRUD Operations with ORM Model
- Handling Relationships
- Creating Event
- Creating and Loading ORM Models with Relations
- Example
- Contributing
- License
- Contact
- Author
Requirements
- PHP 7.4 or higher.
- Composer (optional but recommended for autoloading).
Installation
You can install gp_dbms
using Composer. Run the following command in your terminal:
Getting Started
After installation, you can start using the package by including the autoloader:
Features
- Flexible Database Connections: Supports multiple database drivers like
PDO
andmysqli
. -
Query Builder: Easily create and execute SQL queries with chaining methods.
-
Query Building
- Select: Supports selecting specific columns or all columns.
- Insert: Allows inserting data with direct values and function-based values.
- Update: Updates data with field-value pairs and optional conditions.
- Delete: Deletes rows from a table with optional conditions.
- Where Conditions
AND
andOR
conditions can be applied using thewhere()
andorWhere()
methods.- Supports single conditions, array-based conditions, and parameterized conditions.
- Joins
- Supports
INNER JOIN
.
- Supports
- Ordering and Limiting
- Sorting via
orderBy()
method. - Limiting rows with
limit()
method.
- Sorting via
- Query Resetting
- Resets the query state using
reset()
or_resetQuery()
.
- Resets the query state using
- Query Execution Helpers
- Builds the query string using
getQuery()
. - Provides bind values for prepared statements with
getBindValues()
.
- Builds the query string using
- Extensibility
- The
DBQuery
class is designed to be extended by database-specific drivers. For instance:
- The
-
- Object-Relational Mapping (ORM): Map tables to PHP classes for seamless data manipulation.
- Relationships: Handle
HasOne
andHasMany
relationships with lazy and eager loading. - Eager & Lazy Loading: Supports Eager and lazy loading.
- Lifecycle Events: Trigger hooks like
onSave
,onDelete
, andonUpdate
for models. - Exception Handling: Provides meaningful error messages through
DatabaseException
. - Transaction Support: Execute operations within transactions with commit/rollback capabilities.
- Extensibility: Extend models and relationships to customize functionality as needed.
Clasess
DBQuery
The DBQuery
class is a versatile and extensible query builder designed for database operations. It provides methods to build SELECT
, INSERT
, UPDATE
, and DELETE
queries with various features like joins, where conditions, ordering, grouping, and more. This class is meant to be extended by specific database drivers.
Methods Documentation
Core Methods | Method | Description |
---|---|---|
select(...$columns): DBQuery | Builds the SELECT query with the specified columns. | |
insert(string $table, array $fields, array $funcfields = []): DBQuery | Constructs the INSERT query. | |
update(string $table, array $fields, mixed $where = null, ?string $join = null): DBQuery | Builds the UPDATE query. | |
delete(string $table, mixed $where = null): DBQuery | Creates the DELETE query. | |
where(array|string ...$args): DBQuery | Adds AND conditions to the query. | |
orWhere(array|string ...$args): DBQuery | Adds OR conditions to the query. |
Helper Methods | Method | Description |
---|---|---|
getQuery(): string | Returns the constructed query string. | |
getBindValues(): array | Returns the bind values for prepared statements. | |
reset(): void | Resets the query state. |
Notes
- All queries are parameterized to prevent SQL injection.
- Ensure proper handling of bind values in the database execution layer.
- Extend this class to add database-specific functionality as needed.
Database
The Database
class is an abstract superclass designed to serve as the base for all database-related operations. It provides foundational methods for query building, query execution, and transaction management across various database drivers. All driver-specific classes should extend this class and implement the abstract methods to provide database-specific functionality.
Methods Documentation
Core Methods | Method | Description |
---|---|---|
query(string $query, array $bindValues = []): bool | Executes a raw SQL query with optional bind values. | |
execute(): bool | Executes the previously built query using DBQuery. | |
set(string $name, string $value): bool | Sets a SQL variable. | |
begin(): bool | Starts a transaction. | |
commit(): bool | Commits the current transaction. | |
rollback(): bool | Rolls back the current transaction. | |
getOne() | Fetches a single row from the result set. | |
getAll() | Fetches all rows from the result set. | |
setQuery($query) | Sets a custom query. | |
setDbQuery($dbQuery) | Sets the DBQuery object. |
Abstract Methods (To Be Implemented by Drivers)
Method | Description |
---|---|
close() | Closes the database connection. |
runQuery(string $sql, array $bindValues = []): bool | Executes a query and returns a success flag. |
executeQuery(): bool | Executes the previously built query and stores the result. |
fetch() | Fetches a single row from the result set. |
getInstance(string $host, string $user, string $pass, string $db, array $configs = []): Database | Retrieves a singleton instance of the database driver. |
insertId(): int | Returns the ID of the last inserted row. |
escape(string $value): string | Escapes a string for safe usage in queries. |
Properties
Property | Description |
---|---|
$con | Holds the database connection object. |
$result | Stores the result set of a query. |
$dbQuery | Instance of the DBQuery class for query building. |
$query | Contains the executed query string. |
$bindValues | Holds the bind values for the query. |
$instance | Singleton instance of the database class. |
Notes
- Error Handling: The DatabaseException is thrown for invalid method calls or query errors.
- Extensibility: Extend this class to implement database driver-specific functionality.
-
Security: All queries should use parameterized statements to prevent SQL injection.
Model
The Model
class is an abstract base class for all database models. It provides methods for common ORM operations such as creating, reading, updating, and deleting records. It also includes support for handling relationships through HasMany and HasOne relations.
- HasMany - The HasMany class represents a one-to-many relationship between two models. For example, a User model can have many Post models.
- HasOne - The HasOne class represents a one-to-one relationship between two models. For example, a User model might have one Profile model.
Methods Documentation
Method | Description |
---|---|
save($_is_dirty_update) | Saves the model. Inserts a new record or updates an existing one. |
delete() | Deletes the model based on its unique key. |
find($_identifier) | Finds a record by its unique identifier. |
findAll($_query) | Finds all records matching the query. |
insert($_data) | Inserts a new record into the database. |
update($_data, $_where) | Updates an existing record in the database. |
toDbRow() | Converts the model to a database row format. |
fromDbRow($_data) | Loads the model from a database row. |
setDbQuery($query) | Sets the DBQuery instance for the model. |
triggerEvent($_event) | Triggers an event such as beforeSave, afterSave, etc. |
getUniqueId() | Returns the unique key and its value for the model. |
getTableName() | Returns the name of the database table associated with the model. |
Notes
- Extensibility: Override getTableName and getUniqueKey to customize table names and primary keys.
- Events: Use event hooks to add custom logic during save, update, insert, or delete operations.
- Lazy vs Eager Loading: Use lazy loading for on-demand related data and eager loading for preloading related models.
Events
The Events
class in the ORM system is an abstract class that defines constants for various lifecycle events and provides a mechanism for handling these events. It is primarily used to trigger specific actions during the lifecycle of a model, such as before or after saving, deleting, loading, inserting, or updating.
Methods Documentation
Constant | Description |
---|---|
EVENT_BEFORE_SAVE | Triggered before a model is saved. |
EVENT_AFTER_SAVE | Triggered after a model is saved. |
EVENT_BEFORE_DELETE | Triggered before a model is deleted. |
EVENT_AFTER_DELETE | Triggered after a model is deleted. |
EVENT_BEFORE_INSERT | Triggered before a model is inserted into the database. |
EVENT_AFTER_INSERT | Triggered after a model is inserted into the database. |
EVENT_BEFORE_UPDATE | Triggered before a model is updated. |
EVENT_AFTER_UPDATE | Triggered after a model is updated. |
EVENT_BEFORE_LOAD | Triggered before a model is loaded from the database. |
EVENT_AFTER_LOAD | Triggered after a model is loaded from the database. |
Abstract Method | Method | Description |
---|---|---|
handle(Model $_model) | Handles events for a given model. Must be implemented in derived classes. |
Usage
FrameQueries
Select Query
Insert Query
Update Query
Delete Query
Running Queries
Transaction Management
Creating custom DB Driver
Creating an ORM Model
Performing CRUD Operations with ORM Model
Handling Relationships
HasMany
HasOne
Creating Event
Creating and Loading ORM Models with Relations
Example
Contributing
Contributions are welcome! If you would like to contribute to gp_validator, please follow these steps:
- Fork the repository.
- Create a new branch (git checkout -b feature/- YourFeature).
- Make your changes and commit them (git commit -m 'Add some feature').
- Push to the branch (git push origin feature/YourFeature).
- Open a pull request.
- Please ensure that your code adheres to the coding standards and includes appropriate tests.
License
This package is licensed under the MIT License. See the LICENSE file for more information.
Contact
For questions or issues, please reach out to the development team or open a ticket.
Author
- Periyandavar Github ([email protected])