Exemple #1
0
    def run(self, statement, DBNAME):
        """Execute the sql in the database and return the results. The results
		are a list of tuples. Each tuple has 4 values
		(title, rows, headers, status).
		"""
        #print self.conn
        if not hasattr(self, 'conn') or not self.conn:
            self._connect(DBNAME)
        # Remove spaces and EOL
        statement = statement.strip()
        if not statement:  # Empty string
            yield (None, None, None, None)

        # Split the sql into separate queries and run each one.
        # Unless it's saving a favorite query, in which case we
        # want to save them all together.
        if statement.startswith("\\fs"):
            components = [statement]
        else:
            components = sqlparse.split(statement)
            #print components
        for sql in components:
            # Remove spaces, eol and semi-colons.
            sql = sql.rstrip(";")

            # \G is treated specially since we have to set the expanded output.
            if sql.endswith("\\G"):
                special.set_expanded_output(True)
                sql = sql[:-2].strip()

            if not self.conn and not (
                    sql.startswith(".open") or sql.lower().startswith("use")
                    or sql.startswith("\\u") or sql.startswith("\\?")
                    or sql.startswith("\\q") or sql.startswith("help")
                    or sql.startswith("exit") or sql.startswith("quit")):
                _logger.debug(
                    "Not connected to database. Will not run statement: %s.",
                    sql)

                #yield self.get_result2()
                raise OperationalError("Not connected to database.")
                # yield ('Not connected to database', None, None, None)
                # return

            cur = self.conn.cursor() if self.conn else None
            try:  # Special command
                _logger.debug("Trying a dbspecial command. sql: %r", sql)
                for result in special.execute(cur, sql):
                    yield result
            except special.CommandNotFound:  # Regular SQL
                _logger.debug("Regular sql statement. sql: %r", sql)

                cur.execute(sql)
                yield self.get_result(cur)
Exemple #2
0
    def run(self, statement, BUCKET_NAME):
        """Execute the sql in the database and return the results. The results
		are a list of tuples. Each tuple has 4 values
		(title, rows, headers, status).
		"""
        #print self.conn
        if not hasattr(self, 'bucket') or not self.bucket:
            self._connect(BUCKET_NAME)
        # Remove spaces and EOL
        statement = statement.strip()
        if not statement:  # Empty string
            yield (None, None, None, None)

        # Split the sql into separate queries and run each one.
        # Unless it's saving a favorite query, in which case we
        # want to save them all together.
        if statement.startswith("\\fs"):
            components = [statement]
        else:
            components = sqlparse.split(statement)
            #print components
        for sql in components:
            # Remove spaces, eol and semi-colons.
            sql = sql.rstrip(";")

            # \G is treated specially since we have to set the expanded output.
            if sql.endswith("\\G"):
                special.set_expanded_output(True)
                sql = sql[:-2].strip()

            if not self.bucket and not (
                    sql.startswith(".open") or sql.lower().startswith("use")
                    or sql.startswith("\\u") or sql.startswith("\\?")
                    or sql.startswith("\\q") or sql.startswith("help")
                    or sql.startswith("exit") or sql.startswith("quit")):
                _logger.debug(
                    "Not connected to database. Will not run statement: %s.",
                    sql)

                #yield self.get_result2()
                raise OperationalError("Not connected to database.")
                # yield ('Not connected to database', None, None, None)
                # return

            cur = self.conn.cursor() if self.conn else None
            if 1:
                print(statement)
                #export LANG=en_US.utf-8
                #export LC_ALL=en_US.utf-8
                k = Key(self.bucket)
                stm = statement.split()
                limit = 25
                if len(stm) == 2:
                    kname, limit = stm
                else:
                    kname = stm[0]
                k.key = kname
                k.open()

                gzipped = GzipFile(None, 'rb', fileobj=k)
                reader = csv.reader(io.TextIOWrapper(gzipped,
                                                     newline="",
                                                     encoding="utf-8"),
                                    delimiter='^')
                if 1:
                    data = []
                    for id, line in enumerate(reader):
                        if id >= int(limit): break
                        data.append([id + 1] + line)

            try:  # Special command
                _logger.debug("Trying a dbspecial command. sql: %r", sql)
                for result in special.execute(cur, sql):
                    yield result
            except special.CommandNotFound:  # Regular SQL
                _logger.debug("Regular sql statement. sql: %r", sql)
                #print(sql)
                #cur.execute(sql)

                yield self.get_result2(data)
