예제 #1
0
    def _fetch(self):
        if self.is_closed:
            raise ProgrammingError('Connection is closed.')
        if not self.has_result_set:
            raise ProgrammingError('No result set.')

        if not self._result_set.next():
            return None
        self._rownumber += 1
        return tuple([
            self._converter.convert(self._meta_data.getColumnType(i), self._result_set, i)
            for i in xrange(1, self._meta_data.getColumnCount() + 1)
        ])
예제 #2
0
    def _fetch(self):
        if self.is_closed:
            raise ProgrammingError('Connection is closed.')
        if not self.has_result_set:
            raise ProgrammingError('No result set.')

        if not self._result_set.next():
            return None
        self._rownumber += 1
        return tuple([
            self._converter.convert(column.getSQLColumnType(), row.getVarCharValue())
            for column, row in zip(self._columns(), self._rows())
        ])
예제 #3
0
    def execute(self, operation, parameters=None):
        if self.is_closed:
            raise ProgrammingError('Connection is closed.')

        query = self._formatter.format(operation, parameters)
        _logger.debug(query)
        try:
            self._reset_state()
            result_set = self._statement.executeQuery(query)
            if result_set:
                self._result_set = result_set
                self._result_set.setFetchSize(self._arraysize)
                self._meta_data = result_set.getMetaData()
                self._update_count = -1
            else:
                self._update_count = self._statement.getUpdatecount()
            query_execution = self._query_execution()

            result_conf = query_execution.getResultConfiguration()
            self._output_location = result_conf.getOutputLocation()

            status = query_execution.getStatus()
            self._completion_date_time = to_datetime(status.getCompletionDateTime())
            self._submission_date_time = to_datetime(status.getSubmissionDateTime())

            statistics = query_execution.getStatistics()
            self._data_scanned_in_bytes = statistics.getDataScannedInBytes()
            self._execution_time_in_millis = statistics.getEngineExecutionTimeInMillis()
        except Exception as e:
            _logger.exception('Failed to execute query.')
            raise_from(DatabaseError(*e.args), e)
예제 #4
0
    def format(self, operation, parameters=None):
        if not operation or not operation.strip():
            raise ProgrammingError('Query is none or empty.')
        operation = operation.strip()

        if operation.upper().startswith('SELECT') or operation.upper().startswith('WITH'):
            escaper = _escape_presto
        else:
            escaper = _escape_hive

        kwargs = dict()
        if parameters:
            if isinstance(parameters, dict):
                for k, v in iteritems(parameters):
                    func = self.get_formatter(v)
                    kwargs.update({k: func(self, escaper, v)})
            else:
                raise ProgrammingError("Unsupported parameter " +
                                       "(Support for dict only): {0}".format(parameters))

        return (operation % kwargs).strip()
예제 #5
0
    def execute(self, operation, parameters=None):
        if self.is_closed:
            raise ProgrammingError('Connection is closed.')

        query = self._formatter.format(operation, parameters)
        _logger.debug(query)
        try:
            self._reset_state()
            has_result_set = self._statement.execute(query)
            if has_result_set:
                self._result_set = self._statement.getResultSet()
                self._result_set.setFetchSize(self._arraysize)
                self._meta_data = self._result_set.getMetaData()
                self._update_count = -1
            else:
                self._update_count = self._statement.getUpdateCount()
        except Exception as e:
            _logger.exception('Failed to execute query.')
            raise_from(DatabaseError(unwrap_exception(e)), e)
예제 #6
0
 def cancel(self):
     # TODO AthenaStatement dose not support cancel operation.
     #      Because ResultSet is not NULL only when query state finished.
     # public void cancel() throws SQLException {
     #     this.checkOpen();
     #     if(!this.isCancelled.get()) {
     #         this.lock.lock();
     #         try {
     #             if(this.currentResult.get() != null) {
     #                 ((AthenaResultSet)this.currentResult.get()).getClient().cancel();
     #                 this.isCancelled.compareAndSet(false, true);
     #             }
     #         } finally {
     #             this.lock.unlock();
     #         }
     #     }
     # }
     if self.is_closed:
         raise ProgrammingError('Connection is closed.')
     self._statement.cancel()
예제 #7
0
    def format(self, operation, *parameter_args, **parameters_kwargs):
        if not operation or not operation.strip():
            raise ProgrammingError('Query is none or empty.')
        operation = operation.strip()

        if operation.startswith('SELECT'):
            escaper = _escape_presto
        else:
            escaper = _escape_hive

        args = []
        for v in parameter_args:
            func = self.get_formatter(v)
            args.append(func(self, escaper, v))

        kwargs = dict()
        for k, v in iteritems(parameters_kwargs):
            func = self.get_formatter(v)
            kwargs.update({k: func(self, escaper, v)})

        return operation.format(*args, **kwargs).strip()
예제 #8
0
파일: cursor.py 프로젝트: zhaohc10/airflow2
    def execute(self, operation, parameters=None):
        if self.is_closed:
            raise ProgrammingError('Connection is closed.')

        query = self._formatter.format(operation, parameters)
        try:
            _logger.debug(query)
            self._description = None
            self._rownumber = 0
            result_set = self._statement.executeQuery(query)
            if result_set:
                self._result_set = result_set
                self._result_set.setFetchSize(self._arraysize)
                self._meta_data = result_set.getMetaData()
                self._update_count = -1
            else:
                self._result_set = None
                self._meta_data = None
                self._update_count = self._statement.getUpdatecount()
        except Exception:
            _logger.exception('Failed to execute query.')
            reraise_dbapi_error()
예제 #9
0
 def arraysize(self, value):
     if value <= 0 or value > self.DEFAULT_FETCH_SIZE:
         raise ProgrammingError('MaxResults is more than maximum allowed length {0}.'.format(
             self.DEFAULT_FETCH_SIZE))
     self._arraysize = value
예제 #10
0
 def cancel(self):
     if self.is_closed:
         raise ProgrammingError('Connection is closed.')
     self._statement.cancel()
예제 #11
0
 def cursor(self):
     if self.is_closed:
         raise ProgrammingError('Connection is closed.')
     return Cursor(self._jdbc_conn, self._converter, self._formatter)
예제 #12
0
파일: cursor.py 프로젝트: zhaohc10/airflow2
 def query_id(self):
     if not self.has_result_set:
         raise ProgrammingError('No result set.')
     return self._result_set.getClient().getQueryExecutionId()