KjwSql Class Reference
[Database (SQL) handling]

KjwSql abstract class. More...

Inheritance diagram for KjwSql:

Inheritance graph
[legend]

List of all members.

Public Member Functions

 KjwSql ($sqltype, $hostname, $portnum, $username, $password, $database)
 Constructor.
 destroy ()
 Destructor performing cleanup.
 getType ()
 Get the sql type.
 connect ()
 Connect to the SQL server.
 disconnect ()
 Disconnect from the SQL server.
 beginWork ()
 Begin a transaction / atomic operation.
 commitWork ()
 Commit a running transaction / atomic operation.
 rollbackWork ()
 Revert/ignore all from the running transaction / atomic operation.
 execute ($query)
 Execute a query.
 selectAll ($query, $max=-1, $skip=0)
 Get an entire array from a select query.
 selectAtom ($query)
 Get a single value (atom) from a database.
 selectAtomOrNull ($query)
 Get a single value (atom) from a database or null if there is no result.
 selectAtomRow ($query)
 Get a single row from a database.
 selectAtomRowOrNull ($query)
 Get a single row from a database or null if there is no result.
 safeQuote ($mixed)
 Makes sure that any variable is quoted like its correct type should be.
 nameQuote ($string)
 Quote a name for an object (column or table).
 insertArray ($table, $args)
 A safe insert function.
 updateArray ($table, $args, $where=null)
 A safe update function.
 affectedRows ()
 How many rows were affected (touched) by the last INSERT, UPDATE, REPLACE or DELETE query.
 insertId ()
 Returns the id of the last INSERT clause that uses an IDENTITY/SEQUENCE/AUTO_INCREMENT value.
 debugStat ()
 Return an array with statistical information about this KjwSql object.
 _getLastQuery ()
 [PRIVATE] Returns the last query used.
 _setLastQuery ($query)
 [PRIVATE] Store the last executed query internally.

Public Attributes

 $_sqltype
 SQL server type (mysql, pgsql, mssql, etc).
 $_hostname
 Server ip/hostname.
 $_portnum
 Port number.
 $_username
 User name.
 $_password
 Password.
 $_database
 Database name.
 $_debug_query_count
 A query counter.
 $_last_query
 The last executed query, stored for debug purposes.


Detailed Description

KjwSql abstract class.

Use derived classes that actually have a database backend.

Definition at line 32 of file KjwSql.php.


Member Function Documentation

KjwSql::_getLastQuery (  ) 

[PRIVATE] Returns the last query used.

Returns:
A string or an array of strings.

Definition at line 395 of file KjwSql.php.

Referenced by KjwMsSql::_execute_3(), selectAll(), selectAtom(), selectAtomOrNull(), selectAtomRow(), and selectAtomRowOrNull().

KjwSql::_setLastQuery ( query  ) 

[PRIVATE] Store the last executed query internally.

Parameters:
$query The query string to store.

Definition at line 406 of file KjwSql.php.

References KjwObject::trace().

Referenced by KjwMsSql::_execute_1(), KjwPgSql::execute(), and KjwMySql::execute().

KjwSql::affectedRows (  ) 

How many rows were affected (touched) by the last INSERT, UPDATE, REPLACE or DELETE query.

It's unspecified if an UPDATE count includes updates that weren't actual changes. I.e. either the WHERE-match-count is returned, or only the subset where the update was a change.

Returns:
The number of rows affected.
Note:
Don't try to be funny and do SELECT or whatever queries before calling this.

Reimplemented in KjwMsSql, KjwMySql, and KjwPgSql.

Definition at line 366 of file KjwSql.php.

References KjwObject::notImplemented().

KjwSql::connect (  ) 

Connect to the SQL server.

Returns:
True.
Todo:
Put timing on connect, so we don't have to wait 60secs on firewalled server.

Reimplemented in KjwFakeSql, KjwMsSql, KjwMySql, and KjwPgSql.

