# Path: sesql/config.py
# CONFIG_PATH_FALLBACK = 'sesql_config'
#     CONFIG_PATH = getattr(settings, 'SESQL_CONFIG_PATH', CONFIG_PATH_FALLBACK)
#     CONFIG_PATH = CONFIG_PATH_FALLBACK
# 
# Path: sesql/typemap.py
# class TypeMap(object):
#     def __init__(self):
#     def all_subclasses(klass, done = None):
#     def all_tables(self):
#     def all_classes(self):
#     def all_class_names(self):
#     def get_class_names_for(self, table):
#     def get_classes_for(self, table):
#     def get_table_for(self, klass):
#     def get_class_by_name(self, klass):
# 
# Path: sesql/fieldmap.py
# class FieldMap(object):
#     def __init__(self):
#     def all_fields(self):
#     def get_field(self, field):
#     def get_primary(self):
# 
# Path: sesql/results.py
# class SeSQLResultSet(object):
#     """
#     A lazy SeSQL result set
#     It mimicks a bit the Django QuerySet, but doesn't work the same way,
#     and doesn't provide exactly the same methods
#     """
#     def __init__(self, objs, fields):
#         """
#         Constructor
#         Objs must be a list of (class, id) with optionally extra fields
#         """
#         self.objs = objs
#         self.fields = fields
# 
#     def brains(self):
#         """
#         Get the raw objects from SeSQL index, aka the "brains", as dictionnaries
#         """
#         for obj in self.objs:
#             yield dict(zip(self.fields, obj))
# 
#     def count(self):
#         """
#         Count results
#         """
#         return len(self.objs)
#     __len__ = count
# 
#     def iterator(self):
#         """
#         Iterate on self
#         """
#         for obj in self.objs:
#             try:
#                 yield self.load(obj)
#             except config.orm.not_found:
#                 log.warning("Object %r does not exist ! Broken index ?" % (obj,))
#     __iter__ = iterator
# 
#     def all(self):
#         """
#         Get all the results as a list
#         """
#         return list(self)
# 
#     def get(self, index):
#         """
#         Get the row at given index
#         """
#         return self.load(self.objs[index])
#     __getitem__ = get
# 
#     def __getslice__(self, i, j):
#         """
#         Get a slice
#         """
#         res = [ self.load(obj) for obj in self.objs[i:j] ]
#         return res
# 
#     @staticmethod
#     def load(obj):
#         """
#         Get a given object
#         """
#         objclass, objid = obj[:2]
#         objclass = typemap.get_class_by_name(objclass)
#         if not objclass:
#             return config.orm.not_found
#         entry = "%s:%s" % (objclass.__name__, objid)
#         log.debug("Fetching %s" % entry)
#         return config.orm.load_object(objclass, objid)
# 
#     def historize(self, query):
#         """save in the database the query for future processing"""
#         nb_results = self.count()
#         query_text = query.get_fulltext_query()[2][0]
#         config.orm.historize(query=query_text, nb_results=nb_results)


import logging
from sesql import config
from sesql.typemap import typemap
from sesql.fieldmap import fieldmap
from sesql.results import SeSQLResultSet
        """
        Get the SQL pattern of the query, as tuple (pattern, values)
        """
        if isinstance(node, config.orm.node_class):
            if node.negated:
                node.negated = False
                pattern, values = self.get_pattern_for(node)
                node.negated = True
                pattern = "NOT (%s)" % pattern
                return pattern, values

            patterns = []
            values = []
            for child in node.children:
                pattern, vals = self.get_pattern_for(child)
                values.extend(list(vals))
                patterns.append(pattern)

            connector = ') %s (' % node.connector

            pattern = '(' + connector.join(patterns) + ')'
            return pattern, values

        query, value = node
        if "__" in query:
            field, method = query.split("__", 1)
        else:
            field = query
            method = "default"


