-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
185 lines (146 loc) · 4.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
73
74
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
119
120
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
156
157
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
package main
import (
_ "expvar"
"fmt"
"log"
"net/http"
"os"
"strings"
"html/template"
)
var Port string
func init() {
Port = os.Getenv("PORT")
if Port == "" {
Port = "9876"
}
}
type HTMLPage struct {
Title string
URL string
}
//calculating collection name, queryType, remaining string
func calculateCollectionName(dbQuery string) (string, string, string) {
var queryType, rem string
fmt.Println("dbQuery > ", dbQuery)
a := strings.Split(dbQuery, ".")
fmt.Println("a : > ", a)
a2 := strings.Split(a[1], ".")
fmt.Println("a : > ", a2)
a3 := strings.Split(a[2], "(")
fmt.Println("a : > ", a3)
a4 := strings.Split(a3[1], "{")
fmt.Println("a : > ", a4)
a5 := strings.Split(a4[0], "}")
fmt.Println("a : > ", a5)
//getting collection Name
queryType = strings.Title(a3[0])
rem = a3[1]
//if rem contains array //append []int
if strings.Contains(rem, "[") {
}
//return collectionName, QueryType
return a2[0], queryType, rem
}
func calculateMonGoQuery(dbName, dbQuery string) (string, error) {
//Possible Queries
//1. Inserting Documents
//MongoEx
//Input :
// 1. Db Name
// 2. Query : db.ships.insert({name:'USS Enterprise-D',operator:'Starfleet',type:'Explorer',class:'Galaxy',crew:750,codes:[10,11,12]})
//GoEx
//Output : session.DB("Test").C("ships").Insert({Name: "USS Enterprise-D", Operator: "Starfleet", Type: "Explorer", Class: "Galaxy", Crew : 750, Codes: []int{10, 11, 12}}
//getting collectionName, queryType and remaining string to be appended in answer
collectionName, queryType, rem := calculateCollectionName(dbQuery)
outputString := "session.DB(\"" + dbName + "\").C(\"" + collectionName + "\")." + queryType + "(" + rem
return outputString, nil
//2. Finding Documents
//3. Finding Documents using Operators
//3.1 $gt / $gte || $lt / $lte - greater than / greater than equals , lesser than / lesser than equals
//3.2 $exists - does an attribute exist or not
//3.3 $regex - Perl-style pattern matching
//3.4 $type - search by type of an element
//4. Updating Documents
//5. Removing Documents
//6. Working with Indexes
//7. Pipeline Stages
//7.1 $project
//7.2 $match
//7.3 $group
//7.4 $sort
//7.5 $skip
//7.6 $limit
//7.7 $unwind
//8. Aggregation Expressions
//8.1 $sum
//8.2 $avg
//8.3 $min / $max
//8.4 $push
//8.5 $addToSet
//8.6 $first / $last
}
func Convert(w http.ResponseWriter, r *http.Request) {
//fmt.Println("inside convert ")
//fmt.Println("r >", r.Method)
var dbName, dbQuery, generateButton string
if r.Method == "GET" {
template := template.Must(template.ParseFiles("index.html"))
myvar := HTMLPage{Title:"Query Translater", URL: "https://cryptic-ravine-58923.herokuapp.com/"}
e := template.ExecuteTemplate(w, "index.html", myvar)
if e != nil {
fmt.Println("ewewewewewe getting error", e.Error())
}
} else {
// Form submitted
r.ParseMultipartForm(32 << 20)
//validation : len(r.Form["username"][0])
dbName = r.FormValue("dbName")
if len(dbName) == 0 {
fmt.Fprint(w, "dbName cannot be empty")
os.Exit(0)
}
fmt.Fprintln(w, "Entered dbName is :", dbName)
dbQuery = r.FormValue("dbQuery")
fmt.Fprintln(w, "Entered query is :", dbQuery)
ans, err := calculateMonGoQuery(dbName, dbQuery)
if err != nil {
log.Println("calculateMonGoQuery Error", err.Error())
}
fmt.Fprint(w, "outputString > "+ans)
//POST : generate button action
generateButton = r.FormValue("generate")
//check if button is pressed
if len(generateButton) != 0 {
ans, err := calculateMonGoQuery(dbName, dbQuery)
if err != nil {
log.Println("calculateMonGoQuery Error", err.Error())
}
fmt.Fprint(w, ans)
} else {
fmt.Println("generatebutton is not pressed!")
//fmt.lo(w , "generatebutton is not pressed!")
}
}
}
func handler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
var dbName, dbQuery string
// Form submitted
r.ParseMultipartForm(32 << 20)
//validation : len(r.Form["username"][0])
dbName = r.FormValue("dbName")
if len(dbName) == 0 {
fmt.Fprint(w, "dbName cannot be empty")
os.Exit(0)
}
fmt.Fprint(w, "Entered dbName is :", dbName)
dbQuery = r.FormValue("dbQuery")
fmt.Fprint(w, "Entered query is :", dbQuery)
}
func main() {
http.HandleFunc("/", Convert)
//http.HandleFunc("/tt", handler)
log.Println("Listening....on port: " + Port)
http.ListenAndServe(":"+Port, nil)
}