Esempio n. 1
0
 def find(cls, *args, **kwargs):
     """
     Execute the query on this object class and return a ``Result`` object.
     """
     s = kwargs.pop('session', None)
     session = get_session(s)
     tmp_cursor = session.cursor()
     condition = cls._get_conditions(*args, **kwargs)
     
     order = kwargs.pop('order', [])
     fields = kwargs.pop('fields', None)
     limit = kwargs.pop('limit', None)
     offset = kwargs.pop('offset', None)
     query = cls._get_query(condition, order, fields, limit, offset)
     query_hash = b64encode(sha1(tmp_cursor.mogrify(*query)).digest())
     cursor = session.cursor()
     try:
         cursor.execute(*query)
     except ProgrammingError as err:
         if err.pgcode == UNDEFINED_TABLE:
             session._rollback(True)
             return []
         else:
             raise
     
     return Result(session, cursor, cls, fields)
Esempio n. 2
0
    def get(cls, id = None, session = None, *args, **kwargs):
        from eplasty import conditions as cond
        args = list(args)
        session = get_session(session)
        cached =  session.find_cached(cls.__table_name__, id)
        if cached:
            return cached
        if isinstance(id, cond.Condition):
            args.append(id)
        elif id is not None:
            args.append(cond.Equals(cls.get_pk(), id)) 
        
            
        cursor = session.cursor()
        
        condition = cls._get_conditions(*args, **kwargs)
        order = kwargs.get('order', [])
        try:
            cursor.execute(*cls._get_query(condition, order))
        except ProgrammingError as err:
            if err.pgcode == UNDEFINED_TABLE:
                session._rollback(True)
                raise NotFound("Didn't find anything")
            else:
                raise

        
        if cursor.rowcount == 1:
            r = cls.hydrate(cursor.fetchone(), session)
            session.add(r)
            return r
        elif cursor.rowcount == 0:
            raise NotFound("Didn't find anything")
        else:
            raise TooManyFound("Found more than one row")
Esempio n. 3
0
 def get_full_count(self, session=None):
     """Returns the full size of the dataset."""
     q = SelectQuery(from_=self.class_.__table_name__, columns=('COUNT(*)',))
     session = get_session(session)
     cursor = session.cursor()
     cursor.execute(*q.render())
     return cursor.fetchall()[0][0]
Esempio n. 4
0
 def test_no_connection(self):
     """Test trying to use nonexistent ctx raises exc"""
     self.assertRaises(CtxError, lambda: get_connection())
     self.assertRaises(CtxError, lambda: get_cursor())
     self.assertRaises(CtxError, lambda: get_session())
     self.assertRaises(CtxError, lambda: commit())
     self.assertRaises(CtxError, lambda: add())
 def setUp(self):
     
     class Knight(Object):
         set_context(get_test_conn())
         start_session()
         title = f.CharacterVarying(
             length = 5, null = False, default = 'Sir'
         )
         name = f.CharacterVarying(length = 20, null = False)
         nickname = f.CharacterVarying(length = 20, null = True)
         score = f.Integer()
         
     self.Knight = Knight
     self.root = Root()
     Traverser(class_=self.Knight, field='name', session=get_session()).mount(self.root, 'knights')
     
     for ktup in [
         ('Sir', 'Galahad', 'The Pure', 10),
         ('King', 'Arthur', None, 20),
         ('Sir', 'Robin', 'The Brave', 30),
         ('Sir', 'Lancelot', None, 40),
     ]:
         title, name, nickname, score = ktup
         k = Knight(
             title = title,
             name = name,
             nickname = nickname,
             score = score
         )
         add(k)
         
     commit()
     start_session()
Esempio n. 6
0
 def test_get_session_with_session(self):
     """Test passing a session to get_session() returns itself"""
     session = Session(get_test_conn())
     self.assertEqual(get_session(session), session)