Connection View code

A class defining a database connection, and mechanisms for working with a database.

This library is largely compatible with adodb_lite with the pear,extended,transaction plugins with a few notable differences:

Differences:

  • GenID will not automatically create a sequence table.

    We encourage you to not use sequence tables and use auto-increment fields instead.

package

CMS

author

Robert Campbell

copyright

Copyright (c) 2015, Robert Campbell calguy1000@cmsmadesimple.org

since 2.2
property-read

The total query time so far in this request (in seconds)

property-read

The total number of queries executed so far.

Methods

__construct(\CMSMS\Database\ConnectionSpec $spec) 

Construct a new Connection.

NewDataDictionary() : \CMSMS\Database\DataDictionary

Create a new data dictionary object.

abstract

Data Dictionary objects are used for manipulating tables, i.e: creating, altering and editing them.

Response

\CMSMS\Database\DataDictionary

DbType() : string

Return the database type.

abstract

Response

string

Connect() : boolean

Open the database connection.

abstract

Response

boolean

Success or failure

Disconnect() 

Close the database connection.

abstract
IsConnected() : boolean

Test if the connection object is connected to the database.

abstract

Response

boolean

Close() 

An alias for Disconnect.

final
QMagic(string $str) : string

Quote a string magically using the magic quotes flag.

deprecated

This method is now just a deprecated alias for the qstr flag as we now require magic quotes to be disabled.

Arguments

string $str

Response

string

qstr(string $str) : string

Quote a string in a database agnostic manner.

abstract

Warning: This method may require two way traffic with the database depending upon the database.

Arguments

string $str

Response

string

concat() : string

output the mysql expression for a string concatenation.

abstract

This function accepts a variable number of string arguments.

Response

string

IfNull(string $field, string $ifNull) : string

Output the mysql expression to test if an item is null.

abstract

Arguments

string $field

The field to test

string $ifNull

The value to use if $field is null.

Response

string

Affected_Rows() : integer

Output the number of rows affected by the last query.

abstract

Response

integer

Insert_ID() : integer

Return the numeric ID of the last insert query into a table with an auto-increment field.

abstract

Response

integer

Prepare(string $sql) : \CMSMS\Database\Statement

Create a prepared statement object.

abstract

Arguments

string $sql

The SQL query

Response

\CMSMS\Database\Statement

SelectLimit(string $sql, integer $nrows = -1, integer $offset = -1,  $inputarr = null) : \CMSMS\Database\ResultSet

Execute an SQL Select and limit the output.

Arguments

string $sql

integer $nrows

The number of rows to return

integer $offset

The starting offset of rows to return

$inputarr

Response

\CMSMS\Database\ResultSet

Execute(string $sql, array $inputarr = null) : \CMSMS\Database\ResultSet

Execute an SQL Command

Arguments

string $sql

The SQL statement to execute.

array $inputarr

Any parameters marked as placeholders in the SQL statement.

Response

\CMSMS\Database\ResultSet

GetArray(string $sql, array $inputarr = null) : array

Execute an SQL Commmand and return all of the results as an array.

Arguments

string $sql

The SQL statement to execute.

array $inputarr

Any parameters marked as placeholders in the SQL statement.

Response

array

An associative array of matched results.

GetAll(string $sql, array $inputarr = null) : array

An alias for the GetArray method.

Arguments

string $sql

The SQL statement to execute.

array $inputarr

Any parameters marked as placeholders in the SQL statement.

Response

array

GetAssoc(string $sql, array $inputarr = null, boolean $force_array = false, boolean $first2cols = false) 

A method to return an associative array.

deprecated

Arguments

string $sql

The SQL statement to execute

array $inputarr

Any parameters marked as placeholders in the SQL statement.

boolean $force_array

Force each element of the output to be an associative array.

boolean $first2cols

Only output the first 2 columns in an associative array. Does not work with force_array.

GetCol(string $sql, array $inputarr = null, boolean $trim = false) : array

Execute an SQL statement that returns one column, and return all of the matches as an array.

