-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
170 lines (126 loc) · 4.11 KB
/
add.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
package add
import (
"github.com/macinnir/dvc/core/lib"
"go.uber.org/zap"
)
const CommandName = "add"
// Cmd adds an object to the database
func Cmd(log *zap.Logger, config *lib.Config, args []string) error {
return nil
}
// database := lib.LoadSchema(config)
// reader := bufio.NewReader(os.Stdin)
// sqlParts := []string{}
// sql := ""
// tableName := ""
// if len(args) > 0 {
// tableName = args[0]
// } else {
// tableName = lib.ReadCliInput(reader, "Table Name:")
// }
// // Creating a new table
// if _, ok := database.Tables[tableName]; !ok {
// log.Infof("Create table `%s`", tableName)
// start := fmt.Sprintf("CREATE TABLE `%s` (", tableName)
// sql += start
// sqlParts = append(sqlParts, start)
// cols := []string{
// fmt.Sprintf("%sID INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT", tableName),
// fmt.Sprintf("IsDeleted TINYINT UNSIGNED NOT NULL DEFAULT 0"),
// fmt.Sprintf("DateCreated BIGINT UNSIGNED NOT NULL DEFAULT 0"),
// fmt.Sprintf("LastUpdated BIGINT UNSIGNED NOT NULL DEFAULT 0"),
// }
// for k := range cols {
// fmt.Printf("`%s`.Column(%d): %s\n", tableName, k+1, cols[k])
// }
// includeColumns := lib.ReadCliInput(reader, fmt.Sprintf("Include the above columns (Y/n)?"))
// n := 1
// if includeColumns == "Y" {
// n = len(cols) + 1
// } else {
// cols = []string{}
// }
// for {
// col := lib.ReadCliInput(reader, fmt.Sprintf("`%s`.Column(%d):", tableName, n))
// if len(col) == 0 {
// break
// }
// // Remove trailing space
// col = strings.Trim(col, " ")
// // Remove trailing comma if exists
// if col[len(col)-1:] == "," {
// col = col[0 : len(col)-1]
// }
// colParts := strings.Split(col, " ")
// // Validate the sql part
// if len(colParts) < 2 {
// log.Error("Invalid sql part. Try again...")
// continue
// }
// // Validate the datatype
// dataType := strings.ToLower(colParts[1])
// if strings.Contains(dataType, "(") {
// dataType = strings.Split(dataType, "(")[0]
// }
// if lib.IsValidSQLType(dataType) == false {
// log.Errorf("Invalid SQL type: %s. Try gain...\n", dataType)
// continue
// }
// n++
// for k := range colParts {
// if k == 0 {
// continue
// }
// colParts[k] = strings.ToUpper(colParts[k])
// }
// col = "`" + colParts[0] + "` " + strings.Join(colParts[1:], " ")
// sqlParts = append(sqlParts, col)
// cols = append(cols, col)
// }
// sql += strings.Join(cols, ", ") + ")"
// sqlParts = append(sqlParts, ")")
// } else {
// col := lib.ReadCliInput(reader, fmt.Sprintf("`%s`.Column:", tableName))
// if len(col) == 0 {
// log.Error("Column cannot be empty")
// return
// }
// colParts := strings.Split(col, " ")
// if _, ok := database.Tables[tableName].Columns[colParts[0]]; ok {
// fmt.Printf("Column `%s`.`%s` already exists", tableName, colParts[0])
// return
// }
// for k := range colParts {
// if k == 0 {
// continue
// }
// colParts[k] = strings.ToUpper(colParts[k])
// }
// col = "`" + colParts[0] + "` " + strings.Join(colParts[1:], " ")
// sql = fmt.Sprintf("ALTER TABLE `%s` ADD COLUMN %s", tableName, col)
// sqlParts = append(sqlParts, sql)
// }
// fmt.Print("\n------------------------------------------------------\n")
// fmt.Print("--------------------- REVIEW -------------------------\n")
// fmt.Print("------------------------------------------------------\n")
// fmt.Println(sql)
// // for k := range sqlParts {
// // // Not first or last line
// // if k != 0 && k != len(sqlParts)-1 {
// // fmt.Print("\t")
// // }
// // fmt.Printf("%s", sqlParts[k])
// // // Not first or last line
// // if k != 0 && k != len(sqlParts)-1 {
// // fmt.Print(",")
// // }
// // fmt.Print("\n")
// // }
// fmt.Print("\n------------------------------------------------------\n")
// if lib.ReadCliInput(reader, "Are you sure want to execute the above SQL (Y/n)?") == "Y" {
// connector, _ := lib.(config.DatabaseType, c.Config)
// x := lib.NewExecutor(c.Config, connector)
// x.RunSQL(sql)
// c.Refresh([]string{})
// }
// }