schoenbeck / phpdatabase
Database Connector, Adaptor and Configurator for php
Requires
- symfony/yaml: ^5.1
This package is auto-updated.
Last update: 2022-04-07 21:37:15 UTC
README
Easy to install with composer
$ composer require schoenbeck/phpdatabase
Functions
- Create a connection with a database
- Send requests to a database
- Check and configure the database and tables
Usage
Create connection
First you have to load the connection configuration for the database. You need to declare the following parts:
- host
- port
- driver
- user
- password
- database
You can choose between two ways to load them:
- Save configuration in
$GLOBALS
variable - Create
DatabaseConfig
Object and pass it to theDatabaseConnection
Object.
1. Save configuration in $GLOBALS
variable
Structure of $GLOBALS
variable:
$GLOBALS['GLOBAL_CONFIG']['DB']['host'] = "host"; $GLOBALS['GLOBAL_CONFIG']['DB']['port'] = "port"; $GLOBALS['GLOBAL_CONFIG']['DB']['driver'] = "driver"; $GLOBALS['GLOBAL_CONFIG']['DB']['user'] = "user"; $GLOBALS['GLOBAL_CONFIG']['DB']['password'] = "password"; $GLOBALS['GLOBAL_CONFIG']['DB']['database'] = "database";
Create a new DatabaseConnection
Object and the configuration will be loaded.
$databaseConnection = new DatabaseConnection($databaseConfiguration);
2. Create DatabaseConfig
Object
$databaseConfiguration = new DatabaseConfig('host', 'user', 'password', 'port', 'database', 'driver'); $databaseConnection = new DatabaseConnection($databaseConfiguration);
Send requests
You can send requests directly with the DatabaseConnection
Object.
$query = 'SELECT * FROM User;'; $databaseConnection->execSQLStatement($query);
Another way to send request to the database is to use the DatabaseAdapter
class, which build the query statements for you.
$databaseAdaptor = new DatabaseAdaptor($databaseConnection); $result = $databaseAdaptor->selectFromTable('User', ['id', 'firstName', 'lastName']);
The result will automatic formatted as an php array.
Use the DatabaseConfigurator
to create, alter or drop tables and columns
Before you can configure the database you have to create a config file. This file holds the configuration of the tables.
Note that the
DatabaseConfigurator
always add an index to every table. Do not declare one in the configuration file.
In the current version of this package the following config file types are supported:
- Yaml
tables: User: firstName: type: varchar(50) default: lastName: type: int(10) default: -1 notNull: true
After creating the config file you can now load them. For that you create a DatabaseConfigurator
Object and pass it a DatabaseAdaptor
Object.
$databaseConfigurator = new DatabaseConfigurator($databaseAdaptor);
For each supported config file type exist an own method to load and configure the database.
$databaseConfigurator->checkDatabaseConfigYamlFile('database.yml')
DryRun
The DatabaseConfigurator
support a 'dryRun' mode. It return an array which contains the queries the configurator would send.
$databaseConfigurator->checkDatabaseConfigYamlFile('database.yml', $dryRun = true)
License
MIT Licensed, http://www.opensource.org/licenses/MIT