/**
  * This function prepares the specified expression as a value.
  *
  * @access public
  * @override
  * @param string $expr                          the expression to be prepared
  * @param char $escape                          the escape character
  * @return string                               the prepared expression
  *
  * @see http://www.postgresql.org/docs/7.4/static/datatype-boolean.html
  */
 public function prepare_value($expr, $escape = NULL)
 {
     if ($expr === NULL) {
         return 'NULL';
     } else {
         if ($expr === TRUE) {
             return "'t'";
             // TRUE, 't', 'true', 'y', 'yes', '1'
         } else {
             if ($expr === FALSE) {
                 return "'f'";
                 // FALSE, 'f', 'false', 'n', 'no', '0'
             } else {
                 if (is_array($expr)) {
                     $buffer = array();
                     foreach ($expr as $value) {
                         $buffer[] = $this->prepare_value($value, $escape);
                     }
                     return DB_SQL_Builder::_OPENING_PARENTHESIS_ . implode(', ', $buffer) . DB_SQL_Builder::_CLOSING_PARENTHESIS_;
                 } else {
                     if (is_object($expr)) {
                         if ($expr instanceof DB_PostgreSQL_Select_Builder) {
                             return DB_SQL_Builder::_OPENING_PARENTHESIS_ . $expr->statement(FALSE) . DB_SQL_Builder::_CLOSING_PARENTHESIS_;
                         } else {
                             if ($expr instanceof DB_SQL_Expression) {
                                 return $expr->value($this);
                             } else {
                                 if (class_exists('Database_Expression') and $expr instanceof Database_Expression) {
                                     return $expr->value();
                                 } else {
                                     if ($expr instanceof Data) {
                                         return $expr->as_hexcode("x'%s'");
                                     } else {
                                         if ($expr instanceof BitField) {
                                             return $expr->as_binary("b'%s'");
                                         } else {
                                             return static::prepare_value((string) $expr);
                                             // Convert the object to a string
                                         }
                                     }
                                 }
                             }
                         }
                     } else {
                         if (is_integer($expr)) {
                             return (int) $expr;
                         } else {
                             if (is_double($expr)) {
                                 return sprintf('%F', $expr);
                             } else {
                                 if (is_string($expr) and preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}(\\s[0-9]{2}:[0-9]{2}:[0-9]{2})?$/', $expr)) {
                                     // is_datetime($expr)
                                     return "'{$expr}'";
                                 } else {
                                     if ($expr === '') {
                                         return "''";
                                     } else {
                                         return DB_Connection_Pool::instance()->get_connection($this->data_source)->quote($expr, $escape);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #2
2
 /**
  * This function executes the SQL statement via the DAO class.
  *
  * @access public
  * @param boolean $auto_increment		  	        whether to query for the last insert id
  * @return integer                      	        the last insert id
  */
 public function execute()
 {
     $auto_increment = (func_num_args() > 0 and func_get_arg(0) === TRUE);
     $connection = DB_Connection_Pool::instance()->get_connection($this->data_source);
     $connection->execute($this->statement(TRUE));
     $primary_key = $auto_increment ? $connection->get_last_insert_id() : 0;
     return $primary_key;
 }
Exemple #3
1
 /**
  * This function executes the built SQL statement.
  *
  * @access public
  */
 public function execute()
 {
     $connection = DB_Connection_Pool::instance()->get_connection($this->data_source);
     $connection->execute($this->statement());
 }
Exemple #4
0
 /**
  * This function returns a result set of indexes for the specified table.
  *
  * +---------------+---------------+------------------------------------------------------------+
  * | field         | data type     | description                                                |
  * +---------------+---------------+------------------------------------------------------------+
  * | schema        | string        | The name of the schema that contains the table.            |
  * | table         | string        | The name of the table.                                     |
  * | index         | string        | The name of the index.                                     |
  * | column        | string        | The name of the column.                                    |
  * | seq_index     | integer       | The sequence index of the index.                           |
  * | ordering      | string        | The ordering of the index.                                 |
  * | unique        | boolean       | Indicates whether index on column is unique.               |
  * | primary       | boolean       | Indicates whether index on column is a primary key.        |
  * +---------------+---------------+------------------------------------------------------------+
  *
  * @access public
  * @override
  * @param string $table                 the table to evaluated
  * @param string $like                  a like constraint on the query
  * @return DB_ResultSet                 a result set of indexes for the specified
  *                                      table
  *
  * @see http://dev.mysql.com/doc/refman/5.6/en/show-index.html
  */
 public function indexes($table, $like = '')
 {
     $connection = DB_Connection_Pool::instance()->get_connection($this->data_source);
     $schema = $this->precompiler->prepare_identifier($this->data_source->database);
     $table = $this->precompiler->prepare_identifier($table);
     $sql = "SHOW INDEXES FROM {$table} FROM {$schema}";
     if (!empty($like)) {
         $sql .= ' WHERE `Key_name` LIKE ' . $this->precompiler->prepare_value($like);
     }
     $sql .= ';';
     $reader = $connection->reader($sql);
     $records = array();
     while ($reader->read()) {
         $buffer = $reader->row('array');
         $record = array('schema' => $this->data_source->database, 'table' => $buffer['Table'], 'index' => $buffer['Key_name'], 'column' => $buffer['Column_name'], 'seq_index' => $buffer['Seq_in_index'], 'ordering' => $buffer['Collation'] == 'A' ? 'ASC' : NULL, 'unique' => $buffer['Non_unique'] == '0', 'primary' => $buffer['Key_name'] == 'PRIMARY');
         $records[] = $record;
     }
     $reader->free();
     $results = new DB_ResultSet($records);
     return $results;
 }
Exemple #5
0
 /**
  * This function creates a new root node in the specified scope.
  *
  * @access public
  * @static
  * @param integer $scope                            the new scope to be create
  * @param string $name                              the name to given to the node
  * @param array $fields                             an associated array of additional field
  *                                                  name/value pairs
  * @return DB_ORM_MPTT                              the newly created root node
  **/
 public static function add_root($scope, $name, array $fields = NULL)
 {
     $data_source = static::data_source(DB_DataSource::MASTER_INSTANCE);
     $table = static::table();
     $connection = DB_Connection_Pool::instance()->get_connection($data_source);
     $connection->lock->add($table)->acquire();
     $builder = DB_SQL::insert($data_source)->into($table)->column('scope', $scope)->column('name', $name)->column('parent_id', NULL)->column('lft', 1)->column('rgt', 2);
     if (is_array($fields)) {
         foreach ($fields as $field => $value) {
             $builder->column($field, $value);
         }
     }
     $insert = $builder->statement();
     $connection->execute($insert);
     $id = $connection->get_last_insert_id();
     $connection->lock->release();
     $model = get_called_class();
     $root = new $model();
     $root->id = $id;
     $root->scope = $scope;
     $root->name = $name;
     $root->parent_id = NULL;
     $root->lft = 1;
     $root->rgt = 2;
     if (is_array($fields)) {
         foreach ($fields as $field => $value) {
             $root->{$field} = $value;
         }
     }
     return $root;
 }
Exemple #6
0
 /**
  * This function performs a query using the built SQL statement.
  *
  * @access public
  * @param integer $limit                            the "limit" constraint
  * @return DB_ResultSet                             the result set
  */
 public function query($limit = NULL)
 {
     if ($limit !== NULL) {
         $this->limit($limit);
     }
     $connection = DB_Connection_Pool::instance()->get_connection($this->data_source);
     $records = $connection->query($this->statement(), $this->model);
     return $records;
 }
Exemple #7
0
 /**
  * This function executes the SQL statement.
  *
  * @access public
  * @return integer                                  the last insert id
  */
 public function execute()
 {
     $model = $this->model;
     $auto_increment = $model::is_auto_incremented();
     $connection = DB_Connection_Pool::instance()->get_connection($this->data_source);
     $connection->execute($this->statement());
     $primary_key = $auto_increment ? $connection->get_last_insert_id() : 0;
     return $primary_key;
 }
Exemple #8
0
 /**
  * This function returns a data reader that is initialized with the SQL
  * statement.
  *
  * @access public
  * @return DB_SQL_DataReader                    the data reader
  */
 public function reader()
 {
     $connection = DB_Connection_Pool::instance()->get_connection($this->data_source);
     $reader = $connection->reader($this->statement(TRUE));
     return $reader;
 }