Properties

$mConnectionResource

$mConnectionResource : resource

Resource created by the database connection

Type

resource

$mDbHost

$mDbHost : string

Database host name

Type

string

$mDbUser

$mDbUser : string

Database username

Type

string

$mDbPass

$mDbPass : string

Database password

Type

string

$mDbName

$mDbName : string

Database schema name

Type

string

$mVersionsTable

$mVersionsTable : string

The name of the table that holds the schema version update history

Type

string

Methods

__construct()

__construct(string $dbHost, string $dbUser, mixed $dbPass, string $dbName)

Class constructor.

Setup parameters through AiCommon and access the database through that class' instance of this class

Parameters

string $dbHost

The database host name

string $dbUser

The database username

mixed $dbPass

The database password

string $dbName

The name of the database

connect()

connect() : void

Connect to the database and set the member connection resource

query()

query(string $query, string $file, int $line) : resource

Perform a query of the database.

This does not assume that strings have been escaped, so be sure to escape them.

Use the PHP macros FILE and LINE for the 2nd and 3rd parameters.

Parameters

string $query

The query to perform

string $file

The file making the query

int $line

The line number in the querying file

Returns

resource

escapeString()

escapeString(string $string) : string

Escape a string in a manner that the chosen DBMS can handle

Parameters

string $string

The string to escape

Returns

string

blockInjection()

blockInjection(string $string) : string

Returns any part of a query before a semi-colon

If there is an attempt of an injection attack, or if your string input legitimately has semicolons, this is likely to cause truncated input.

If your input may contain semicolons, be sure that you trust the source before allowing it to perfom a query.

Parameters

string $string

The string to de-inject

Returns

string

fetchRow()

fetchRow(resource $result) : array

Fetch a row form the database

Parameters

resource $result

The conenction resource returned by a query

Returns

array

fetchArray()

fetchArray(resource $result) : array

Fetch a row form the database and return it as an indexed array and an associative array with field names as indexes

For consistency, returned keys are cast to lower case

Parameters

resource $result

The conenction resource returned by a query

Returns

array

fieldName()

fieldName(resource $result, int $columnOffset) : string

Get the name of a field at the given offset

For consistency, returned values are cast to lower case

Parameters

resource $result

The conenction resource returned by a query

int $columnOffset

The offset of the requested column (0 is the first column)

Returns

string

numRows()

numRows(resource $result) : int

The number of rows returned by a query

Parameters

resource $result

The conenction resource returned by a query

Returns

int

numFields()

numFields(resource $result) : int

The number of fields returned by a query

Parameters

resource $result

The conenction resource returned by a query

Returns

int

fieldType()

fieldType(resource $result, int $columnOffset) : string

The type of field at the given offset

Parameters

resource $result

The conenction resource returned by a query

int $columnOffset

The offset of the requested column (0 is the first column)

Returns

string

simpleFieldType()

simpleFieldType(resource $result, int $columnOffset) : string

A uniform type of field at the given offset.

Returns a string that represents a simplified field type for consistency across DBMS'

We don't check every field type. This has been written for the field types that APP(ideas) uses commonly. It will probably need modification to suit the needs of others.

This globs a lot of different types together. You may need to make them more fine-grained

Parameters

resource $result

The conenction resource returned by a query

int $columnOffset

The offset of the requested column (0 is the first column)

Returns

string

fixBoolean()

fixBoolean(string $booleanValue) : bool

Convert a database-formatted boolean value into something consistent with PHP

Since PostgreSQL has a native boolean data type, this is looking for "TRUE" or "FALSE"

Parameters

string $booleanValue

A boolean value retrieved from a database query

Returns

bool

fixDbBoolean()

fixDbBoolean(string $booleanValue) : string

Fixes a boolean input value of any kind to one understood by the DBMS

Since PostgreSQL has a boolean data type, this will return either "true" or "false"

Parameters

string $booleanValue

A boolean value retrieved from a database query

Returns

string

insertBlank()

insertBlank(string $tableName, string $fieldName, mixed $requiredFields) : int

Inserts a blank record into the requested table and returns the value of the surrogate key of the new record.

This allows us to use a "modify" method for saving new data rather than separate "add" and "modify"

If one of your "requiredFields" is a string, you must enclose it in apostrophes on input

Parameters

string $tableName

The name of the table into which we are inserting

string $fieldName

The name of the field into which we are inserting

mixed $requiredFields

An array of other fields and values that must not be null on a new record insert

Returns

int

beginTransaction()

beginTransaction(string $file, int $line) : void

Begins a database transaction if supported by the DBMS

Parameters

string $file

The file making the query. Leave empty to report the DB connector file name.

int $line

The line number in the querying file. Leave empty to report the DB connector line number.

endTransaction()

endTransaction(string $file, int $line) : void

Ends (commits) a database transaction if supported by the DBMS

Parameters

string $file

The file making the query. Leave empty to report the DB connector file name.

int $line

The line number in the querying file. Leave empty to report the DB connector line number.

getCurrentTimestamp()

getCurrentTimestamp(string $format) : string

Retrieves a value that can be inserted into the database as a date or timestamp indicating the current date and/or time

Parameters

string $format

The format of the string to return. One of 'database' 'epoch' or a PHP date() format. The default is 'database'

Returns

string

getCurrentVersion()

getCurrentVersion() : int

Returns -1 if the versions table cannot be found, the current version number otherwise

Returns

int

getUpgradeFiles()

getUpgradeFiles(string $fromDir) : array

Retrieves a list of all available files for possible upgrade/downgrade of the database schema.

Will echo an error and exit if file validation does not pass

Parameters

string $fromDir

The input directory

Returns

array

validateUpgradeFiles()

validateUpgradeFiles(array $files) : bool

Makes sure there are no skips in the version numbers

Parameters

array $files

The retrieved files

Returns

bool

doUpgrade()

doUpgrade(int $fromVersion, string $direction) : string

Performs an upgrade to the latest version or downgrade a single version

Parameters

int $fromVersion

The user's current schema version

string $direction

"up" for an upgrade or "down" for a downgrade

Returns

string

recordSchemaChange()

recordSchemaChange(string $action, int $versionNumber) : void

Records a schema change in the database

Parameters

string $action

The action to perform. Either "add" or "delete"

int $versionNumber

The version number to record