Coverage for qutebrowser/misc/sql.py : 89%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2016-2018 Ryan Roden-Corrent (rcorre) <ryan@rcorre.net> # # This file is part of qutebrowser. # # qutebrowser is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # qutebrowser is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Raised on an error interacting with the SQL database.
Attributes: environmental: Whether the error is likely caused by the environment and not a qutebrowser bug. """
"""Get a short text to display."""
"""A SQL error with a QSqlError available.
Attributes: error: The QSqlError object. """
debug.qenum_key(QSqlError, error.type())))
# https://sqlite.org/rescode.html # https://github.com/qutebrowser/qutebrowser/issues/2930 # https://github.com/qutebrowser/qutebrowser/issues/3004 '5', # SQLITE_BUSY ("database is locked") '8', # SQLITE_READONLY '11', # SQLITE_CORRUPT '13', # SQLITE_FULL ] # At least in init(), we can get errors like this: # type: ConnectionError # database text: out of memory # driver text: Error opening database # error code: -1 "out of memory", ] errcode in environmental_errors or (errcode == -1 and error.databaseText() in environmental_strings))
def from_query(cls, what, query, error): """Construct an error from a failed query.
Arguments: what: What we were doing when the error happened. query: The query which was executed. error: The QSqlError object. """
"""Initialize the SQL database connection.""" raise SqlError('Failed to add database. ' 'Are sqlite and Qt sqlite support installed?', environmental=True) error = database.lastError() raise SqliteError("Failed to open sqlite database at {}: {}" .format(db_path, error.text()), error)
# Enable write-ahead-logging and reduce disk write frequency # see https://sqlite.org/pragma.html and issues #2930 and #3507
"""Close the SQL connection."""
"""Return the sqlite version string.""" init(':memory:') ver = Query("select sqlite_version()").run().value() close() return ver except SqlError as e: return 'UNAVAILABLE ({})'.format(e)
"""A prepared SQL Query."""
"""Prepare a new sql query.
Args: querystr: String to prepare query from. forward_only: Optimization for queries that will only step forward. Must be false for completion queries. """ raise SqliteError.from_query('prepare', querystr, self.lastError())
raise SqlError("Cannot iterate inactive query")
"""Execute the prepared query.""" self.lastError())
"""Return the result of a single-value query (e.g. an EXISTS).""" raise SqlError("No result for single-result query")
"""Interface to a sql table.
Attributes: _name: Name of the SQL table this wraps.
Signals: changed: Emitted when the table is modified. """
"""Create a new table in the sql database.
Does nothing if the table already exists.
Args: name: Name of the table. fields: A list of field names. constraints: A dict mapping field names to constraint strings. """
for field in fields] .format(name=name, column_defs=', '.join(column_defs)))
"""Create an index over this table.
Args: name: Name of the index, should be unique. field: Name of the field to index. """ .format(name=name, table=self._name, field=field))
"""Iterate rows in the table."""
"""Return a prepared query that checks for the existence of an item.
Args: field: Field to match. """ "SELECT EXISTS(SELECT * FROM {table} WHERE {field} = :val)" .format(table=self._name, field=field))
"""Return the count of rows in the table."""
"""Remove all rows for which `field` equals `value`.
Args: field: Field to use as the key. value: Key value to delete.
Return: The number of rows deleted. """ .format(table=self._name, field=field))
verb=verb, table=self._name, columns=', '.join(values), params=params))
"""Append a row to the table.
Args: values: A dict with a value to insert for each field name. replace: If set, replace existing values. """
"""Performantly append multiple rows to the table.
Args: values: A dict with a list of values to insert for each field name. replace: If true, overwrite rows with a primary key match. """
"""Remove all rows from the table."""
"""Prepare, run, and return a select statement on this table.
Args: sort_by: name of column to sort by. sort_order: 'asc' or 'desc'. limit: max number of rows in result, defaults to -1 (unlimited).
Return: A prepared and executed select query. """ "LIMIT :limit" .format(table=self._name, sort_by=sort_by, sort_order=sort_order)) |