summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmjith Ramanujam <amjith.r@gmail.com>2024-05-03 10:13:08 -0700
committerGitHub <noreply@github.com>2024-05-03 10:13:08 -0700
commit0678e03653a4f31eb41c6cf06f08e3ff58390557 (patch)
treecca58d327eb006259fcefbad63d5dcbbc97cb46e
parent02718a609a3720e186b0b5a19ccefed51d52832a (diff)
parent95eee73153f7e5f34451fc44c37bc3c1a8207ecd (diff)
Merge pull request #178 from dbcli/random-stuff
Random stuff
-rw-r--r--.github/workflows/ci.yml2
-rw-r--r--CHANGELOG.md5
-rw-r--r--TODO3
-rw-r--r--TODO.md6
-rw-r--r--litecli/main.py10
-rw-r--r--litecli/packages/special/dbcommands.py2
6 files changed, 16 insertions, 12 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 9ee36cf..d4de9d0 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
- python-version: ["3.7", "3.8", "3.9", "3.10"]
+ python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 21b4121..fce91e3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,9 @@
## Upcoming - TBD
-### Features
+### Improvements
+
+* When an empty `\d` is invoked the list of tables are returned instead of an error.
+* Show SQLite version at startup.
### Bug Fixes
diff --git a/TODO b/TODO
deleted file mode 100644
index 7c854dc..0000000
--- a/TODO
+++ /dev/null
@@ -1,3 +0,0 @@
-* [] Sort by frecency.
-* [] Add completions when an attach database command is run.
-* [] Add behave tests.
diff --git a/TODO.md b/TODO.md
new file mode 100644
index 0000000..58e2ebc
--- /dev/null
+++ b/TODO.md
@@ -0,0 +1,6 @@
+* [ ] Change to use ruff
+* [ ] Automate the release process via GH actions. [Article](https://simonwillison.net/2024/Jan/16/python-lib-pypi/)
+
+* [] Sort by frecency.
+* [] Add completions when an attach database command is run.
+* [] Add behave tests.
diff --git a/litecli/main.py b/litecli/main.py
index 35638bd..ebbc5ba 100644
--- a/litecli/main.py
+++ b/litecli/main.py
@@ -10,7 +10,7 @@ from time import time
from datetime import datetime
from io import open
from collections import namedtuple
-from sqlite3 import OperationalError
+from sqlite3 import OperationalError, sqlite_version
import shutil
from cli_helpers.tabular_output import TabularOutputFormatter
@@ -380,10 +380,8 @@ class LiteCli(object):
key_bindings = cli_bindings(self)
if not self.less_chatty:
- print("Version:", __version__)
- print("Mail: https://groups.google.com/forum/#!forum/litecli-users")
+ print(f"LiteCli: {__version__} (SQLite: {sqlite_version})")
print("GitHub: https://github.com/dbcli/litecli")
- # print("Home: https://litecli.com")
def get_message():
prompt = self.get_prompt(self.prompt_format)
@@ -823,7 +821,7 @@ class LiteCli(object):
headers,
format_name="vertical" if expanded else None,
column_types=column_types,
- **output_kwargs
+ **output_kwargs,
)
if isinstance(formatted, (text_type)):
@@ -845,7 +843,7 @@ class LiteCli(object):
headers,
format_name="vertical",
column_types=column_types,
- **output_kwargs
+ **output_kwargs,
)
if isinstance(formatted, (text_type)):
formatted = iter(formatted.splitlines())
diff --git a/litecli/packages/special/dbcommands.py b/litecli/packages/special/dbcommands.py
index dec3507..687c9a4 100644
--- a/litecli/packages/special/dbcommands.py
+++ b/litecli/packages/special/dbcommands.py
@@ -224,7 +224,7 @@ def describe(cur, arg, **_):
arg
)
else:
- raise ArgumentMissing("Table name required.")
+ return list_tables(cur)
log.debug(query)
cur.execute(query)
4'>334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
import traceback
import logging
import psycopg2
import psycopg2.extras
import psycopg2.errorcodes
import psycopg2.extensions as ext
import sqlparse
import pgspecial as special
import select
from psycopg2.extensions import POLL_OK, POLL_READ, POLL_WRITE
from .packages.parseutils.meta import FunctionMetadata, ForeignKey
from .encodingutils import unicode2utf8, PY2, utf8tounicode

