/** * Contructor * @param string $class : Name of model of objects to be listed * @param string $condition [optional default NULL] : SQL query condition * @param string $extra [optional default NULL] : append some commands at the end of the SQL query (such as ORDER BY, LIMIT) * @param array $extra_fields [optional default NULL] : Array of fields to get from DB that are not listed in data model. You may use names in the form : "tablealias.field as field", table name's will then be converted to real tables' names as defined in config file */ public function __construct($class, $condition = null, $extra = null, $extra_fields = null) { $this->db = BF::gdb($class::$db); $this->class = $class; // fields to load if (!is_array($class::$default_fields)) { // all fields $fields = "*"; } else { $fields_array = $class::$default_fields; if (is_array($extra_fields)) { $fields_array = array_unique(array_merge($fields_array, $extra_fields)); } $fields = implode(', ', $fields_array); } // built query $query_str = "SELECT " . $fields . " FROM " . $class::$table; if ($condition != NULL) { $query_str .= " WHERE " . $condition; } if ($extra != NULL) { $query_str .= " " . $extra; } // perfom query $this->resource = $this->db->query($query_str); }
/** * Function to quote a variable into a string. Depending on flags, quotes (double by default) will be added * @param int/string $flags [optional default Q_ALL] : * IF flag is a string : SQL quoting for given class name (based on what is required from database used) * IF flag is int : * For value === NULL or value === '' * - if Q_NULL => 'null' * - if Q_STRING => '""' * - if Q_INT or Q_FLOAT => '0' * - if Q_BOOL => 'false' * For value === TRUE / FALSE * - if Q_BOOL => 'true' / 'false' * - if Q_INT or Q_FLOAT => '1' / '0' * - if Q_STRING => '"1"' / '""' * For value is an INT (123) * - if Q_INT => '123' * - if Q_STRING => '"123"' * - if Q_BOOL => 'true' (if int!=0) / 'false' (if int==0) * For value is a FLOAT (12.3) * - if Q_FLOAT => '12.3' * else round and act like an INT * For value is a string not empty ('abc') * - if Q_STRING => '"abc"' * - if Q_INT or Q_FLOAT => cast to numeric and act as an int/float * - if Q_BOOL => 'true' * For value is array or object : * - if Q_ARRAY => print_r array/object and act as string * * Q_FLOAT implies Q_INT * Q_ESCAPE_HTML : Escape quotes and all other html entities into their HTML code (see htmlspecialchars on php doc) * Q_JSON_WRAPER : Wrap variables in JSON structure (ie. {} or [] if array, normal quotes otherwise) * Q_JSON : Format in JSON style. Implies Q_ALL, Q_ARRAY and Q_JSON_WRAPER * Q_HTML will set Q_STRING and Q_HTML_ESCAPE * Q_ALL will set Q_NULL, Q_FLOAT, Q_BOOL and Q_STRING (for use in sql queries) but NOT Q_ARRAY * Q_SINGLE will set quoting with single quotes instead of double quotes * If could not be determined, or only Q_NULL is set, fatal error is thrown */ function Q($value, $flags = Q_ALL) { if (is_string($flags)) { return BF::gdb($flags::$db)->Q($value); } if ($flags <= Q_NULL) { throw new exception('Invalid flag passed to Q'); } // array and objects if (is_object($value)) { return Q((array) $value, $flags); } if (is_array($value)) { if ($flags & Q_ARRAY && $flags & Q_JSON_WRAPER) { $parts = array(); //Find out if the given array is a numerical array $is_list = 0 !== array_reduce(array_keys($value), 'Q_callbackReduceNotAssociativeArray', 0); foreach ($value as $key => $val) { $parts[] = ($is_list ? '' : '"' . $key . '":') . Q($val, $flags); } return str_replace(array("\n", "\t"), array("\\n", "\\t"), $is_list ? '[' . implode(',', $parts) . ']' : '{' . implode(',', $parts) . '}'); } elseif ($flags & Q_ARRAY) { $value = print_r($value, true); } else { throw new exception('Calling quote on an array or object without Q_ARRAY flag set'); } } if ($value === NULL || $value === '') { if ($flags & Q_NULL) { return 'null'; } elseif ($flags & Q_STRING) { $value = ''; } elseif ($flags & Q_INT) { return '0'; } elseif ($flags & Q_BOOL) { return 'false'; } else { throw new exception('Invalid flags passed to Q()'); } } elseif ($value === TRUE || $value === FALSE || !($flags & Q_INT) && !($flags & Q_STRING)) { if ($flags & Q_BOOL) { return $value ? 'true' : 'false'; } elseif ($flags & Q_INT) { return $value ? '1' : '0'; } elseif ($flags & Q_STRING) { $value = $value ? '1' : '0'; } else { throw new exception('Invalid flags passed to Q()'); } } elseif ((is_int($value) || is_float($value) || !($flags & Q_STRING)) && $flags & Q_INT) { return $flags & Q_FLOAT ? (string) (double) $value : (string) (int) $value; } elseif (!($flags & Q_STRING)) { throw new exception('Invalid flags passed to Q()'); } return ($flags & Q_SINGLE ? '\'' : '"') . ($flags & Q_ESCAPE_HTML ? htmlspecialchars($value, $flags & Q_SINGLE ? ENT_QUOTES : ENT_COMPAT, BF::$encoding) : addslashes($value)) . ($flags & Q_SINGLE ? '\'' : '"'); }
print ' If you see no error above, then include was ' . test::ok("successful") . ' <h3>Initializing framework</h3>'; BF::init(); print ' If you see no error above, then initialization was ' . test::ok("successful") . ' <h3>Database connexions</h3>'; if (count(test::dbconnections()) > 0) { print '<table cellspacing="0" cellpadding="5" class="tests"><tr>'; foreach (test::dbconnections() as $id => $data) { $error = false; try { BF::load_module("database"); BF::load_module("database." . $data[0]); $class = "BF_DB_" . $data[0]; if (!call_user_func(array($class, "supported"))) { throw new exception($data[0] . " is not supported by your PHP server"); } $db = BF::gdb($id); } catch (exception $e) { $error = $e->getMessage(); } print ' <tr> <td class="title"><b>ID: ' . $id . '</b> (' . implode(", ", $data) . ')</td> <td>' . ($error ? test::invalid($error) : test::ok()) . '</td> </tr>'; } print '</table>'; } else { print ' ' . test::ok("None"); } }
/** * Count objects that match condition * @param string $class_name : model class name of objects to be counted * @param string $condition [optional default NULL] : SQL query condition * @param string $extra [optional default NULL] : append some commands at the end of the SQL query (such as LIMIT) * @return int */ public static function gcount($class_name, $condition = NULL, $extra = NULL) { BF::load_module("BF_DB_list"); BF::load_model($class_name); return BF::gdb($class_name::$db)->count($class_name::$table, $condition, $extra); }
/** * Contructor * @param string $class : Name of the table holding the object's data * @param int $id [optional default NULL] : Identifier of the element * @param mixed $fields [optional default NULL] : List of fields to load, or FALSE for default as defined in class, NULL for all fields, or string (eg. "*") * @return void */ public function __construct($id = NULL, $fields = false) { $class = get_class($this); $this->_class = $class; if (BF::gdb($class::$db) != null) { $this->_db = BF::gdb($class::$db); } else { $this->_db = null; } $this->_fields = $fields === false ? $class::$default_fields : $fields; $this->_id = $id; }