Skip to content
This repository was archived by the owner on Nov 16, 2022. It is now read-only.

Go v2 #81

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More around Tuples
  • Loading branch information
jpopadak committed Feb 1, 2021
commit 71a8d45ef92a888ca226be1ff3500a407bdd9cfd
6 changes: 6 additions & 0 deletions expression.go2
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ type NumberExpression[T numeric] interface {
NotIn(v ...TypedExpression[T]) NumberExpression[T]
}

type FactoryExpression[T any] interface {
TypedExpression[T]
Args() []Expression
New() T
}

// =======================================================
// Implementations

Expand Down
78 changes: 59 additions & 19 deletions queries.go2
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
)

type WhereClause[T any] interface {
Where(e ...PredicateExpression) WhereClause[T]
Where(e ...PredicateExpression) T
}

type SimpleQuery[T any] interface {
WhereClause[T]
Distinct() SimpleQuery[T]
Limit(v int64) SimpleQuery[T]
Offset(v int64) SimpleQuery[T]
Restrict(m QueryModifiers) SimpleQuery[T]
OrderBy(o ...Order) SimpleQuery[T]
Set(e ParamExpression, value interface{}) SimpleQuery[T]
Distinct() T
Limit(v int64) T
Offset(v int64) T
Restrict(m QueryModifiers) T
OrderBy(o ...Order) T
Set(e ParamExpression, value interface{}) T
}

type Query[T any] interface {
SimpleQuery[T]
GroupBy(e ...Expression) Query[T]
Having(e ...PredicateExpression) Query[T]
GroupBy(e ...Expression) T
Having(e ...PredicateExpression) T
}

type Fetchable[T any] interface {
Expand All @@ -36,18 +36,50 @@ type Fetchable[T any] interface {
FetchOneOnly(ctx context.Context) (T, error)
// TODO - FetchPage(ctx context.Context, limit, offset int64) (T, error)
}

func Select[T, Q any](exp TypedExpression[T]) FetchableQuery[T, Q] {
return nil
}

type FetchableQuery[T, Q any] interface {
Fetchable[T]
// Fetchable[T]
SimpleQuery[Q]
// Select(exp TypedExpression[T]) FetchableQuery[T]
// SelectMany(exp []Expression) FetchableQuery[Tuple?]
}

func SelectColumn[T any](exp TypedExpression[T]) SimpleQuery[T] {
q := SQLQuery[TypedExpression[T]]{}
md := &BasicQueryMetadata{}
q.mixin = NewQueryMixin(q, md, true)
q.mixin.SetProjection(exp)
return q
}
type SQLQuery[T any] struct {
mixin QueryMixin[T]
}
func (q SQLQuery[T]) Distinct() T {
return q.mixin.SetDistinct(true)
}
func (q SQLQuery[T]) Limit(v int64) T {
return q.mixin.Limit(v)
}
func (q SQLQuery[T]) Offset(v int64) T {
return q.mixin.Offset(v)
}
func (q SQLQuery[T]) Restrict(m QueryModifiers) T {
return q.mixin.Restrict(m)
}
func (q SQLQuery[T]) OrderBy(o ...Order) T {
return q.mixin.OrderBy(o...)
}
func (q SQLQuery[T]) Set(e ParamExpression, value interface{}) T {
return q.mixin.SetParam(e, value)
}
func (q SQLQuery[T]) Where(p ...PredicateExpression) T {
return q.mixin.Where(p...)
}






type SubQueryExpression interface {
Expression
Metadata() QueryMetadata
Expand Down Expand Up @@ -237,6 +269,12 @@ const (



func NewQueryMixin2[T any](qm QueryMetadata, expandPaths bool) QueryMixin[T] {
return QueryMixin[T]{
qm: qm,
expandPaths: expandPaths,
}
}

func NewQueryMixin[T any](self T, qm QueryMetadata, expandPaths bool) QueryMixin[T] {
return QueryMixin[T]{
Expand Down Expand Up @@ -267,9 +305,10 @@ func (q QueryMixin[T]) AddJoinFlag(jf JoinFlag) T {
// return q.self
// }

// func (q QueryMixin[T]) SetProjection(e Expression) Expression {
//
// }
func (q QueryMixin[T]) SetProjection(e Expression) Expression {
q.qm.SetProjection(e)
return e
}

func (q QueryMixin[T]) Distinct() T {
q.qm.SetDistinct(true)
Expand Down Expand Up @@ -395,8 +434,9 @@ func (q QueryMixin[T]) SetParam(e ParamExpression, v interface{}) T {
return q.self
}

func (q QueryMixin[T]) SetDistinct(d bool) {
func (q QueryMixin[T]) SetDistinct(d bool) T {
q.qm.SetDistinct(d)
return q.self
}
func (q QueryMixin[T]) SetSelf(s T) {
q.self = s
Expand Down
42 changes: 39 additions & 3 deletions tuple.go2
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
package lingo

type binding struct {
exp Expression
pos int
}

type Tuple struct {
values []interface{}
values []Expression
bindings []binding
}

func NewTuple(args []Expression) Tuple {
values := make([]Expression, len(args))
copy(values, args)
return Tuple{
values: values,
bindings: createBindings(args),
}
}

func createBindings(args []Expression) []binding {
// might need to change this to a map from `binding`
var bindings []binding
for idx, arg := range args {
// if its an alias, include the alias name
if op, ok := arg.(Operation); ok {
if op.Operator() == OpAlias {
bindings = append(bindings, binding{
exp: op.Arg(1),
pos: idx,
})
}
}
bindings = append(bindings, binding{
exp: arg,
pos: idx,
})
}
return bindings
}

func NewTuple(args []interface{}) {

func (t Tuple) Args() []Expression {
return t.values
}