예제 #1
0
 /**
  * Set query
  *
  * Set the query to be executed
  *
  * @param string $query  target query
  * @param int    $limit  number of results to show
  * @param int    $offset result offset
  *
  * @return AeInterface_Database self
  */
 public function setQuery($query, $limit = null, $offset = 0)
 {
     if ($limit instanceof AeScalar) {
         $limit = $limit->getValue();
     }
     if ($offset instanceof AeScalar) {
         $offset = $offset->getValue();
     }
     // *** Check if limit and offset were set
     if (is_numeric($limit) && !preg_match('#^\\s*INSERT\\s+#i', $query) && !preg_match('#\\s+LIMIT\\s+(?:\\d+\\s*,\\s*)?\\d+(?:\\s+OFFSET\\s+\\d+)?\\s*$#i', $query)) {
         $query .= "\nLIMIT " . (int) $limit;
         if (is_numeric($offset) && $offset > 0) {
             $query .= ' OFFSET ' . (int) $offset;
         }
     }
     $this->setResult(null);
     $this->_query = AeDatabase::replacePrefix((string) $query, $this->getPrefix());
     return $this;
 }
예제 #2
0
 /**
  * Get connection
  *
  * Returns a database connection. If the connection is not established, uses
  * options provided to establish one.
  *
  * @throws AeSessionDriverDatabaseException #400 if database options are
  *                                          invalid
  *
  * @return AeInterface_Database
  */
 public function getConnection()
 {
     if ($this->_connection instanceof AeInterface_Database && !$this->_connection->isConnected()) {
         // *** The connection is lost, try to reestablish it
         $this->_connection = null;
     }
     if (!$this->_connection instanceof AeInterface_Database) {
         // *** Establish connection first
         if ($this->_options instanceof AeArray) {
             // *** An array of settings
             if (isset($this->_options['name'])) {
                 // *** Connection name and settings file path assumed
                 $this->_connection = AeDatabase::getConnection($this->_options['name'], $this->_options['settings']);
             } else {
                 if (isset($this->_options['driver'])) {
                     // *** Connection options array assumed
                     $this->_connection = AeDatabase::getInstance($this->_options['driver'], $this->_options['username'], $this->_options['password'], $this->_options['options']->value);
                 } else {
                     // *** Invalid options array
                     throw new AeSessionDriverDatabaseException('Invalid database connection options', 400);
                 }
             }
         } else {
             if ($this->_options instanceof AeCallback) {
                 // *** A callback
                 $this->_connection = $this->_options->call();
                 if (!$this->_connection instanceof AeInterface_Database) {
                     // *** Invalid callback return value
                     throw new AeSessionDriverDatabaseException('Invalid database connection options', 400);
                 }
             } else {
                 // *** Invalid options value
                 throw new AeSessionDriverDatabaseException('Invalid database connection options', 400);
             }
         }
     }
     return $this->_connection;
 }