-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbutils.go
53 lines (46 loc) · 1.25 KB
/
dbutils.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
package main
import (
"context"
"os"
"time"
"github.com/jackc/pgx/v5"
)
func prepareDBConn(ctx context.Context) (tx pgx.Tx, conn *pgx.Conn, config *pgx.ConnConfig, message string, err error) {
config, err = pgx.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
message = "Error parsing database URL"
return
}
// Since neondb doesn't support prepared statements, we need to use simple protocol
config.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
// Connect to DB
conn, err = pgx.ConnectConfig(ctx, config)
if err != nil {
message = "Error connecting to database"
return
}
// Start transaction
tx, err = conn.Begin(ctx)
if err != nil {
message = "Error starting transaction"
return
}
return
}
// Helper function to convert time.Time into microseconds since midnight
func toMicrosSinceMidnight(t time.Time) int64 {
hour, min, sec := t.Clock()
return int64(hour)*int64(time.Hour/time.Microsecond) +
int64(min)*int64(time.Minute/time.Microsecond) +
int64(sec)*int64(time.Second/time.Microsecond) +
int64(t.Nanosecond())/int64(time.Microsecond)
}
func filterEmptyStrings(input []string) []string {
var result []string
for _, str := range input {
if str != "" {
result = append(result, str)
}
}
return result
}