Class SQLiteAdmin

Description

SQLiteAdmin - Class implementation

This class is a wrapper for the new SQLite extension of PHP5. It's an OOP environment designed for easier Usage of SQLite. This class is designed for and tested with SQLite Version 2.8.14. There is no guarantee that it will run with any other SQLite Version, although it will work most likely with versions prior to 2.8.14. As SQLite itself, this is only a testing stage and there is no guarantee for complete work.

The SQLite Project itself could be called a 'front-end' to this class, since it simply passes all tasks to it.

You can use SQLiteAdmin by spawning this class:

 $sqlite = new SQLiteAdmin('filename.db');

See the descriptions of the class members and methods for further information.

Located in /sqlite_admin/includes/SQLiteAdmin.class.php (line 72)


	
			
Variable Summary
string $base64name
string $filename
string $message
string $shortname
resource $_dblink
resource $_result
Method Summary
SQLiteAdmin __construct ([string $filename = ''], [array $datatypes = NULL], [array $constraints = NULL])
boolean closeDB ()
boolean createDB (string $filename, [string $tablename = ''])
boolean createIndex (string $name, string $type, string $table, array $fields)
boolean createView (string $name, string $sql)
boolean dropIndex (string $name)
boolean dropView (string $name)
boolean|array fetchArray ([resource|NULL $res = NULL], [integer $type = SQLITE_ASSOC])
boolean|array getIndexInfo (string $tablename)
boolean|array getTableInfo ([string $tablename = ''])
string getValidContent (string $cont)
boolean|array getViewInfo ()
boolean hasResult ()
boolean hasTable (string $tablename)
string lastError ([string $format = 'Error #%02d: "%s"'])
integer lastInsertRowID ()
integer numRows ([resoure|NULL $result = NULL])
boolean openDB (string $filename)
boolean|resource query (string $query, [boolean $remember = true])
boolean queryAlterWrapper (string $query)
boolean|resource queryFormatted (string $format, boolean $remember, mixed $args)
void resetResult ()
boolean tableAddField (string $tblname, string $fldname, string $fldtype, string $fldconst, string $fldsize, string $flddefault)
boolean tableCreate (string $tablename)
boolean tableDelete (string $tablename)
boolean tableDeleteField (string $tblname, string $fldname)
boolean tableEditField (string $tblname, string $fldoldname, string $fldname, string $fldtype, string $fldconst, string $fldsize)
boolean tableRename (string $tblname, mixed $newtblname, string $fldname)
Variables
string $base64name = '' (line 148)

Database filename

This stores the complete path and file name of the opened database file, encoded by PHP's base64_encode public function. That is primarily for correct filename passing via HTTP. If this is empty, no database has (yet) been opened.

  • access: public
array $constraintsArray = array() (line 113)

SQL Constraints array

List of accepted constraints. Used by getTableInfo.

  • access: public
array $datatypeArray = array() (line 104)

SQL Datatype array

List of accepted data types. Used by getTableInfo.

  • access: public
string $filename = '' (line 126)

Database filename

This stores the complete path and file name of the opened database file. If this is empty, no database has (yet) been opened.

  • access: public
string $message = '' (line 159)

Message buffer

This stores all messages produced during processes done by this class. Mostly in cases of error, this string will contain a more accurate description of what went wrong.

  • access: public
string $shortname = '' (line 136)

Database short filename

This stores the file name without path of the opened database file. If this is empty, no database has (yet) been opened.

  • access: public
resource $_dblink = NULL (line 83)

Internal database resource

The internally stored database handle. It is set by openDB or createDB and used by most of the public functions. You do not have to take care of this.

  • access: protected
resource $_result = NULL (line 95)

Internal result resource

The internally stored result resource. It can be an SQLite query resource or a boolean value depending on the kind of query (whether or not 'SELECT'). It is set whenever the query public function is called with the parameter '$remember' set to true.

  • access: protected
Methods
Constructor __construct (line 175)

Initializes a SQLiteAdmin instance

