Re: [sqlalchemy] generate raw insert statement

2017-10-20 Thread Антонио Антуан
Excuse me, missed your response.
Found this soultiuon:

>>> print(compile_query(Foo.__table__.insert().values(bar='zoo').compile(
Session.bind.dialect))
INSERT INTO foos (bar) VALUES ('zoo')





пятница, 13 октября 2017 г., 19:41:29 UTC+3 пользователь Mike Bayer написал:
>
> On Fri, Oct 13, 2017 at 11:21 AM, Антонио Антуан  > wrote: 
> > I've seen it and thought that some ways available... 
>
>
> you can maybe "capture" it via a logger but that's only as it runs. 
> The INSERT statement has to also return data to the ORM so it's not 
> tailored for "offline" use. 
>
> if you can give me a clue what you want to do I might have suggestions. 
>
>
>
> > пт, 13 окт. 2017 г. в 17:30, Mike Bayer  >: 
> >> 
> >> On Fri, Oct 13, 2017 at 8:43 AM, Антонио Антуан  > 
> >> wrote: 
> >> > I use such code to generate raw SQL from SQLA-queries: 
> >> > 
> >> > def compile_statement(statement): 
> >> > comp = statement.compile(dialect=postgresql.dialect()) 
> >> > params = {} 
> >> > for k, v in comp.params.items(): 
> >> > if isinstance(v, unicode): 
> >> > v = v.encode(cls.encoding) 
> >> > params[k] = adapt(v) 
> >> > return (comp.string.encode(cls.encoding) % 
> >> > params).decode(cls.encoding) 
> >> > 
> >> > 
> >> > def compile_query(query): 
> >> > return cls.compile_statement(query.statement) 
> >> > 
> >>  print(compile_query(Foo.query.filter(Foo.bar == 
> >>  'zoo').with_entities(Foo.bar)) 
> >> > SELECT foos.bar as foos_bar 
> >> > FROM foos 
> >> > WHERE foos.bar = 'zoo' 
> >> > 
> >> > 
> >> > Is there any way to generate raw SQL insert statement, generated by 
> this 
> >> > code? 
> >> > Foo(bar='zoo').save() 
> >> 
> >> if you mean that save() is the flask-sqlalchemy thing that calls 
> >> flush(), and you want to get the INSERT that the ORM uses directly, 
> >> there's not really a way to get that on the fly.   The insert is 
> >> generated internally based on a scan of what's in the object and it 
> >> occurs across several private functions which pass lots of internal 
> >> state around. 
> >> 
> >> 
> >> 
> >> > 
> >> > -- 
> >> > SQLAlchemy - 
> >> > The Python SQL Toolkit and Object Relational Mapper 
> >> > 
> >> > http://www.sqlalchemy.org/ 
> >> > 
> >> > To post example code, please provide an MCVE: Minimal, Complete, and 
> >> > Verifiable Example. See http://stackoverflow.com/help/mcve for a 
> full 
> >> > description. 
> >> > --- 
> >> > You received this message because you are subscribed to the Google 
> >> > Groups 
> >> > "sqlalchemy" group. 
> >> > To unsubscribe from this group and stop receiving emails from it, 
> send 
> >> > an 
> >> > email to sqlalchemy+...@googlegroups.com . 
> >> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> >> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> >> > For more options, visit https://groups.google.com/d/optout. 
> >> 
> >> -- 
> >> SQLAlchemy - 
> >> The Python SQL Toolkit and Object Relational Mapper 
> >> 
> >> http://www.sqlalchemy.org/ 
> >> 
> >> To post example code, please provide an MCVE: Minimal, Complete, and 
> >> Verifiable Example.  See  http://stackoverflow.com/help/mcve for a 
> full 
> >> description. 
> >> --- 
> >> You received this message because you are subscribed to the Google 
> Groups 
> >> "sqlalchemy" group. 
> >> To unsubscribe from this group and stop receiving emails from it, send 
> an 
> >> email to sqlalchemy+...@googlegroups.com . 
> >> To post to this group, send email to sqlal...@googlegroups.com 
> . 
> >> Visit this group at https://groups.google.com/group/sqlalchemy. 
> >> For more options, visit https://groups.google.com/d/optout. 
> > 
> > -- 
> > 
> > Антон 
> > 
> > -- 
> > SQLAlchemy - 
> > The Python SQL Toolkit and Object Relational Mapper 
> > 
> > http://www.sqlalchemy.org/ 
> > 
> > To post example code, please provide an MCVE: Minimal, Complete, and 
> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> > description. 
> > --- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "sqlalchemy" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to sqlalchemy+...@googlegroups.com . 
> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 

Re: [sqlalchemy] generate raw insert statement

2017-10-13 Thread Mike Bayer
On Fri, Oct 13, 2017 at 11:21 AM, Антонио Антуан  wrote:
> I've seen it and thought that some ways available...


you can maybe "capture" it via a logger but that's only as it runs.
The INSERT statement has to also return data to the ORM so it's not
tailored for "offline" use.

if you can give me a clue what you want to do I might have suggestions.



> пт, 13 окт. 2017 г. в 17:30, Mike Bayer :
>>
>> On Fri, Oct 13, 2017 at 8:43 AM, Антонио Антуан 
>> wrote:
>> > I use such code to generate raw SQL from SQLA-queries:
>> >
>> > def compile_statement(statement):
>> > comp = statement.compile(dialect=postgresql.dialect())
>> > params = {}
>> > for k, v in comp.params.items():
>> > if isinstance(v, unicode):
>> > v = v.encode(cls.encoding)
>> > params[k] = adapt(v)
>> > return (comp.string.encode(cls.encoding) %
>> > params).decode(cls.encoding)
>> >
>> >
>> > def compile_query(query):
>> > return cls.compile_statement(query.statement)
>> >
>>  print(compile_query(Foo.query.filter(Foo.bar ==
>>  'zoo').with_entities(Foo.bar))
>> > SELECT foos.bar as foos_bar
>> > FROM foos
>> > WHERE foos.bar = 'zoo'
>> >
>> >
>> > Is there any way to generate raw SQL insert statement, generated by this
>> > code?
>> > Foo(bar='zoo').save()
>>
>> if you mean that save() is the flask-sqlalchemy thing that calls
>> flush(), and you want to get the INSERT that the ORM uses directly,
>> there's not really a way to get that on the fly.   The insert is
>> generated internally based on a scan of what's in the object and it
>> occurs across several private functions which pass lots of internal
>> state around.
>>
>>
>>
>> >
>> > --
>> > SQLAlchemy -
>> > The Python SQL Toolkit and Object Relational Mapper
>> >
>> > http://www.sqlalchemy.org/
>> >
>> > To post example code, please provide an MCVE: Minimal, Complete, and
>> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full
>> > description.
>> > ---
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "sqlalchemy" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to sqlalchemy+unsubscr...@googlegroups.com.
>> > To post to this group, send email to sqlalchemy@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/sqlalchemy.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> SQLAlchemy -
>> The Python SQL Toolkit and Object Relational Mapper
>>
>> http://www.sqlalchemy.org/
>>
>> To post example code, please provide an MCVE: Minimal, Complete, and
>> Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full
>> description.
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "sqlalchemy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to sqlalchemy+unsubscr...@googlegroups.com.
>> To post to this group, send email to sqlalchemy@googlegroups.com.
>> Visit this group at https://groups.google.com/group/sqlalchemy.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
>
> Антон
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] generate raw insert statement

2017-10-13 Thread Антонио Антуан
I've seen it and thought that some ways available...
пт, 13 окт. 2017 г. в 17:30, Mike Bayer :

> On Fri, Oct 13, 2017 at 8:43 AM, Антонио Антуан 
> wrote:
> > I use such code to generate raw SQL from SQLA-queries:
> >
> > def compile_statement(statement):
> > comp = statement.compile(dialect=postgresql.dialect())
> > params = {}
> > for k, v in comp.params.items():
> > if isinstance(v, unicode):
> > v = v.encode(cls.encoding)
> > params[k] = adapt(v)
> > return (comp.string.encode(cls.encoding) %
> params).decode(cls.encoding)
> >
> >
> > def compile_query(query):
> > return cls.compile_statement(query.statement)
> >
>  print(compile_query(Foo.query.filter(Foo.bar ==
>  'zoo').with_entities(Foo.bar))
> > SELECT foos.bar as foos_bar
> > FROM foos
> > WHERE foos.bar = 'zoo'
> >
> >
> > Is there any way to generate raw SQL insert statement, generated by this
> > code?
> > Foo(bar='zoo').save()
>
> if you mean that save() is the flask-sqlalchemy thing that calls
> flush(), and you want to get the INSERT that the ORM uses directly,
> there's not really a way to get that on the fly.   The insert is
> generated internally based on a scan of what's in the object and it
> occurs across several private functions which pass lots of internal
> state around.
>
>
>
> >
> > --
> > SQLAlchemy -
> > The Python SQL Toolkit and Object Relational Mapper
> >
> > http://www.sqlalchemy.org/
> >
> > To post example code, please provide an MCVE: Minimal, Complete, and
> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> > description.
> > ---
> > You received this message because you are subscribed to the Google Groups
> > "sqlalchemy" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to sqlalchemy+unsubscr...@googlegroups.com.
> > To post to this group, send email to sqlalchemy@googlegroups.com.
> > Visit this group at https://groups.google.com/group/sqlalchemy.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
-- 

Антон

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] generate raw insert statement

2017-10-13 Thread Mike Bayer
On Fri, Oct 13, 2017 at 8:43 AM, Антонио Антуан  wrote:
> I use such code to generate raw SQL from SQLA-queries:
>
> def compile_statement(statement):
> comp = statement.compile(dialect=postgresql.dialect())
> params = {}
> for k, v in comp.params.items():
> if isinstance(v, unicode):
> v = v.encode(cls.encoding)
> params[k] = adapt(v)
> return (comp.string.encode(cls.encoding) % params).decode(cls.encoding)
>
>
> def compile_query(query):
> return cls.compile_statement(query.statement)
>
 print(compile_query(Foo.query.filter(Foo.bar ==
 'zoo').with_entities(Foo.bar))
> SELECT foos.bar as foos_bar
> FROM foos
> WHERE foos.bar = 'zoo'
>
>
> Is there any way to generate raw SQL insert statement, generated by this
> code?
> Foo(bar='zoo').save()

if you mean that save() is the flask-sqlalchemy thing that calls
flush(), and you want to get the INSERT that the ORM uses directly,
there's not really a way to get that on the fly.   The insert is
generated internally based on a scan of what's in the object and it
occurs across several private functions which pass lots of internal
state around.



>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] generate raw insert statement

2017-10-13 Thread Антонио Антуан
I use such code to generate raw SQL from SQLA-queries:

def compile_statement(statement):
comp = statement.compile(dialect=postgresql.dialect())
params = {}
for k, v in comp.params.items():
if isinstance(v, unicode):
v = v.encode(cls.encoding)
params[k] = adapt(v)
return (comp.string.encode(cls.encoding) % params).decode(cls.encoding)


def compile_query(query):
return cls.compile_statement(query.statement)

>>> print(compile_query(Foo.query.filter(Foo.bar == 
>>> 'zoo').with_entities(Foo.bar))
SELECT foos.bar as foos_bar
FROM foos
WHERE foos.bar = 'zoo'


Is there any way to generate raw SQL insert statement, generated by this 
code?
Foo(bar='zoo').save()

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.