# Path: reggata/data/db_schema.py
# class JsonUnsupportedVersionError(Exception):
# class User(Base):
# class HistoryRec(Base):
# class Item(Base, memento.Serializable):
# class DataRef(Base, memento.Serializable):
# class Thumbnail(Base):
# class Tag(Base):
# class Item_Tag(Base):
# class Field(Base):
# class Item_Field(Base):
#     def __init__(self, msg=None, cause=None):
#     def __init__(self, login=None, password=None, group=USER):
#     def checkValid(self):
#     def __init__(self, item_id=None, item_hash=None, data_ref_hash=None, \
#                  data_ref_url=None, operation=None, user_login=None, \
#                  parent1_id=None, parent2_id=None):
#     def _get_data_ref_url(self):
#     def _set_data_ref_url(self, value):
#     def __eq__(self, obj):
#     def __ne__(self, obj):
#     def __str__(self):
#     def __init__(self, user_login=None, title=None, date_created=None, alive=True):
#     def __init_on_load__(self):
#     def __listOfTagsAndTheirOwners(self):
#     def __listOfFieldValsAndTheirOwners(self):
#     def toJson(self):
#     def fromJson(objState):
#     def fromJsonVersion0(objState):
#     def fromJsonVersionCurrent(objState):
#     def hash(self):
#     def getFieldValue(self, fieldName, userLogin=None):
#     def hasTagsExceptOf(self, userLogin):
#     def hasFieldsExceptOf(self, userLogin):
#     def addTag(self, name, userLogin):
#     def setFieldValue(self, name, value, userLogin):
#     def format_field_vals(self):
#     def format_tags(self):
#     def removeTag(self, tagName):
#     def removeField(self, fieldName):
#     def hasTag(self, tagName):
#     def hasField(self, fieldName, fieldValue=None):
#     def checkValid(self):
#     def hasDataRef(self):
#     def __init__(self, objType=None, url=None, date_created=None):
#     def __init_on_load__(self):
#     def toJson(self):
#     def fromJson(objState):
#     def fromJsonVersion0(objState):
#     def fromJsonVersionCurrent(objState):
#     def _get_url(self):
#     def _set_url(self, value):
#     def _sql_from():
#     def is_image(self):
#     def __init__(self):
#     def _sql_from():
#     def __init__(self, name=None):
#     def _sql_from():
#     def __init__(self, tag=None, user_login=None):
#     def _sql_from():
#     def __init__(self, name=None):
#     def _sql_from():
#     def __init__(self, field=None, value=None, user_login=None):
#     def _sql_from():
#     USER = 'USER'
#     ADMIN = 'ADMIN'
#     CREATE = "CREATE"
#     UPDATE = "UPDATE"
#     DELETE = "DELETE"
#     MERGE = "MERGE"
#     ERROR_FILE_NOT_FOUND = 1
#     ERROR_FILE_HASH_MISMATCH = 2
#     CURRENT_JSON_VERSION = 1
#     FILE = "FILE"
#     URL = "URL" #TODO: URL type is deprecated. Do not use it anymore. User can always save an url for an item with a field
#     CURRENT_JSON_VERSION = 1


