order_by
Order by clause for sorting query results.
Asc
Bases: OrderByClause
Represents an ascending sort order for a column.
from embar.query.order_by import Asc
from embar.column.base import ColumnBase, ColumnInfo
col = ColumnBase()
col.info = ColumnInfo(
_table_name=lambda: "users",
name="age",
col_type="INTEGER",
py_type=int,
primary=False,
not_null=False
)
asc = Asc(col, nulls="last")
sql = asc.sql()
assert sql == '"users"."age" ASC NULLS LAST'
Source code in src/embar/query/order_by.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
__init__(col, nulls=None)
Create an ascending sort order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
ColumnBase
|
The column to sort by |
required |
nulls
|
NullsOrdering | None
|
Optional nulls ordering ("first" or "last") |
None
|
Source code in src/embar/query/order_by.py
55 56 57 58 59 60 61 62 63 64 | |
sql()
Generate the SQL fragment.
Source code in src/embar/query/order_by.py
66 67 68 69 70 71 72 | |
BareColumn
Bases: OrderByClause
Represents a bare column reference (defaults to ASC).
This is used internally when a column is passed directly to order_by().
from embar.query.order_by import BareColumn
from embar.column.base import ColumnBase, ColumnInfo
col = ColumnBase()
col.info = ColumnInfo(
_table_name=lambda: "users",
name="id",
col_type="INTEGER",
py_type=int,
primary=False,
not_null=False
)
bare = BareColumn(col)
sql = bare.sql()
assert sql == '"users"."id"'
Source code in src/embar/query/order_by.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
__init__(col)
Create a bare column reference.
Source code in src/embar/query/order_by.py
148 149 150 | |
sql()
Generate the SQL fragment (just the column FQN).
Source code in src/embar/query/order_by.py
152 153 154 155 | |
Desc
Bases: OrderByClause
Represents a descending sort order for a column.
from embar.query.order_by import Desc
from embar.column.base import ColumnBase, ColumnInfo
col = ColumnBase()
col.info = ColumnInfo(
_table_name=lambda: "users",
name="age",
col_type="INTEGER",
py_type=int,
primary=False,
not_null=False
)
desc = Desc(col, nulls="first")
sql = desc.sql()
assert sql == '"users"."age" DESC NULLS FIRST'
Source code in src/embar/query/order_by.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
__init__(col, nulls=None)
Create a descending sort order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
ColumnBase
|
The column to sort by |
required |
nulls
|
NullsOrdering | None
|
Optional nulls ordering ("first" or "last") |
None
|
Source code in src/embar/query/order_by.py
101 102 103 104 105 106 107 108 109 110 | |
sql()
Generate the SQL fragment.
Source code in src/embar/query/order_by.py
112 113 114 115 116 117 118 | |
OrderBy
dataclass
Represents an ORDER BY clause for sorting query results.
from embar.query.order_by import OrderBy, Asc, Desc, BareColumn
from embar.column.base import ColumnBase, ColumnInfo
col1 = ColumnBase()
col1.info = ColumnInfo(
_table_name=lambda: "users",
name="age",
col_type="INTEGER",
py_type=int,
primary=False,
not_null=False
)
col2 = ColumnBase()
col2.info = ColumnInfo(
_table_name=lambda: "users",
name="name",
col_type="TEXT",
py_type=str,
primary=False,
not_null=False
)
order = OrderBy((
Desc(col1),
Asc(col2, nulls="first"),
))
sql = order.sql()
assert sql == '"users"."age" DESC, "users"."name" ASC NULLS FIRST'
Source code in src/embar/query/order_by.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
sql()
Generate the full ORDER BY SQL clause.
from embar.query.order_by import OrderBy, Asc, BareColumn
from embar.column.base import ColumnBase, ColumnInfo
col1 = ColumnBase()
col1.info = ColumnInfo(
_table_name=lambda: "users",
name="id",
col_type="INTEGER",
py_type=int,
primary=False,
not_null=False
)
col2 = ColumnBase()
col2.info = ColumnInfo(
_table_name=lambda: "users",
name="name",
col_type="TEXT",
py_type=str,
primary=False,
not_null=False
)
order = OrderBy((BareColumn(col1), Asc(col2)))
sql = order.sql()
assert sql == '"users"."id", "users"."name" ASC'
Source code in src/embar/query/order_by.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
OrderByClause
Bases: ABC
Base class for ORDER BY clause components.
An ORDER BY clause can contain: - A bare column (defaults to ASC) - An Asc(column) or Desc(column) wrapper with optional nulls handling - Raw SQL via Sql(t"...")
Source code in src/embar/query/order_by.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
sql()
abstractmethod
Generate the SQL fragment for this ORDER BY component.
Source code in src/embar/query/order_by.py
23 24 25 26 | |
RawSqlOrder
Bases: OrderByClause
Represents raw SQL in an ORDER BY clause.
from embar.query.order_by import RawSqlOrder
from embar.sql import Sql
from embar.table import Table
from embar.column.common import Integer
class User(Table):
id: Integer = Integer()
raw = RawSqlOrder(Sql(t"{User.id} DESC"))
sql = raw.sql()
assert sql == '"user"."id" DESC'
Source code in src/embar/query/order_by.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
__init__(sql_obj)
Create a raw SQL order clause.
Source code in src/embar/query/order_by.py
179 180 181 | |
sql()
Generate the SQL fragment.
Source code in src/embar/query/order_by.py
183 184 185 186 | |