classes / obfdb.php
OBFDB
Class
Database class. Manages SQL connections and queries, keeps track of last query and errors. Also hosts some utility methods.
public
&get_instance
(
)
public
&get_instance
(
)
Create an instance of OBFDB or return the already created instance.
instance
public
__construct
(
)
public
__construct
(
)
OBFDB constructor. Adds connection information to the instance based on what's defined in OB global setting (OB_DB_HOST, OB_DB_USER, OB_DB_PASS, OB_DB_NAME), and fires a few queries to set the SQL mode.
public
add_profile
(
query, duration
)
public
add_profile
(
query, duration
)
Add a database profile. Returns FALSE if profiling is disabled for this instance.
query |
|
duration |
status
public
affected_rows
(
)
public
affected_rows
(
)
Get the number of affected rows from the last query.
num_affected
public
assoc_list
(
)
public
assoc_list
(
)
Get an associative list from the previous query as an array.
assoc_list
public
assoc_row
(
)
public
assoc_row
(
)
Get the next associative array from the last query. Useful when there is only one presumed row (e.g. when selecting with WHERE ID = x).
assoc_row
public
autocommit
(
bool $value
)
public
autocommit
(
bool $value
)
Enable or disable autocommit. Useful for limiting model methods to validation only, or otherwise testing before writing changes to the database.
bool |
value |
public
calc_found_rows
(
)
public
calc_found_rows
(
)
Call this to be able to use found_rows() after the select.
public
commit
(
)
public
commit
(
)
Commit changes and re-enable autocommit.
public
delete
(
table
)
public
delete
(
table
)
High-level delete function using parameters set by Active-Record methods. Returns the result of query() or FALSE if no WHERE is set (to avoid inadvertently deleting all rows).
table |
result
public
disable_profiling
(
)
public
disable_profiling
(
)
Disable database profiling.
public
enable_profiling
(
)
public
enable_profiling
(
)
Enable database profiling. Note that this clears any pre-existing profiles set for this instance by assigning a new empty array to it.
public
error
(
)
public
error
(
)
Get a string description of the last error.
error
public
escape
(
str
)
public
escape
(
str
)
Escape a string.
str |
escaped_str
public
format_backticks
(
value
)
public
format_backticks
(
value
)
Format a value by escaping it and wrapping backticks around it.
value |
formatted_value
public
format_datetime
(
timestamp = null
)
public
format_datetime
(
timestamp = null
)
Generate a SQL-formatted datetime from a timestamp.
timestamp |
sql_timestamp
public
format_table_column
(
value
)
public
format_table_column
(
value
)
Format a table.column value by wrapping backticks around both table and column.
value |
formatted_value
public
format_value
(
value
)
public
format_value
(
value
)
Format a value to be used in SQL queries, escaping it and wrapping quotes around it if it's not an integer.
value |
formatted_value
public
found_rows
(
)
public
found_rows
(
)
Get the number of 'found rows' (ignoring limit/offset) from the last query. Returns FALSE if no rows were found or calc_found_rows() wasn't used.
num_rows
public
get
(
table
)
public
get
(
table
)
High-level get function, using previously set parameters in Active-Record. Returns associative list with results or FALSE.
table |
assoc_list
public
get_one
(
table
)
public
get_one
(
table
)
Same as get(), but returns only the first result. Useful when selecting based on a specific ID. Returns FALSE if no row is returned.
table |
assoc_list
public
get_profiles
(
)
public
get_profiles
(
)
Get database profiles.
public
id_exists
(
table, id
)
public
id_exists
(
table, id
)
Quick query to check if an ID exists in a table.
table |
|
id |
public
indexed_list
(
)
public
indexed_list
(
)
Get an indexed array from the last query.
index_list
public
indexed_row
(
)
public
indexed_row
(
)
Get the next indexed row from the last query.
index_row
public
insert
(
table, data
)
public
insert
(
table, data
)
High-level insert function using parameters set with Active-Record methods. Returns insertion ID or FALSE if the query failed.
table |
|
data |
insert_id
public
insert_id
(
)
public
insert_id
(
)
Get the ID of the last insert query.
id
public
last_query
(
)
public
last_query
(
)
Get the last query.
last_query
public
leftjoin
(
table, column1, column2
)
public
leftjoin
(
table, column1, column2
)
Active-Record, add LEFT JOIN to query.
table |
|
column1 |
|
column2 |
public
limit
(
limit
)
public
limit
(
limit
)
Active-Record, add LIMIT to query. Returns FALSE if no valid integer provided.
limit |
status
public
num_rows
(
)
public
num_rows
(
)
Get the number of rows from the last select query.
num_rows
public
offset
(
offset
)
public
offset
(
offset
)
Active-Record, add OFFSET to query. Returns FALSE if no valid integer provided.
offset |
status
public
orderby
(
column, dir = 'asc'
)
public
orderby
(
column, dir = 'asc'
)
Active-Record, add ORDER BY to query.
column |
|
dir |
Direction, default 'asc'. |
public
orderby_string
(
string
)
public
orderby_string
(
string
)
Set a custom ORDER BY string. Is NOT escaped, so needs to be used with care.
string |
public
query
(
sql
)
public
query
(
sql
)
Basic SQL query. Pass SQL to the method which will be run on the configured database.
Note that this method does NOT provide the safeties in place in query-builders. As a result, be EXTRA sure to strip any possible problematic characters from queries, and just entirely avoid using this with user-controlled input if at all possible.
sql |
The SQL query to run on the database. |
public
random_order
(
)
public
random_order
(
)
Return query result in random order.
private
reset_hlvars
(
)
private
reset_hlvars
(
)
Reset the Active-Record query.
public
rollback
(
)
public
rollback
(
)
Cancel database updates and re-enable autocommit.
public
update
(
table, data
)
public
update
(
table, data
)
High-level update function using parameters set with Active-Record methods. Returns the result from query(), or TRUE if nothing to update.
table |
|
data |
result
public
what
(
column, as = null, escape = true
)
public
what
(
column, as = null, escape = true
)
Active-Record, add WHAT to query.
column |
|
as |
|
escape |
Escape column string, default TRUE. |
public
where
(
column, value, operator = '='
)
public
where
(
column, value, operator = '='
)
Active-Record, add WHERE to query.
column |
|
value |
|
operator |
Comparison operator, default '='. |
public
where_col
(
column, value, operator = '='
)
public
where_col
(
column, value, operator = '='
)
Equivalent to where().
column |
|
value |
|
operator |
Comparison operator, default '='. |
public
where_like
(
column, value
)
public
where_like
(
column, value
)
Active-Record, add WHERE LIKE to query.
column |
|
value |
public
where_mode
(
mode
)
public
where_mode
(
mode
)
Set the WHERE mode for the query to OR or AND. Returns FALSE when an invalid mode is selected.
mode |
status
public
where_not_like
(
column, value
)
public
where_not_like
(
column, value
)
Active-Record, add WHERE NOT LIKE to query.
column |
|
value |
public
where_string
(
string
)
public
where_string
(
string
)
Set a custom WHERE string. Is NOT escaped, so needs to be used with care.
string |