MCPcopy
hub / github.com/dosco/graphjin / RenderOrderBy

Method RenderOrderBy

core/internal/dialect/sqlite.go:147–207  ·  view source on GitHub ↗
(ctx Context, sel *qcode.Select)

Source from the content-addressed store, hash-verified

145
146
147func (d *SQLiteDialect) RenderOrderBy(ctx Context, sel *qcode.Select) {
148 if len(sel.OrderBy) == 0 {
149 return
150 }
151 ctx.WriteString(` ORDER BY `)
152
153 for i, ob := range sel.OrderBy {
154 if i != 0 {
155 ctx.WriteString(`, `)
156 }
157 if ob.KeyVar != "" && ob.Key != "" {
158 ctx.WriteString(` CASE WHEN `)
159 ctx.AddParam(Param{Name: ob.KeyVar, Type: "text"})
160 ctx.WriteString(` = `)
161 ctx.WriteString(fmt.Sprintf("'%s'", strings.ReplaceAll(ob.Key, "'", "''")))
162 ctx.WriteString(` THEN `)
163 }
164
165 if ob.Var != "" {
166 ctx.ColWithTable("_gj_ob_"+ob.Col.Name, "ord")
167 } else if ob.Alias != "" {
168 ctx.Quote(ob.Alias)
169 } else if sel.Rel.Type == sdata.RelEmbedded {
170 // Embedded JSON relationships in SQLite are rendered from json_each
171 // rows, so order by expressions must read from __sr_<id>.value.
172 ctx.WriteString(`json_extract(`)
173 ctx.Quote(fmt.Sprintf("__sr_%d", sel.ID))
174 ctx.WriteString(`."value", '$."`)
175 ctx.WriteString(ob.Col.Name)
176 ctx.WriteString(`"')`)
177 } else {
178 if ob.IsFunc {
179 ctx.WriteString(strings.ToUpper(ob.Func.Name))
180 ctx.WriteString(`(`)
181 }
182 ctx.ColWithTable(ob.Col.Table, ob.Col.Name)
183 if ob.IsFunc {
184 ctx.WriteString(`)`)
185 }
186 }
187
188 if ob.KeyVar != "" && ob.Key != "" {
189 ctx.WriteString(` END `)
190 }
191
192 switch ob.Order {
193 case qcode.OrderAsc:
194 ctx.WriteString(` ASC`)
195 case qcode.OrderDesc:
196 ctx.WriteString(` DESC`)
197 case qcode.OrderAscNullsFirst:
198 ctx.WriteString(` ASC NULLS FIRST`)
199 case qcode.OrderDescNullsFirst:
200 ctx.WriteString(` DESC NULLS FIRST`)
201 case qcode.OrderAscNullsLast:
202 ctx.WriteString(` ASC NULLS LAST`)
203 case qcode.OrderDescNullsLast:
204 ctx.WriteString(` DESC NULLS LAST`)

Callers

nothing calls this directly

Calls 4

WriteStringMethod · 0.65
AddParamMethod · 0.65
ColWithTableMethod · 0.65
QuoteMethod · 0.65

Tested by

no test coverage detected