-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.go
More file actions
214 lines (188 loc) · 7.55 KB
/
Copy pathadapter.go
File metadata and controls
214 lines (188 loc) · 7.55 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package sqlseeder
import (
"fmt"
"reflect"
"strings"
"github.com/tangzero/inflector"
)
// AdapterInterface defines methods for adapting data and generating SQL-related information.
type AdapterInterface interface {
// GetLastIndex returns the last index of the given data structure (slice or array).
GetLastIndex(a interface{}) int
// WrapWithSingleQoute wraps a value in single quotes (e.g., 'value').
WrapWithSingleQoute(value string) string
// ParseManyToManyColumns parses a list of many-to-many column names and returns
// a map of ManyToManyRelation structs.
// This map will hold the column name as a key and an object of ManyToManyRelation as the value.
ParseManyToManyColumns(columns []string, schemaName string, tableName string) (map[string]ManyToManyRelation, error)
// ParseManyToMany parses a single many-to-many column name and returns a
// ManyToManyRelation struct.
ParseManyToMany(columnName string, schemaName string, tableName string) (ManyToManyRelation, error)
// IsOneToMany checks if a column represents a one-to-many relationship.
IsOneToMany(columnName string) bool
// IsHashedColumn checks if a column represents a password so it should be hashed
IsHashedColumn(columnName string) bool
// GetFullTableName returns the full table name with the schema (if provided).
GetFullTableName(schemaName string, tableName string) string
// GetPrimaryKeyFromTableName generates the primary key column name from a table name.
GetPrimaryKeyFromTableName(tableName string) string
IsArrayColumn(columnName string) bool
// SplitColumnsToStatemntParts splits the columns of a row into root columns and
// many-to-many columns.
SplitColumnsToStatemntParts(row map[string]interface{}) ColumnsStatemntParts
// ParseOneToMany parses a one-to-many column name and returns an
// OneToManyRelation struct.
ParseOneToMany(columnName string, tableName string) (OneToManyRelation, error)
}
// Adapter implements the AdapterInterface.
type Adapter struct {
OneToManyDelimiter string
ManyToManyDelimiter string
}
// NewAdapter creates a new Adapter with the specified delimiters.
func NewAdapter(oneToManyDelimiter string, manyToManyDelimiter string) AdapterInterface {
return &Adapter{
OneToManyDelimiter: oneToManyDelimiter,
ManyToManyDelimiter: manyToManyDelimiter,
}
}
// IsArrayColumn checks if a column represents an array.
func (a *Adapter) IsArrayColumn(columnName string) bool {
return strings.HasSuffix(columnName, "[]")
}
// GetLastIndex returns the last index of the given data structure.
func (a *Adapter) GetLastIndex(data interface{}) int {
return reflect.ValueOf(data).Len() - 1
}
// GetFullTableName returns the full table name with the schema (if provided).
func (a *Adapter) GetFullTableName(schemaName string, tableName string) string {
name := tableName
if schemaName != "" {
name = fmt.Sprintf("%s.%s", schemaName, tableName)
}
return name
}
func (a *Adapter) IsHashedColumn(columnName string) bool {
return strings.Contains(columnName, "#") && len(strings.Split(columnName, "#")) == 2
}
// IsOneToMany checks if a column represents a one-to-many relationship.
func (a *Adapter) IsOneToMany(columnName string) bool {
return strings.Contains(columnName, a.OneToManyDelimiter) && !strings.Contains(columnName, a.ManyToManyDelimiter)
}
// WrapWithSingleQoute wraps a value in single quotes.
func (a *Adapter) WrapWithSingleQoute(value string) string {
if value == "" || value == "NULL" || value == "null" {
return "NULL"
}
if strings.Contains(value, "SELECT") {
return value
}
return fmt.Sprintf("'%s'", value)
}
// ParseManyToMany parses a many-to-many relationship column name.
//
// Formula: <joining_table_primary_key><ManyToManyDelimiter><joining_table_name><ManyToManyDelimiter><second_table_name><ManyToManyDelimiter><second_table_search_column><ManyToManyDelimiter><first_table_search_column>
// Example: tag_id***product_tags***tags***tag_name***product_product_name
// Should return:
//
// ManyToManyRelation{
// Table: "product_tags",
// FirstTable: "products", // Assuming the current table is "products"
// SecondTable: "tags",
// FirstSearchColumn: "product_name",
// SecondSearchColumn: "tag_name",
// Columns: ["product_id**products**product_name", "tag_id**tags**tag_name"],
// }
func (a *Adapter) ParseManyToMany(columnName string, schemaName string, tableName string) (ManyToManyRelation, error) {
parts := strings.Split(columnName, a.ManyToManyDelimiter)
response := ManyToManyRelation{}
if len(parts) != 5 {
return response, fmt.Errorf("not valid many to many column name: %s", columnName)
}
fullTableName := a.GetFullTableName(schemaName, tableName)
firstColumn := fmt.Sprintf("%s%s%s%s%s", a.GetPrimaryKeyFromTableName(tableName), a.OneToManyDelimiter, fullTableName, a.OneToManyDelimiter, parts[4])
secondColumn := fmt.Sprintf("%s%s%s%s%s", parts[0], a.OneToManyDelimiter, parts[2], a.OneToManyDelimiter, parts[3])
result := []string{firstColumn, secondColumn}
response = ManyToManyRelation{
Table: parts[1],
FirstTable: tableName,
SecondTable: parts[2],
FirstSearchColumn: parts[4],
SecondSearchColumn: parts[3],
Columns: result,
}
return response, nil
}
// ParseManyToManyColumns parses a list of many-to-many column names.
func (a *Adapter) ParseManyToManyColumns(columns []string, schemaName string, tableName string) (map[string]ManyToManyRelation, error) {
result := make(map[string]ManyToManyRelation)
for _, column := range columns {
row, err := a.ParseManyToMany(column, schemaName, tableName)
if err != nil {
return nil, err
}
result[column] = row
}
return result, nil
}
// ParseOneToMany parses a one-to-many relationship column name.
//
// Formula: <primary_key_column><OneToManyDelimiter><table_name><OneToManyDelimiter><search_key_column>
// Example: category_id**categories**category_name
// Should return:
//
// OneToManyRelation{
// Table: "categories",
// PrimaryKey: "category_id",
// SearchKey: "category_name",
// }
func (a *Adapter) ParseOneToMany(columnName string, tableName string) (OneToManyRelation, error) {
parts := strings.Split(columnName, a.OneToManyDelimiter)
response := OneToManyRelation{}
if len(parts) != 3 && len(parts) != 4 {
return response, fmt.Errorf("not valid one to many column name: %s", columnName)
}
if len(parts) == 3 {
response = OneToManyRelation{
ForeignKey: parts[0],
PrimaryKey: parts[0],
Table: parts[1],
SearchKey: parts[2],
}
}
if len(parts) == 4 {
response = OneToManyRelation{
ForeignKey: parts[0],
PrimaryKey: parts[1],
Table: parts[2],
SearchKey: parts[3],
}
}
return response, nil
}
// SplitColumnsToStatemntParts splits the columns of a row into root columns and many-to-many columns.
func (a *Adapter) SplitColumnsToStatemntParts(row map[string]interface{}) ColumnsStatemntParts {
manyToManyColumns := []string{}
rootColumns := []string{}
for key := range row {
if strings.Contains(key, a.ManyToManyDelimiter) {
manyToManyColumns = append(manyToManyColumns, key)
continue
}
rootColumns = append(rootColumns, key)
}
return ColumnsStatemntParts{
RootColumns: rootColumns,
ManyToManyColumns: manyToManyColumns,
}
}
// GetPrimaryKeyFromTableName generates the primary key column name from a table name
// using the formula (singular name)_id.
//
// Examples:
// - table: "products" => "product_id"
// - table: "categories" => "category_id"
func (a *Adapter) GetPrimaryKeyFromTableName(tableName string) string {
singluraizedName := inflector.Singularize(tableName)
return fmt.Sprintf("%s_id", singluraizedName)
}