_logger = logging.getLogger(__name__)

# Cast all database input to unicode automatically.
# See http://initd.org/psycopg/docs/usage.html#unicode-handling for more info.
ext.register_type(ext.UNICODE)
ext.register_type(ext.UNICODEARRAY)
ext.register_type(ext.new_type((705,), "UNKNOWN", ext.UNICODE))
# See https://github.com/dbcli/pgcli/issues/426 for more details.
# This registers a unicode type caster for datatype 'RECORD'.
ext.register_type(ext.new_type((2249,), "RECORD", ext.UNICODE))

# Cast bytea fields to text. By default, this will render as hex strings with
# Postgres 9+ and as escaped binary in earlier versions.
ext.register_type(ext.new_type((17,), 'BYTEA_TEXT', psycopg2.STRING))

# TODO: Get default timeout from pgclirc?
_WAIT_SELECT_TIMEOUT = 1


def _wait_select(conn):
    """
        copy-pasted from psycopg2.extras.wait_select
        the default implementation doesn't define a timeout in the select calls
    """
    while 1:
        try:
            state = conn.poll()
            if state == POLL_OK:
                break
            elif state == POLL_READ:
                select.select([conn.fileno()], [], [], _WAIT_SELECT_TIMEOUT)
            elif state == POLL_WRITE:
                select.select([], [conn.fileno()], [], _WAIT_SELECT_TIMEOUT)
            else:
                raise conn.OperationalError("bad state from poll: %s" % state)
        except KeyboardInterrupt:
            conn.cancel()
            # the loop will be broken by a server error
            continue


# When running a query, make pressing CTRL+C raise a KeyboardInterrupt
# See http://initd.org/psycopg/articles/2014/07/20/cancelling-postgresql-statements-python/
# See also https://github.com/psycopg/psycopg2/issues/468
ext.set_wait_callback(_wait_select)


def register_date_typecasters(connection):
    """
    Casts date and timestamp values to string, resolves issues with out of
    range dates (e.g. BC) which psycopg2 can't handle
    """
    def cast_date(value, cursor):
        return value
    cursor = connection.cursor()
    cursor.execute('SELECT NULL::date')
    date_oid = cursor.description[0][1]
    cursor.execute('SELECT NULL::timestamp')
    timestamp_oid = cursor.description[0][1]
    cursor.execute('SELECT NULL::timestamp with time zone')
    timestamptz_oid = cursor.description[0][1]
    oids = (date_oid, timestamp_oid, timestamptz_oid)
    new_type = psycopg2.extensions.new_type(oids, 'DATE', cast_date)
    psycopg2.extensions.register_type(new_type)


def register_json_typecasters(conn, loads_fn):
    """Set the function for converting JSON data for a connection.

    Use the supplied function to decode JSON data returned from the database
    via the given connection. The function should accept a single argument of
    the data as a string encoded in the database's character encoding.
    psycopg2's default handler for JSON data is json.loads.
    http://initd.org/psycopg/docs/extras.html#json-adaptation

    This function attempts to register the typecaster for both JSON and JSONB
    types.

    Returns a set that is a subset of {'json', 'jsonb'} indicating which types
    (if any) were successfully registered.
    """
    available = set()

    for name in ['json', 'jsonb']:
        try:
            psycopg2.extras.register_json(conn, loads=loads_fn, name=name)
            available.add(name)
        except psycopg2.ProgrammingError:
            pass

    return available


def register_hstore_typecaster(conn):
    """
    Instead of using register_hstore() which converts hstore into a python
    dict, we query the 'oid' of hstore which will be different for each
    database and register a type caster that converts it to unicode.
    http://initd.org/psycopg/docs/extras.html#psycopg2.extras.register_hstore
    """
    with conn.cursor() as cur:
        try:
            cur.execute("SELECT 'hstore'::regtype::oid")
            oid = cur.fetchone()[0]
            ext.register_type(ext.new_type((oid,), "HSTORE", ext.UNICODE))
        except Exception:
            pass