This initializes an SQLiteAdmin instance. If $filename is not empty, the object try to open (see openDB) the database file and establish a connection to it.
Additionally, it sets up two internal data arrays for keyword lists in the later Table structure parser. These can be given by parameter, too.

  • access: public
SQLiteAdmin __construct ([string $filename = ''], [array $datatypes = NULL], [array $constraints = NULL])
  • string $filename: [optional] Database file, that should be opened.
  • array $datatypes: [optional] List of accepted SQL data types.
  • array $constraints: [optional] List of accepted SQL constraints.
closeDB (line 270)

Close currently opened database

This will close the currently opened database connection. In any normal case, this will be done at the end of your script automatically. You will need this only if you want to open another database with the same SQLiteAdmin instance.

  • return: State of success
  • access: public
boolean closeDB ()
createDB (line 243)

Create a new database file

This will try to create a new database file. In addition, if it succeeds it will setup the internal $_dblink and create the specific filename strings. If if a table name is passed, it will automatically try to create a new table using tableCreate.

  • return: State of success
  • access: public
boolean createDB (string $filename, [string $tablename = ''])
  • string $filename: Database file to create.
  • string $tablename: [optional] Table to create in new database.
createIndex (line 1315)

Create an Index

This will create the Index named $name on the table $table in the currently opened database. It will be of the type $type and affect the fields $fields.

  • return: State of success
  • access: public
boolean createIndex (string $name, string $type, string $table, array $fields)
  • string $name: Index name
  • string $table: Table on which the index shall be created
  • string $type: Index type (STANDARD or UNIQUE)
  • array $fields: Affected fields
createView (line 1218)

Create a new VIEW

This will create a new VIEW named $name with the SELECT statement $sql in the currently opened database.

  • return: State of success
  • access: public
boolean createView (string $name, string $sql)
  • string $name: VIEW name
  • string $sql: SELECT statement
dropIndex (line 1334)

Delete an Index

This will delete the Index named $name in the currently opened database.

  • return: State of success
  • access: public
boolean dropIndex (string $name)
  • string $name: Index name
dropView (line 1235)

Delete a VIEW

This will delete the VIEW named $name in the currently opened database.

  • return: State of success
  • access: public
boolean dropView (string $name)
  • string $name: VIEW name
fetchArray (line 488)

Fetch a result array

This will perform a FetchArray operation using the given data. If $res is NULL, the internal $_result resource will be used. In any other case $res will be treated as result resource. $type is the kind of array that shoud be returned. Will return false if for whatever reason no result row can be fetched.

  • return: An array containing the current query result row, false if fails.
  • access: public
boolean|array fetchArray ([resource|NULL $res = NULL], [integer $type = SQLITE_ASSOC])
  • resource|NULL $res: [optional, default=NULL] Result resource
  • integer $type: [optional, default=SQLITE_ASSOC] The type of the result row that should be delivered. See an SQLite manual for further information.
getIndexInfo (line 1269)

Get Index information

This will return an array of information of all indices on the table $tablename in the currently opened database.

The returned array contains the following information. (Excerpt of one element):

 array {
     ["name"]           => Index name
     ["rootpage"]       => Index rootpage
     ["sql"]            => Complete SQL string that has been used for creating
                           the Index
     ["type"]           => Index type ('STANDARD' or 'UNIQUE')
     ["columns"]        => Linear array of names of affected columns
     ["columns_string"] => Comma-seperated list of names of affected columns
 }

  • return: VIEW information or false if fails
  • access: public
boolean|array getIndexInfo (string $tablename)
  • string $tablename: Table name
getTableInfo (line 713)

Get table information

This will return an array of information of all tables in the currently opened database. If $tablename is given the information will be limited to this specific table.

The returned array contains the following information. (Excerpt of one element):

 array {
     ["name"]    => Table name
     ["entries"] => Number of entries in that table
     ["fields"]  => Array containing field data
         array {
             [linear_index]=>
                 array {
                     ["field_name"] => Field name
                     ["field_type"] => Field type
                     ["field_keys"] => Field constraints
                     ["field_size"] => Field size
               }
         }
 }

  • return: Table information or false if fails
  • access: public
