# Class Database

DataTables Database connection object.

Create a database connection which may then have queries performed upon it.

This is a database abstraction class that can be used on multiple different databases. As a result of this, it might not be suitable to perform complex queries through this interface or vendor specific queries, but everything required for basic database interaction is provided through the abstracted methods.

Namespace: DataTables
Located at Database.php

Methods summary
  `public` | # `__construct( string[] $opts )` Database instance constructor. Database instance constructor. #### Parameters $opts Array of connection parameters for the database:
      array(
               "user" => "", // User name
               "pass" => "", // Password
               "host" => "", // Host name
               "port" => "", // Port
               "db"   => "", // Database name
               "type" => ""  // Datable type: "Mysql", "Postgres" or "Sqlite"
           )
`public boolean` | # `any( string|string[] $table, array $where = null )` Determine if there is any data in the table that matches the query condition Determine if there is any data in the table that matches the query condition #### Parameters $table Table name(s) to act upon. $where Where condition for what to select - see Query::where. #### Returns boolean Boolean flag - true if there were rows
`public `DataTables\Database`` | # `commit( )` Commit a database transaction. Commit a database transaction. Use with `DataTables\Database::transaction()` and `DataTables\Database::rollback()`. #### Returns `DataTables\Database`
`public Number` | # `count( string|string[] $table, string $field = "id", array $where = null )` Get a count from a table. Get a count from a table. #### Parameters $table Table name(s) to act upon. $field Primary key field name $where Where condition for what to select - see Query::where. #### Returns Number
`public boolean|`DataTables\Database`` | # `debug( boolean $set = null )` Get / set debug mode. Get / set debug mode. #### Parameters $set $_ Debug mode state. If not given, then used as a getter. #### Returns boolean|`DataTables\Database` Debug mode state if no parameter is given, or self if used as a setter.
`public `DataTables\Database\Result`` | # `delete( string|string[] $table, array $where = null )` Perform a delete query on a table. Perform a delete query on a table. This is a short cut method that creates an update query and then uses the query('delete'), table, where and exec methods of the query. #### Parameters $table Table name(s) to act upon. $where Where condition for what to delete - see Query::where. #### Returns `DataTables\Database\Result`
`public `DataTables\Database\Result`` | # `insert( string|string[] $table, array $set, array $pkey = '' )` Insert data into a table. Insert data into a table. This is a short cut method that creates an update query and then uses the query('insert'), table, set and exec methods of the query. #### Parameters $table Table name(s) to act upon. $set Field names and values to set - see Query::set. $pkey Primary key column names (this is an array for forwards compt, although only the first item in the array is actually used). This doesn't need to be set, but it must be if you want to use the `Result->insertId()` method. #### Returns `DataTables\Database\Result`
`public `DataTables\Database\Result`` | # `push( string|string[] $table, array $set, array $where = null, array $pkey = '' )` Update or Insert data. When doing an insert, the where condition is added as a set field Update or Insert data. When doing an insert, the where condition is added as a set field #### Parameters $table Table name(s) to act upon. $set Field names and values to set - see Query::set. $where Where condition for what to update - see Query::where. $pkey Primary key column names (this is an array for forwards compt, although only the first item in the array is actually used). This doesn't need to be set, but it must be if you want to use the `Result->insertId()` method. Only used if an insert is performed. #### Returns `DataTables\Database\Result`
`public `DataTables\Database\Query`` | # `query( string $type, string|string[] $table = null )` Create a query object to build a database query. Create a query object to build a database query. #### Parameters $type Query type - select, insert, update or delete. $table Table name(s) to act upon. #### Returns `DataTables\Database\Query`
`public string` | # `quote( string $val, string $type = \PDO::PARAM_STR )` Quote a string for a quote. Note you should generally use a bind! Quote a string for a quote. Note you should generally use a bind! #### Parameters $val Value to quote $type Value type #### Returns string
`public `DataTables\Database\Result`` | # `raw( )` Create a `Query` object that will execute a custom SQL query. This is similar to the `sql` method, but in this case you must call the `exec()` method of the returned `Query` object manually. This can be useful if you wish to bind parameters using the query `bind` method to ensure data is properly escaped. Create a `Query` object that will execute a custom SQL query. This is similar to the `sql` method, but in this case you must call the `exec()` method of the returned `Query` object manually. This can be useful if you wish to bind parameters using the query `bind` method to ensure data is properly escaped. #### Returns `DataTables\Database\Result` #### Example Safely escape user input
    $db
         ->raw()
         ->bind( ':date', $_POST['date'] )
         ->exec( 'SELECT * FROM staff where date < :date' );
`public resource` | # `resource( )` Get the database resource connector. This is typically a PDO object. Get the database resource connector. This is typically a PDO object. #### Returns resource PDO connection resource (driver dependent)
`public `DataTables\Database`` | # `rollback( )` Rollback the database state to the start of the transaction. Rollback the database state to the start of the transaction. Use with `DataTables\Database::transaction()` and `DataTables\Database::commit()`. #### Returns `DataTables\Database`
`public `DataTables\Database\Result`` | # `select( string|string[] $table, array $field = "*", array $where = null, array $orderBy = null )` Select data from a table. Select data from a table. This is a short cut method that creates an update query and then uses the query('select'), table, get, where and exec methods of the query. #### Parameters $table Table name(s) to act upon. $field Fields to get from the table(s) - see Query::get. $where Where condition for what to select - see Query::where. $orderBy Order condition - see Query::order. #### Returns `DataTables\Database\Result`
`public `DataTables\Database\Result`` | # `selectDistinct( string|string[] $table, array $field = "*", array $where = null, array $orderBy = null )` Select distinct data from a table. Select distinct data from a table. This is a short cut method that creates an update query and then uses the query('select'), distinct ,table, get, where and exec methods of the query. #### Parameters $table Table name(s) to act upon. $field Fields to get from the table(s) - see Query::get. $where Where condition for what to select - see Query::where. $orderBy Order condition - see Query::order. #### Returns `DataTables\Database\Result`
`public `DataTables\Database\Result`` | # `sql( string $sql )` Execute an raw SQL query - i.e. give the method your own SQL, rather than having the Database classes building it for you. Execute an raw SQL query - i.e. give the method your own SQL, rather than having the Database classes building it for you. This method will execute the given SQL immediately. Use the `raw()` method if you need the ability to add bound parameters. #### Parameters $sql SQL string to execute (only if _type is 'raw'). #### Returns `DataTables\Database\Result` #### Example Basic select
    $result = $db->sql( 'SELECT * FROM myTable;' );
Set the character set of the connection
    $db->sql("SET character_set_client=utf8");
       $db->sql("SET character_set_connection=utf8");
       $db->sql("SET character_set_results=utf8");
`public `DataTables\Database`` | # `transaction( )` Start a new database transaction. Start a new database transaction. Use with `DataTables\Database::commit()` and `DataTables\Database::rollback()`. #### Returns `DataTables\Database`
`public `DataTables\Database\Result`` | # `update( string|string[] $table, array $set = null, array $where = null )` Update data. Update data. This is a short cut method that creates an update query and then uses the query('update'), table, set, where and exec methods of the query. #### Parameters $table Table name(s) to act upon. $set Field names and values to set - see Query::set. $where Where condition for what to update - see Query::where. #### Returns `DataTables\Database\Result`