
This patch follows the rules about import in HACKING.rst https://github.com/openstack/marconi/blob/master/HACKING.rst#imports * reorder imports * Add missing blank lines before code Change-Id: I4abcfb3a2640499c5df34cbf75b5eaecb3d19823
131 lines
3.8 KiB
Python
131 lines
3.8 KiB
Python
# Copyright (c) 2013 Rackspace, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import contextlib
|
|
import sqlite3
|
|
import uuid
|
|
|
|
import msgpack
|
|
from oslo.config import cfg
|
|
|
|
from marconi.queues import storage
|
|
from marconi.queues.storage.sqlite import controllers
|
|
from marconi.queues.storage.sqlite import utils
|
|
|
|
|
|
_SQLITE_OPTIONS = [
|
|
cfg.StrOpt('database', default=':memory:',
|
|
help='Sqlite database to use.')
|
|
]
|
|
|
|
cfg.CONF.register_opts(_SQLITE_OPTIONS, group='queues:drivers:storage:sqlite')
|
|
CFG = cfg.CONF['queues:drivers:storage:sqlite']
|
|
|
|
|
|
class Driver(storage.DriverBase):
|
|
|
|
def __init__(self):
|
|
self.__path = CFG.database
|
|
self.__conn = sqlite3.connect(self.__path,
|
|
detect_types=sqlite3.PARSE_DECLTYPES)
|
|
self.__db = self.__conn.cursor()
|
|
self.run('''PRAGMA foreign_keys = ON''')
|
|
|
|
@staticmethod
|
|
def pack(o):
|
|
"""Converts a Python variable to a custom SQlite `DOCUMENT`.
|
|
|
|
:param o: a Python str, unicode, int, long, float, bool, None
|
|
or a dict or list of %o
|
|
"""
|
|
return sqlite3.Binary(msgpack.dumps(o))
|
|
|
|
sqlite3.register_converter('DOCUMENT', lambda s:
|
|
msgpack.loads(s, encoding='utf-8'))
|
|
|
|
@staticmethod
|
|
def uuid(o):
|
|
"""Converts a UUID object to a custom SQlite `UUID`.
|
|
|
|
:param o: a UUID object
|
|
"""
|
|
return sqlite3.Binary(o.bytes)
|
|
|
|
sqlite3.register_converter('UUID', lambda s:
|
|
uuid.UUID(hex=s))
|
|
|
|
def run(self, sql, *args):
|
|
"""Performs a SQL query.
|
|
|
|
:param sql: a query string with the '?' placeholders
|
|
:param args: the arguments to substitute the placeholders
|
|
"""
|
|
return self.__db.execute(sql, args)
|
|
|
|
def run_multiple(self, sql, it):
|
|
"""Iteratively perform multiple SQL queries.
|
|
|
|
:param sql: a query string with the '?' placeholders
|
|
:param it: an iterator which yields a sequence of arguments to
|
|
substitute the placeholders
|
|
"""
|
|
self.__db.executemany(sql, it)
|
|
|
|
def get(self, sql, *args):
|
|
"""Runs %sql and returns the first entry in the results.
|
|
|
|
:param sql: a query string with the '?' placeholders
|
|
:param args: the arguments to substitute the placeholders
|
|
:raises: utils.NoResult if the result set is empty
|
|
"""
|
|
try:
|
|
return next(self.run(sql, *args))
|
|
|
|
except StopIteration:
|
|
raise utils.NoResult()
|
|
|
|
@property
|
|
def affected(self):
|
|
"""Checks whether a row is affected in the last operation."""
|
|
assert self.__db.rowcount in (0, 1)
|
|
return self.__db.rowcount == 1
|
|
|
|
@property
|
|
def lastrowid(self):
|
|
"""Returns the last inserted row id."""
|
|
return self.__db.lastrowid
|
|
|
|
@contextlib.contextmanager
|
|
def __call__(self, isolation):
|
|
self.run('begin ' + isolation)
|
|
try:
|
|
yield
|
|
self.__conn.commit()
|
|
except Exception:
|
|
self.__conn.rollback()
|
|
raise
|
|
|
|
@property
|
|
def _queue_controller(self):
|
|
return controllers.QueueController(self)
|
|
|
|
@property
|
|
def _message_controller(self):
|
|
return controllers.MessageController(self)
|
|
|
|
@property
|
|
def _claim_controller(self):
|
|
return controllers.ClaimController(self)
|