public
|
#
__construct( array $data )
Constructor
Parameters
- $data
- the connection properties, with keys host, dbname, username, password
|
public static
|
#
add( string $name, array $params )
Add a configuration for a database connection
Add a configuration for a database connection
Parameters
- $name
- The instance name
- $params
The connection parameters.
Each element of this array is an array itself, containing the connections data :
- host : The database hostname / ip and port,
- username : The user name
- password : The user password
- dbname : The database name,
When openning the connection, if the first connection fails,
a connection will be tried on the second element, and then.
This is usefull for master / slave connections
|
public static
Hawk\DB
|
#
get( String $name )
Get the open connection, or open it if not already open.
This method manages master / slaves connections
Get the open connection, or open it if not already open.
This method manages master / slaves connections
Parameters
- $name
- The name of the instance
Returns
|
public
mixed
|
#
query( string $sql, array $binds = array(), array $options = array() )
Execute a SQL query on the SQL server, and get the result of execution
Execute a SQL query on the SQL server, and get the result of execution
Parameters
- $sql
- The query to execute,
- $binds
- The binded value to send to the server
- $options
The result options. This array can contains the following data :
- 'return' (mixed) : The type of return (default DB::RETURN_STATUS). It can be set to :
. DB::RETURN_STATUS : Returns the execution status
. DB::RETURN_ARRAY : Each result row is an associative array, with columns as keys
. DB::RETURN_OBJECT : Eeach result row is a StdClass instance, with columns as properties
. DB::RETURN_LAST_INSERT_ID : Return the last inserted id
. DB::RETURN_AFFECTED_ROWS : Returns the number of affected rows
. DB::RETURN_QUERY : Returns the query itself
. DB::RETURN_CURSOR : Returns a cursor to fetch the results
. A classname : Each result row is an instance of the given classname, with columns as properties
- 'onerow' (bool) : If true, the function will return the first row of the result set (default false)
- 'index' (string) : Defines the column values to get as array result index (default null)
- 'args' (array) : This can be used when you define a classname as 'return', to pass arguments to the class constructor
Returns
mixed The execution result, depending on what type of return has been defined in $options parameter
|
public
boolean
|
#
selectDb( string $dbname )
Select a database
Parameters
- $dbname
- The database to select
Returns
boolean true if the database has been sucessfully selected, false in other cases
|
public
mixed
|
#
select( array $query )
Build and execute a SELECT query on the selected database, and return the result
Build and execute a SELECT query on the selected database, and return the result
Parameters
- $query
The parameters of the query. This parameter can have the following data :
- 'from' string (required) : The table name where to search
- 'fields' array (optionnal) : The fields to get. Each element of this array can be :
- A single element of the column name
- A key/value combination that will be parse to 'key as value' in the SQL query
- 'where' string | DBExample (optionnal) : The search condition. Can be a SQL expression or a DBExample instance
- 'group' array (optionnal) : The columns to group the result, each column in an array element
- 'having' string (optinnal) : The 'HAVING' expression, formatted as a SQL expression
- 'orderby' array (optionnal) : The result sort, where each key is a column name, and the values define the order (ASC, DESC)
- 'start' int (optionnal) : The first row number to get the results
- 'limit' int (optionnal) : The maximum number of rows to return
- 'one' bool (optionnal) : If set to true, then the first row will be returned
- 'binds' array (optionnal) : The binded values
- 'index' string (optionnal) : If set, the result arrar will be indexed by the value of the column set by this property
- 'return' mixed (default : DB::RETURN_ARRAY) : The return type (all possible values are defined on the method 'query')
Returns
mixed The query result
|
public
mixed
|
#
insert( string $table, array $insert = array(), string $flag = '', string $onduplicatekey = '' )
Insert a row in a table
Parameters
- $table
- The table where to insert data
- $insert
- The data to insert, where keys are the columns names, and values the values to insert
- $flag
- A flag on the INSERT query (IGNORE or DELAYED)
- $onduplicatekey
- The ON DUPLICATE KEY expression
Returns
mixed The value of the last inserted id
|
public
mixed
|
#
replace( string $table, array $insert = array() )
Replace data in a table
Parameters
- $table
- the table where to replace data
- $insert
- The data to insert, where keys are the columns names, and values the values to insert
Returns
mixed The value of the last inserted id
|
public
integer
|
#
update( string $table, string|Hawk\DBExample $where = null, array $update = array(), array $binds = array() )
Update records in a table
Update records in a table
Parameters
- $table
- The table to update
- $where
- The condition to find rows to update
- $update
- The columns to update, where keys are the columns names and values are the values to update
- $binds
- The binded values, in case of $where is a SQL expression
Returns
integer The number of updated rows
|
public
integer
|
#
delete( string $table, string|Hawk\DBExample $where = null, array $binds = array() )
Delete records in a table
Delete records in a table
Parameters
- $table
- The table to update
- $where
- The condition to find rows to delete
- $binds
- The binded values, in case of $where is a SQL expression
Returns
integer The number of deleted rows
|
public
integer
|
#
count( string $table, string|Hawk\DBExample $where = null, array $binds = array(), string $field = null, array $group = array() )
Count elements in a table
Count elements in a table
Parameters
- $table
- The table to count elements
- $where
- The condition to find rows to count
- $binds
- The binded values, in case of $where is a SQL expression
- $field
- To count a specific field
- $group
- Groups rows before counting them
Returns
integer The number of found elements
|
public static
string
|
#
formatField( string $str )
Format a string to a SQL field format : [table .]field
Format a string to a SQL field format : [table .]field
Parameters
- $str
- The string to format
Returns
string The formatted string
|
public
array
|
#
getLogs( )
Get the DB logs
Returns
array The logged queries
|
public
string
|
#
quote( string $str, integer $type = \PDO::PARAM_STR )
Quote a string
Parameters
- $str
- The data to quote
- $type
- The data type
Returns
string The quoted string
|
public static
string
|
#
getFullTablename( string $table, string $prefix = null )
Get the real name of a table, with the configured prefix
Get the real name of a table, with the configured prefix
Parameters
- $table
- The base table name
- $prefix
- If set, this prefix will replace the one configured for the application
Returns
string The complete name of the table
|