boolean|array getTableInfo ([string $tablename = ''])
  • string $tablename: [optional] Table name
getValidContent (line 1354)

Get a valid content string

Returns a valid content string created from given content string. Some strings are key contents (like NULL) in SQLite. They must not be set in single quotes. All others have to. That is what this public function takes care of. Mostly for internal use.

  • return: Valid content string
  • access: public
string getValidContent (string $cont)
  • string $cont: Given content string
getViewInfo (line 1182)

Get VIEW information

This will return an array of information of all VIEWs in the currently opened database.

The returned array contains the following information. (Excerpt of one element):

 array {
     ["name"]      => VIEW name
     ["rootpage"]  => VIEW rootpage
     ["sql"]       => Complete SQL string that has been used for creating
                      the VIEW
     ["sql_short"] => The SELECT statement the VIEW represents
     ["table"]     => Name of the table the VIEW uses
 }

  • return: VIEW information or false if fails
  • access: public
boolean|array getViewInfo ()
hasResult (line 522)

Check if result exists

This returns true if there is currently a valid $_result resource that can be used for fetchArray() operations.

  • return: Whether or not there is a result.
  • access: public
boolean hasResult ()
hasTable (line 672)

Check if table exists

Returns true, if the given table exists in currently opened database, or false in any other case.

  • return: Whether (true) or not (false) $tablename exists.
  • access: public
boolean hasTable (string $tablename)
  • string $tablename: Name of table
lastError (line 614)

Get last error

This will return an error message string of the last error that occured during an SQLite operation. It will return an sprintfed string (error number is first and message is second parameter).

  • return: Error message
  • access: public
string lastError ([string $format = 'Error #%02d: "%s"'])
  • string $format: Message format string
lastInsertRowID (line 552)

Last inserted row id

Returns the row id created by the last insert instruction.

  • return: Last inserted row id
  • access: public
integer lastInsertRowID ()
numRows (line 536)

Get number of rows in current result set

Returns the number of rows in the given result set. If $result is NULL, the internal result resource link will be used.

  • return: Number of rows in result set
  • access: public
integer numRows ([resoure|NULL $result = NULL])
  • resoure|NULL $result: Result resource link
openDB (line 215)

Open an existing database file

This will try to open the given database file. In addition, if it succeeds it will setup the internal $_dblink and create the specific filename strings.

  • return: State of success
  • access: public
boolean openDB (string $filename)
  • string $filename: Database file to open
query (line 295)

Execute an SQL Query

This will execute an SQL query using the internal database link. $remember decides whether or not an result resource of a SELECT query shall be stored in the internal $_result resource. If that's the case, you can call fetchArray without any other parameters.

  • return: Result resource in case of SELECT queries, state of success in any other case.
  • access: public
boolean|resource query (string $query, [boolean $remember = true])
  • string $query: SQL Query to execute
  • boolean $remember: [optional, default=true] Save in internal result resource?
queryAlterWrapper (line 349)

Handle ALTER TABLE queries

This is a wrapper function for ALTER TABLE queries, since SQLite does not provide that functionality by itself. It merely parses the given query string and provides the collected data to the internal functions tableAddField(), tableEditField() and tableDeleteField.

  • return: State of success.
  • access: public
boolean queryAlterWrapper (string $query)
  • string $query: ALTER TABLE SQL Query to execute
queryFormatted (line 585)

Performs a query using a format string and a list of arguments.

This will perform a query using the given format string and the list of arguments passed. It works similar to the php printf function. If the first parameter after the format string is boolean it will be taken as the $remember parameter (see query()) and the other parameters as list. In any other case, all parameters after the format string will be taken for the parameter list.

