Code Directory
 Visual Basic & VB.NET
New Code
White-label Grocery Delivery App Solution 2.0
Best Classified Script 5.1
Readymade B2B Script 1.3.1
Uber Clone with Safety Measure Addons 2.0
Equity Investing Software 1.3.2
C# QR Code Generator 2020.6.0.0
The .Net PDF Library 2020.7.1
dbExpress driver for MySQL 7.2
dbForge Documenter for Oracle 1.2
dbForge Studio for Oracle 4.2
Excel .Net Library 2020.6
fsMediaLibrary.NET 2019.11
VaxVoIP SIP Server SDK 5.2.0
Database Workbench Pro 5.7.4
dbForge Data Generator for Oracle 2.2
Top Code
phpEnter 5.1.
Single Leg MLM 1.2.1
Azizi search engine script PHP 4.1.10
Paste phpSoftPro 1.4.1
Extreme Injector 3.7
Deals and Discounts Website Script 1.0.2
Solid File System OS edition 5.1
Classified Ad Lister 1.0
Aglowsoft SQL Query Tools 8.2
Invoice Manager by PHPJabbers 3.0
ICPennyBid Penny Auction Script 4.0
PHP Review Script 1.0
ATN Resume Finder 2.0
ATN Site Builder 3.0
Availability Booking Calendar PHP 1.0
Report About Generator expressions for database requests
- required fields

Please enter text on the image

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( for r in plane_tbl if == "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 ( are converted to table_name
- dictionaries : the keys are the same as the attribute names above
- lists

For instance :
- iterating on query(name for r in plane_tbl) returns objects with an attribute name
- iterating on query( 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((, 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