PVDatabase controls the connections to various databases ranging from Mysql to MongoDB.
For future development, the class needs to be written, but it offers a lot of powerful features that including prepared statements, schema manipulation, sanitization, and other features.
Example:
//Initialize the class
PVDatabase::init();
//Two Different Configurations
$mysql_options = array(
'dbhost' => 'localhost',
'dbuser' => 'jondoe',
'dbpass'=>'abc123',
'dbtype'=>'mysql',
'dbname'=>'example1',
'dbport'=>3306
);
//Add The Connection
PVDatabase::addConnection('connection1', $mysql_options);
$postgres_options = array(
'dbhost' => 'localhost',
'dbuser' => 'janedoe',
'dbpass'=>'doeraeme',
'dbtype'=>'postgresql',
'dbname'=>'example2',
'dbport'=>5432
);
//Add The Connection
PVDatabase::addConnection('connection2', $postgres_options);
//Connect To the Mysql Database
PVDatabase::setDatabase('connection1');
//Sanitize input
$value = PVDatabase::makeSafe('SELECT ItemName, ItemDescription FROM Items WHERE ItemNumber = 999; DROP TABLE USERS ');
//Execute A Query
PVDatabase::query("INSERT INTO users(name) VALUES(${value})");
//Change the Database connection
PVDatabase::setDatabase('connection2');
todo |
break apart class into seperate database handlers |
---|---|
package |
system |
__callStatic(string $method, mixed $args = array()) : mixed
access |
public |
---|
string
The key/name assigned to the method when added
mixed
Arguements to pass to the annoymous function. The function is called using call_user_func_array.
mixed
$value The value returned is the value the stored function returns
_applyFilter(string $class, string $method, mixed $data, array $options = array()) : mixed
access |
protected |
---|
string
The name of the class the filter is in
string
The method the filter is in
mixed
The data that is being passed to the filter
array
options to be passed to the filter. Passed options we be passed to the function. -'default_return' mixed: If no filter is return, the data passed in by default will be return. Can be overriden -'event' string: An event to associate with the filter. Default is null
mixed
$data The data the function returns
_callAdapter(string $class, string $method) : mixed
access |
protected |
---|
string
The name of the class the adapter is in
string
THe name of the method the class is being called from.
mixed
$value A value that the adapter returns
_hasAdapter( $class, string $method) : \boolea
access |
protected |
---|
string
The associated method
\boolea
$hasAdapter Returns true if it has an adapter or false if it doesn not
_hasFilter(string $class, string $method)
string
The class the filter is in
string
The method of the class that the filter is in
_invokeMethod(string $class, string $method, array $args) : mixed
access |
protected |
---|
string
The name of the class to be called
string
The name of the method in the class to be called
array
An array of arguements. Arguements have to be embedded in an array to be called.
mixed
$data Data returned by the function called
_invokeStaticMethod(string $class, string $method, array $args) : mixed
access |
protected |
---|
string
The name of the class to be called
string
The name of the method in the class to be called
array
An array of arguements. Arguements have to be embedded in an array to be called.
mixed
$data Data returned by the function called
_logAdapter(array $data) : void
access |
private |
---|
array
The data in the adapter
_logFilter(array $data) : void
access |
private |
---|
array
The data in the filter
_logObserver(array $data) : void
access |
private |
---|
array
The data in the observer
_notify(string $event) : void
access |
protected |
---|
string
The name of the even that occured that will trigger notifies
_prepareLogData(array $data) : string
access |
private |
---|
array
string
$message JSON encode message of information about the data
_setMongoCollection(string $table_name, array $options = array()) : object
string
Not really a table but a collection in a Mongo Database
array
Options to pass to Mongo collection
object
The collection
addAdapter(string $trigger_class, string $trigger_method, string $call_class, array $options = array()) : void
access |
public |
---|---|
todo |
add ability to adapt singleton class |
string
The class that contains the function the adapter will respond too
string
The method called that will have the adapter to be called.
string
The new class to be called that has the same method name
array
An array of options that be called -'object' string : Assumes that default method in the class to be called is static. If called object needs to be instantiated, change to object to 'instance' and one will be created before the adapter calls the function -'call_method' string: By default the method to be called to override the current one should be the same name. But this can be ovveridden to call a different method. -'type' string: The type of method being called. Default is class_method but if the method is a closure, set the type to be 'closure' and make the $trigger_method the closure
addClassAdapter(mixed $trigger_class, string $call_class, array $options = array()) : void
access |
public |
---|---|
todo |
Add ability to use singleton classes |
mixed
This can either be the name of the class or an object whose methods will be adapted to another class. The class should be included or be autoloaded by this point.
string
The call class is the classes methods that will be called in place of the methods in the trigger_class. These class does not have to be included as this point.
array
Options that be used to further distinguish the behavior of the adapters added -'object' string: Determines if the object being adapted to is static or an instance.Default is static -'call_class' string: The name of the class that the methods will be adapted too. -'class' string: The name of the whose methods will be adapted to another class
addColumn(string $table_name, string $column_name, array $column_data = array(), array $options = array()) : string
access |
public |
---|
string
The name of the table that the column will be added too
string
The name of the column to be adding to the table
array
The data that will define the column to be created. The array should contain the same information would would be passed too formatColumn (@see formatColumn).
array
Options that define how adding a column operates. -'format_table' boolean: Formats the table name by adding the prefix set in the database config. Default is false. -'execute' boolean: Execute the query to create the table. Default is true. -'return_query' bolean: Return the generated query. Default is true;
string
$query Returns the query for creating the table name
addConnection(mixed $connection_name, array $args) : void
Example
$connection=array( 'dbhost'=>'localhost', 'dbuser'=>'admin', 'dbpass'=>'abc123' 'dbname'=>'mydb', 'dbtype'=>'postgresql' );
PVDatabase::addConnection('connection_1',$connection);
access |
public |
---|
mixed
Connection name can either be a string or integer.
array
And array that contains the information for connecting to the database.
addFilter(string $class, string $method, string $filter_class, string $filter_method, array $options = array()) : void
access |
public |
---|
string
The name of the class the filter is going in
string
The name of the method the filter is in
string
The class that the filter resides in.
string
The method in the class that the parameters will be passed too.
array
Options that can be set for further modifying the filter. -'object' string: If the method being called is static, static should be inserted. If its in an instance, 'instance' should be set. Default is set to static. -'event' string: Associate this filter with an event. -'type' string: The type of function being called. Default is class_method but if the function is a closure, set the type to be 'closure' and make the $filter_method the closure
addMethod(string $method, \function $closure) : void
access |
public |
---|
string
The key/value that will be used to call the function
\function
The anonymous function/closure to be added
addObserver(string $event, string $class, string $method, array $options = array()) : void
access |
public |
---|
string
The name of the event that will cause a certain class and method to fire
string
The name of the class that contains the function that will be fired for this event
string
The name of the method that will be fired when the event occurs
array
Options to further the define the firing of an event -'object' string : If the method being called is static, should be set to static. Else set to instance -'class' stinrg : The name of the class to be called. Default is the class that is passed in. -'method' string: The name of the method to be called. Default is the method that is passed in. -'type' string: The type of function being called. Default is class_method but if the function is a closure, set the type to be 'closure' and make the $method the closure
addToCollection(mixed $data) : void
access |
public |
---|
mixed
Any data type( Object, Array, int, etc) to add to the public data collection
addToCollectionWithName(string $name, mixed $data) : void
access |
public |
---|---|
todo |
check the relevance of get and set |
string
The key/index to assign the value to
mixed
Data to be stored in the collection
bindParameters(string &$statement, array &$params)
string
The sql statement
array
The parameters to bind with
catchDBError()
todo |
dig into the function and redo |
---|---|
clearFilters(string $class, string $method) : void
access |
public |
---|
string
The class the filter is in
string
The method of the class that the filter is in
clearObservers(string $event) : void
access |
public |
---|
string
The event to remove all the observers from
clearTableData(string $tablename, string $options = '') : void
access |
public |
---|
string
The name of the table to clear
string
Options to be added at the end of the SQL query
closeDB() : void
access |
public |
---|---|
columnExist(string $table_name, string $field_name) : boolean
Example: if(!PVDatabase::columnExist('test.contacts', 'first_name' )){ //Code to create table }
string
The name of the table to be checked
string
The name of the column to check if exist
boolean
$exist Returns true if exist, otherwise return false
columnTypeMap(string $type) : string
access |
public |
---|
string
The type of variabel to be matched
string
$match The matched type found
connect() : void
access |
private |
---|---|
createTable(string $table_name, array $columns = array(), array $options = array()) : string
access |
public |
---|
string
The name of the to be created
array
The columns that are to be created with the table. The syntax for creating the columns are from @see formatColumn. The column name is the key and parameters that create the column is the array that will be passed to formatColumns
array
Options that control the creation of a table. -'format_table' boolean: Formats the table by adding the table prefix set in the database configuration. Default is false. -'execute' boolean: Execute the query to create the table. Default is true. -'return_query' boolean: Returns the query that would create the table. Default is true -'primary_key' string: The primary key(s) of the table
string
$query The return query to create the table or false
dbAverageFunction( $field) : \:
Example: $query="SELECT ".PVDatabase:::dbAverageFunction('age')." as average_age FROM Table
\:
string average_function: The function needed to get the average value ina SQL string
deleteStatement(array $args, array $options = array()) : object
array
Arguements that define how the query will be created
array
Options that define how the query will run
object
$result The result of the query
dropColumn(string $table_name, string $column_name, array $options = array()) : string
access |
public |
---|
string
The name of the table to remove the column from
string
The name name of the column to be removed
array
Options that define how removing a column operates. -'format_table' boolean: Formats the table name by adding the prefix set in the database config. Default is false. -'execute' boolean: Execute the query to remove the column. Default is true. -'return_query' bolean: Return the generated query. Default is true;
string
$query Returns the query for removing the column
dropTable(string $table_name, array $options = array()) : string
access |
public |
---|
string
The name of the table to be dropped
array
Options that define how to remove the table -'format_table' boolean: Formats the table name by adding the prefix set in the database config. Default is false. -'execute' boolean: Execute the query to the table. Default is true. -'return_query' bolean: Return the generated query. Default is true;
string
$query Returns the query for dropping the stable
fetchArray( $result) : array
Example: $result=PVDatabase::query('SELECT title, description FROM TABLE'); while($row=PVDatabase::fetchArray($result)){ echo $row['title']; }
array
row: An assoctive array of a row from a table
fetchFields(object $result) : array
Example: $result=PVDatabase::query('SELECT title, description FROM TABLE'); while($row=PVDatabase::fetchArray($result)){ echo $row['title']; }
object
A result from a query object
array
$row An assoctive array of a row from a table
formatColumn(string $name, $options = array()) : string
access |
public |
---|
string
The name of the column to be formated
string
$format The column will be returned with arguements formatted to the set database.
formatData( $string) : mixed
Example: $name=PVDatabase::formatData($row['name']); OR $row=PVDatabase::formatData($row);
mixed
data: Data with database characters removed
formatTableName(string $table_name, boolean $append_schema = true, boolean $append_prefix = true) : string
access |
public |
---|
string
The name of the table to be formated
boolean
Will append the schema to the table name
boolean
Will append a prefix to the tablee, but behind the schema
string
$table_name The name of the table with the values appened in front of it
get(string $index) : mixed
access |
public |
---|
string
The index to retrieve a value from
mixed
$data The data that was stored at that index
getAutoIncrement() : string
access |
public |
---|---|
string
$increment The auto increment method with is database dependent
getConnectionName() : string
access |
public |
---|---|
string
$connection_name The name of the current connect
getDatabaseLink() : \dbojbect
For external madification of the link, this method will retun the current link.
\dbojbect
link: Connection to the set database.
getDatabaseType() : string
string
database: The database being used
getInstance() : object
access |
public |
---|---|
object
$instance Returns the instance of a class.
getIterator() : \PVIterator
getPagininationOffset(string $table, string $join_clause = '', string $where_clause = '', integer $current_page, integer $results_per_page = 20, string $order_by = '', string $fields = 'COUNT(*) as count') : array
string
The main table to call pagination from
string
Any tables that are joined in this query
string
Where SQL statement
integer
The current page. All pages or done by pageNumber-1. 0 is the first page
integer
The number of results to return per page
string
How to order the results.
string
How to count the results, default is 'COUNT(*) as count'
array
results: Returns the
getPreparedPlaceHolder(integer $count = 1) : \$string
access |
public |
---|
integer
The placeholder spot. Used for postgresql
\$string
The placeholder for the current database
getSchema(boolean $append_period = true) : string
Example: $table_name=PVDatabase::getSchema.'contacts'; $query="INSERT INTO $table_name(name, phone) VALUES('John Smith', '999-9999')"; PVDatabase::query($query);
access |
public |
---|
boolean
Will appaned a period to the schema name
string
$schema Returns the name of the current schema.
getSQLRandomOperator() : string
Example: $query="SELECT * TABLE ORDER BY ".PVDATABASE::getSQLRandomOperator;
access |
public |
---|---|
string
$avg_function
init(array $config = array()) : void
array
Configuration options to pass into the class
insertStatement( $table_name, $data, $options = array()) : void
access |
public |
---|
makeSafe(mixed $string) : mixed
Example:: $name=PVDatabase::makeSafe($_POST['name']); $number=PVDatabase::makeSafe($_POST['number']); PVDatabase::query("INSERT INTO TABLE(name, number) VALES('$name', '$number');
access |
public |
---|
mixed
String can either be a string or an array of data
mixed
$sanitized_data If the input is a string, a string will be return if the input is an array, an array will be returned/
parseOperators(string $column, array $args = array(), string $key = 'AND', string $operator = '=', boolean $first = true) : string
array('>' = '5') will become column > 5
todo |
Rewrite this function and description for clarity |
---|
string
The name of column to do the comparison operation
array
The args in key value and subkey value. The keys are conditionals and the value are what te conditio is being compparied too
string
They conditional, either AND or OR for the query
string
How to compare values
boolean
For recursive operation, is this the first value
string
$query A query to execute
preparedDelete(string $table, array $wherelist = array(), array $whereformats = array(), array $options = array()) : void
string
The table the information will be deleted from.
array
An array of whats fields to use when deleting the data. The key of the array should be the column name and the array's key value should be the value present in the column.
array
Formats for the where.
array
Options mainly for MongoDB
preparedInsert(string $table_name, array $data, array $formats = array())
access |
public |
---|---|
todo |
write better code |
string
array
array
preparedQuery(string $query, array $data, string $formats = '')
todo |
fix |
---|---|
string
array
string
preparedReturnLastInsert( $table_name, $returnField, $returnTable, array $data, array $formats = array(), array $options = array())
The query will be a prepared statement.
access |
public |
---|---|
todo |
write better code |
array
The data to be inserted in the format of the key being the column and the key's value being the data.
array
Still in progress. Formats a preparted statemet.
array
Options mainly used for Mongo
preparedSelect(string $query, array $data, array $formats = array(), array $options = array()) : \data
access |
public |
---|---|
todo |
write better code |
string
A query of formatted data to be inserted into the database.
array
Data to be inserted into the database. The key should be the column name and the value should be the column's value.
array
The formats for a prepared statement
array
Options than can be used to alter the query and its function -prequery string: SQL to add before the query -postquery string: Additonal information to add at the end of the normal
\data
result: Retuns a result that will need to be run through fetch process.
preparedUpdate(string $table, array $data, array $wherelist, array $formats = array(), array $whereformats = array(), array $options = array())
access |
public |
---|---|
todo |
write better code |
string
The name of the table to be updated.
array
array
array
array
array
query(string $query) : object
Example: $query='Select * FROM TABLE'; $result=PVDatabase::query($query);
access |
public |
---|
string
A SQL query
object
$result Returns an object result related to the query passed
removeAdapter( $class, string $method) : void
access |
public |
---|
string
The associated method
removeClassAdapter( $class) : void
access |
public |
---|
resultRowCount(object $result) : integer
Example: $result=PVDatabase::query("SELECT * FROM TABLE"); $count=PVDatabase::resultRowCount($result);
access |
public |
---|
object
A result from a query
integer
$count The number of rows in that result.
return_last_insert_query(string $query, string $returnField = '', string $returnTable = '') : mixed
returnField and returnTable are generally optional but required for databases such as PostgreSQL and MSSSQL
Example: $query="INSERT INTO TABLE('Test Data') VALUES('abc', '123')"; $id=PVDatabase::return_last_insert_query($query, 'id', 'TABLE');
access |
public |
---|
string
The query thing to be executed
string
The field that is auto incremented and will be returned
string
The table the auto-incremented value exist in
mixed
$id The id of the last inserted field
selectPreparedStatement(array $args, array $options = array())
array
array
selectStatement(array $args, array $options = array()) : \$mixed
access |
public |
---|
array
An array of arguement that define the select statement
array
Options to alter the select statement
\$mixed
$results
set(string $index, mixed $value) : void
access |
public |
---|
string
The key or index to store the value at
mixed
A mixed value that can be anytype
setAdapterTrace(boolean $trace = false) : void
access |
public |
---|
boolean
Default is false. If set to true, will trace adatper.
setDatabase( $profile_id) : void
Example: PVDatabase::setDatabase(0);
access |
public |
---|
setFilterTrace(boolean $trace = false) : void
access |
public |
---|
boolean
Default is false. If set to true, will trace filter.
setObserverTrace(boolean $trace = false) : void
access |
public |
---|
boolean
Default is false. If set to true, will trace observer.
stmt_bind_assoc(string &$stmt, string &$out)
string
The mysql statement
string
The output
tableExist(string $tablename, string $schema = '') : boolean
Example: if(!PVDatabase::tableExist('contacts')){ //Create table code }
access |
public |
---|
string
The name of the table being checked if it exist
string
Add a schema to check against
boolean
$exist Will be true if the tabe exist, else false;
updateStatement(string $table, array $data, array $wherelist, array $options = array()) : void
access |
public |
---|
string
The name of the table to update
array
The data to update in key => value ( column => value ) format
array
Options defined on where to update the value
array
Extra options when updating a table
theQuery :
link :
version :
mySQLConnection :
postgreSQLConnection :
oracleConnection :
msSQLConnection :
sqLiteConnection :
mongoConnection :
connections :
mysql_error_report :
dbhost :
dbname :
dbuser :
dbpass :
dbtype :
dbschema :
dbprefix :
dbport :
current_connecton :
row :
_collection :
_methods :
_adapters :
_observers :
_instances :
_filters :
_traceAdapters :
_traceFilters :
_traceObservers :