Skip to content

Commit ba6eb81

Browse files
committed
feat(core): add "add" functionality
feat(frontend): fix up add functionality
1 parent 5b75050 commit ba6eb81

File tree

27 files changed

+378
-146
lines changed

27 files changed

+378
-146
lines changed

core/graph/generated.go

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/graph/model/models_gen.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/graph/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Record {
3131
input RecordInput {
3232
Key: String!
3333
Value: String!
34+
Extra: [RecordInput!]
3435
}
3536

3637
type StorageUnit {

core/graph/schema.resolvers.go

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/engine/plugin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type PluginConfig struct {
1515
type Record struct {
1616
Key string
1717
Value string
18+
Extra map[string]string
1819
}
1920

2021
type StorageUnit struct {
@@ -60,7 +61,7 @@ type PluginFunctions interface {
6061
GetStorageUnits(config *PluginConfig, schema string) ([]StorageUnit, error)
6162
AddStorageUnit(config *PluginConfig, schema string, storageUnit string, fields map[string]string) (bool, error)
6263
UpdateStorageUnit(config *PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error)
63-
AddRow(config *PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error)
64+
AddRow(config *PluginConfig, schema string, storageUnit string, values []Record) (bool, error)
6465
GetRows(config *PluginConfig, schema string, storageUnit string, where string, pageSize int, pageOffset int) (*GetRowsResult, error)
6566
GetGraph(config *PluginConfig, schema string) ([]GraphUnit, error)
6667
RawExecute(config *PluginConfig, query string) (*GetRowsResult, error)

core/src/plugins/common/common.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

core/src/plugins/elasticsearch/add.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ func (p *ElasticSearchPlugin) AddStorageUnit(config *engine.PluginConfig, schema
2828
return true, nil
2929
}
3030

31-
func (p *ElasticSearchPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error) {
31+
func (p *ElasticSearchPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (bool, error) {
3232
client, err := DB(config)
3333
if err != nil {
3434
return false, err
3535
}
3636

37-
documentBytes, err := json.Marshal(values)
37+
jsonValue := map[string]string{}
38+
for _, value := range values {
39+
jsonValue[value.Key] = value.Value
40+
}
41+
42+
documentBytes, err := json.Marshal(jsonValue)
3843
if err != nil {
3944
return false, fmt.Errorf("error marshaling document to JSON: %v", err)
4045
}

core/src/plugins/mongodb/add.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66

77
"github.com/clidey/whodb/core/src/engine"
8+
"go.mongodb.org/mongo-driver/bson"
89
"go.mongodb.org/mongo-driver/mongo"
910
)
1011

@@ -25,7 +26,7 @@ func (p *MongoDBPlugin) AddStorageUnit(config *engine.PluginConfig, schema strin
2526
return true, nil
2627
}
2728

28-
func (p *MongoDBPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error) {
29+
func (p *MongoDBPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (bool, error) {
2930
client, err := DB(config)
3031
if err != nil {
3132
return false, err
@@ -35,8 +36,8 @@ func (p *MongoDBPlugin) AddRow(config *engine.PluginConfig, schema string, stora
3536
collection := client.Database(schema).Collection(storageUnit)
3637

3738
document := make(map[string]interface{})
38-
for k, v := range values {
39-
document[k] = v
39+
for _, value := range values {
40+
document[value.Key] = value.Value
4041
}
4142

4243
_, err = collection.InsertOne(context.Background(), document)
@@ -48,7 +49,7 @@ func (p *MongoDBPlugin) AddRow(config *engine.PluginConfig, schema string, stora
4849
}
4950

5051
func createCollectionIfNotExists(database *mongo.Database, collectionName string) error {
51-
collections, err := database.ListCollectionNames(context.Background(), nil)
52+
collections, err := database.ListCollectionNames(context.Background(), bson.D{})
5253
if err != nil {
5354
return err
5455
}

core/src/plugins/mysql/add.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,24 @@ func (p *MySQLPlugin) AddStorageUnit(config *engine.PluginConfig, schema string,
2020
}
2121
defer sqlDb.Close()
2222

23+
if len(fields) == 0 {
24+
return false, errors.New("no fields provided for table creation")
25+
}
26+
2327
var columns []string
2428
for field, fieldType := range fields {
25-
columns = append(columns, fmt.Sprintf("%s %s", field, fieldType))
29+
columns = append(columns, fmt.Sprintf("`%s` %s", field, fieldType))
2630
}
2731

28-
createTableSQL := fmt.Sprintf("CREATE TABLE %s.%s (%s);", schema, storageUnit, strings.Join(columns, ", "))
32+
createTableSQL := fmt.Sprintf("CREATE TABLE `%s`.`%s` (%s);", schema, storageUnit, strings.Join(columns, ", "))
2933

3034
if err := db.Exec(createTableSQL).Error; err != nil {
3135
return false, err
3236
}
3337

3438
return true, nil
3539
}
36-
37-
func (p *MySQLPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error) {
40+
func (p *MySQLPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (bool, error) {
3841
db, err := DB(config)
3942
if err != nil {
4043
return false, err
@@ -54,14 +57,23 @@ func (p *MySQLPlugin) AddRow(config *engine.PluginConfig, schema string, storage
5457
placeholders := make([]string, 0, len(values))
5558
args := make([]interface{}, 0, len(values))
5659

57-
for column, value := range values {
58-
columns = append(columns, column)
59-
placeholders = append(placeholders, "?")
60-
args = append(args, value)
60+
for _, value := range values {
61+
columns = append(columns, fmt.Sprintf("`%s`", value.Key))
62+
if value.Extra["Config"] == "sql" {
63+
placeholders = append(placeholders, value.Value)
64+
} else {
65+
switch value.Extra["Type"] {
66+
case "VARCHAR", "CHAR", "TEXT":
67+
placeholders = append(placeholders, "?")
68+
default:
69+
placeholders = append(placeholders, fmt.Sprintf("CAST(? AS %v)", value.Extra["Type"]))
70+
}
71+
args = append(args, value.Value)
72+
}
6173
}
6274

6375
insertSQL := fmt.Sprintf(
64-
"INSERT INTO %s.%s (%s) VALUES (%s);",
76+
"INSERT INTO `%s`.`%s` (%s) VALUES (%s);",
6577
schema, storageUnit,
6678
strings.Join(columns, ", "),
6779
strings.Join(placeholders, ", "),

core/src/plugins/mysql/mysql.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"log"
88

99
"github.com/clidey/whodb/core/src/engine"
10-
"github.com/clidey/whodb/core/src/plugins/common"
1110
"gorm.io/gorm"
1211
)
1312

@@ -138,10 +137,6 @@ func getTableSchema(db *gorm.DB, schema string) (map[string][]engine.Record, err
138137
}
139138

140139
func (p *MySQLPlugin) GetRows(config *engine.PluginConfig, schema string, storageUnit string, where string, pageSize int, pageOffset int) (*engine.GetRowsResult, error) {
141-
if !common.IsValidSQLTableName(storageUnit) {
142-
return nil, errors.New("invalid table name")
143-
}
144-
145140
query := fmt.Sprintf("SELECT * FROM `%v`.`%s`", schema, storageUnit)
146141
if len(where) > 0 {
147142
query = fmt.Sprintf("%v WHERE %v", query, where)

core/src/plugins/postgres/add.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (p *PostgresPlugin) AddStorageUnit(config *engine.PluginConfig, schema stri
3434
return true, nil
3535
}
3636

37-
func (p *PostgresPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error) {
37+
func (p *PostgresPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (bool, error) {
3838
db, err := DB(config)
3939
if err != nil {
4040
return false, err
@@ -54,10 +54,14 @@ func (p *PostgresPlugin) AddRow(config *engine.PluginConfig, schema string, stor
5454
placeholders := make([]string, 0, len(values))
5555
args := make([]interface{}, 0, len(values))
5656

57-
for column, value := range values {
58-
columns = append(columns, column)
59-
placeholders = append(placeholders, "?")
60-
args = append(args, value)
57+
for _, value := range values {
58+
columns = append(columns, value.Key)
59+
if value.Extra["Config"] == "sql" {
60+
placeholders = append(placeholders, value.Value)
61+
} else {
62+
placeholders = append(placeholders, fmt.Sprintf("?::%v", value.Extra["Type"]))
63+
args = append(args, value.Value)
64+
}
6165
}
6266

6367
insertSQL := fmt.Sprintf(

core/src/plugins/postgres/postgres.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"log"
88

99
"github.com/clidey/whodb/core/src/engine"
10-
"github.com/clidey/whodb/core/src/plugins/common"
1110
"gorm.io/gorm"
1211
)
1312

@@ -144,10 +143,6 @@ func getTableSchema(db *gorm.DB, schema string) (map[string][]engine.Record, err
144143
}
145144

146145
func (p *PostgresPlugin) GetRows(config *engine.PluginConfig, schema string, storageUnit string, where string, pageSize int, pageOffset int) (*engine.GetRowsResult, error) {
147-
if !common.IsValidSQLTableName(storageUnit) {
148-
return nil, errors.New("invalid table name")
149-
}
150-
151146
query := fmt.Sprintf("SELECT * FROM \"%v\".\"%s\"", schema, storageUnit)
152147
if len(where) > 0 {
153148
query = fmt.Sprintf("%v WHERE %v", query, where)

core/src/plugins/redis/add.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (p *RedisPlugin) AddStorageUnit(config *engine.PluginConfig, schema string,
6666
return true, nil
6767
}
6868

69-
func (p *RedisPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error) {
69+
func (p *RedisPlugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (bool, error) {
7070
client, err := DB(config)
7171
if err != nil {
7272
return false, err
@@ -86,24 +86,24 @@ func (p *RedisPlugin) AddRow(config *engine.PluginConfig, schema string, storage
8686
switch keyType {
8787
case "hash":
8888
hashFieldsInterface := make(map[string]interface{}, len(values))
89-
for k, v := range values {
90-
hashFieldsInterface[k] = v
89+
for _, value := range values {
90+
hashFieldsInterface[value.Key] = value.Value
9191
}
9292
if err := client.HMSet(ctx, storageUnit, hashFieldsInterface).Err(); err != nil {
9393
return false, err
9494
}
9595
case "list":
9696
listValuesInterface := make([]interface{}, len(values))
97-
for _, v := range values {
98-
listValuesInterface = append(listValuesInterface, v)
97+
for _, value := range values {
98+
listValuesInterface = append(listValuesInterface, value.Value)
9999
}
100100
if err := client.RPush(ctx, storageUnit, listValuesInterface...).Err(); err != nil {
101101
return false, err
102102
}
103103
case "set":
104104
setValuesInterface := make([]interface{}, len(values))
105-
for _, v := range values {
106-
setValuesInterface = append(setValuesInterface, v)
105+
for _, value := range values {
106+
setValuesInterface = append(setValuesInterface, value.Value)
107107
}
108108
if err := client.SAdd(ctx, storageUnit, setValuesInterface...).Err(); err != nil {
109109
return false, err

core/src/plugins/sqlite3/add.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (p *Sqlite3Plugin) AddStorageUnit(config *engine.PluginConfig, schema strin
3838
return true, nil
3939
}
4040

41-
func (p *Sqlite3Plugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values map[string]string) (bool, error) {
41+
func (p *Sqlite3Plugin) AddRow(config *engine.PluginConfig, schema string, storageUnit string, values []engine.Record) (bool, error) {
4242
db, err := DB(config)
4343
if err != nil {
4444
return false, err
@@ -58,10 +58,14 @@ func (p *Sqlite3Plugin) AddRow(config *engine.PluginConfig, schema string, stora
5858
placeholders := make([]string, 0, len(values))
5959
args := make([]interface{}, 0, len(values))
6060

61-
for column, value := range values {
62-
columns = append(columns, column)
63-
placeholders = append(placeholders, "?")
64-
args = append(args, value)
61+
for _, value := range values {
62+
columns = append(columns, value.Key)
63+
if value.Extra["Config"] == "sql" {
64+
placeholders = append(placeholders, value.Value)
65+
} else {
66+
placeholders = append(placeholders, fmt.Sprintf("CAST(? AS %v)", value.Extra["Type"]))
67+
args = append(args, value.Value)
68+
}
6569
}
6670

6771
insertSQL := fmt.Sprintf(

core/src/plugins/sqlite3/db.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ var errDoesNotExist = errors.New("unauthorized or the database doesn't exist")
2323

2424
func DB(config *engine.PluginConfig) (*gorm.DB, error) {
2525
database := config.Credentials.Database
26-
if !isValidDatabaseFileName(database) {
27-
return nil, errDoesNotExist
28-
}
2926
fileNameDatabase := filepath.Join(getDefaultDirectory(), database)
3027
if _, err := os.Stat(fileNameDatabase); errors.Is(err, os.ErrNotExist) {
3128
return nil, errDoesNotExist

core/src/plugins/sqlite3/sqlite3.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99

1010
"github.com/clidey/whodb/core/src/engine"
11-
"github.com/clidey/whodb/core/src/plugins/common"
1211
"gorm.io/gorm"
1312
)
1413

@@ -138,10 +137,6 @@ func getTableSchema(db *gorm.DB) (map[string][]engine.Record, error) {
138137
}
139138

140139
func (p *Sqlite3Plugin) GetRows(config *engine.PluginConfig, schema string, storageUnit string, where string, pageSize int, pageOffset int) (*engine.GetRowsResult, error) {
141-
if !common.IsValidSQLTableName(storageUnit) {
142-
return nil, errors.New("invalid table name")
143-
}
144-
145140
query := fmt.Sprintf("SELECT * FROM \"%s\"", storageUnit)
146141
if len(where) > 0 {
147142
query = fmt.Sprintf("%v WHERE %v", query, where)

core/src/plugins/sqlite3/utils.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)