Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions go/goqu/left_join_demo.go
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [valk-guard] reported by reviewdog 🐶
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...

Comment on lines +39 to +46

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [valk-guard] reported by reviewdog 🐶
VG105: projection column "ghost_status" not found in table "orders" schema; check SELECT list and schema/model mappings | Origin: goqu query builder | Query: SELECT users.id, users.email, orders.ghost_status FROM users LEFT JOIN orders ON 1=1 WHERE...

Comment on lines +39 to +46

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [valk-guard] reported by reviewdog 🐶
VG106: filter predicate column "missing_flag" not found in table "orders" schema; check predicate/group/order columns in schema/model mappings | Origin: goqu query builder | Query: SELECT users.id, users.email, orders.ghost_status FROM users LEFT JOIN orders ON 1=1 WHERE...

return err
}
2 changes: 2 additions & 0 deletions go/goqu/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package goqu

import "time"

// Left-join demo branch keeps this model file in the changed-file scan context.

// Order represents a customer order.
type Order struct {
ID int64 `db:"id" goqu:"skipinsert"`
Expand Down
26 changes: 26 additions & 0 deletions python/sqlalchemy/left_join_demo.py
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [valk-guard] reported by reviewdog 🐶
VG004: SELECT without LIMIT may return unbounded rows; add LIMIT or FETCH FIRST | Origin: SQLAlchemy query builder | Query: SELECT "users"."id", "users"."email", "orders"."ghost_status" FROM "users" LEFT JOIN "orde...

Comment on lines +22 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [valk-guard] reported by reviewdog 🐶
VG105: projection column "ghost_status" not found in table "orders" schema; check SELECT list and schema/model mappings | Origin: SQLAlchemy query builder | Query: SELECT "users"."id", "users"."email", "orders"."ghost_status" FROM "users" LEFT JOIN "orde...

Comment on lines +22 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [valk-guard] reported by reviewdog 🐶
VG106: filter predicate column "missing_flag" not found in table "orders" schema; check predicate/group/order columns in schema/model mappings | Origin: SQLAlchemy query builder | Query: SELECT "users"."id", "users"."email", "orders"."ghost_status" FROM "users" LEFT JOIN "orde...

)
1 change: 1 addition & 0 deletions python/sqlalchemy/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""SQLAlchemy model definitions.

Clean ORM model declarations — no SQL anti-patterns here.
This file is also touched in the LEFT JOIN demo branch so schema-aware rules keep model context.
"""

from sqlalchemy import Boolean, Column, DateTime, Float, Integer, String, func
Expand Down
1 change: 1 addition & 0 deletions sql/migrations/001_create_tables.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- Left-join demo branch keeps this migration file in the changed-file scan context.
-- 001_create_tables.sql
-- Clean schema setup. No anti-patterns — valk-guard should report zero findings here.

Expand Down