\Hazaar\DBI
Adapter
Relational Database Interface

The DB Adapter module provides classes for access to relational database via PDO (PHP Data Object) drivers and classes. This approach allows developers to use these classes to access a range of different database servers.

PDO has supporting drivers for:

Access to database functions is all done using a common class structure.

Example Usage

$db = new Hazaar\DBI\Adapter(); $result = $this->execute('SELECT * FROM users'); while($row = $result->fetch()){ //Do things with $row here }

Summary
Methods Properties Constants
log
No constants
Properties
$config
$config
Static
$connections
$connections
Static
$default_config
$default_config
$driver
$driver
$migration_log
$migration_log
$schema_file
$schema_file
$statements
$statements
Methods
__call()
__call($tablename, $args)

Parameters

$tablename

No description

$args

No description

__construct()
__construct($config_env = null)

Parameters

$config_env

No description

__get()
__get($tablename)

Parameters

$tablename

No description

addColumn()
addColumn($table, $column_spec)

Parameters

$table

No description

$column_spec

No description

addConstraint()
addConstraint($info, $table)

Parameters

$info

No description

$table

No description

alterColumn()
alterColumn($table, $column, $column_spec)

Parameters

$table

No description

$column

No description

$column_spec

No description

beginTransaction()
beginTransaction()
call()
call($method, $args = Array ( ) )

Parameters

$method

No description

$args

No description

colExists()
colExists($needle, $haystack, $key = 'name')

Parameters

$needle

No description

$haystack

No description

$key

No description

commit()
commit()
configure()
configure($config)

Parameters

$config

No description

connect()
connect($dsn, $username = null, $password = null, $driver_options = null)

Parameters

$dsn

No description

$username

No description

$password

No description

$driver_options

No description

createIndex()
createIndex($index_name, $idx_info)

Parameters

$index_name

No description

$idx_info

No description

createInfoTable()
createInfoTable()
Creates the info table that stores the version info of the current database.
createSchema()
createSchema($schema)
Takes a schema definition and creates it in the database.

Parameters

$schemaarray

No description

createTable()
createTable($name, $columns)
Create a new table in the database.

Parameters

$namestring

No description

$columnsstring

No description

delete()
delete($table, $criteria)

Parameters

$table

No description

$criteria

No description

deleteAll()
deleteAll($table)

Parameters

$table

No description

describeSequence()
describeSequence($name)

Parameters

$name

No description

describeTable()
describeTable($name, $sort = null)

Parameters

$name

No description

$sort

No description

dropColumn()
dropColumn($table, $column)

Parameters

$table

No description

$column

No description

dropConstraint()
dropConstraint($name, $table)

Parameters

$name

No description

$table

No description

dropIndex()
dropIndex($name)

Parameters

$name

No description

dropTable()
dropTable($name)

Parameters

$name

No description

errorCode()
errorCode()
errorInfo()
errorInfo()
exec()
exec($sql)

Parameters

$sql

No description

execCount()
execCount()
execute()
execute($name, $input_parameters)

Parameters

$name

No description

$input_parameters

No description

exists()
exists($table, $criteria = Array ( ) )

Parameters

$table

No description

$criteria

No description

getAttribute()
getAttribute($option)

Parameters

$option

No description

getAvailableDrivers()
getAvailableDrivers()
getColumn()
getColumn($needle, $haystack, $key = 'name')

Parameters

$needle

No description

$haystack

No description

$key

No description

getColumnDiff()
getColumnDiff($new, $old)

Parameters

$new

No description

$old

No description

Static
getDefaultConfig()
getDefaultConfig($env = null)

Parameters

$env

No description

getDriver()
getDriver()
getMigrationLog()
getMigrationLog()
Returns the migration log

Snapshots and migrations are complex processes where many things happen in a single execution. This means stuff can go wrong and you will probably want to know what/why when they do.

When running \Hazaar\Adapter::snapshot() or \Hazaar\Adapter::migrate() a log of what has been done is stored internally in an array of timestamped messages. You can use the \Hazaar\Adapter::getMigrationLog() method to retrieve this log so that if anything goes wrong, you can see what and fix it/

getPreparedStatements()
getPreparedStatements()
getSchemaVersion()
getSchemaVersion()
getSchemaVersions()
getSchemaVersions($with_file_obj = false)

Parameters

$with_file_obj

No description

getTableDiffs()
getTableDiffs($new, $old)

Parameters

$new

No description

$old

No description

inTransaction()
inTransaction()
insert()
insert($table, $fields, $returning = null)

Parameters

$table

No description

$fields

No description

$returning

No description

lastInsertId()
lastInsertId()
listConstraints()
listConstraints($table = null, $type = null, $invert_type = false)

Parameters

$table

No description

$type

No description

$invert_type

No description

listForeignKeys()
listForeignKeys($table = null)

Parameters

$table

No description

listIndexes()
listIndexes($table = null)

Parameters

$table

No description

listPreparedStatements()
listPreparedStatements()
listPrimaryKeys()
listPrimaryKeys($table = null)

Parameters

$table

No description

listSequences()
listSequences()
listTables()
listTables()
List all tables currently in the connected database.

Tags

Since

2.0

log()
log($msg)
Logs a message to the migration log.

Parameters

$msgstring

The message to log.

migrate()
migrate($version = null, $test = false) : boolean
Database migration method.

This method does some fancy database migration magic. It makes use of the ‘db’ subdirectory in the project directory which should contain the schema.json file. This file is the current database schema definition.

A few things can occur here.

  1. If the database schema does not exist, then a new schema will be created using the schema.json schema definition file. This will create the database at the latest version of the schema.
  2. If the database schema already exists, then the current version is checked against the version requested using the $version parameter. If no version is requested ($version is NULL) then the latest version number is used.
  3. If the version numbers are different, then a migration will be performed.
  4. # If the requested version is greater than the current version, the migration mode will be ‘up’.
  5. # If the requested version is less than the current version, the migration mode will be ‘down’.
  6. All migration files between the two selected versions (current and requested) will be replayed using the migration mode.

This process can be used to bring a database schema up to the latest version using database migration files stored in the db/migrate project subdirectory. These migration files are typically created using the \Hazaar\Adapter::snapshot() method although they can be created manually. Take care when using manually created migration files.

The migration is performed in a database transaction (if the database supports it) so that if anything goes wrong there is no damage to the database. If something goes wrong, errors will be availabl in the migration log accessible with \Hazaar\Adapter::getMigrationLog(). Errors in the migration files can be fixed and the migration retried.

Parameters

$versionint

The database schema version to migrate to.

$test

No description

Returns

boolean

Returns true on successful migration. False if no migration was neccessary. Throws an Exception on error.

prepare()
prepare($sql, $name = null)
Prepared statements

Parameters

$sql

No description

$name

No description

query()
query($sql)

Parameters

$sql

No description

quote()
quote($string)

Parameters

$string

No description

renameTable()
renameTable($from_name, $to_name)

Parameters

$from_name

No description

$to_name

No description

replay()
replay($schema, $test = false)
Reply a database migration schema file

This should only be used internally by the migrate method to replay an individual schema migration file.

Parameters

$schemaarray

The JSON decoded schema to replay.

$test

No description

rollBack()
rollBack()
setAttribute()
setAttribute()
Static
setDefaultConfig()
setDefaultConfig($config, $env = null)

Parameters

$config

No description

$env

No description

snapshot()
snapshot($comment = null, $test = false) : boolean
Snapshot the database schema and create a new schema version with migration replay files.

This method is used to create the database schema migration files. These files are used by the \Hazaar\Adapter::migrate() method to bring a database up to a certain version. Using this method simplifies creating these migration files and removes the need to create them manually when there are trivial changes.

When developing your project

Currently only the following changes are supported:

  • Table creation, removal and rename.
  • Column creation, removal and alteration.
  • Index creation and removal.

Table rename detection works by comparing new tables with removed tables for tables that have the same columns. Because of this, rename detection will not work if columns are added or removed at the same time the table is renamed. If you want to rename a table, make sure that this is the only operation being performed on the table for a single snapshot. Modifying other tables will not affect this. If you want to rename a table AND change it’s column layout, make sure you do either the rename or the modifications first, then snapshot, then do the other operation before snapshotting again.

Tags

Throws

\Exception

Parameters

$commentstring

A comment to add to the migration file.

$test

No description

Returns

boolean

True if the snapshot was successful. False if no changes were detected and nothing needed to be done.

table()
table($name, $alias = null)

Parameters

$name

No description

$alias

No description

tableExists()
tableExists($table)
Test that a table exists in the connected database.

Parameters

$tablestring

The name of the table to check for.

update()
update($table, $fields, $criteria = Array ( ) )

Parameters

$table

No description

$fields

No description

$criteria

No description