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 |
|---|
stringThe key/name assigned to the method when added
mixedArguements 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 |
|---|
stringThe name of the class the filter is in
stringThe method the filter is in
mixedThe data that is being passed to the filter
arrayoptions 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 |
|---|
stringThe name of the class the adapter is in
stringTHe 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 |
|---|
stringThe associated method
\boolea$hasAdapter Returns true if it has an adapter or false if it doesn not
_hasFilter(string $class, string $method)
stringThe class the filter is in
stringThe method of the class that the filter is in
_invokeMethod(string $class, string $method, array $args) : mixed
| access |
protected |
|---|
stringThe name of the class to be called
stringThe name of the method in the class to be called
arrayAn 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 |
|---|
stringThe name of the class to be called
stringThe name of the method in the class to be called
arrayAn 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 |
|---|
arrayThe data in the adapter
_logFilter(array $data) : void
| access |
private |
|---|
arrayThe data in the filter
_logObserver(array $data) : void
| access |
private |
|---|
arrayThe data in the observer
_notify(string $event) : void
| access |
protected |
|---|
stringThe 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
stringNot really a table but a collection in a Mongo Database
arrayOptions to pass to Mongo collection
objectThe collection
addAdapter(string $trigger_class, string $trigger_method, string $call_class, array $options = array()) : void
| access |
public |
|---|---|
| todo |
add ability to adapt singleton class |
stringThe class that contains the function the adapter will respond too
stringThe method called that will have the adapter to be called.
stringThe new class to be called that has the same method name
arrayAn 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 |
mixedThis 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.
stringThe 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.
arrayOptions 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 |
|---|
stringThe name of the table that the column will be added too
stringThe name of the column to be adding to the table
arrayThe data that will define the column to be created. The array should contain the same information would would be passed too formatColumn (@see formatColumn).
arrayOptions 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 |
|---|
mixedConnection name can either be a string or integer.
arrayAnd 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 |
|---|
stringThe name of the class the filter is going in
stringThe name of the method the filter is in
stringThe class that the filter resides in.
stringThe method in the class that the parameters will be passed too.
arrayOptions 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 |
|---|
stringThe key/value that will be used to call the function
\functionThe anonymous function/closure to be added
addObserver(string $event, string $class, string $method, array $options = array()) : void
| access |
public |
|---|
stringThe name of the event that will cause a certain class and method to fire
stringThe name of the class that contains the function that will be fired for this event
stringThe name of the method that will be fired when the event occurs
arrayOptions 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 |
|---|
mixedAny 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 |
stringThe key/index to assign the value to
mixedData to be stored in the collection
bindParameters(string &$statement, array &$params)
stringThe sql statement
arrayThe parameters to bind with
catchDBError()
| todo |
dig into the function and redo |
|---|---|
clearFilters(string $class, string $method) : void
| access |
public |
|---|
stringThe class the filter is in
stringThe method of the class that the filter is in
clearObservers(string $event) : void
| access |
public |
|---|
stringThe event to remove all the observers from
clearTableData(string $tablename, string $options = '') : void
| access |
public |
|---|
stringThe name of the table to clear
stringOptions 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 }
stringThe name of the table to be checked
stringThe name of the column to check if exist
boolean$exist Returns true if exist, otherwise return false
columnTypeMap(string $type) : string
| access |
public |
|---|
stringThe 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 |
|---|
stringThe name of the to be created
arrayThe 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
arrayOptions 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
arrayArguements that define how the query will be created
arrayOptions 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 |
|---|
stringThe name of the table to remove the column from
stringThe name name of the column to be removed
arrayOptions 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 |
|---|
stringThe name of the table to be dropped
arrayOptions 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']; }
arrayrow: 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']; }
objectA result from a query object
array$row An assoctive array of a row from a table
formatColumn(string $name, $options = array()) : string
| access |
public |
|---|
stringThe 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);
mixeddata: Data with database characters removed
formatTableName(string $table_name, boolean $append_schema = true, boolean $append_prefix = true) : string
| access |
public |
|---|
stringThe name of the table to be formated
booleanWill append the schema to the table name
booleanWill 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 |
|---|
stringThe 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.
\dbojbectlink: Connection to the set database.
getDatabaseType() : string
stringdatabase: 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
stringThe main table to call pagination from
stringAny tables that are joined in this query
stringWhere SQL statement
integerThe current page. All pages or done by pageNumber-1. 0 is the first page
integerThe number of results to return per page
stringHow to order the results.
stringHow to count the results, default is 'COUNT(*) as count'
arrayresults: Returns the
getPreparedPlaceHolder(integer $count = 1) : \$string
| access |
public |
|---|
integerThe placeholder spot. Used for postgresql
\$stringThe 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 |
|---|
booleanWill 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
arrayConfiguration 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 |
|---|
mixedString 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 |
|---|
stringThe name of column to do the comparison operation
arrayThe args in key value and subkey value. The keys are conditionals and the value are what te conditio is being compparied too
stringThey conditional, either AND or OR for the query
stringHow to compare values
booleanFor 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
stringThe table the information will be deleted from.
arrayAn 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.
arrayFormats for the where.
arrayOptions 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 |
arrayThe data to be inserted in the format of the key being the column and the key's value being the data.
arrayStill in progress. Formats a preparted statemet.
arrayOptions mainly used for Mongo
preparedSelect(string $query, array $data, array $formats = array(), array $options = array()) : \data
| access |
public |
|---|---|
| todo |
write better code |
stringA query of formatted data to be inserted into the database.
arrayData to be inserted into the database. The key should be the column name and the value should be the column's value.
arrayThe formats for a prepared statement
arrayOptions 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
\dataresult: 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 |
stringThe 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 |
|---|
stringA SQL query
object$result Returns an object result related to the query passed
removeAdapter( $class, string $method) : void
| access |
public |
|---|
stringThe associated method
removeClassAdapter( $class) : void
| access |
public |
|---|
resultRowCount(object $result) : integer
Example: $result=PVDatabase::query("SELECT * FROM TABLE"); $count=PVDatabase::resultRowCount($result);
| access |
public |
|---|
objectA 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 |
|---|
stringThe query thing to be executed
stringThe field that is auto incremented and will be returned
stringThe 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 |
|---|
arrayAn array of arguement that define the select statement
arrayOptions to alter the select statement
\$mixed$results
set(string $index, mixed $value) : void
| access |
public |
|---|
stringThe key or index to store the value at
mixedA mixed value that can be anytype
setAdapterTrace(boolean $trace = false) : void
| access |
public |
|---|
booleanDefault 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 |
|---|
booleanDefault is false. If set to true, will trace filter.
setObserverTrace(boolean $trace = false) : void
| access |
public |
|---|
booleanDefault is false. If set to true, will trace observer.
stmt_bind_assoc(string &$stmt, string &$out)
stringThe mysql statement
stringThe output
tableExist(string $tablename, string $schema = '') : boolean
Example: if(!PVDatabase::tableExist('contacts')){ //Create table code }
| access |
public |
|---|
stringThe name of the table being checked if it exist
stringAdd 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 |
|---|
stringThe name of the table to update
arrayThe data to update in key => value ( column => value ) format
arrayOptions defined on where to update the value
arrayExtra 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 :