Exemple #1
0
 def load(cls, id, session = None,):
     """
     Load from the database with a id.
     
     You must call methods thids way :
     
     inst = MyObject.load(id, session = None)
     
     if session is None take 
     
     ex : 
     bl = Block.load(4)
     anasig = AnalogSignal.load(id)
     """
     
     #~ if not hasattr(self, 'metadata'):
         #~ self.metadata = globalvars.globalmetadata
     
     if session is None:
         session = globalvars.globalSession()
     
     
     query = session.query(cls).filter_by(id=id)
     if query.count()==0:
         return None
     
     loaded = query.one()
     #~ loaded.metadata = self.metadata
     loaded.session = session
     return loaded
Exemple #2
0
    def save(self  , session = None):
        """
        Usefull to create a new field in script mode (based on globalsession and global metadata).
        
        Save the object in the database (perform a session.commit()).
        
        If self.id is None => insert
        If self.id is not None => update
        
        
        """
        if session is None:
            session = globalvars.globalSession()

        if self.id is None:
            session.add(self)
            session.commit()
        else:
            merged = session.merge(self)
            session.commit()
            id = merged.id

        if ECHO:
            print self.tablename , ' saved', self.id
        
        return self.id
Exemple #3
0
 def test_load(cls, id, session = None,):
     
     print 'cls', cls
     if session is None:
         session = globalvars.globalSession()
     
     query = session.query(cls).filter_by(id=id)
     if query.count()==0:
         return cls()
     return query.one()
def execute_sql(query ,*args,  **kargs):
    if 'metadata' in kargs :
        metadata = kargs['metadata']
        if 'session' in kargs:
            session = kargs['session']
        else:
            #~ session = create_session(bind=metadata.bind, autocommit=False, autoflush=True )
            Session = sessionmaker(bind=metadata.bind , autocommit=False, autoflush=True)
            session = Session()
    else:
        metadata = globalvars.globalmetadata
        if 'session' in kargs:
            session = kargs['session']
        else:
            session = globalvars.globalSession()
        
    
    if 'column_split' in kargs :
        column_split = kargs['column_split']
    else:
        column_split = True   

    if 'Array' in kargs :
        Array = kargs['Array']
    else:
        Array = True   
    
    
    
    pres = session.execute(query, kargs)
    if pres.rowcount == 0:
        if column_split :
            
            if sqlalchemy.__version__.startswith('0.5.8'):
                if Array:
                    res = [ numpy.array([ ]) for i in pres.keys ]
                else:
                    res = [ [ ] for i in pres.keys ]
            else:
                if Array:
                    res = [ numpy.array([ ]) for i in pres.keys() ]
                else:
                    res = [ [ ] for i in pres.keys() ]
        else:
            res = [ ]
    else:
        res = pres.fetchall()
        if column_split :
            
            #debug for oursql
            if len(res) == 0:
                if Array:
                    res = [ numpy.array([ ]) for i in pres.keys() ]
                else:
                    res = [ [ ] for i in pres.keys() ]
                return res    
             #end debug


            res2 = numpy.array( res, dtype = object)
            res = [ ]
            for i in range(res2.shape[1]):
                if Array:
                    res += [ res2[:,i] ]
                else :
                    res += [ res2[:,i].tolist() ]
    
    session.close()
    return res
Exemple #5
0
def execute_sql(query, *args, **kargs):
    if 'metadata' in kargs:
        metadata = kargs['metadata']
        if 'session' in kargs:
            session = kargs['session']
        else:
            #~ session = create_session(bind=metadata.bind, autocommit=False, autoflush=True )
            Session = sessionmaker(bind=metadata.bind,
                                   autocommit=False,
                                   autoflush=True)
            session = Session()
    else:
        metadata = globalvars.globalmetadata
        if 'session' in kargs:
            session = kargs['session']
        else:
            session = globalvars.globalSession()

    if 'column_split' in kargs:
        column_split = kargs['column_split']
    else:
        column_split = True

    if 'Array' in kargs:
        Array = kargs['Array']
    else:
        Array = True

    pres = session.execute(query, kargs)
    if pres.rowcount == 0:
        if column_split:

            if sqlalchemy.__version__.startswith('0.5.8'):
                if Array:
                    res = [numpy.array([]) for i in pres.keys]
                else:
                    res = [[] for i in pres.keys]
            else:
                if Array:
                    res = [numpy.array([]) for i in pres.keys()]
                else:
                    res = [[] for i in pres.keys()]
        else:
            res = []
    else:
        res = pres.fetchall()
        if column_split:

            #debug for oursql
            if len(res) == 0:
                if Array:
                    res = [numpy.array([]) for i in pres.keys()]
                else:
                    res = [[] for i in pres.keys()]
                return res
            #end debug

            res2 = numpy.array(res, dtype=object)
            res = []
            for i in range(res2.shape[1]):
                if Array:
                    res += [res2[:, i]]
                else:
                    res += [res2[:, i].tolist()]

    session.close()
    return res