API Documentation for: 1.1.1
Show:

Database Class

Namespace: Canteen\Database

Abstract database connection. Use's the mysqli API.

$db = new Database('localhost', 'root', '12341234', 'my_database');

Constructor

Database

(
  • host
  • username
  • password
  • databases
)

Parameters:

  • host String

    The database host

  • username String

    The database user

  • password String

    The database password

  • databases Dictionary | String

    The database name aliases as key=>names, or a single database name string

Methods

cacheAll

(
  • [enabled=true]
)

If we want to turn on caching for all select, row, result or count

Parameters:

  • [enabled=true] Boolean optional

    If we should enable this defaults to true

cacheNext

()

Cache the next select, row, result or count

create

(
  • table
)
CreateQuery

Create a new table on the database

Parameters:

  • table String

    The name of the table to create

Returns:

CreateQuery:

The create query

currentAlias

() String

Get the currently selected database alias

Returns:

String:

Name database alias

currentDatabase

() String

Get the currently selected database

Returns:

String:

Name of the actual database name (not the alias)

delete

(
  • tables
)
DeleteQuery

A method for deleting rows from table(s)

Parameters:

  • tables String multiple

    The tables as arguments e.g. delete('users', 'users_other')

Returns:

DeleteQuery:

New delete query

disconnect

()

Close the database connection, if any is open

drop

(
  • table
)
DropQuery

Remove a table

Parameters:

  • table String

    The name of an existing table to drop

Returns:

DropQuery:

The new drop query

escapeString

(
  • value
)
String | Array

Escape a value to add

Parameters:

  • value String | Array

    The value to add or collection of values

Returns:

String | Array:

The escaped string or collection of strings

execute

(
  • sql
)
Mysqli_result

Execute a query

Parameters:

Returns:

Mysqli_result:

The mysqli query object

flush

() Boolean

Flush the cache

Returns:

Boolean:

If flush was successful

getArray

(
  • sql
  • [cache=false]
)
Array

Fetch an array of associate array results for a query

Parameters:

  • sql String | Array

    The SQL query or collection of queries to get array results for

  • [cache=false] Boolean optional

    If we should cache the result (default is false)

Returns:

Array:

The array of results or null (if invalid result)

getDatabaseName

(
  • [alias='default']
)
String

Get the name of the database by an alias

Parameters:

  • [alias='default'] String optional

    The valid database alias to check by

Returns:

String:

The name of the database

getLength

(
  • sql
  • [cache=false]
)
Int

Count the number of return rows for a sql query

Parameters:

  • sql String

    The SQL query

  • [cache=false] Boolean optional

    If this should be cached

Returns:

Int:

Number of rows

getResult

(
  • sql
  • field
  • [position=0]
  • [cache=false]
)
Mixed

Fetch a single value from an sql call

Parameters:

  • sql String

    The SQL query

  • field String

    The name of the field

  • [position=0] Int optional

    The position of the row to return, default is first row

  • [cache=false] Boolean optional

    If this should be cached

Returns:

Mixed:

The value of the field or null

getRow

(
  • sql
  • [cache=false]
)
Array

Fetch a single row from the database

Parameters:

  • sql String

    The SQL query

  • [cache=false] Boolean optional

    If this should be cached

Returns:

Array:

The row as a non-associative array

insert

(
  • tables
)
InsertQuery

Insert query

Parameters:

  • tables String multiple

    The tables as arguments e.g. insert('users', 'users_other')

Returns:

InsertQuery:

New insert query

insertId

() Int

Returns the auto generated id used in the last query

Returns:

Int:

The integer for the last insert

internalExecute

(
  • sql
)
Array | Mysqli_result private

Execute a query internally

Parameters:

Returns:

Array | Mysqli_result:

The mysqli query object

isConnected

() Boolean

Check to see if the database is currently connected

Returns:

Boolean:

If the database is connected

nextId

(
  • table
  • field
)
Int

Function to get the next ordered ID from a table index

Parameters:

  • table String

    The table to search on

  • field String

    The name of the field to search