Exemple #3
0
        def one_iteration(text=None):

            if text is None:
                try:
                    text = self.prompt_app.prompt()

                except KeyboardInterrupt:
                    return

                special.set_expanded_output(False)

                try:
                    text = self.handle_editor_command(text)
                except RuntimeError as e:
                    #logger.error("sql: %r, error: %r", text, e)
                    #logger.error("traceback: %r", traceback.format_exc())
                    self.showerr(e)
                    return

            if not text.strip():
                return

            if self.destructive_warning:
                destroy = confirm_destructive_query(text)
                if destroy is None:
                    pass  # Query was not destructive. Nothing to do here.
                elif destroy is True:
                    self.echo("Your call!")
                else:
                    self.echo("Wise choice!")
                    return

            # Keep track of whether or not the query is mutating. In case
            # of a multi-statement query, the overall query is considered
            # mutating if any one of the component statements is mutating
            mutating = False

            try:
                logger.debug("sql: %r", text)

                special.write_tee(self.get_prompt(self.prompt_format) + text)
                if self.logfile:
                    self.logfile.write("\n# %s\n" % datetime.now())
                    self.logfile.write(text)
                    self.logfile.write("\n")

                successful = False
                start = time()
                res = sqlexecute.run(text, DBNAME)
                self.formatter.query = text
                successful = True
                result_count = 0
                for title, cur, headers, status in res:
                    logger.debug("headers: %r", headers)
                    logger.debug("rows: %r", cur)
                    logger.debug("status: %r", status)
                    threshold = 1000
                    if is_select(status) and cur and cur.rowcount > threshold:
                        self.echo(
                            "The result set has more than {} rows.".format(
                                threshold),
                            fg="red",
                        )
                        if not confirm("Do you want to continue?"):
                            self.echo("Aborted!", err=True, fg="red")
                            break

                    if self.auto_vertical_output:
                        max_width = self.prompt_app.output.get_size().columns
                    else:
                        max_width = None

                    formatted = self.format_output(
                        title, cur, headers, special.is_expanded_output(),
                        max_width)

                    t = time() - start
                    try:
                        if result_count > 0:
                            self.echo("")
                        try:
                            self.output(formatted, status)
                        except KeyboardInterrupt:
                            pass
                        self.echo("Time: %0.03fs" % t)
                    except KeyboardInterrupt:
                        pass

                    start = time()
                    result_count += 1
                    mutating = mutating or is_mutating(status)
                special.unset_once_if_written()
            except EOFError as e:
                raise e
            except KeyboardInterrupt:
                # get last connection id
                connection_id_to_kill = sqlexecute.connection_id
                logger.debug("connection id to kill: %r",
                             connection_id_to_kill)
                # Restart connection to the database
                sqlexecute.connect()
                try:
                    for title, cur, headers, status in sqlexecute.run(
                            "kill %s" % connection_id_to_kill):
                        status_str = str(status).lower()
                        if status_str.find("ok") > -1:
                            logger.debug(
                                "cancelled query, connection id: %r, sql: %r",
                                connection_id_to_kill,
                                text,
                            )
                            self.echo("cancelled query", err=True, fg="red")
                except Exception as e:
                    self.echo(
                        "Encountered error while cancelling query: {}".format(
                            e),
                        err=True,
                        fg="red",
                    )
            except NotImplementedError:
                self.echo("Not Yet Implemented.", fg="yellow")
            except OperationalError as e:
                logger.debug("Exception: %r", e)
                if e.args[0] in (2003, 2006, 2013):
                    logger.debug("Attempting to reconnect.")
                    self.echo("Reconnecting...", fg="yellow")
                    try:
                        sqlexecute.connect()
                        logger.debug("Reconnected successfully.")
                        one_iteration(text)
                        return  # OK to just return, cuz the recursion call runs to the end.
                    except OperationalError as e:
                        logger.debug("Reconnect failed. e: %r", e)
                        self.echo(str(e), err=True, fg="red")
                        # If reconnection failed, don't proceed further.
                        return
                else:
                    #logger.error("sql: %r, error: %r", text, e)
                    #logger.error("traceback: %r", traceback.format_exc())
                    self.showerr(e)
            except Exception as e:
                #logger.error("sql: %r, error: %r", text, e)
                #logger.error("traceback: %r", traceback.format_exc())
                self.showerr(e)
            else:
                if is_dropping_database(text, self.sqlexecute.dbname):
                    self.sqlexecute.dbname = None
                    self.sqlexecute.connect()

                # Refresh the table names and column names if necessary.
                if need_completion_refresh(text):
                    self.refresh_completions(reset=need_completion_reset(text))
            finally:
                if self.logfile is False:
                    self.echo("Warning: This query was not logged.",
                              err=True,
                              fg="red")
            query = Query(text, successful, mutating)
            self.query_history.append(query)
Exemple #4
0
        def one_iteration():
            document = self.cli.run()
            special.set_expanded_output(False)

            try:
                document = self.handle_editor_command(self.cli, document)
            except RuntimeError as e:
                self.echo(str(e), err=True, fg='red')
                return

            if not document.text.strip():
                return

            mutating = False

            try:
                special.write_tee(self.get_prompt(self.prompt) + document.text)
                successful = False
                start = time()
                res = self.sqlexecute.run(document.text)
                successful = True
                threshold = 1000
                result_count = 0

                for title, cur, headers, status in res:
                    if (is_select(status) and cur
                            and cur.rowcount > threshold):
                        self.echo(
                            'The result set has more than {} rows.'.forma(
                                threshold),
                            fg='red')
                        if not confirm('Do you want to continue?'):
                            self.echo('Aborted!', err=True, fg='red')
                            break

                    formatted = self.format_output(
                        title, cur, headers, special.is_expanded_output(),
                        None)

                    t = time() - start
                    try:
                        if result_count > 0:
                            self.echo('')
                        try:
                            self.output(formatted, status)
                        except KeyboardInterrupt:
                            pass

                        if special.is_timing_enabled():
                            self.echo('Time: %0.03fs' % t)
                    except KeyboardInterrupt:
                        pass

                    start = time()
                    result_count += 1
                    mutating = mutating or is_mutating(status)
                special.unset_once_if_written()
            except EOFError as e:
                raise e
            except KeyboardInterrupt:
                pass
            except NotImplementedError:
                self.echo('Not Yet Implemented.', fg="yellow")

            query = Query(document.text, successful, mutating)