Re: [sqlalchemy] How should I do inheritance using DeclarativeReflectedBase?

2012-05-14 Thread Ignas Mikalajunas
On Sun, May 13, 2012 at 1:05 PM, Michael Bayer mike...@zzzcomputing.com wrote:
 Michael Bayer wrote:

 On May 11, 2012, at 1:16 PM, Ignas Mikalajunas wrote:

 Even though the latest version of the DeclarativeRefletive example
 includes some handling for inheritance, I still can not get it to
 work. I try doing (mostly modified example from
 https://bitbucket.org/sqlalchemy/sqlalchemy/src/408388e5faf4/examples/declarative_reflection/declarative_reflection.py):


 and sqlalchemy tries to find the type column in the table 'bar'. Am I
 doing the inheritance set up wrong or is it some bug in
 DeclarativeReflectiveBase?

 would need to spend some time with it, the declarativereflective example
 hasn't been worked out for setting up an inheritance relationship as of
 yet.   So there could be any number of issues with it (part of why it's
 only an example and not a real feature)..


 this feature will require changes to the declarative extension itself,
 which I will attempt to prepare as part of 0.8.  when the feature is done,
 the 0.8 version of declarative.py can be used with 0.7 as well in the
 interim before 0.8 is released.


I see, thank you very much. I have a very good test case waiting for
this feature :)

Ignas

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: How do I patch SQLAlchemy declarative model dynamically with Columns of different type?

2012-05-14 Thread gostones
Does anyone have an idea what I may be doing wrong?

Thanks for your help in advance.


On May 11, 4:29 pm, gostones gosto...@gmail.com wrote:
 I am running mysql in production but would like to run a simple tests
 in a sqlite in memory db.

 The legacy mysql db has tables with columns that are mysql specific
 types, Which are declared in declarative models (subclassing
 declarative_base). I would like to run some simple tests without going
 to mysql and so would need to swap out the columns of the model.

 How do I do this? I've tried writing a patcher/unpatcher to swap out
 table in my model, but when I run some tests, I get

 OperationalError: (OperationalError) near ): syntax error u'\nCREATE
 TABLE my_table (\n)\n\n' ()

 Which makes my think that I am not patching the columns properly.

 Does anyone know how I can do this? What am I doing wrong?

 Currently, I create new columns and attach brand new Table object to
 __table__ and save the old table.

 The DB is created, create_all() is and convert_columns is run in
 setUp. drop_all() and revert_columns is run during tearDown in my
 tests

 mysql_sqlite_mapping = {INTEGER: Integer,
                         MEDIUMINT: Integer,
                         TEXT: text}

 def convert_columns(self, my_class, mapping):
     for column in my_class.__table__.columns:
         if type(column.type) in mapping:
             replacement_col = Column(column.name,
                                      mapping[type(column.type)],
                                      primary_key=column.primary_key,
                                      nullable=column.nullable,
                                      key=column.key,
                                      unique=column.unique)

             converted_columns.append(replacement_col)

     self.registry[my_class] = my_class.__table__

     my_class.__table__.metadata.remove(my_class.__table__)
     my_class.__table__ = Table(my_class.__table__.name,
                                my_class.__table__.metadata)

     for column in converted_columns:
         my_class.__table__.append_column(column)

     return my_class

 def revert_columns(self, my_class):
     saved_table = self.registry[my_class]

     metadata = my_class.__table__.metadata
     my_class.__table__.metadata.remove(my_class.__table__)

     model_class.__table__ = Table(saved_table.name,
                                   metadata)

     for column in saved_table.columns:
         column.table = None
         my_class.__table__.append_column(column)

     self.registry.pop(my_class)

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Oracle CLOB: empty field

2012-05-14 Thread FrancescaL
Hi group,

I have a question about Oracle CLOBs.

I expect to be able to manage all 3 different LOB column states:
(a) Populated
(b) Empty
(c) NULL
First of all: is it my expectation correct?

