pclib  2.3.2
Lightweight PHP framework
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Db Class Reference

Simple database wrapper. More...

Inheritance diagram for Db:
BaseObject

Public Member Functions

 __construct ($dataSource=null)
 Establish database connection. More...
 
 connect ($dataSource)
 Establish database connection. More...
 
 close ()
 Close database connection.
 
 codePage ($cp)
 Set connection character set.
 
 seek ($res, $rowno)
 Seek resource $res to $rowno. More...
 
 setLimit ($numrows, $offset=0)
 The portable way to perform limit. More...
 
 query ($_sql, $param=null)
 Perform database query with parameters - return result resource. More...
 
 field ($dsstr)
 Perform SELECT query and return value of one database field. More...
 
 select ($dsstr)
 Perform SELECT query and return one row of result as assoc-array. More...
 
 selectAll ($dsstr)
 Perform SELECT query and return ALL rows of result as assoc-array. More...
 
 selectOne ($dsstr)
 Perform SELECT query, return first column of result as indexed array. More...
 
 selectPair ($dsstr)
 Perform SELECT query, return first and second column as associative array (lookup query). More...
 
 insert ($tab, $data)
 Perform INSERT query, return inserted ID. More...
 
 insertAll ($tab, array $data)
 Insert multiple rows.
 
 runDump ($fileName, $skipErrors=false)
 Run database dump file $fileName. More...
 
 replace ($tab, $data)
 Perform REPLACE query, return inserted ID. More...
 
 update ($tab, $data, $cond)
 Perform UPDATE query. More...
 
 delete ($tab, $cond)
 Perform DELETE query. More...
 
 count ($dsstr=null)
 Return number of rows in query result / table. More...
 
 exists ($dsstr)
 Return true if some row which is satisfying condition exists. More...
 
 fetch ($res=null, $fmt= 'a')
 Fetch one row from query result. More...
 
 fetchAll ($res=null, $fmt= 'a')
 Fetch ALL rows from query result. More...
 
 dbName ()
 Return current database name.
 
 columns ($table)
 Return columns metadata (name,size,type,nullable,default) of table $table as associative array. More...
 
 indexes ($table)
 Return indexes for table $table. More...
 
 export ($dba, $fmt= 'html')
 Export query result to format $fmt. More...
 
 getLookup ($lkpName)
 Return lookup table as array. More...
 
 escape ($str, $type= 'string')
 Escapes string for use in database query. More...
 
 setParams ($sql, $params)
 Place $params into $sql and return result string. More...
 
- Public Member Functions inherited from BaseObject
 loadDefaults ($className=null)
 Load default parameters of class $className into object instance.
 
 setProperties (array $defaults)
 Set public properties of object from the array. More...
 
 addEvent ($name, $callback)
 Add event handler to $object->$name property. More...
 
 toArray ()
 Convert object to array. More...
 

Data Fields

 $disabled = false
 If enabled, no query is executed. More...
 
 $lastQuery
 SQL of last executed query. More...
 
 $onBeforeQuery
 Occurs before sql-query is executed. More...
 
 $onAfterQuery
 Occurs after sql-query is executed. More...
 
 $forceReconnect = true
 Create new connection even if connection with same params exists.
 
 $drv
 var AbstractDriver Database driver
 
- Data Fields inherited from BaseObject
 $onNewInstance
 Occurs when new instance of the class is created. More...
 
 $onMissingMethod
 Occurs when called method does not exist. More...
 
 $serviceLocator
 var function() Return service object when requested with service(). More...
 

Protected Member Functions

 getSelectSql ($dsstr, $args)
 Helper for building SELECT sql. More...
 
 getWhereSql ($cond, $args)
 Helper for building WHERE clausule of sql query. More...
 
 isSql ($dsstr, $type= '')
 True if $dsstr is SQL string (of type $type)
 
- Protected Member Functions inherited from BaseObject
 fireEvent ($name, array $args=array())
 Run all event handlers in $object->$name property. More...
 
 service ($service, $default=null)
 Try acquire $service and load it into property $this->$service. More...
 

Protected Attributes

 $app
 var App
 

Additional Inherited Members

- Static Public Member Functions inherited from BaseObject
static defaults ()
 Set or retireve default parameters of the object. More...
 

Detailed Description

Simple database wrapper.

Features:

Constructor & Destructor Documentation

__construct (   $dataSource = null)

Establish database connection.

Parameters
string | array$dataSourceFormat: 'driver://user:passw/database'
See also
connect()

Member Function Documentation

columns (   $table)

Return columns metadata (name,size,type,nullable,default) of table $table as associative array.

Returns
array $columns
connect (   $dataSource)

Establish database connection.

Format of $datasource can be:

  • 'driver://user:passw/database'
  • 'driver://user:passw/database?parameters'
  • array with keys: driver, host, dbname, user, passw
Parameters
string | array$dataSource
count (   $dsstr = null)

Return number of rows in query result / table.

Without parameters it returns number of rows in last query. For using with parameters see examples.

Ex:

  • count('TABLE') //return number of rows in table
  • count($res) //return no. of rows in query result $res
  • count() //return no. of rows in last query
  • count('PERSONS', 'MONEY>100') //perform "select count(*) from PERSONS where MONEY>100"
  • count('select ID from T where F>10') //return no. of rows of the query
Parameters
string$dsstrDatasource string
string$condwhere condition
Returns
int $num Number of rows
delete (   $tab,
  $cond 
)

Perform DELETE query.

See Examples of using database functions parameters.

Parameters
string$tabTable name
string$condwhere condition (required)
Returns
bool $success
See also
insert()
escape (   $str,
  $type = 'string' 
)

Escapes string for use in database query.

Note
If you use {param} parameters in sql, it will be escaped automatically.
Parameters
string$strString (parameter) used in sql query
Returns
string $str escaped string
exists (   $dsstr)

Return true if some row which is satisfying condition exists.

See select() for possible parameters. See Examples of using database functions parameters.

Ex: if($db->exists('PERSONS', 'MONEY>1000')) ...

Parameters
string$dsstrDatasource string
string$condwhere condition
Returns
bool $found
See also
select()
export (   $dba,
  $fmt = 'html' 
)

Export query result to format $fmt.

Only html is supported now.

Parameters
string | resource$dbaResource or $dsstr string - see select() for details
string$fmtexport format ('html' or 'html/vertical')
Returns
string Query result in format $fmt
See also
select()
fetch (   $res = null,
  $fmt = 'a' 
)

Fetch one row from query result.

If result is ommited, it uses last query.

Parameters
resource$resQuery result resource
string$fmtformat of result a - assoc, o - object, r - row, ar - array, f - field
Returns
array|object $row result row
fetchAll (   $res = null,
  $fmt = 'a' 
)

Fetch ALL rows from query result.

Parameters
resource$resQuery result resource
string$fmtformat of result
Returns
array $rows = array($row_0, $row_1, $row_2, ...)
See also
fetch()
field (   $dsstr)

Perform SELECT query and return value of one database field.

This function is shortcut for SQL SELECT command. Parameters consist from two parts: datasource string and condition. Datasource can be:

  • tablename
  • tablename:fieldlist
  • sql-query

You can use Dynamic SQL. See Examples of using database functions parameters

Parameters
string$dsstrdatasource string.
string$condWHERE condition.
Returns
value $field
getLookup (   $lkpName)

Return lookup table as array.

Parameters
string$lkpnameLookup table name
Returns
array $items Lookup table items
getSelectSql (   $dsstr,
  $args 
)
protected

Helper for building SELECT sql.

See also
select()
getWhereSql (   $cond,
  $args 
)
protected

Helper for building WHERE clausule of sql query.

See also
update()
indexes (   $table)

Return indexes for table $table.

Returns
array $indexes
insert (   $tab,
  $data 
)

Perform INSERT query, return inserted ID.

See Examples of using database functions parameters.

Parameters
string$tabTable name
array | string$dataassoc-array of 'FIELDNAME' => 'FIELDVALUE' pairs.
Returns
int $inserted_id
query (   $_sql,
  $param = null 
)

Perform database query with parameters - return result resource.

You can use Dynamic SQL. See Examples of using database functions parameters

Parameters
string$_sqlsql-query
array$paramquery parameters.
Returns
resource $result
replace (   $tab,
  $data 
)

Perform REPLACE query, return inserted ID.

See Examples of using database functions parameters.

Parameters
string$tabTable name
array | string$dataassoc-array of 'FIELDNAME' => 'FIELDVALUE' pairs.
Returns
int $inserted_id
runDump (   $fileName,
  $skipErrors = false 
)

Run database dump file $fileName.

Returns
int Number of executed queries.
seek (   $res,
  $rowno 
)

Seek resource $res to $rowno.

select (   $dsstr)

Perform SELECT query and return one row of result as assoc-array.

This function is shortcut for SQL SELECT command. Parameters consist from two parts: datasource string and condition. Datasource can be:

  • tablename
  • tablename:fieldlist
  • sql-query

You can use Dynamic SQL. See Examples of using database functions parameters

Parameters
string$dsstrdatasource string.
string$condWHERE condition.
Returns
array $row
selectAll (   $dsstr)

Perform SELECT query and return ALL rows of result as assoc-array.

This function is shortcut for SQL SELECT command. Parameters consist from two parts: datasource string and condition. Datasource can be:

  • tablename
  • tablename:fieldlist
  • sql-query

You can use Dynamic SQL. See Examples of using database functions parameters

Parameters
string$dsstrdatasource string.
string$condWHERE condition.
Returns
array $result = array ($row0_array, $row1_array, $row2_array, ...)
selectOne (   $dsstr)

Perform SELECT query, return first column of result as indexed array.

This function is shortcut for SQL SELECT command. Parameters consist from two parts: datasource string and condition. Datasource can be:

  • tablename
  • tablename:fieldlist
  • sql-query

You can use Dynamic SQL. See Examples of using database functions parameters

Parameters
string$dsstrdatasource string.
string$condWHERE condition.
Returns
array $column. Ex: select_one('PERSONS:NAME') will return array('John', 'Jack', ...)
selectPair (   $dsstr)

Perform SELECT query, return first and second column as associative array (lookup query).

This function is shortcut for SQL SELECT command. Parameters consist from two parts: datasource string and condition. Datasource can be:

  • tablename
  • tablename:fieldlist
  • sql-query

You can use Dynamic SQL. See Examples of using database functions parameters

Parameters
string$dsstrdatasource string.
string$condWHERE condition.
Returns
array $lookup. Ex: select_pair('PERSONS:NAME,MONEY') will return array('John' => 12000, 'Jack' => 200, ...)
setLimit (   $numrows,
  $offset = 0 
)

The portable way to perform limit.

Next query result will be limited according setlimit parameters.

Parameters
int$numrowsnumber of rows in result
int$offsetoffset from which result started
setParams (   $sql,
  $params 
)

Place $params into $sql and return result string.

See query() for details about $sql and $param format.

Parameters
string$sqlSQL-query string
array$paramsParameters
Returns
string $sql
See also
query()
update (   $tab,
  $data,
  $cond 
)

Perform UPDATE query.

See Examples of using database functions parameters.

Parameters
string$tabTable name
array | string$dataarray of values.
string$condwhere condition (required).
Returns
bool $success.
See also
insert()

Field Documentation

$disabled = false

If enabled, no query is executed.

$lastQuery

SQL of last executed query.

$onAfterQuery

Occurs after sql-query is executed.

$onBeforeQuery

Occurs before sql-query is executed.


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