# Path: sesql/longquery.py
# @utils.log_time
# def longquery(query, order=None, limit=None, queryid=None, historize=False,
#               fields = ()):
#     """
#     Perform a long query and return a lazy Django result set
# 
#     If queryid is provided, then the query will be loaded from the
#     cache if possible, and redone else. Be careful, if the query is
#     redone, results may have changed.
# 
#     If fields are specified, will fetch those fields from the index
#     """
#     if queryid:
#         _query_cache.lock.acquire()
#         try:
#             results = _query_cache[queryid]
#             if results:
#                 return results
#             log.warning('Cached query id %r expired, re-querying.' % queryid)
#         finally:
#             _query_cache.lock.release()
# 
#     query = SeSQLQuery(query, order, fields)
#     results = query.longquery(limit)
# 
#     _query_cache.lock.acquire()
#     try:
#         # Generate a new query id, ensuring it's unique
#         if not queryid:
#             while True:
#                 letters = string.ascii_letters + string.digits
#                 queryid = ''.join([ random.choice(letters) for i in range(32) ])
#                 if queryid not in _query_cache:
#                     break
#         _query_cache[queryid] = results
#         results.queryid = queryid
#     finally:
#         _query_cache.lock.release()
# 
#     if historize: # suggest feature hook
#         results.historize(query)
# 
#     return results


from django.core.management.base import BaseCommand
from django.core.management import call_command
from django.db import connection, transaction
from django.db.models import Q
from sesql.longquery import longquery
import settings
import sys
# (at your option) any later version.

# SeSQL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with SeSQL.  If not, see <http://www.gnu.org/licenses/>.

"""
This will preform a SeSQL long query and displays the results
It's mostly useful to check your SeSQL installation
"""


class Command(BaseCommand):
    help = "Perform a SeSQL long query"
    
    def handle(self, *apps, **options):
        """
        Handle the command
        """
        if not 1 <= len(apps) <= 2:
            print "Syntax : sesqllongquery <query> [<order>]"
            sys.exit(1)

        query = eval(apps[0])
        order = len(apps) == 2 and eval(apps[1]) or None
        

# Path: sesql/longquery.py
# @utils.log_time
# def longquery(query, order=None, limit=None, queryid=None, historize=False,
#               fields = ()):
#     """
#     Perform a long query and return a lazy Django result set
# 
#     If queryid is provided, then the query will be loaded from the
#     cache if possible, and redone else. Be careful, if the query is
#     redone, results may have changed.
# 
#     If fields are specified, will fetch those fields from the index
#     """
#     if queryid:
#         _query_cache.lock.acquire()
#         try:
#             results = _query_cache[queryid]
#             if results:
#                 return results
#             log.warning('Cached query id %r expired, re-querying.' % queryid)
#         finally:
#             _query_cache.lock.release()
# 
#     query = SeSQLQuery(query, order, fields)
#     results = query.longquery(limit)
# 
#     _query_cache.lock.acquire()
#     try:
#         # Generate a new query id, ensuring it's unique
#         if not queryid:
#             while True:
#                 letters = string.ascii_letters + string.digits
#                 queryid = ''.join([ random.choice(letters) for i in range(32) ])
#                 if queryid not in _query_cache:
#                     break
#         _query_cache[queryid] = results
#         results.queryid = queryid
#     finally:
#         _query_cache.lock.release()
# 
#     if historize: # suggest feature hook
#         results.historize(query)
# 
#     return results


# Path: sesql/management/commands/sesqllongquery.py
from django.core.management.base import BaseCommand
from django.core.management import call_command
from django.db import connection, transaction
from django.db.models import Q
from sesql.longquery import longquery
import settings
import sys

# (at your option) any later version.

# SeSQL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with SeSQL.  If not, see <http://www.gnu.org/licenses/>.

"""
This will preform a SeSQL long query and displays the results
It's mostly useful to check your SeSQL installation
"""


class Command(BaseCommand):
    help = "Perform a SeSQL long query"
    
    def handle(self, *apps, **options):
        """
        Handle the command
        """
        if not 1 <= len(apps) <= 2:
            print "Syntax : sesqllongquery <query> [<order>]"
            sys.exit(1)

        query = eval(apps[0])
        order = len(apps) == 2 and eval(apps[1]) or None
        

        print longquery(query, order).objs