# Path: sesql/config.py
# CONFIG_PATH_FALLBACK = 'sesql_config'
#     CONFIG_PATH = getattr(settings, 'SESQL_CONFIG_PATH', CONFIG_PATH_FALLBACK)
#     CONFIG_PATH = CONFIG_PATH_FALLBACK
# 
# Path: sesql/typemap.py
# class TypeMap(object):
#     def __init__(self):
#     def all_subclasses(klass, done = None):
#     def all_tables(self):
#     def all_classes(self):
#     def all_class_names(self):
#     def get_class_names_for(self, table):
#     def get_classes_for(self, table):
#     def get_table_for(self, klass):
#     def get_class_by_name(self, klass):
# 
# Path: sesql/fieldmap.py
# class FieldMap(object):
#     def __init__(self):
#     def all_fields(self):
#     def get_field(self, field):
#     def get_primary(self):
# 
# Path: sesql/results.py
# class SeSQLResultSet(object):
#     """
#     A lazy SeSQL result set
#     It mimicks a bit the Django QuerySet, but doesn't work the same way,
#     and doesn't provide exactly the same methods
#     """
#     def __init__(self, objs, fields):
#         """
#         Constructor
#         Objs must be a list of (class, id) with optionally extra fields
#         """
#         self.objs = objs
#         self.fields = fields
# 
#     def brains(self):
#         """
#         Get the raw objects from SeSQL index, aka the "brains", as dictionnaries
#         """
#         for obj in self.objs:
#             yield dict(zip(self.fields, obj))
# 
#     def count(self):
#         """
#         Count results
#         """
#         return len(self.objs)
#     __len__ = count
# 
#     def iterator(self):
#         """
#         Iterate on self
#         """
#         for obj in self.objs:
#             try:
#                 yield self.load(obj)
#             except config.orm.not_found:
#                 log.warning("Object %r does not exist ! Broken index ?" % (obj,))
#     __iter__ = iterator
# 
#     def all(self):
#         """
#         Get all the results as a list
#         """
#         return list(self)
# 
#     def get(self, index):
#         """
#         Get the row at given index
#         """
#         return self.load(self.objs[index])
#     __getitem__ = get
# 
#     def __getslice__(self, i, j):
#         """
#         Get a slice
#         """
#         res = [ self.load(obj) for obj in self.objs[i:j] ]
#         return res
# 
#     @staticmethod
#     def load(obj):
#         """
#         Get a given object
#         """
#         objclass, objid = obj[:2]
#         objclass = typemap.get_class_by_name(objclass)
#         if not objclass:
#             return config.orm.not_found
#         entry = "%s:%s" % (objclass.__name__, objid)
#         log.debug("Fetching %s" % entry)
#         return config.orm.load_object(objclass, objid)
# 
#     def historize(self, query):
#         """save in the database the query for future processing"""
#         nb_results = self.count()
#         query_text = query.get_fulltext_query()[2][0]
#         config.orm.historize(query=query_text, nb_results=nb_results)


# Path: sesql/query.py
import logging
from sesql import config
from sesql.typemap import typemap
from sesql.fieldmap import fieldmap
from sesql.results import SeSQLResultSet

        """
        Get the SQL pattern of the query, as tuple (pattern, values)
        """
        if isinstance(node, config.orm.node_class):
            if node.negated:
                node.negated = False
                pattern, values = self.get_pattern_for(node)
                node.negated = True
                pattern = "NOT (%s)" % pattern
                return pattern, values

            patterns = []
            values = []
            for child in node.children:
                pattern, vals = self.get_pattern_for(child)
                values.extend(list(vals))
                patterns.append(pattern)

            connector = ') %s (' % node.connector

            pattern = '(' + connector.join(patterns) + ')'
            return pattern, values

        query, value = node
        if "__" in query:
            field, method = query.split("__", 1)
        else:
            field = query
            method = "default"


        field = fieldmap.get_field(field)