# Path: ob2/util/time.py
# def now_str():
#     """Gets the current datetime in a serializable format (for database)."""
#     return format_time(now())
# 
# Path: ob2/util/assignments.py
# def get_assignment_name_set():
#     global _assignment_name_set
#     if _assignment_name_set is None:
#         _assignment_name_set = {assignment.name for assignment in config.assignments}
#     return _assignment_name_set
# 
# Path: ob2/util/build_constants.py
# QUEUED = -2
# 
# Path: ob2/util/config_data.py
# def get_repo_type(repo_name):
#     """
#     Returns either "group" or "personal", depending on what repo_name appears to be. If repo is
#     unidentifiable, return None.
# 
#     """
#     if re.match(r'^group\d+$', repo_name):
#         repo_type = "group"
#     elif re.match(r'^[a-z]{2,3}$', repo_name):
#         repo_type = "personal"
#     else:
#         repo_type = None
# 
#     # This is a useful hook to change how repos are classified, based on their name. The default is
#     # pretty good, but you may have extra repos that don't exactly follow this naming scheme.
#     #
#     # Arguments:
#     #   repo_type -- The original guess for the repo_type (see code above)
#     #   repo_name -- The name of the repo (e.g. "aa" or "group1")
#     #
#     # Returns:
#     #   Either "group" or "personal"
#     repo_type = apply_filters("get-repo-type", repo_type, repo_name)
# 
#     return repo_type
# 
# Path: ob2/util/group_constants.py
# ACCEPTED = 1


from collections import Counter
from ob2.util.time import now_str
from ob2.util.assignments import get_assignment_name_set
from ob2.util.build_constants import QUEUED
from ob2.util.config_data import get_repo_type
from ob2.util.group_constants import ACCEPTED

Each function takes a database cursor as its first parameter. The caller is responsible for
ensuring the proper transaction semantics are preserved and lock starvation is minimized.

"""




def get_next_autoincrementing_value(c, option_name):
    c.execute('''SELECT value FROM options WHERE key = ?''', [option_name])
    results = c.fetchall()
    if not results:
        next_value = 1
        c.execute('''INSERT INTO options (key, value) VALUES (?, ?)''', [option_name,
                                                                         str(next_value)])
    else:
        (current_value,), = results
        next_value = int(current_value) + 1
        c.execute('''UPDATE options SET value = ? WHERE key = ?''', [str(next_value),
                                                                     option_name])
    return next_value


def get_repo_owners(c, repo_name):
    """
    Given the name of a repository, return the list of users (user id's) that own the repository.
    An empty list may be returned if no users match.

    """

# Path: ob2/util/time.py
# def now_str():
#     """Gets the current datetime in a serializable format (for database)."""
#     return format_time(now())
# 
# Path: ob2/util/assignments.py
# def get_assignment_name_set():
#     global _assignment_name_set
#     if _assignment_name_set is None:
#         _assignment_name_set = {assignment.name for assignment in config.assignments}
#     return _assignment_name_set
# 
# Path: ob2/util/build_constants.py
# QUEUED = -2
# 
# Path: ob2/util/config_data.py
# def get_repo_type(repo_name):
#     """
#     Returns either "group" or "personal", depending on what repo_name appears to be. If repo is
#     unidentifiable, return None.
# 
#     """
#     if re.match(r'^group\d+$', repo_name):
#         repo_type = "group"
#     elif re.match(r'^[a-z]{2,3}$', repo_name):
#         repo_type = "personal"
#     else:
#         repo_type = None
# 
#     # This is a useful hook to change how repos are classified, based on their name. The default is
#     # pretty good, but you may have extra repos that don't exactly follow this naming scheme.
#     #
#     # Arguments:
#     #   repo_type -- The original guess for the repo_type (see code above)
#     #   repo_name -- The name of the repo (e.g. "aa" or "group1")
#     #
#     # Returns:
#     #   Either "group" or "personal"
#     repo_type = apply_filters("get-repo-type", repo_type, repo_name)
# 
#     return repo_type
# 
# Path: ob2/util/group_constants.py
# ACCEPTED = 1


# Path: ob2/database/helpers.py
from collections import Counter
from ob2.util.time import now_str
from ob2.util.assignments import get_assignment_name_set
from ob2.util.build_constants import QUEUED
from ob2.util.config_data import get_repo_type
from ob2.util.group_constants import ACCEPTED


Each function takes a database cursor as its first parameter. The caller is responsible for
ensuring the proper transaction semantics are preserved and lock starvation is minimized.

"""




def get_next_autoincrementing_value(c, option_name):
    c.execute('''SELECT value FROM options WHERE key = ?''', [option_name])
    results = c.fetchall()
    if not results:
        next_value = 1
        c.execute('''INSERT INTO options (key, value) VALUES (?, ?)''', [option_name,
                                                                         str(next_value)])
    else:
        (current_value,), = results
        next_value = int(current_value) + 1
        c.execute('''UPDATE options SET value = ? WHERE key = ?''', [str(next_value),
                                                                     option_name])
    return next_value


def get_repo_owners(c, repo_name):
    """
    Given the name of a repository, return the list of users (user id's) that own the repository.
    An empty list may be returned if no users match.

    """

    repo_type = get_repo_type(repo_name)