Definition at line 88 of file KjwSql.php.

References KjwObject::notImplemented().

KjwSql::debugStat (  ) 

Return an array with statistical information about this KjwSql object.

Returns:
An associative array with key `query_count`.

Definition at line 386 of file KjwSql.php.

KjwSql::destroy (  ) 

Destructor performing cleanup.

Don't rely on this being called ever. The user must call it by hand.

Reimplemented from KjwObject.

Reimplemented in KjwFakeSql, KjwMsSql, KjwMySql, and KjwPgSql.

Definition at line 68 of file KjwSql.php.

References disconnect().

KjwSql::disconnect (  ) 

Disconnect from the SQL server.

Returns:
Success boolean.

Reimplemented in KjwFakeSql, KjwMsSql, KjwMySql, and KjwPgSql.

Definition at line 97 of file KjwSql.php.

References KjwObject::notImplemented().

Referenced by destroy().

KjwSql::execute ( query  ) 

Execute a query.

Parameters:
$query The query to execute.
Returns:
Returns a boolean specifying success or a KjwResult object on a select/show/describe/explain.

Reimplemented in KjwFakeSql, KjwMsSql, KjwMySql, and KjwPgSql.

Definition at line 128 of file KjwSql.php.

References KjwObject::notImplemented().

Referenced by beginWork(), commitWork(), insertArray(), rollbackWork(), selectAll(), selectAtom(), selectAtomOrNull(), selectAtomRow(), selectAtomRowOrNull(), and updateArray().

KjwSql::getType (  ) 

Get the sql type.

Returns:
Type of sql server as a string.

Definition at line 78 of file KjwSql.php.

KjwSql::insertArray ( table,
args 
)

A safe insert function.

Parameters:
$table The table into which the arguments in $args should be inserted.
$args An associative array of key value pairs to be inserted. If the value is an array, its first element will be used verbatim (e.g. for array('NOW()')).
Returns:
Success boolean.

Definition at line 294 of file KjwSql.php.

References execute(), nameQuote(), and safeQuote().

KjwSql::insertId (  ) 

Returns the id of the last INSERT clause that uses an IDENTITY/SEQUENCE/AUTO_INCREMENT value.

Returns:
The new id.
Note:
Don't try to be funny and do SELECT or whatever queries before calling this.

Reimplemented in KjwMsSql, KjwMySql, KjwPgSql, and KjwXmlSql.

Definition at line 377 of file KjwSql.php.

References KjwObject::notImplemented().

KjwSql::KjwSql ( sqltype,
hostname,
portnum,
username,
password,
database 
)

Constructor.

Parameters:
$sqltype A string identifier specifying the SQL server brand/make.
$hostname Server name or IP.
$portnum Port number of 0 if default.
$username User name.
$password Password.
$database Database name.

Definition at line 52 of file KjwSql.php.

References KjwObject::KjwObject().

Referenced by KjwFakeSql::KjwFakeSql(), KjwMsSql::KjwMsSql(), KjwMySql::KjwMySql(), and KjwPgSql::KjwPgSql().

KjwSql::nameQuote ( string  ) 

Quote a name for an object (column or table).

For MySQL this would be by surrounding the name with backticks.

Parameters:
$string An identifier (column or table name) to be quoted.
Returns:
The quoted identifier.

Reimplemented in KjwFakeSql, KjwMsSql, KjwMySql, and KjwPgSql.

Definition at line 282 of file KjwSql.php.

References KjwObject::notImplemented().

Referenced by insertArray(), safeQuote(), and updateArray().

KjwSql::safeQuote ( mixed  ) 

Makes sure that any variable is quoted like its correct type should be.

Integers and booleans are returned as integers. Strings are returned as quoted strings with all quotes escaped. If the escaping mechanism is not known, all <32 and >127 characters REMOVED, otherwise the escaping for the specific database make is used.

