# Path: data/search_rows.py
# def search_rows(select_clause, where_clause, order_by_clause, **kwargs):
#     """Fetch rows.
# 
#     Parameters
#     ----------
#     order_by_clause
#     select_clause
#     where_clause
# 
#     Returns
#     -------
#     row_list
#     """
#     try:
#         row_list = select_clause.where(where_clause).order_by(
#             order_by_clause).dicts()
#     except:
#         raise
# 
#     return row_list
# 
# Path: logic/identifier/identifier.py
# class Identifier(BaseModel):
#     """Identifier database table."""
#     id = pwe.PrimaryKeyField()
#     name = pwe.CharField(default=u'', index=True, unique=True)
#     obfuscated_name = pwe.CharField(default=u'', index=True, unique=True)
#     obfuscated_lower = pwe.CharField(default=u'', index=True)
#     created_timestamp = pwe.DateTimeField(default=datetime.datetime.now)
# 
#     class Meta:
#         db_table = 'identifier'
# 
#     @validate('Identifier', 'name')
#     def validate_name(self):
#         """Validate the identifier name."""
#         if not self.name:
#             raise AssertionError('Name must be given.')
# 
#     @validate('Identifier', 'obfuscated_name')
#     def validate_obfuscated_name(self):
#         """Make sure the obfuscated name is unique."""
#         if self.name != self.obfuscated_name:
#             is_unique = False
#             while not is_unique:
#                 if not self.obfuscated_name:
#                     random_num = base_random_number(5)
#                     self.obfuscated_name = base_alphabet_encode(random_num, 5)
#                 try:
#                     get_reserved_by_name(self.obfuscated_name)
#                 except pwe.DoesNotExist:
#                     is_unique = True
#                 else:
#                     self.obfuscated_name = None
# 
#     @validate('Identifier', 'obfuscated_lower')
#     def validate_obfuscated_lower(self):
#         """Store lowercase value of the obfuscated name."""
#         self.obfuscated_lower = self.obfuscated_name.lower()


import re
import peewee as pwe
from data.search_rows import search_rows
from logic.identifier.identifier import Identifier




def search_identifiers(search_text, **kwargs):
    """Fetch identifier rows.

    Parameters
    ----------
    search_text : str or None

    Returns
    -------
    identifier_list
    """
    is_first = True
    where_clause = None
    if search_text:
        search_values = re.sub('[~`%&-+=]', '', search_text).split()
        for s in search_values:
            s_ = u'%'+s+u'%'
            if is_first:
                where_clause = (Identifier.name ** s_)
                is_first = False
            else:
                where_clause = where_clause & \
                    (Identifier.name ** s_)
    if is_first:
        where_clause = (~(Identifier.id >> None))


# Path: data/search_rows.py
# def search_rows(select_clause, where_clause, order_by_clause, **kwargs):
#     """Fetch rows.
# 
#     Parameters
#     ----------
#     order_by_clause
#     select_clause
#     where_clause
# 
#     Returns
#     -------
#     row_list
#     """
#     try:
#         row_list = select_clause.where(where_clause).order_by(
#             order_by_clause).dicts()
#     except:
#         raise
# 
#     return row_list
# 
# Path: logic/identifier/identifier.py
# class Identifier(BaseModel):
#     """Identifier database table."""
#     id = pwe.PrimaryKeyField()
#     name = pwe.CharField(default=u'', index=True, unique=True)
#     obfuscated_name = pwe.CharField(default=u'', index=True, unique=True)
#     obfuscated_lower = pwe.CharField(default=u'', index=True)
#     created_timestamp = pwe.DateTimeField(default=datetime.datetime.now)
# 
#     class Meta:
#         db_table = 'identifier'
# 
#     @validate('Identifier', 'name')
#     def validate_name(self):
#         """Validate the identifier name."""
#         if not self.name:
#             raise AssertionError('Name must be given.')
# 
#     @validate('Identifier', 'obfuscated_name')
#     def validate_obfuscated_name(self):
#         """Make sure the obfuscated name is unique."""
#         if self.name != self.obfuscated_name:
#             is_unique = False
#             while not is_unique:
#                 if not self.obfuscated_name:
#                     random_num = base_random_number(5)
#                     self.obfuscated_name = base_alphabet_encode(random_num, 5)
#                 try:
#                     get_reserved_by_name(self.obfuscated_name)
#                 except pwe.DoesNotExist:
#                     is_unique = True
#                 else:
#                     self.obfuscated_name = None
# 
#     @validate('Identifier', 'obfuscated_lower')
#     def validate_obfuscated_lower(self):
#         """Store lowercase value of the obfuscated name."""
#         self.obfuscated_lower = self.obfuscated_name.lower()


# Path: logic/identifier/search_rows.py
import re
import peewee as pwe
from data.search_rows import search_rows
from logic.identifier.identifier import Identifier





def search_identifiers(search_text, **kwargs):
    """Fetch identifier rows.

    Parameters
    ----------
    search_text : str or None

    Returns
    -------
    identifier_list
    """
    is_first = True
    where_clause = None
    if search_text:
        search_values = re.sub('[~`%&-+=]', '', search_text).split()
        for s in search_values:
            s_ = u'%'+s+u'%'
            if is_first:
                where_clause = (Identifier.name ** s_)
                is_first = False
            else:
                where_clause = where_clause & \
                    (Identifier.name ** s_)
    if is_first:
        where_clause = (~(Identifier.id >> None))


    select_clause = pwe.SelectQuery(Identifier, Identifier.id, Identifier.name)