Example:

 // without 'remember':
 $res = $cfg_sql->queryFormatted('SELECT * FROM %s WHERE 1', false, 'table');
 $row = $cfg_sql->fetchArray($res);

 // with 'remember':
 $cfg_sql->queryFormatted('SELECT * FROM %s WHERE 1', 'table');
 $row = $cfg_sql->fetchArray();

  • return: Result resource in case of SELECT queries, state of success in any other case.
  • access: public
boolean|resource queryFormatted (string $format, boolean $remember, mixed $args)
  • string $format: Format string for query
  • boolean $remember: [optional, default=true] Whether or not store result in internal result link
  • mixed $args: Argument list for format string
resetResult (line 509)

Reset internal result resource

This will reset the internal $_result resource, so that it can be freely used again for further operations.

  • access: public
void resetResult ()
tableAddField (line 956)

Add a field to a table.

This will add the field $fldname to the table $tblname. Specifications of the new field are passed. (See parameter list)

WARNING: This public function should be considered instable. Since there are no builtin public functions for table altering in SQLite, we have to do a quite bad workaround by backing all data up, deleting the table and recreating it, then putting all the data back. There has not yet been very intensive testing as this is only a side project of Crafty.

  • return: State of success
  • access: public
boolean tableAddField (string $tblname, string $fldname, string $fldtype, string $fldconst, string $fldsize, string $flddefault)
  • string $tblname: Name of target table
  • string $fldname: Name of new field
  • string $fldtype: Type of new field
  • string $fldconst: Constraints of new field
  • string $fldsize: Size of new field
  • string $flddefault: Default value of new field (for already existing database entries)
tableCreate (line 630)

Create new table

This will try to create a new table with the name $tablename in the currently opened database. It will return true if it succeeds, false if fails. It will automatically create an 'id INTEGER PRIMARY KEY' field.

  • return: State of success
  • access: public
boolean tableCreate (string $tablename)
  • string $tablename: Name of the table to create
tableDelete (line 652)

Delete table

This will try to delete the table with the name $tablename in the currently opened database. It will return true if it succeeds, false if fails.

  • return: State of success
  • access: public
boolean tableDelete (string $tablename)
  • string $tablename: Name of the table to delete
tableDeleteField (line 869)

Delete a field from a table.

This will delete the field $fldname from the table $tblname.

WARNING: This public function should be considered instable. Since there are no builtin public functions for table altering in SQLite, we have to do a quite bad workaround by backing all data up, deleting the table and recreating it, then putting all the data back. There has not yet been very intensive testing as this is only a side project of Crafty.

  • return: State of success
  • access: public
boolean tableDeleteField (string $tblname, string $fldname)
  • string $tblname: Name of target table
  • string $fldname: Name of target field
tableEditField (line 1062)

Alter a field of a table.

This will alter the field $fldname in the table $tblname. Specifications of the field are passed. (See parameter list)

WARNING: This public function should be considered instable. Since there are no builtin public functions for table altering in SQLite, we have to do a quite bad workaround by backing all data up, deleting the table and recreating it, then putting all the data back. There has not yet been very intensive testing as this is only a side project of Crafty.

  • return: State of success
  • access: public
boolean tableEditField (string $tblname, string $fldoldname, string $fldname, string $fldtype, string $fldconst, string $fldsize)
  • string $tblname: Name of target table
  • string $fldoldname: Name of target field
  • string $fldname: New name of target field
  • string $fldtype: New type of target field
  • string $fldconst: New constraints of target field
  • string $fldsize: New size of target field
tableRename (line 797)

Rename a table.

This will rename $tblname to $tblnewname.

WARNING: This public function should be considered instable. Since there are no builtin public functions for table altering in SQLite, we have to do a quite bad workaround by backing all data up, deleting the table and recreating it, then putting all the data back. There has not yet been very intensive testing as this is only a side project of Crafty.

  • return: State of success
  • access: public
boolean tableRename (string $tblname, mixed $newtblname, string $fldname)
  • string $tblname: Name of target table
  • string $fldname: New name of target table

Documentation generated on Sat, 19 Feb 2005 01:43:59 +0100 by phpDocumentor 1.3.0RC3