Class CRUDites

Description

CRUDités - ezSQL - MySQL utility wrapper

CRUDités is an extension of the great and well known ezSQL class. It's expecially written for MySQL Database. Depends on ezSQL_core and ezSQL_mysql

  • author: Marco "DWJ" Solazzi <hello@dwightjack.com>
  • version: 0.2b
  • copyright: Copyright &copy; 2008-2009, Marco Solazzi
  • link: http://www.dwightjack.com
  • license: Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL (http://www.opensource.org/licenses/gpl-2.0.php) licenses.

Located in /class.crudites.php (line 16)

ezSQL_mysql
   |
   --CRUDites
Variable Summary
Method Summary

Methods legend:
 Class method
Only works on bound tables Works on bound tables only (see here for usage)

 CRUDites __construct ([string $dbuser = ''], [string $dbpassword = ''], [string $dbname = ''], [string $dbhost = 'localhost'], [string $prefix = false])
 void add_table (string $table, [array $fields = null], [string $primary = 'id'], [bool $override = false])
 void add_tables ([array $array = array()], [bool $override = false])
 string array_to_string ([array $array = null], [ $inner_glue = '='], [ $outer_glue = ' AND '], [bool $recurse = true], string $innerglue, string $outerglue)
Only works on bound tables void bind (strin $table)
 string build_query (string $table, array $value_array, string $type, mixed $where, [bool $compat = false], bool $all)
 mixed check_quotes (mixed $var)
Only works on bound tablesint count ([string|array $where = ''])
 int count_rows ([string $table = null], [string|array $where = ''])
Only works on bound tablesbool delete ([string|array $where = ''], [ $col = null], string $table)
 bool delete_from (string $table, [string|array $where = ''])
Only works on bound tablesbool|int exists ([string $val = ''], [string $col = null], [string $return = false])
 array flat_array (array $array, [mixed $key1 = 0], [mixed $key2 = 1])
 array|bool get_col_values ([string $table = null], string $field)
 array|bool get_data ([array $queryArray = array()], [constant $output = OBJECT])
 mixed get_option (string $var)
 array get_pair (string $table, string $key1, string $key2, [string $where = ""])
 array|bool get_table ([string $table = null], [constant $output = OBJECT])
 mixed get_value ([string $table = null], string $field, [string|array $where = null])
 array|bool get_where (mixed $string[,)
 string implode (string $glue, array $array)
 string insert_query (string $table, array $value_array, [bool $compat = false])
Only works on bound tablesbool insert_unique (string $value, string $query)
 bool insert_unique_to (string $table, string|array $field, string $value, string $query)
 string mysql_curdate ()
 string mysql_date_format ([string $string = null], [string $input = null], [string $output = null])
 string mysql_now ()
Only works on bound tablesarray pair (string $key1, string $key2, [string $where = ""])
 bool|int row_exists ([string $table = null], string|array $field, [string $value = ""], [string $return = false])
 string select_query ([array $array = array()])
Only works on bound tablesarray|bool select_rows ([array $array = array()], [constant $output = OBJECT])
 bool send_query (string $query, [bool $isupdate = false])
 void set_option (string $var, string $value)
 bool set_value ([string $table = null], string $field, string $value, [string|array $where = null])
Only works on bound tablesbool store (array $value_array)
 string strptime_to_timestamp ([array $array = null])
 string table_name ([string $table = ''])
 string to_timestamp (string $string, string $format)
 string update_query (string $table, array $value_array, mixed $where, [bool $all = false], [bool $compat = false])
Variables
string $current_primary = null (line 51)
  • var: Primary key for current bound table, if exists
  • access: protected
array $current_table = null (line 45)
  • var: Array listing fields' names of current bound table
  • access: protected
mixed $dbprefix = false (line 21)
  • var: Tables prefix
  • access: protected
string $dbprefix_placeholder = "#__" (line 27)
  • var: Placeholder to use in queries if database prefix is used
  • access: protected
array $options = array(
'date_locale' => '%d/%m/%Y',
'time_locale' => '%H:%M:%S',
'datetime_locale' => '%d/%m/%Y %H:%M:%S'
)
(line 57)
  • var: Utility options like date format
  • access: protected
array $tables = array() (line 39)
  • var: Array of bindable tables
  • access: protected
mixed $table_bind = false (line 33)
  • var: Table to which queries are bound
  • access: protected
Methods
Constructor __construct (line 73)

Constructor function

CRUDites __construct ([string $dbuser = ''], [string $dbpassword = ''], [string $dbname = ''], [string $dbhost = 'localhost'], [string $prefix = false])
  • string $dbuser: Database username
  • string $dbpassword: Database password
  • string $dbname: Database name
  • string $dbhost: (Optional) Database host, defaults to localhost
  • string $prefix: (Optional) Tables' prefix, defaults to none
add_table (line 716)

Adds a table to the internal table reference

Example:

  1.  $db->add_table(
  2.          '#__my_table',
  3.          array(
  4.              'id',
  5.              'name',
  6.              'surname'
  7.          )
  8.  );

void add_table (string $table, [array $fields = null], [string $primary = 'id'], [bool $override = false])
  • string $table: Table name (with or without prefix)
  • array $fields: An array containing all table fields
  • string $primary: (Optional) Table primary key (defaults to 'id')
  • bool $override: (Optional) If set to true any already added table with the same name will be overwritten. Defaults to false
add_tables (line 747)

Same as add_table(), but accepts as first arguments a multidimensional associative array with tables' names as keys and field array as value.

Primary key can be marked inside the field array as a associative key __primary => 'fieldname'

Example:

  1.  $db->add_tables(array(
  2.          '#__my_table' => array (
  3.              'id',
  4.              'name',
  5.              'surname',
  6.              '__primary' => 'id'
  7.          ),
  8.          '#__my_second_table' => array (
  9.              [...]
  10.          )
  11.  ));

void add_tables ([array $array = array()], [bool $override = false])
  • array $array: The multidimensional associative array of tables to insert
  • bool $override: (Optional) If set to true any already added table with the same name will be overwritten. Defaults to false.
array_to_string (line 98)

Converts an array to a string with parameters, by combining keys and values.

Values which are arrays may be imploded as a comma separated string

Inspired by Joomla 1.5 Framework

string array_to_string ([array $array = null], [ $inner_glue = '='], [ $outer_glue = ' AND '], [bool $recurse = true], string $innerglue, string $outerglue)
  • array $array: Input array
  • string $innerglue: (Optional) String to place between each key and value. Defaults to "="
  • string $outerglue: (Optional) String to place between each key/value pair. Defaults to " AND "
  • bool $recurse: (Optional) If set to true it will implode array values which are arrays in a comma separated string. Defaults to true
  • $inner_glue
  • $outer_glue
Only works on bound tables bind (line 765)

Selects a table from the internal table reference store to be use with binding methods, if it exists

Example:

  1.  $db->bind('#__my_table');

void bind (strin $table)
  • strin $table: Name of selected table
build_query (line 436)

Returns either an update or insert query based on param $type

string build_query (string $table, array $value_array, string $type, mixed $where, [bool $compat = false], bool $all)
  • string $table: Table name
  • array $value_array: An array where keys are table fields' names and values are fields' values to update
  • string $type: Type of query. Allowed values are update or insert
  • mixed $where: (Not needed if type is insert) Conditional string or array ('fieldname'=>'fieldvalue') to define rows to be updated
  • bool $all: (Optional. Not needed if type is insert) Uses a compatibility routine with old versions of CRUDites
  • bool $compat: (Optional) Uses a compatibility routine with old versions of CRUDites
check_quotes (line 125)

Checks if a string is correctly escaped and quoted for SQL queries

  • access: protected
mixed check_quotes (mixed $var)
  • mixed $var: A variable to check
Only works on bound tables count (line 796)

Counts the number of rows from bound table

int count ([string|array $where = ''])
  • string|array $where: (Optional) where statement
count_rows (line 570)

Counts number of rows with optional where statement

int count_rows ([string $table = null], [string|array $where = ''])
  • string $table: Table name
  • string|array $where: (Optional) Where statement
Only works on bound tables delete (line 850)

Deletes a row from bound table

  • return: True when row is successfully deleted
bool delete ([string|array $where = ''], [ $col = null], string $table)
  • string $table: Table name
  • string|array $where: (Optional) Where statement as string or as an array of field => fieldvalue
  • $col
delete_from (line 627)

Deletes a row from passed in table

  • return: True when row is successfully deleted
bool delete_from (string $table, [string|array $where = ''])
  • string $table: Table name
  • string|array $where: (Optional) Where statement as string or as an array of field => fieldvalue
Only works on bound tables exists (line 808)

Returns true or number of occurrence of a row if it exists

bool|int exists ([string $val = ''], [string $col = null], [string $return = false])
  • string $val: Value to search for
  • string $col: (Optional) Field to use for search. Defaults to primary key of bound table
  • string $return: (Optional) If set to true will return the number of occurrence of the searched values, else will return just true (defaults to false)
flat_array (line 258)

Takes a multidimensional array and returns an array with one value as key and another as value

Example:

  1.  $inputArray array(
  2.          => array('one','two','three'),
  3.          => array('four','five','six')
  4.  );
  5.  $flatArray $db->flat_array($inputArray);
  6.  print_r($flatArray);
  7.  Array (
  8.      'one'  => 'two',
  9.      'four' => 'five'
  10.  )

array flat_array (array $array, [mixed $key1 = 0], [mixed $key2 = 1])
  • array $array: Input Array
  • mixed $key1: (Optional) Key of elements to use as keys. Defaults to 0
  • mixed $key2: (Optional) Key of elements to use as values. Defaults to 1
get_col_values (line 676)

Returns an array of values from a single column

  • return: An array of values or false on error
array|bool get_col_values ([string $table = null], string $field)
  • string $table: Table name
  • string $field: Table column to return
get_data (line 479)

Returns an array of database rows, either in object or array format

array|bool get_data ([array $queryArray = array()], [constant $output = OBJECT])
  • array $queryArray: An array containing required statements
  • constant $output: (Optional) Type of rows in the list. May be OBJECT,ARRAY_N (array with numeric index) or ARRAY_A (associative array). Defaults to OBJECT
get_option (line 277)

Gets an option value

  • return: Option value or null
  • see: $option
mixed get_option (string $var)
  • string $var: Option name
get_pair (line 557)

Returns an array with two given database columns as array keys and values

Example:

  1.  $results $db->get_pair("my_table","id","name","surname='Doe'")
  2.  print_r($results);
  3.  
  4.  Array (
  5.          => 'John',
  6.          => 'Jack',
  7.          => ...
  8.  )

array get_pair (string $table, string $key1, string $key2, [string $where = ""])
  • string $table: Table name
  • string $key1: Column name to use as key in the resulting array
  • string $key2: Column name to use as value in the resulting array
  • string $where: (Optional) Condition to filter results
get_table (line 497)

Returns an array with all rows and colums from a give table

array|bool get_table ([string $table = null], [constant $output = OBJECT])
  • string $table: Table to use
  • constant $output: (Optional) Type of rows in the list. May be OBJECT,ARRAY_N (array with numeric index) or ARRAY_A (associative array). Defaults to OBJECT
get_value (line 644)

Gets a single value from a single row from a table

mixed get_value ([string $table = null], string $field, [string|array $where = null])
  • string $table: Table name
  • string $field: Field to return
  • string|array $where: (Optional) Where statement as string or as an array of field => fieldvalue
get_where (line 516)

Returns an array of database rows

Example:

  1.  $results $db->get_where("name","my_table","surname='Doe'");
  2.  //will output an array of objects
  3.  //to get all columns use this syntax
  4.  $results $db->get_where("my_table","surname='Doe'");
Works like get_data() but accepts a variable number of strin arguments

array|bool get_where (mixed $string[,)
  • mixed $string[,: $string2...] Select statements. May be what to select, from which table and where, or just from which table and where. An additional last argument may be the output format
implode (line 140)

String safe version of PHP implode function

string implode (string $glue, array $array)
  • string $glue: String to use to join array elements
  • array $array: Input array
insert_query (line 362)

Returns an insert query

string insert_query (string $table, array $value_array, [bool $compat = false])
  • string $table: Table name
  • array $value_array: An array where keys are table fields' names and values are fields' values to store
  • bool $compat: (Optional) Uses a compatibility routine with old versions of CRUDites
Only works on bound tables insert_unique (line 836)

Inserts a row in the bound table only if there's no other row with passed in value in current primary key

  • return: True on query success
bool insert_unique (string $value, string $query)
  • string $value: Value to search for in primary key
  • string $query: Insert query
insert_unique_to (line 611)

Inserts a row in the table only if there's no other row with passed in column value

bool insert_unique_to (string $table, string|array $field, string $value, string $query)
  • string $table: Table name
  • string|array $field: Field to use for search or array with field => fieldvalue
  • string $value: (Optional) Value to search for if $field is a string
  • string $query: Insert query
mysql_curdate (line 161)

Returns current date in a MySQL DATE formatted string (yyyy-mm-dd)

string mysql_curdate ()
mysql_date_format (line 174)

Formats a formatted date or datetime to MySQL date or datetime

string mysql_date_format ([string $string = null], [string $input = null], [string $output = null])
  • string $string: String to format
  • string $input: Input format. May be a default locale (date,time or datetime) or a strptime allowed format strptime
  • string $output: MySQL output format. May be mysql_date, mysql_time, mysql_datetime or any date allowed format date
mysql_now (line 152)

Returns current date and time in a MySQL DATE formatted string (yyyy-mm-dd 00:00:00)

string mysql_now ()
Only works on bound tables pair (line 825)

Returns an array with two given table columns as array keys and values

array pair (string $key1, string $key2, [string $where = ""])
  • string $key1: Column name to use as key in the resulting array
  • string $key2: Column name to use as value in the resulting array
  • string $where: (Optional) Condition to filter results
row_exists (line 585)

Returns true or number of occurrence of a row if it exists

bool|int row_exists ([string $table = null], string|array $field, [string $value = ""], [string $return = false])
  • string $table: Table name
  • string|array $field: Field to use for search or array with field => fieldvalue
  • string $value: (Optional) Value to search for if $field is a string
  • string $return: (Optional) If set to true will return the number of occurrence of the searched values, else will return just true (defaults to false)
select_query (line 333)

Returns a select query with options

Example:

  1.  Available statements (could be in random order)
  2.  $query1 $db->select_query(array(
  3.          'select' => 'id,name,surname',
  4.          'from' => 'customer_names',
  5.          'where' => 'id >= 1'
  6.  ));
  7.  
  8.  $query2 $db->select_query(array(
  9.          'select' => 'id,surname',
  10.          'from' => 'customer_names',
  11.          'where' => array('name'=>'Bob','city' => 'London'),
  12.          'order' => 'name ASC,id ASC ',
  13.          'limit' => //just two rows
  14.  ));
  15.  
  16.  If select statement is not specified all columns will be retrieved

string select_query ([array $array = array()])
  • array $array: An array containing required statements
Only works on bound tables select_rows (line 784)

Returns an array of database rows from bound table

If no params are passed in all rows from the bound table are returned as an array of objects

array|bool select_rows ([array $array = array()], [constant $output = OBJECT])
  • array $array: (Optional) An array containing select statements
  • constant $output: (Optional) Type of rows in the list. May be OBJECT,ARRAY_N (array with numeric index) or ARRAY_A (associative array). Defaults to OBJECT
send_query (line 459)

Submits passed in query string to the database

  • return: Returns true if query succeeded else false
bool send_query (string $query, [bool $isupdate = false])
  • string $query: Query to submit
  • bool $isupdate: (Optional) Set this parameter to true if query is an update query. Defaults to false
set_option (line 287)

Sets an option

void set_option (string $var, string $value)
  • string $var: Option name
  • string $value: Value of the option
set_value (line 661)

Sets value of a single field in a table

  • return: True on query success
bool set_value ([string $table = null], string $field, string $value, [string|array $where = null])
  • string $table: Table name
  • string $field: Field to return
  • string $value: Value to set
  • string|array $where: (Optional) Where statement as string or as an array of field => fieldvalue
Only works on bound tables store (line 886)

Stores data in the bound table, either inserting a new row or updateing an existing one.

If in passed in data array is present a key with same name as the current primary key, row is updated, else a new row will be stored

Example:

  1.  // add a new table and bind the object to it
  2.  $db->add_table('#__my_table',
  3.          array(
  4.              'id',
  5.              'name'
  6.          ),
  7.          'id' //id is the primary key
  8.  );
  9.  $db->bind('#__my_table');
  10.  
  11.  // adds a new row
  12.  $new_row array('name'=>'John');
  13.  $db->store($new_row);
  14.  
  15.  // update a row
  16.  $new_row array('id'=>2,'name'=>'Mark');
  17.  $db->store($new_row);

  • return: True if query is successful, else false
bool store (array $value_array)
  • array $value_array: An array containing fields as keys and fields value as values
strptime_to_timestamp (line 222)

Converts and array generated by strptime to a timestamp

  • access: protected
string strptime_to_timestamp ([array $array = null])
  • array $array: (Optional) Input array, if none is pass current timestamp will be returned
table_name (line 300)

Gets a table name by applying prefix placeholder (#__) subsitution if needed

  • access: protected
string table_name ([string $table = ''])
  • string $table: Table name
to_timestamp (line 211)

Converts a passed in date/time string to timestamp

string to_timestamp (string $string, string $format)
  • string $string: Input string
  • string $format: Input string format. May be a default locale (date,time or datetime) or a strptime allowed format strptime
update_query (line 399)

Returns an update query

string update_query (string $table, array $value_array, mixed $where, [bool $all = false], [bool $compat = false])
  • string $table: Table name
  • array $value_array: An array where keys are table fields' names and values are fields' values to update
  • mixed $where: Conditional string or array ('fieldname'=>'fieldvalue') to define rows to be updated
  • bool $all: (Optional) Uses a compatibility routine with old versions of CRUDites
  • bool $compat: (Optional) Uses a compatibility routine with old versions of CRUDites

Documentation generated by phpDocumentor 1.4.2