Repository navigation
demo: LEFT JOIN validation in Goqu and SQLAlchemy #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Package goqu demonstrates LEFT JOIN query patterns with Goqu. | ||
| package goqu | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
|
|
||
| goqulib "github.com/doug-martin/goqu/v9" | ||
| ) | ||
|
|
||
| // JoinAuditStore demonstrates safe and broken LEFT JOIN queries. | ||
| type JoinAuditStore struct { | ||
| db *goqulib.Database | ||
| } | ||
|
|
||
| // NewJoinAuditStore creates a new JoinAuditStore. | ||
| func NewJoinAuditStore(db *sql.DB) *JoinAuditStore { | ||
| return &JoinAuditStore{db: goqulib.New("postgres", db)} | ||
| } | ||
|
|
||
| // ListUserOrderStatus demonstrates a safe LEFT JOIN query. | ||
| func (s *JoinAuditStore) ListUserOrderStatus(ctx context.Context) error { | ||
| _ = ctx | ||
| _, _, err := goqulib.From("users"). | ||
| LeftJoin( | ||
| goqulib.T("orders"), | ||
| goqulib.On(goqulib.I("orders.user_id").Eq(goqulib.I("users.id"))), | ||
| ). | ||
| Select("users.id", "users.email", "orders.status"). | ||
| Where(goqulib.I("users.active").Eq(true)). | ||
| Limit(25). | ||
| ToSQL() | ||
| return err | ||
| } | ||
|
|
||
| // ListBrokenUserOrderStatus demonstrates a broken LEFT JOIN query. | ||
| func (s *JoinAuditStore) ListBrokenUserOrderStatus(ctx context.Context) error { | ||
| _ = ctx | ||
| _, _, err := goqulib.From("users"). | ||
| LeftJoin( | ||
| goqulib.T("orders"), | ||
| goqulib.On(goqulib.I("orders.user_id").Eq(goqulib.I("users.id"))), | ||
| ). | ||
| Select("users.id", "users.email", "orders.ghost_status"). | ||
| Where(goqulib.I("orders.missing_flag").Eq("pending")). | ||
| ToSQL() | ||
|
Comment on lines
+39
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [valk-guard] reported by reviewdog 🐶
Comment on lines
+39
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [valk-guard] reported by reviewdog 🐶 |
||
| return err | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """LEFT JOIN demo for SQLAlchemy builder scanning.""" | ||
|
|
||
| from sqlalchemy.orm import Session | ||
|
|
||
| from models import Order, User | ||
|
|
||
|
|
||
| def list_user_order_status(session: Session): | ||
| """Return joined user/order rows with a bounded LEFT JOIN query.""" | ||
| return ( | ||
| session.query(User.id, User.email, Order.status) | ||
| .outerjoin(Order, Order.user_id == User.id) | ||
| .filter(User.active.is_(True)) | ||
| .limit(25) | ||
| .all() | ||
| ) | ||
|
|
||
|
|
||
| def list_broken_user_order_status(session: Session): | ||
| """Return a broken LEFT JOIN query that should trigger schema findings.""" | ||
| return ( | ||
| session.query(User.id, User.email, Order.ghost_status) | ||
| .outerjoin(Order, Order.user_id == User.id) | ||
| .filter(Order.missing_flag == "pending") | ||
| .all() | ||
|
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [valk-guard] reported by reviewdog 🐶
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [valk-guard] reported by reviewdog 🐶 |
||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VG004: SELECT without LIMIT may return unbounded rows; add LIMIT or FETCH FIRST | Origin: goqu query builder | Query:
SELECT users.id, users.email, orders.ghost_status FROM users LEFT JOIN orders ON 1=1 WHERE...