forked from bokwoon95/go-structured-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.txt
69 lines (64 loc) · 3.1 KB
/
todo.txt
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
Additional Features that don't break API
----------------------------------------
- Query compiling + argument rebinding
- Custom loggers with structured logging support
- Fleshed out JSON support
- Migrate to pgx
Queries can be precompiled to avoid the paying the price of string serialization over and over
- Clauses can be added
- Clauses can be removed
- Clauses can be replaced (all or nothing, you can't modify just part of a clause)
- This could be achieved by noting down the index where the each clause begins and ends
i.e. adding clause == inserting a string after where the previous clause ends
removing clause == deleting the substring from start index to end index
replacing clause == deleting substring and reinserting a new string
The index of where the clause args begin and end need to be noted down too
- Let users insert dummy placeholder arguments marked with a string name, then they can rebind those arguments later for that string name
q := Select(tbl.col1, tbl.col2).From(tbl).Where(tbl.col1.Eq(sq.Placeholder("to_be_added_later"))).Compile()
q.Bind("to_be_added_later", someVar)
q.SelectRowx(user.RowMapper()).Fetch(db)
When I added WithLogFunc I saw the time taken for a query (TestSelect_Fetch/Accumulator) increase by 500% (tested multiple times, result was always in that ballpark). That scared me off and I promptly reverted all LogFunc changes.
In the meantime I've removed custom logging altogether so that I can add it back in a more performant and structured logging way in the future.
type LogInfo struct {
LogFlag LogFlag
LogSkip int // in case someone needs to hook up to logger.Output
Query string
Args []interface{}
Action LogAction // One of: ToSQL, Fetch or Exec
TimeTaken time.Duration
// Fetch
RowsFetched int64
result string // string containing the results
// Exec
ExecFlag ExecFlag
RowsAffected int64
LastInsertID int64 // mysql only
}
type LogFunc(LogInfo)
WithLogFunc(fn).Select(x, y, z)
Prejoined tables
- Extract table joining into its own function so that you can reuse the same JOINs everywhere
- A PrejoinedTable takes the place of From (Fromx) in a SelectQuery, and Using (Usingx) in an INSERT/DELETE query
- You can add join new tables onto PrejoinedTables and it will figure out the correct set of JOINs accordingly
type JOIN_USERS_ALL struct {
sq.JoinedTables
U tables.TABLE_USERS
UR tables.TABLE_USER_ROLES
URS tables.TABLE_USER_ROLES_STUDENTS
}
func USERS_ALL() JOIN_USERS_ALL {
join := JOIN_USERS_ALL{}
join.U := tables.USERS().As("u")
join.UR := tables.USERS_ROLES().As("ur")
join.URS := tables.USERS_ROLES_STUDENTS().As("urs")
join.JoinTables = sq.
From(join.U).
Join(join.UR, join.U.USER_ID.Eq(join.UR.USER_ID)).
Join(join.URS, join.URS.USER_ROLE_ID.Eq(join.UR.USER_ROLE_ID))
return join
}
ua := USERS_ALL()
err := sq.Fromx(ua).Where(ua.URS.TEAM_ID.EqInt(15)).SelectRowx(func(row *sq.Row) {
user.UserID = row.Int(ua.U.USER_ID)
user.TeamID = row.Int(ua.URS.TEAM_ID)
}).Fetch(db)