Arguments

string $sql

The SQL statement to execute.

array $inputarr

Any parameters marked as placeholders in the SQL statement.

boolean $trim

Optionally trim the output results.

Response

array

A single flat array of results, one entry per row matched.

GetRow(string $sql, array $inputarr = null) : array

Exeute an SQL statement that returns one row of results, and return that row as an associative array.

Arguments

string $sql

The SQL statement to execute.

array $inputarr

Any parameters marked as placeholders in the SQL statement.

Response

array

An associative array representing a single resultset row.

GetOne(string $sql, array $inputarr = null) : mixed

Execute an SQL statement and return a single value.

Arguments

string $sql

The SQL statement to execute.

array $inputarr

Any parameters marked as placeholders in the SQL statement.

Response

mixed

BeginTrans() 

Begin a transaction

abstract
StartTrans() 

Begin a smart transaction

abstract
CompleteTrans() 

Complete a smart transaction.

abstract

This method will either do a rollback or a commit depending upon if errors have been detected.

CommitTrans(boolean $ok = true) 

Commit a simple transaction.

abstract

Arguments

boolean $ok

Indicates wether there is success or not.

RollbackTrans() 

Roll back a simple transaction.

abstract
FailTrans() 

Mark a transaction as failed.

abstract
HasFailedTrans() : boolean

Test if a transaction has failed.

abstract

Response

boolean

GenID(string $seqname) : integer

For use with sequence tables, this method will generate a new ID value.

deprecated abstract

This function will not automatically create the sequence table if not specified.

Arguments

string $seqname

The name of the sequence table.

Response

integer

CreateSequence(string $seqname, integer $startID) : boolean

Create a new sequence table.

deprecated abstract

Arguments

string $seqname

the name of the sequence table.

integer $startID

Response

boolean

DropSequence(string $seqname) : boolean

Drop a sequence table

abstract

Arguments

string $seqname

The name of the sequence table.

Response

boolean

DBTimeStamp(integer $timestamp) : string

A utility method to convert a unix timestamp into a database specific string suitable for use in queries.

Arguments

integer $timestamp

Response

string

UnixTimeStamp(string $str) : integer

A convenience method for converting a database specific string representing a date and time into a unix timestamp.

Arguments

string $str

Response

integer

DBDate(mixed $date) : string

Convert a date into something that is suitable for writing to a database.

Arguments

mixed $date

Either a string date, or an integer timestamp

Response

string

UnixDate() : integer

Generate a unix timestamp representing the current date at midnight.

deprecated

Response

integer

Time() : integer

An alias for the UnixTimestamp method.

Response

integer

Date() : integer

An Alias for the UnixDate method.

Response

integer

ErrorMsg() : string

Return a string describing the latest error (if any)

abstract

Response

string

ErrorNo() : integer

Return the latest error number (if any)

abstract

Response

integer

SetErrorHandler(callable $fn = null) 

Set an error handler function

Arguments

callable $fn

SetDebugMode(boolean $flag = true, callable $debug_handler = null) 

Toggle debug mode.

Arguments

boolean $flag

Enable or Disable debug mode.

callable $debug_handler

SetDebugCallback(callable $debug_handler = null) 

Set the debug callback.

Arguments

callable $debug_handler

Initialize(\CMSMS\Database\Connectionspec $spec) : \CMSMS\Database\Connection

Create a new database connection object.

static

This is the preferred wa to open a new database connection.

Arguments

\CMSMS\Database\Connectionspec $spec

An object describing the database to connect to.

Response

\CMSMS\Database\Connection

Constants

This constant defines an error with connecting to the database.

ERROR_CONNECT

This constant defines an error with an execute statement.

ERROR_EXECUTE

This constant defines an error with a transaction.

ERROR_TRANSACTION

This constant defines an error in a datadictionary command.

ERROR_DATADICT

Properties

query_time_total

query_time_total : float

The total query time so far in this request (in seconds)

Type(s)

float

query_count

query_count : integer

The total number of queries executed so far.

Type(s)

integer