comparison pylearn/dbdict/api0.py @ 622:d2d582bcf7dc

api0 and sql seem to work with session closing
author James Bergstra <bergstrj@iro.umontreal.ca>
date Sun, 18 Jan 2009 21:39:28 -0500
parents 7041749cf804
children ae954c27fd11
comparison
equal deleted inserted replaced
621:7041749cf804 622:d2d582bcf7dc
406 h_self._Query = _Query 406 h_self._Query = _Query
407 407
408 def __iter__(h_self): 408 def __iter__(h_self):
409 return h_self.query.__iter__() 409 return h_self.query.__iter__()
410 410
411 def insert_kwargs(h_self, **dct): 411 def insert_kwargs(h_self, session=None, **dct):
412 """ 412 """
413 @rtype: DbHandle with reference to self 413 @rtype: DbHandle with reference to self
414 @return: a DbHandle initialized as a copy of dct 414 @return: a DbHandle initialized as a copy of dct
415 415
416 @type dct: dict-like instance whose keys are strings, and values are 416 @type dct: dict-like instance whose keys are strings, and values are
417 either strings, integers, floats 417 either strings, integers, floats
418 418
419 @param dct: dictionary to insert 419 @param dct: dictionary to insert
420 420
421 """ 421 """
422 rval = h_self._Dict() 422 return h_self.insert(dct, session=session)
423 if dct: rval.update(dct) 423
424 return rval 424 def insert(h_self, dct, session=None):
425 def insert(h_self, dct):
426 """ 425 """
427 @rtype: DbHandle with reference to self 426 @rtype: DbHandle with reference to self
428 @return: a DbHandle initialized as a copy of dct 427 @return: a DbHandle initialized as a copy of dct
429 428
430 @type dct: dict-like instance whose keys are strings, and values are 429 @type dct: dict-like instance whose keys are strings, and values are
431 either strings, integers, floats 430 either strings, integers, floats
432 431
433 @param dct: dictionary to insert 432 @param dct: dictionary to insert
434 433
435 """ 434 """
436 rval = h_self._Dict() 435 if session is None:
437 if dct: rval.update(dct) 436 s = h_self.session()
437 rval = h_self._Dict(s)
438 if dct: rval.update(dct, session=s)
439 s.commit()
440 s.close()
441 else:
442 rval = h_self._Dict(session)
443 if dct: rval.update(dct, session=session)
438 return rval 444 return rval
439 445
440 def query(h_self, session): 446 def query(h_self, session):
441 """Construct an SqlAlchemy query, which can be subsequently filtered 447 """Construct an SqlAlchemy query, which can be subsequently filtered
442 using the instance methods of DbQuery""" 448 using the instance methods of DbQuery"""
553 def postgres_db(user, password, host, database, echo=False, poolclass=sqlalchemy.pool.NullPool, **kwargs): 559 def postgres_db(user, password, host, database, echo=False, poolclass=sqlalchemy.pool.NullPool, **kwargs):
554 """Create an engine to access a postgres_dbhandle 560 """Create an engine to access a postgres_dbhandle
555 """ 561 """
556 db_str ='postgres://%(user)s:%(password)s@%(host)s/%(database)s' % locals() 562 db_str ='postgres://%(user)s:%(password)s@%(host)s/%(database)s' % locals()
557 563
558 engine = create_engine(db_str, pool_size=pool_size, echo=echo, poolclass=poolclass) 564 engine = create_engine(db_str, echo=echo, poolclass=poolclass)
559 565
560 return db_from_engine(engine, **kwargs) 566 return db_from_engine(engine, **kwargs)
561 567