public function info($message) { if (Painless::isProfile(\Painless::DEV)) { $this->open(); fwrite($this->file, "{$message}\n"); } }
/** * Executes an SQL query and returns the results. * * Options supported by $extra: * 'return' => specifies the return value * self::RET_ROW_COUNT = returns an integer count of the number of rows affected by the query * self::RET_ID = returns the last inserted ID * self::RET_ARRAY = fetches an indexed array * self::RET_ASSOC = fetches an associative array where the key is the column name * self::RET_OBJ = fetches an object where the property is the column name * self::RET_STMT = returns the PDO statement * * 'close' => specifies whether or not to close the statement * self::STMT_CLOSE = closes the statement after execution * self::STMT_IGNORE = don't close the statement after execution (usually used in conjunction with RET_STMT) * * 'bind' => an array of bound parameters where the key is the field name and the value is the datum value * * @param string $cmd the command to execute (usually a plain SQL string) * @param array $extra any extra commands to add to the execution * @return mixed varies depending on the return type specified in $extra['return'] */ public function execute($cmd, $extra = array()) { // Lazy init the connection if (NULL == $this->_conn) { $this->init(); } // Construct the log item $log = array($cmd, $extra); try { // Create a PDOStatement object $stmt = $this->_conn->query($cmd); if (FALSE === $stmt) { return FALSE; } // Get the execution options $retType = (int) array_get($extra, 'return', self::RET_ROW_COUNT); $closeStmt = (bool) array_get($extra, 'close', self::STMT_CLOSE); // $extra['return'] will tell us what stuff to return, so let's parse it // now $ret = NULL; switch ($retType) { case self::RET_ROW_COUNT: $ret = (int) $stmt->rowCount(); break; case self::RET_ID: $ret = $this->_conn->lastInsertId(); break; case self::RET_ARRAY: $stmt->setFetchMode(\PDO::FETCH_NUM); $ret = $stmt->fetchAll(); break; case self::RET_ASSOC: $stmt->setFetchMode(\PDO::FETCH_ASSOC); $ret = $stmt->fetchAll(); break; case self::RET_OBJ: $stmt->setFetchMode(\PDO::FETCH_OBJ); $ret = $stmt->fetchAll(); break; case self::RET_STMT: $ret = $stmt; default: throw new \ErrorException('Unsupported return type [' . $retType . ']'); } } catch (Exception $e) { // Don't forget to log the operation before exiting static::log($log); throw new \ErrorException($e); } // Close the statement if necessary if ($closeStmt && !$ret instanceof \PDOStatement) { $stmt->closeCursor(); } // Save the return data if required if (\Painless::isProfile(\Painless::DEV)) { $log[] = $ret; } static::log($log); return $ret; }