class PGExecute(object):

    # The boolean argument to the current_schemas function indicates whether
    # implicit schemas, e.g. pg_catalog
    search_path_query = '''
        SELECT * FROM unnest(current_schemas(true))'''

    schemata_query = '''
        SELECT  nspname
        FROM    pg_catalog.pg_namespace
        ORDER BY 1 '''

    tables_query = '''
        SELECT  n.nspname schema_name,
                c.relname table_name
        FROM    pg_catalog.pg_class c
                LEFT JOIN pg_catalog.pg_namespace n
                    ON n.oid = c.relnamespace
        WHERE   c.relkind = ANY(%s)
        ORDER BY 1,2;'''

    databases_query = '''
        SELECT d.datname
        FROM pg_catalog.pg_database d
        ORDER BY 1'''

    full_databases_query = '''
        SELECT d.datname as "Name",
            pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
            pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
            d.datcollate as "Collate",
            d.datctype as "Ctype",
            pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
        FROM pg_catalog.pg_database d
        ORDER BY 1'''

    def __init__(self, database, user, password, host, port, dsn, **kwargs):
        self.dbname = database
        self.user = user
        self.password = password
        self.host = host
        self.port = port
        self.dsn = dsn
        self.extra_args = {k: unicode2utf8(v) for k, v in kwargs.items()}
        self.connect()

    def connect(self, database=None, user=None, password=None, host=None,
                port=None, dsn=None, **kwargs):

        db = (database or self.dbname)
        user = (user or self.user)
        password = (password or self.password)
        host = (host or self.host)
        port = (port or self.port)
        dsn = (dsn or self.dsn)
        kwargs = (kwargs or self.extra_args)
        pid = -1
        if dsn:
            if password:
                dsn = "{0} password={1}".format(dsn, password)
            conn = psycopg2.connect(dsn=unicode2utf8(dsn))
            cursor = conn.cursor()
        else:
            conn = psycopg2.connect(
                database=unicode2utf8(db),
                user=unicode2utf8(user),
                password=unicode2utf8(password),
                host=unicode2utf8(host),
                port=unicode2utf8(port),
                **kwargs)

            cursor = conn.cursor()

        conn.set_client_encoding('utf8')
        if hasattr(self, 'conn'):
            self.conn.close()
        self.conn = conn
        self.conn.autocommit = True

        if dsn:
            # When we connect using a DSN, we don't really know what db,
            # user, etc. we connected to. Let's read it.
            # Note: moved this after setting autocommit because of #664.
            dsn_parameters = conn.get_dsn_parameters()
            db = dsn_parameters['dbname']
            user = dsn_parameters['user']
            host = dsn_parameters['host']
            port = dsn_parameters['port']

        self.dbname = db
        self.user = user
        self.password = password
        self.host = host
        self.port = port

        cursor.execute("SHOW ALL")
        db_parameters = dict(name_val_desc[:2] for name_val_desc in cursor.fetchall())

        pid = self._select_one(cursor, 'select pg_backend_pid()')[0]
        self.pid = pid
        self.superuser = db_parameters.get('is_superuser') == '1'

        register_date_typecasters(conn)
        register_json_typecasters(self.conn, self._json_typecaster)
        register_hstore_typecaster(self.conn)

    def _select_one(self, cur, sql):
        """
        Helper method to run a select and retrieve a single field value
        :param cur: cursor
        :param sql: string
        :return: string
        """
        cur.execute(sql)
        return cur.fetchone()

    def _json_typecaster(self, json_data):
        """Interpret incoming JSON data as a string.

        The raw data is decoded using the connection's encoding, which defaults
        to the database's encoding.

        See http://initd.org/psycopg/docs/connection.html#connection.encoding
        """

        if PY2:
            return json_data.decode(self.conn.encoding)
        else:
            return json_data


    def failed_transaction(self):
        status = self.conn.get_transaction_status()
        return status == ext.TRANSACTION_STATUS_INERROR


    def valid_transaction(self):
        status = self.conn.get_transaction_status()
        return (status == ext.TRANSACTION_STATUS_ACTIVE or
                status