I'm in process of upgrading to SqlAlchemy 0.6.9 and this seems (to me)
no more possible.

Here it is how to build 3 examples (one for each column state).

# --
# SQL for manual table creation:
DROP TABLE t_tmp_sqla_upgrade;
CREATE TABLE t_tmp_sqla_upgrade (
 id   NUMBER (20) not null,
 text CLOB
);

# --
# Python code:
from sqlalchemy import MetaData, Table, Column, Integer, Text,
create_engine
from sqlalchemy.orm import mapper, sessionmaker

metadata = MetaData()
table = Table('t_tmp_sqla_upgrade', metadata,
Column('id', Integer, primary_key=True),
Column('text', Text())
)

class TmpTableEntity(object):
pass

mapper(TmpTableEntity, table)

kwargs = {}
kwargs['encoding'] = 'utf-8'
engine = create_engine(uoracle+cx_oracle://user:pwd@db, **kwargs)
Session = sessionmaker(bind=engine, autoflush=False)

def createItem(session, text):
e = TmpTableEntity()
e.id = 1
e.text = text
session.add(e)
session.commit()
session.expunge_all()
return e

def readItem(session, id):
return
session.query(TmpTableEntity).filter(TmpTableEntity.id==id).one()

session = Session()

# 3 cases (setup and teardown is needed for each case)

# Case (a): some text (OK)
text = u'some plain ascii text'
item = createItem(session, text)
rItem = readItem(session, 1)
assert rItem.text == text

# Case (b): empty text (KO)
text = u''
item = createItem(session, text)
rItem = readItem(session, 1)
assert rItem.text == None
# But I expect: assert rItem.text == u''
# since this is a CLOB field and not a VARCHAR2 one.

# Case (c): null text (OK)
text = None
item = createItem(session, text)
rItem = readItem(session, 1)
assert rItem.text == None

Thank you for your time,
Francesca

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Execute statement after each connect.

2012-05-14 Thread Oliver Tonnhofer
Hi,

how can I execute a statement for each new connection. I'm using the 
trigram module (pg_trgm) for PostgreSQL and I need to set a threshold by 
calling an SQL function for each new connection (`select set_limit(0.5);`).

I'm using the expression API and `engine.execute`. I looked at the event 
API, but the `connect` event does not give me a Connection to execute the 
statement. Are there any other options?

Regards,
Oliver


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/eBBhnajRmCAJ.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: How do I patch SQLAlchemy declarative model dynamically with Columns of different type?

2012-05-14 Thread Michael Bayer

mutation of Table objects in-place is not something SQLAlchemy supports. 
I would advise using an extension method to TypeEngine called
with_variant() (search the docs for it, cut and paste is not working on
this terminal ) which allows a single type object, like a String(), to
produce multiple variants per backend, such as mysql.VARCHAR() only on the
mysql backend, plain String/VARCHAR on others.



gostones wrote:
 Does anyone have an idea what I may be doing wrong?

 Thanks for your help in advance.


 On May 11, 4:29 pm, gostones gosto...@gmail.com wrote:
 I am running mysql in production but would like to run a simple tests
 in a sqlite in memory db.

 The legacy mysql db has tables with columns that are mysql specific
 types, Which are declared in declarative models (subclassing
 declarative_base). I would like to run some simple tests without going
 to mysql and so would need to swap out the columns of the model.

 How do I do this? I've tried writing a patcher/unpatcher to swap out
 table in my model, but when I run some tests, I get

 OperationalError: (OperationalError) near ): syntax error u'\nCREATE
 TABLE my_table (\n)\n\n' ()

 Which makes my think that I am not patching the columns properly.

 Does anyone know how I can do this? What am I doing wrong?

 Currently, I create new columns and attach brand new Table object to
 __table__ and save the old table.

 The DB is created, create_all() is and convert_columns is run in
 setUp. drop_all() and revert_columns is run during tearDown in my
 tests

 mysql_sqlite_mapping = {INTEGER: Integer,
                         MEDIUMINT: Integer,
                         TEXT: text}

 def convert_columns(self, my_class, mapping):
     for column in my_class.__table__.columns:
         if type(column.type) in mapping:
             replacement_col = Column(column.name,
                                      mapping[type(column.type)],
                                      primary_key=column.primary_key,
                                      nullable=column.nullable,
                                      key=column.key,
                                      unique=column.unique)

             converted_columns.append(replacement_col)

     self.registry[my_class] = my_class.__table__

     my_class.__table__.metadata.remove(my_class.__table__)
     my_class.__table__ = Table(my_class.__table__.name,
                                my_class.__table__.metadata)

     for column in converted_columns:
         my_class.__table__.append_column(column)

     return my_class

 def revert_columns(self, my_class):
     saved_table = self.registry[my_class]

     metadata = my_class.__table__.metadata
     my_class.__table__.metadata.remove(my_class.__table__)

     model_class.__table__ = Table(saved_table.name,
                                   metadata)

     for column in saved_table.columns:
         column.table = None
         my_class.__table__.append_column(column)

     self.registry.pop(my_class)

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Oracle CLOB: empty field

