Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ru51a4 committed Jun 9, 2024
0 parents commit dfa9ade
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/mymodule

go 1.21.6
87 changes: 87 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"fmt"
)

type node struct {
token string
next *node
nextnext *node
}

func lexer(str string) *node {
var s string
var r = &node{}
res := r
var stack []*node

for i := 0; i < len(str); i++ {
if string(str[i]) == "(" {
_t := node{
token: s,
}
s = ""
t := node{
nextnext: &_t,
}
r.next = &t
r = &_t
stack = append(stack, &t)
} else if string(str[i]) == ")" {
r.next = &node{
token: s,
}
s = ""
r = stack[len(stack)-1]
stack = stack[:len(stack)-1]
} else if string(str[i]) == " " {
r.next = &node{
token: s,
}
r = r.next
s = ""
} else {
s = s + string(str[i])
}
}
return res
}

type Query struct {
tokens *string
alias string
}

func subQuery(_node *node) []*Query {
var res []*Query
var deep func(node *node, alias string)
deep = func(node *node, alias string) {
str := ""
query := Query{
tokens: &str,
alias: alias,
}
res = append(res, &query)
t := node
for t.next != nil {
if t.nextnext != nil {
deep(t.nextnext, t.next.next.token)
str += "( " + t.next.next.token + " )"
t = t.next.next.next
} else {
str += t.token + " "
t = t.next
}

}
}
deep(_node.next.nextnext, "main")
return res
}
func main() {
sql := "(SELECT * FROM diaries a JOIN (SELECT diary_id as di, id as kek FROM posts p group by diary_id ) gg ON a.id = gg.di ORDER BY gg.kek desc)"
a := lexer(sql)
b := subQuery(a)
fmt.Print(b)
}

0 comments on commit dfa9ade

Please sign in to comment.