Cradle  0.3.3
Simple library for creating Web-based applications
Sql Class Reference

SQL request wrapper. More...

Static Public Member Functions

static select ($query="", $data=null, $connection=null, $returnStatment=false)
 
static insert ($query, $data=null, $connection=null, $returnStatment=false)
 
static update ($query, $data=null, $connection=null, $returnStatment=false)
 
static delete ($query, $data=null, $connection=null, $returnStatment=false)
 
static getLastError ()
 
static connect ($connection=null)
 

Static Public Attributes

static $default
 

Detailed Description

SQL request wrapper.

A simple SQL (PDO) request wrapper class. Designed to make simple SQL requests more easy and short.

Supported databse types: mysql, pgsql, oci, sqlite, dblib(mssql,sybase) ...and all of PDO db types. Use:

print_r(PDO::getAvailableDrivers());

to see all avalible PDO db types.

Version
3.0
Author
Digger mrdig.nosp@m.ger@.nosp@m.sad-s.nosp@m.yste.nosp@m.ms.ru
Warning
Use of type=[dblib] need to change in file "/usr/local/etc/freetds.conf":
[global] port = 1433 tds version = 8.0

Example of usage:

<?php
//--- Set the connection params:
$conn = [
"type" => "mysql", //<-- mysql by default
"host" => "localhost",
"base" => "test",
"user" => "test",
"pass" => "test",
"table"=> "users", //<-- Option
];
//--- Open connection (option):
if ( $dbh = Sql::connect($conn) ) {
echo "Connected. ";
} else {
die (print_r(Sql::getLastError(), true));
}
//=== SELECT request:
//--- Full use:
$result = Sql::select("select * from table1 where name=:name order by name", array("name"=>"my neme"));
//--- Short use:
$result = Sql::select(); // -> "select * from [TABLE]"
$result = Sql::select( "", "", $conn ); // -> "select * from [TABLE]" using params from $conn
//--- Spesial use:
$result = Sql::select( "from [TABLE]", ["name"=>"my name"] ); // -> "select * from [TABLE] where name='my name'"
$result = Sql::select( "a,b,c from [TABLE]", ["name"=>"my neme"] ); // -> "select a,b,c from [TABLE] where name='my name'"
$result = Sql::select( "table2", ["name"=>"my name"] ); // -> "select * from table2 where name='my name'"
$result = Sql::select( "where name like :name", ["name"=>"my neme"] ); // -> "select * from [TABLE] where name like 'my name'"
$result = Sql::select( "[WHERE] order by name", ["name"=>"new"] ); // -> "select * from [TABLE] where name='new' order by name"
$result = Sql::select( "from [TABLE] order by name" ); // -> "select * from [TABLE] order by name"
print_r($result);
// keyword `[TABLE]` will be autamatic replaced by value from $conn['table'];
//=== INSERT request:
//--- Full use:
echo Sql::insert( "insert into table1 (a,b,c) values (:a,:b,:c)", array("a"=>1, "b"=>2, "c"=>3), $conn )
//--- Short use:
echo Sql::insert( "", ["a"=>1, "b"=>2, "c"=>3] );
//--- Spesial use:
echo Sql::insert( "into [TABLE]", ["id_user"=>"1003", "name"=>"test"] );
echo Sql::insert( "table2", ["id_user"=>"1003", "name"=>"test"] );
//=== UPDATE request:
//--- Full use:
echo Sql::update( "update table1 set name=?, id_user=? where name=?", array("my name", "1002", "old name"), $conn );
//--- Spesial use:
echo Sql::update( "set name=?, id_user=? where name=?", ["my name", "1002", "old name"] );
echo Sql::update( "[TABLE] set name=?, id_user=? where name=?", ["my name", "1002", "old name"] );
echo Sql::update( "table1 set name=?, id_user=? where name=?", ["my name", "1002", "old name"] );
// --------- params ------------
echo Sql::update( "", array( ["name"=>"new name", "id_user"=>1002], ["name" => "old name"] ) );
// ---------- data (set) ------------- --- keys (where) ---
//=== DELETE request:
//--- Full use:
echo Sql::delete( "delete from table1 where name=:a", array("a"=>"my neme"), $conn );
//--- Spesial use:
echo Sql::delete( "", ["id_user"=>"1003", "name"=>"test"] );
echo Sql::delete( "from [TABLE]", ["name"=>"my neme"]);
echo Sql::delete( "table1", ["name"=>"my neme"] );

Member Function Documentation

static connect (   $connection = null)
static

Initiates an SQL connection

Parameters
array$connectionAn array of connection params:
[
"type" => "mysql",
"host" => "hostname",
"base" => "database name",
"user" => "user login",
"pass" => "password",
"table"=> "default table name", //<-- option
];
Returns
connector | false
static delete (   $query,
  $data = null,
  $connection = null,
  $returnStatment = false 
)
static

SQL DELETE request

Parameters
string$query
if $default["autoquery"]==true then:
1. delete from ...
2. from ...
3. ...
4. < empty >
tag [TABLE] - will be replaced by $connection['table']
else $query mast be standard sql-query: "delete from ... where ..."
array$data
1. $query="delete from ... where f1=?, f2=?, f3-?, ..." => $data = array(1, 2, 3 ...)
2. $query="delete from ... where f1=:f1 and f2=:f2 and f3=:f3, ..." => $data = array("f1"=>1, "f2"=>2, "f3"=>3 ...)
array$connection(option) An array of connection params (see a method connect)
boolean$returnStatment[ true | false ]
false - return rowCount
true - return { PDOStatement }
Returns
rowCount | false
static getLastError ( )
static

Returns the last error of request

Returns
array An array contains: [code, message, query]
static insert (   $query,
  $data = null,
  $connection = null,
  $returnStatment = false 
)
static

SQL INSERT Request

Parameters
String$query
if $default["autoquery"]==true then:
1. insert into ...
2. into ...
3. ...
4. < empty >
tag [TABLE] - will be replaced by $connection['table']
else $query mast be standard sql-query: "insert into ... (...) values (...)"
array$data
1. $query="insert into ... (f1,f2,f3...) values (?,?,?,..)" => $data = array(1, 2, 3 ...)
2. $query="insert into ... (f1,f2,f3...) values (:f1,:f2,:f3,...)" => $data = array("f1"=>1, "f2"=>2, "f3"=>3 ...)
array$connection(option) An array of connection params (see a method connect)
boolean$returnStatment[ true | false ]
false - return rowCount
true - return { PDOStatement }
Returns
rowCount | false
static select (   $query = "",
  $data = null,
  $connection = null,
  $returnStatment = false 
)
static

SQL SELECT Request

Parameters
string$query
if $default["autoquery"]==true then:
1. select ...
2. ... from ...
3. from ...
4. where ...
5. < empty >
tag [TABLE] - will be replaced by $connection['table']
tag [WHERE] - will be replaced by auto created WHERE condition (if $data is array)
else $query mast be standard sql-query: "select ... from ... where ..."
array$data
1. $query="select ... where f1=? and f2=? and f3=?" => $data = array(1, 2, 3 ...)
2. $query="select ... where f1=:f1 and f2=:f2 and f3=:f3" => $data = array("f1"=>1, "f2"=>2, "f3"=>3 ...)
array$connection(option) An array of connection params (see a method connect)
boolean$returnStatment[ true | false ]
false - return array( 0=>array(fname=>value, ...), 1=... );
true - return { PDOStatement }
Returns
array | false
static update (   $query,
  $data = null,
  $connection = null,
  $returnStatment = false 
)
static

SQL UPDATE request

Parameters
string$query
if $default["autoquery"]==true then:
1. update ...
2. ...
4. < empty >
tag [TABLE] - will be replaced by $connection['table']
else $query mast be standard sql-query: "update ... set ... where ..."
array$data
1. $query="update ... set f1=?, f2=?, ... where f3=? and f4=?, ..." => $data = array(1, 2, 3, 4 ...)
2. $query="update ... " => $data = array( array("f1"=>1, "f2"=>2, ...), array("f3"=>3, "f4"=>4, ...) )
--- "set" data ------------- ----- "where" keys ---------
array$connection(option) An array of connection params (see a method connect)
boolean$returnStatment[ true | false ]
false - return rowCount
true - return { PDOStatement }
Returns
rowCount | false

Field Documentation

$default
static
Initial value:
= array (
"type" => "mysql",
"host" => "localhost",
"base" => "",
"user" => "",
"pass" => "",
"table" => "",
"autoquery" => true,
)

The documentation for this class was generated from the following file: