-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
181 lines (156 loc) · 4.65 KB
/
database.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
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
type EncProc struct {
ID string
Pk []byte
Params string
}
type EncProcModel struct {
DB *sql.DB
}
// initializeTables creates the required tables if they do not exist.
func (m *EncProcModel) initializeTables() {
// Table: AggregationParams
aggregationParamsQuery := `
CREATE TABLE IF NOT EXISTS AggregationParams (
id VARCHAR(255),
pk MEDIUMBLOB,
params VARCHAR(255),
PRIMARY KEY (id)
);`
_, err := m.DB.Exec(aggregationParamsQuery)
if err != nil {
log.Fatalf("Failed to create AggregationParams table: %v\n", err)
}
// Table: Aggregation
aggregationQuery := `
CREATE TABLE IF NOT EXISTS Aggregation (
id VARCHAR(255) NOT NULL,
ct_aggr MEDIUMBLOB NOT NULL,
sample_size INT NOT NULL DEFAULT 0,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);`
_, err = m.DB.Exec(aggregationQuery)
if err != nil {
log.Fatalf("Failed to create Aggregation table: %v\n", err)
}
log.Println("Database tables initialized successfully")
}
// InsertAggregationParams inserts a new entry into the AggregationParams table.
func (m *EncProcModel) InsertAggregationParams(id string, pk []byte, params string) error {
query := "INSERT INTO AggregationParams (id, pk, params) VALUES (?, ?, ?)"
_, err := m.DB.Exec(query, id, pk, params)
return err
}
// UpdateAggregationParams updates an existing entry in the AggregationParams table.
func (m *EncProcModel) UpdateAggregationParams(id string, pk []byte, params string) error {
query := "UPDATE AggregationParams SET pk = ?, params = ? WHERE id = ?"
result, err := m.DB.Exec(query, pk, params, id)
if err != nil {
return err
}
// Check if any rows were affected
_, err = result.RowsAffected()
if err != nil {
return err
}
/*
if rowsAffected == 0 {
return fmt.Errorf("No entry found with ID: %s", id)
}
*/
return nil
}
// DeleteAggregationParams deletes an entry from the AggregationParams table by ID.
func (m *EncProcModel) DeleteAggregationParams(id string) error {
query := "DELETE FROM AggregationParams WHERE id = ?"
_, err := m.DB.Exec(query, id)
return err
}
// GetAggregationParamsByID retrieves an entry from the AggregationParams table by its ID.
func (m *EncProcModel) GetAggregationParamsByID(id string) (string, []byte, string, error) {
query := "SELECT id, pk, params FROM AggregationParams WHERE id = ?"
// Define variables to hold the retrieved data
var retrievedID string
var pk []byte
var params string
// Execute the query
err := m.DB.QueryRow(query, id).Scan(&retrievedID, &pk, ¶ms)
if err != nil {
if err == sql.ErrNoRows {
return "", nil, "", fmt.Errorf("no entry found for ID: %s", id)
}
return "", nil, "", err
}
return retrievedID, pk, params, nil
}
// IDexists checks whether the given id exists in the AggregationParams table.
func (m *EncProcModel) IDexists(id string) (bool, error) {
query := "SELECT 1 FROM AggregationParams WHERE id = ? LIMIT 1"
var exists int
err := m.DB.QueryRow(query, id).Scan(&exists)
if err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, err
}
return true, nil
}
// InsertAggregation inserts a new entry into the Aggregation table.
func (m *EncProcModel) InsertAggregation(id string, ctAggr []byte, sampleSize int) error {
query := "INSERT INTO Aggregation (id, ct_aggr, sample_size) VALUES (?, ?, ?)"
_, err := m.DB.Exec(query, id, ctAggr, sampleSize)
return err
}
// GetAggregationsByID retrieves all entries from the Aggregation table with the given ID.
func (m *EncProcModel) GetAggregationsByID(id string) ([]struct {
ID string
CtAggr []byte
SampleSize int
CreatedAt string
}, error) {
query := "SELECT id, ct_aggr, sample_size, created_at FROM Aggregation WHERE id = ?"
// Slice to hold results
var aggregations []struct {
ID string
CtAggr []byte
SampleSize int
CreatedAt string
}
// Execute the query
rows, err := m.DB.Query(query, id)
if err != nil {
return nil, err
}
defer rows.Close()
// Iterate through the result set
for rows.Next() {
var aggregation struct {
ID string
CtAggr []byte
SampleSize int
CreatedAt string
}
err := rows.Scan(&aggregation.ID, &aggregation.CtAggr, &aggregation.SampleSize, &aggregation.CreatedAt)
if err != nil {
return nil, err
}
aggregations = append(aggregations, aggregation)
}
if err = rows.Err(); err != nil {
return nil, err
}
return aggregations, nil
}
// DeleteAggregation deletes an entry from the Aggregation table by ID.
func (m *EncProcModel) DeleteAggregation(id string) error {
query := "DELETE FROM Aggregation WHERE id = ?"
_, err := m.DB.Exec(query, id)
return err
}