Generator expressions for database requests
File ID: 65993
Generator expressions for database requests
|User Rating:|| (1 rating)|
Generator expressions for database requests Description
Description: This recipe is a follow-up to #440653, which was easy to implement but very slow because the iteration required to read all the rows of a table
As suggested by Matteo Dell'Amico in a comment, it would be much better if we could write something like
query(r.name for r in plane_tbl if r.country == "France")
where the generator expression is first translated into an SQL select, so that the iteration on the instance of query only reads the rows selected by the SQL statement
The present recipe is an attempt to achieve this. The first problem is to get the source code of the generator expression. I use information from the stack frame to get the file name and the line number, then the tokenize module to read the elements of the generator expression in the source code
Then, to build the SQL statement, the source code must be parsed : this is done using the compiler package and "visitors" that walk the AST tree returned by compiler.parse and do operations on the nodes, depending on their type
Finally, once the SQL statement is built, the iteration on the query instance can start : for the first one, the SQL statement is executed ; then the iteration yields the selected rows one by one.
The items can be :
- objects, with attribute names matching those in the generator expression, except that qualified names (table.name) are converted to table_name
- dictionaries : the keys are the same as the attribute names above
For instance :
- iterating on query(name for r in plane_tbl) returns objects with an attribute name
- iterating on query(r.name for r in plane_tbl) returns objects with an attribute r_name
This is because of iteration on tables which have the same field names
query((r.name,c.name) for r in plane_tbl for c in country_tbl if r.speed > 500 )
The type of the items is set by query.return_type = object, dict or list
|More Similar Code|
A small, fast, in-memory database management program
The database object supports the iterator protocol, so that requests can be expressed with list comprehensions or generator expressions instead of SQL. The equivalent of :
cursor.execute("SELECT name FROM table WHERE age=30")
rows = cursor.fetchall()
rows = table(age=30)
The module stores data in a...
The JET Code Generator is a DataBase code generation system based on industry standard XSLT. If you've ever felt that you're writing the same data access code over and over again, this tool is for you!!
The process represented by the...
EMS Data Generator 2005 for PostgreSQL is a powerful utility for generating test data to several PostgreSQL database tables at once. The wizard application allows you to define tables and fields for generating data, set value ranges, generate char...
EMS Data Generator 2005 for SQL Server is a powerful utility for generating test data to several Microsoft SQL database tables at once. The wizard application allows you to define tables and fields for generating data, set value ranges, generate...
EMS Data Generator 2005 for InterBase/Firebird is a powerful utility for generating test data to several InterBase/FireBird database tables at once. The wizard application allows you to define tables and fields for generating data, set value...
DB form generator, for database-content edition. Based on an existing database structure or an XML-based definition, the tool generates a highly customizable set of tools for editing the database tables (PHP/www and Python/GTK interfaces)
PHP code generator that can quickly and easilly create code for database operations (create-read-update-delete), with flexible code-generation options, based on table data gathered from a DB.
FrameWeb is a website and db administration generator: design the database for a site and generate automatically the administration for the content and the layout.
The target is a web developer that is tired of making cut&paste for...
Multi functional code generator.The Codejay program can be your Web Assistant. / Create web administration for database driven web sites. / Create web reports. / Choose among ASP, PHP, COLDFUSION, ASP.NET as your / programming language without...
Report generator for database end-user. This first version will be helpful for IS developers.
|User Review for Generator expressions for database requests
All files and free downloads are copyright of their respective owners. We do not provide any hacked, cracked, illegal, pirated version of scripts, codes, components downloads. All files are downloaded from the publishers website, our file servers or download mirrors. Always Virus check files downloaded from the web specially zip, rar, exe, trial, full versions etc. Download links from rapidshare, depositfiles, megaupload etc not published.