1# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""This files contains helper methods to interact with the readonly database."""
6
7# Please note this file doesn't contain and should not contain any logic to
8# establish connections outside of Django, as that might lead to effects where
9# connections get leaked, which will lead to Django not cleaning them up
10# properly. See http://crbug.com/422637 for more details on this failure.
11
12from django import db as django_db
13
14_DISABLED = False
15
16def set_globally_disabled(disable):
17    """Disable and enable the use of readonly connections globally.
18
19    If disabled, connection() will return the global connection instead of the
20    readonly connection.
21
22    @param disable: When True, readonly connections will be disabled.
23    """
24    _DISABLED = disable
25
26
27def connection():
28    """Return a readonly database connection."""
29    if _DISABLED:
30        return django_db.connections['global']
31    return django_db.connections['readonly']
32
33
34def cursor():
35    """Return a cursor on the readonly database connection."""
36    return connection().cursor()
37