Returns:

Int:

The integer for the next ID

parseQueries

(
  • sql
  • type
  • [cache=false]
  • [field=null]
  • [position=null]
)
Mixed private

Parse the queries as a type

Parameters:

  • sql String | Query | Array

    The collection or single SQL query

  • type String

    The type of parse, 'getRow', 'getResult', 'getArray', 'getLength'

  • [cache=false] Boolean optional

    If we should cache the result

  • [field=null] String optional

    The field name for getResult

  • [position=null] Int optional

    The position number for getResult

Returns:

Mixed:

The result data

parseResults

(
  • results
  • type
  • [field=null]
  • [position=null]
)
Mixed private

Parse the queries as a type

Parameters:

  • results Mysqli_result | Array

    The collection or single mysqli_result

  • type String

    The type of parse, 'getRow', 'getResult', 'getArray', 'getLength'

  • [field=null] String optional

    The field name for getResult

  • [position=null] Int optional

    The position number for getResult

Returns:

Mixed:

The result data

read

(
  • type
  • sql
  • cache
)
Mixed private

Get the cache data if available and active

Parameters:

  • type String

    The name of the method being cached

  • sql String

    The SQL query

  • cache Boolean

    If this should be cached

Returns:

Mixed:

The cached data or false

save

(
  • type
  • sql
  • data
  • cache
)
Boolean private

Save the cache data

Parameters:

  • type String

    The name of the method being cached

  • sql String

    The SQL query

  • data Mixed

    The data to set

  • cache Boolean

    If this should be cached

Returns:

Boolean:

If the cache was saved

select

(
  • [properties='*']
)
SelectQuery

Create a select query

Parameters:

  • [properties='*'] Array | String optional

    The list of properties, can be multiple arguments

Returns:

SelectQuery:

The select query

setCache

(
  • cache
)

Set a cache object to use

Parameters:

  • cache IDatabaseCache

    The cache object, interface with IDatabaseCache

setDatabase

(
  • [alias='default']
)
Int

Change the database by an alias

Parameters:

  • [alias='default'] String optional

    The alias to change to

Returns:

Int:

Return 1 if we have changed and error's if we haven't

show

(
  • [alias='']
)
ShowQuery

Show the current tables on a database

Parameters:

  • [alias=''] String optional

    The optional alias, defaults to the current database

Returns:

ShowQuery:

The array of tables on this database

startPooling

()

Turn on query pooling, this will not execute queries until stopPooling is called

stopPooling

(
  • [cache=false]
)
Array

Execute the pooled queries in the order in which they were added

Parameters:

  • [cache=false] Boolean optional

    If se should cache the result for later

Returns:

Array:

The result objects or null if no pooling

tableExists

(
  • table
  • [alias='']
)
Boolean

Check to see if a table exists

Parameters:

  • table String

    The name of the table

  • [alias=''] String optional

    to check on a specific database alias

Returns:

Boolean:

If the table exists or not

truncate

(
  • table
)
TruncateQuery

Remove all of the rows from a table

Parameters:

  • table String

    The name of the table to truncate

Returns:

TruncateQuery:

The new truncate query

update

(
  • tables
)
UpdateQuery

An update function

Parameters:

  • tables String multiple

    The tables as arguments e.g. update('users', 'users_other')

Returns:

UpdateQuery:

New update query

Properties

_cache

IDatabaseCache private

The cache object for the database, must implement IDatabaseCache

_cacheAll

Boolean private

If we should cache all calls, no matter what

_cacheNext

Boolean private

If we should cache the next sql statement

_connection

Mysqli private

The mysql connect

_currentAlias

String private

The current alias

_databases

Array private

The database references

_defaultCacheContext

String private

The main cache key for the index of cached calls

_pool

Array private

The query of things to pool

_pooling

Boolean private

If we are pooling queries to execute as once

profilerStart

Array | String private

The optional user callback reference to start profiling

profilerStop

Array | String private

The optional user callback reference to stop profiling