import reggata.helpers as helpers
from reggata.data import db_schema
    def __init__(self):
        super(SingleExtraClause, self).__init__()

    def interpret(self):
        #extra_users_str
        if len(self.extra_users) > 0:
            comma_list = helpers.to_commalist(self.extra_users, lambda x: "'" + x.interpret() + "'", ", ")
            extra_users_str = " i.user_login IN (" + comma_list + ") "
        else:
            extra_users_str = " 1 "

        #extra_paths_str
        if len(self.extra_paths) > 0:
            extra_paths_str = helpers.to_commalist( \
                self.extra_paths, lambda x: "data_refs.url LIKE '" + x.interpret() + "%'", " OR ")
        else:
            extra_paths_str = " 1 "

        #extra_titles_str
        if len(self.extra_titles) > 0:
            extra_titles_str = helpers.to_commalist( \
                self.extra_titles, lambda x: "i.title LIKE '%" + x.interpret() + "%'", " OR ")
        else:
            extra_titles_str = " 1 "

        #TODO filter items by item_tags.user_login and item_fields.user_login also
        s = '''
        --SingleExtraClause.interpret()
        select distinct
            i.*,

# Path: reggata/data/db_schema.py
# class JsonUnsupportedVersionError(Exception):
# class User(Base):
# class HistoryRec(Base):
# class Item(Base, memento.Serializable):
# class DataRef(Base, memento.Serializable):
# class Thumbnail(Base):
# class Tag(Base):
# class Item_Tag(Base):
# class Field(Base):
# class Item_Field(Base):
#     def __init__(self, msg=None, cause=None):
#     def __init__(self, login=None, password=None, group=USER):
#     def checkValid(self):
#     def __init__(self, item_id=None, item_hash=None, data_ref_hash=None, \
#                  data_ref_url=None, operation=None, user_login=None, \
#                  parent1_id=None, parent2_id=None):
#     def _get_data_ref_url(self):
#     def _set_data_ref_url(self, value):
#     def __eq__(self, obj):
#     def __ne__(self, obj):
#     def __str__(self):
#     def __init__(self, user_login=None, title=None, date_created=None, alive=True):
#     def __init_on_load__(self):
#     def __listOfTagsAndTheirOwners(self):
#     def __listOfFieldValsAndTheirOwners(self):
#     def toJson(self):
#     def fromJson(objState):
#     def fromJsonVersion0(objState):
#     def fromJsonVersionCurrent(objState):
#     def hash(self):
#     def getFieldValue(self, fieldName, userLogin=None):
#     def hasTagsExceptOf(self, userLogin):
#     def hasFieldsExceptOf(self, userLogin):
#     def addTag(self, name, userLogin):
#     def setFieldValue(self, name, value, userLogin):
#     def format_field_vals(self):
#     def format_tags(self):
#     def removeTag(self, tagName):
#     def removeField(self, fieldName):
#     def hasTag(self, tagName):
#     def hasField(self, fieldName, fieldValue=None):
#     def checkValid(self):
#     def hasDataRef(self):
#     def __init__(self, objType=None, url=None, date_created=None):
#     def __init_on_load__(self):
#     def toJson(self):
#     def fromJson(objState):
#     def fromJsonVersion0(objState):
#     def fromJsonVersionCurrent(objState):
#     def _get_url(self):
#     def _set_url(self, value):
#     def _sql_from():
#     def is_image(self):
#     def __init__(self):
#     def _sql_from():
#     def __init__(self, name=None):
#     def _sql_from():
#     def __init__(self, tag=None, user_login=None):
#     def _sql_from():
#     def __init__(self, name=None):
#     def _sql_from():
#     def __init__(self, field=None, value=None, user_login=None):
#     def _sql_from():
#     USER = 'USER'
#     ADMIN = 'ADMIN'
#     CREATE = "CREATE"
#     UPDATE = "UPDATE"
#     DELETE = "DELETE"
#     MERGE = "MERGE"
#     ERROR_FILE_NOT_FOUND = 1
#     ERROR_FILE_HASH_MISMATCH = 2
#     CURRENT_JSON_VERSION = 1
#     FILE = "FILE"
#     URL = "URL" #TODO: URL type is deprecated. Do not use it anymore. User can always save an url for an item with a field
#     CURRENT_JSON_VERSION = 1


# Path: reggata/parsers/query_tree_nodes.py
import reggata.helpers as helpers
from reggata.data import db_schema

    def __init__(self):
        super(SingleExtraClause, self).__init__()

    def interpret(self):
        #extra_users_str
        if len(self.extra_users) > 0:
            comma_list = helpers.to_commalist(self.extra_users, lambda x: "'" + x.interpret() + "'", ", ")
            extra_users_str = " i.user_login IN (" + comma_list + ") "
        else:
            extra_users_str = " 1 "

        #extra_paths_str
        if len(self.extra_paths) > 0:
            extra_paths_str = helpers.to_commalist( \
                self.extra_paths, lambda x: "data_refs.url LIKE '" + x.interpret() + "%'", " OR ")
        else:
            extra_paths_str = " 1 "

        #extra_titles_str
        if len(self.extra_titles) > 0:
            extra_titles_str = helpers.to_commalist( \
                self.extra_titles, lambda x: "i.title LIKE '%" + x.interpret() + "%'", " OR ")
        else:
            extra_titles_str = " 1 "

        #TODO filter items by item_tags.user_login and item_fields.user_login also
        s = '''
        --SingleExtraClause.interpret()
        select distinct
            i.*,

            ''' + db_schema.DataRef._sql_from() + '''