2012-05-14 Thread Michael Bayer
FrancescaL wrote:
 Hi group,

 I have a question about Oracle CLOBs.

 I expect to be able to manage all 3 different LOB column states:
 (a) Populated
 (b) Empty
 (c) NULL
 First of all: is it my expectation correct?

possibly not.  my experience with oracle CLOB is that a blank ends up
being a single blank string.

SQLAlchemy doesn't do anything here, data is passed directly from
cx_oracle and back, my advise would be to create a cx_oracle-only test
case (see the docs on cx_oracle's site, I can't copy and paste a URL on
this terminal unfortunately) and email the cx_oracle list if you believe
the DBAPI's direct behavior is erroneous, however this is sort of how it
goes with Oracle and there might not be a workaround.




 I'm in process of upgrading to SqlAlchemy 0.6.9 and this seems (to me)
 no more possible.

 Here it is how to build 3 examples (one for each column state).

 # --
 # SQL for manual table creation:
 DROP TABLE t_tmp_sqla_upgrade;
 CREATE TABLE t_tmp_sqla_upgrade (
  id   NUMBER (20) not null,
  text CLOB
 );

 # --
 # Python code:
 from sqlalchemy import MetaData, Table, Column, Integer, Text,
 create_engine
 from sqlalchemy.orm import mapper, sessionmaker

 metadata = MetaData()
 table = Table('t_tmp_sqla_upgrade', metadata,
   Column('id', Integer, primary_key=True),
   Column('text', Text())
 )

 class TmpTableEntity(object):
   pass

 mapper(TmpTableEntity, table)

 kwargs = {}
 kwargs['encoding'] = 'utf-8'
 engine = create_engine(uoracle+cx_oracle://user:pwd@db, **kwargs)
 Session = sessionmaker(bind=engine, autoflush=False)

 def createItem(session, text):
   e = TmpTableEntity()
   e.id = 1
   e.text = text
   session.add(e)
   session.commit()
   session.expunge_all()
   return e

 def readItem(session, id):
   return
 session.query(TmpTableEntity).filter(TmpTableEntity.id==id).one()

 session = Session()

 # 3 cases (setup and teardown is needed for each case)

 # Case (a): some text (OK)
 text = u'some plain ascii text'
 item = createItem(session, text)
 rItem = readItem(session, 1)
 assert rItem.text == text

 # Case (b): empty text (KO)
 text = u''
 item = createItem(session, text)
 rItem = readItem(session, 1)
 assert rItem.text == None
 # But I expect: assert rItem.text == u''
 # since this is a CLOB field and not a VARCHAR2 one.

 # Case (c): null text (OK)
 text = None
 item = createItem(session, text)
 rItem = readItem(session, 1)
 assert rItem.text == None

 Thank you for your time,
 Francesca

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Oracle CLOB: empty field

2012-05-14 Thread Michael Bayer
Michael Bayer wrote:
 FrancescaL wrote:
 Hi group,

 I have a question about Oracle CLOBs.

 I expect to be able to manage all 3 different LOB column states:
 (a) Populated
 (b) Empty
 (c) NULL
 First of all: is it my expectation correct?

 possibly not.  my experience with oracle CLOB is that a blank ends up
 being a single blank string.

correction, I meant a single space, that is, a string of length one
consisting of  .





 SQLAlchemy doesn't do anything here, data is passed directly from
 cx_oracle and back, my advise would be to create a cx_oracle-only test
 case (see the docs on cx_oracle's site, I can't copy and paste a URL on
 this terminal unfortunately) and email the cx_oracle list if you believe
 the DBAPI's direct behavior is erroneous, however this is sort of how it
 goes with Oracle and there might not be a workaround.




 I'm in process of upgrading to SqlAlchemy 0.6.9 and this seems (to me)
 no more possible.

 Here it is how to build 3 examples (one for each column state).

 # --
 # SQL for manual table creation:
 DROP TABLE t_tmp_sqla_upgrade;
 CREATE TABLE t_tmp_sqla_upgrade (
  id   NUMBER (20) not null,
  text CLOB
 );

 # --
 # Python code:
 from sqlalchemy import MetaData, Table, Column, Integer, Text,
 create_engine
 from sqlalchemy.orm import mapper, sessionmaker

 metadata = MetaData()
 table = Table('t_tmp_sqla_upgrade', metadata,
  Column('id', Integer, primary_key=True),
  Column('text', Text())
 )

 class TmpTableEntity(object):
  pass

 mapper(TmpTableEntity, table)

 kwargs = {}
 kwargs['encoding'] = 'utf-8'
 engine = create_engine(uoracle+cx_oracle://user:pwd@db, **kwargs)
 Session = sessionmaker(bind=engine, autoflush=False)

 def createItem(session, text):
  e = TmpTableEntity()
  e.id = 1
  e.text = text
  session.add(e)
  session.commit()
  session.expunge_all()
  return e

 def readItem(session, id):
  return
 session.query(TmpTableEntity).filter(TmpTableEntity.id==id).one()

 session = Session()

 # 3 cases (setup and teardown is needed for each case)

 # Case (a): some text (OK)
 text = u'some plain ascii text'
 item = createItem(session, text)
 rItem = readItem(session, 1)
 assert rItem.text == text

 # Case (b): empty text (KO)
 text = u''
 item = createItem(session, text)
 rItem = readItem(session, 1)
 assert rItem.text == None
 # But I expect: assert rItem.text == u''
 # since this is a CLOB field and not a VARCHAR2 one.

 # Case (c): null text (OK)
 text = None
 item = createItem(session, text)
 rItem = readItem(session, 1)
 assert rItem.text == None

 Thank you for your time,
 Francesca

 --
 You received this message because you are subscribed to the Google
 Groups
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.



 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Execute statement after each connect.

2012-05-14 Thread Simon King
On Mon, May 14, 2012 at 5:20 PM, Oliver Tonnhofer olt...@gmail.com wrote:
 Hi,

 how can I execute a statement for each new connection. I'm using the trigram
 module (pg_trgm) for PostgreSQL and I need to set a threshold by calling an
 SQL function for each new connection (`select set_limit(0.5);`).

 I'm using the expression API and `engine.execute`. I looked at the event
 API, but the `connect` event does not give me a Connection to execute the
 statement. Are there any other options?


According to:

 
http://docs.sqlalchemy.org/en/rel_0_7/core/events.html#sqlalchemy.events.PoolEvents.connect

'connect' event handlers look like this:

  connect(dbapi_connection, connection_record)

Can't you use the dbapi_connection to call your SQL function?

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.