Parameters:
$mixed The variable that needs to be returned as a safe entity.
Returns:
A quoted/escaped entity.

Reimplemented in KjwFakeSql, KjwMsSql, KjwMySql, and KjwPgSql.

Definition at line 256 of file KjwSql.php.

References KjwObject::croak(), and nameQuote().

Referenced by insertArray(), and updateArray().

KjwSql::selectAll ( query,
max = -1,
skip = 0 
)

Get an entire array from a select query.

You'll want to use this instead of execute() when you're using templates to show a couple of rows.

Parameters:
$query Everything after the SELECT keyword.
$max (optional) At most $max rows, use -1 to get all.
$skip (optional) Skip $skip rows.
Returns:
An array with zero or more elements or false on error.

Definition at line 143 of file KjwSql.php.

References _getLastQuery(), KjwObject::croak(), and execute().

KjwSql::selectAtom ( query  ) 

Get a single value (atom) from a database.

We'll croak if there is more than one value or less than one.

Parameters:
$query Everything after the SELECT keyword.
Returns:
An atomic value or false on error. You need to cast this yourself, unfortunately.

Definition at line 165 of file KjwSql.php.

References _getLastQuery(), KjwObject::croak(), and execute().

KjwSql::selectAtomOrNull ( query  ) 

Get a single value (atom) from a database or null if there is no result.

We'll croak if there is more than one value.

Parameters:
$query Everything after the SELECT keyword.
Returns:
An atomic value or false on error. You need to cast this yourself, unfortunately, unless it's null.

Definition at line 185 of file KjwSql.php.

References _getLastQuery(), KjwObject::croak(), and execute().

KjwSql::selectAtomRow ( query  ) 

Get a single row from a database.

We'll croak if there is more than one value or less than one.

Parameters:
$query Everything after the SELECT keyword.
Returns:
A single row as an array or false on error.

Definition at line 210 of file KjwSql.php.

References _getLastQuery(), KjwObject::croak(), and execute().

KjwSql::selectAtomRowOrNull ( query  ) 

Get a single row from a database or null if there is no result.

We'll croak if there is more than one value.

Parameters:
$query Everything after the SELECT keyword.
Returns:
A single row as an array or null.

Definition at line 229 of file KjwSql.php.

References _getLastQuery(), KjwObject::croak(), and execute().

KjwSql::updateArray ( table,
args,
where = null 
)

A safe update function.

Parameters:
$table The table where rows should be updated with the arguments in $args.
$args An associative array of key value pairs to be inserted. If the value is an array, its first element will be used verbatim (e.g. for array('NOW()')).
$where An optional where-clause (without the "WHERE "). It's your duty to make this clause SQL safe.
Returns:
Success boolean.
Note:
Be sure to escape funky column names in the $where clause, the $table argument is escaped by us.

Definition at line 334 of file KjwSql.php.

References execute(), nameQuote(), and safeQuote().


Member Data Documentation

KjwSql::$_database

Database name.

Definition at line 38 of file KjwSql.php.

KjwSql::$_debug_query_count

A query counter.

Used for debugging purposes only.

Definition at line 39 of file KjwSql.php.

KjwSql::$_hostname

Server ip/hostname.

Definition at line 34 of file KjwSql.php.

KjwSql::$_last_query

The last executed query, stored for debug purposes.

Definition at line 40 of file KjwSql.php.

KjwSql::$_password

Password.

Definition at line 37 of file KjwSql.php.

KjwSql::$_portnum

Port number.

Definition at line 35 of file KjwSql.php.

KjwSql::$_sqltype

SQL server type (mysql, pgsql, mssql, etc).

Definition at line 33 of file KjwSql.php.

KjwSql::$_username

User name.

Definition at line 36 of file KjwSql.php.


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

Generated on Wed Apr 8 22:16:36 2009 for KjwLib-php45 by  doxygen 1.5.8