Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Extends ezSQL so that it can handle active_record.
- * @author: droope <pedro@worcel.com>
- */
- include_once "ez_sql_core.php";
- include_once "ez_sql_mysql.php";
- class ezSQL_active_records extends ezSQL_mysql {
- public $exceptions = array('NOW()', 'NULL', 'CURRENT_TIMESTAMP');
- function ezSQL_active_records($dbuser = '', $dbpassword = '', $dbname = '', $dbhost = 'localhost') {
- $this->dbuser = $dbuser;
- $this->dbpassword = $dbpassword;
- $this->dbname = $dbname;
- $this->dbhost = $dbhost;
- }
- /**
- * @see $this->generate_insert()
- */
- function insert($table, $insertArray) {
- if($this->query ( $this->generate_insert ( $table, $insertArray ) )) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Generates insert queries. Values are automatically escaped.
- *
- * @param $table table name
- * @param $insertArray = array(
- * 'column_name' => 'value',
- * 'column_name2' => 'value2',
- * );
- * @return string
- */
- function generate_insert($table, $insertArray) {
- if(!$this->dbh) {
- $this->autoconnect();
- }
- if (is_array ( $insertArray )) {
- $count = 1;
- foreach ( $insertArray as $column => $value ) {
- $value = $this->escape ( $value );
- if ($this->should_quote($value)) {
- @$valueString .= "'" . $value . "'";
- } else {
- @$valueString .= $value;
- }
- @$columnString .= $this->escape($column);
- if ($count != count ( $insertArray )) {
- $valueString .= ", ";
- $columnString .= ", ";
- }
- $count ++;
- }
- } else {
- $this->register_error ( "Active records error: \$insertArray must be an array in " . __FILE__ . " function " . __FUNCTION__ );
- return false;
- }
- $sql = "INSERT INTO $table ($columnString) VALUES ($valueString);";
- return $sql;
- }
- /*
- * @see $this->generate_update();
- */
- function update($table, $updateArray, $whereArray) {
- $this->query ( $this->generate_update ( $table, $updateArray, $whereArray ) );
- if(!mysql_error()) {
- return 1;
- }
- return 0;
- }
- /*
- * Generates update queries. Values are automatically escaped
- * @param string $table name of table to update
- * @param array $updateArray = array(
- * 'column_name' => 'value',
- * 'column_name2' => 'value2',
- * );
- * @param array $whereArray = array(
- * 'col_name' => 'condition'
- * );
- * @return string SQL
- */
- function generate_update($table, $updateArray, $whereArray) {
- if(!$this->dbh) {
- $this->autoconnect();
- }
- /* Generate SET */
- if (is_array ( $updateArray )) {
- $count = 1;
- foreach ( $updateArray as $column => $value ) {
- if ($this->should_quote($value)) {
- $value = "'" . $this->escape($value) . "'";
- } else {
- $value = $this->escape($value);
- }
- if(!@$beenHere) {
- $beenHere = true;
- $setString = "SET ";
- }
- $setString .= "{$this->escape($column)} = $value";
- if($count != count( $updateArray )) {
- $setString .= ", ";
- }
- $count ++;
- }
- } else {
- $this->register_error ( "Active records error: \$updateArray must be an array in " . __FILE__ . " function " . __FUNCTION__ );
- return false;
- }
- /* Generate WHERE*/
- if(is_array($whereArray)) {
- $whereCount = 1;
- foreach($whereArray as $column => $condition) {
- if(!@$beenWhere) {
- $beenWhere = true;
- $whereString = "WHERE ";
- }
- if( $this->should_quote($condition)) {
- $condition = "'".$this->escape($condition)."'";
- } else {
- $condition = $this->escape($condition);
- }
- $whereString .= "{$this->escape($column)} = $condition";
- if($whereCount != count($whereArray)) {
- $whereString .= " AND ";
- }
- $whereCount++;
- }
- } else {
- $this->register_error ( "Active records error: \$whereArray must be an array in " . __FILE__ . " function " . __FUNCTION__ );
- return false;
- }
- $sql = "UPDATE $table $setString $whereString;";
- return $sql;
- }
- /*
- * @param string $table mysql table
- * @param array $fcv_array {("field", "comparator", "value"), ("field", "comparator", "value"), ...}
- * @param strin $limit limit portion of the query
- * @return string the query
- */
- function generate_select_where($table, $fcv_array, $limit = false) {
- if(!$this->dbh) {
- $this->autoconnect();
- }
- $query = "SELECT * FROM $table WHERE";
- $x = 0;
- foreach($fcv_array as $fcv) {
- $x++;
- $fcv = $this->escape_array($fcv);
- $fcv[2] = $this->should_quote($fcv[2]) ? "'{$fcv[2]}'" : $fcv[2];
- $query .= " {$fcv[0]} {$fcv[1]} {$fcv[2]}";
- if(count($fcv_array) > $x) {
- $query .= " AND";
- }
- }
- if($limit) {
- $limit = $this->escape(trim($limit));
- $query .= " LIMIT $limit";
- }
- $query .= ";";
- return $query;
- }
- /*
- * returns the number of rows to be found with the given parameters
- * @param string $table mysql table
- * @param array $fcv_array {("field", "comparator", "value"), ("field", "comparator", "value"), ...}
- * @return int total rows
- */
- function count_where($table, $fcv_array) {
- $query = $this->generate_select_where($table, $fcv_array);
- $query = preg_replace("/^SELECT (\*)/i", "SELECT count(*)", $query);
- return (int) $this->get_var($query);
- }
- /*
- * @see $this->generate_select_where
- * @return array set of obtained objects
- */
- function select_where($table, $fcv_array, $limit = false) {
- return $this->get_results($this->generate_select_where($table, $fcv_array, $limit));
- }
- /*
- * determine if $string should be wrapped in simple quotes
- * @param string $string the string to be checked
- * @return bool true if should quote, false if should not quote
- */
- function should_quote($string) {
- if(is_numeric($string) || in_array($string, $this->exceptions)) {
- return false;
- }
- return true;
- }
- /*
- * @return array|bool escaped array or false on failure
- */
- function escape_array($array) {
- if(is_array($array)) {
- foreach($array as $key => $a) {
- if(is_array($a)) {
- $array[$key] = $this->escape_array($a);
- } else {
- $array[$key] = $this->escape($a);
- }
- }
- return $array;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement