removing prepare statements

This commit is contained in:
sbouaram 2023-05-03 09:13:34 +02:00
parent 8629bc505b
commit e2ad1d9a50
1 changed files with 38 additions and 58 deletions

View File

@ -58,14 +58,11 @@ func (psql PostgresqlStorage) CreateGroup(group Group) error {
if err != nil { if err != nil {
return err return err
} }
insertGroupStmt, err := psql.DbConnection.Prepare(fmt.Sprintf("INSERT INTO %s (id, namespace, members, data)"+
" VALUES ($1, $2, $3, $4)", query := fmt.Sprintf("INSERT INTO %s (id, namespace, members, data) VALUES ($1, $2, $3, $4)",
psql.Tables["groups"])) psql.Tables["groups"])
if err != nil {
return err _, err = psql.DbConnection.Exec(query, group.ID, group.Namespace, pq.Array(group.Members), dataJson)
}
defer insertGroupStmt.Close()
_, err = insertGroupStmt.Exec(group.ID, group.Namespace, pq.Array(group.Members), dataJson)
if err != nil { if err != nil {
return err return err
} }
@ -98,17 +95,14 @@ func (psql PostgresqlStorage) GetGroup(id string) (*Group, error) {
func (psql PostgresqlStorage) GetGroups(namespaces []string) ([]Group, error) { func (psql PostgresqlStorage) GetGroups(namespaces []string) ([]Group, error) {
var groups []Group var groups []Group
stmt, err := psql.DbConnection.Prepare(fmt.Sprintf(`SELECT id, namespace, members, data query := fmt.Sprintf(`SELECT id, namespace, members, data FROM %s WHERE namespace = ANY($1)`, psql.Tables["groups"])
FROM %s WHERE namespace = ANY($1)`, psql.Tables["groups"]))
if err != nil { rows, err := psql.DbConnection.Query(query, pq.Array(namespaces))
return nil, fmt.Errorf("psql prepare select groups query failed : %s", err)
}
defer stmt.Close()
rows, err := stmt.Query(pq.Array(namespaces))
if err != nil { if err != nil {
return nil, fmt.Errorf("psql select groups query failed : %s", err) return nil, fmt.Errorf("psql select groups query failed : %s", err)
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var ( var (
id string id string
@ -116,15 +110,18 @@ func (psql PostgresqlStorage) GetGroups(namespaces []string) ([]Group, error) {
data []byte data []byte
members pq.StringArray members pq.StringArray
) )
err := rows.Scan(&id, &ns, &members, &data) err := rows.Scan(&id, &ns, &members, &data)
if err != nil { if err != nil {
return nil, fmt.Errorf("psql scan groups row failed : %s", err) return nil, fmt.Errorf("psql scan groups row failed : %s", err)
} }
var groupData map[string]interface{} var groupData map[string]interface{}
err = json.Unmarshal(data, &groupData) err = json.Unmarshal(data, &groupData)
if err != nil { if err != nil {
return nil, fmt.Errorf("psql unmarshal group data failed : %s", err) return nil, fmt.Errorf("psql unmarshal group data failed : %s", err)
} }
groups = append(groups, Group{ groups = append(groups, Group{
ID: id, ID: id,
Namespace: ns, Namespace: ns,
@ -132,6 +129,7 @@ func (psql PostgresqlStorage) GetGroups(namespaces []string) ([]Group, error) {
Data: groupData, Data: groupData,
}) })
} }
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {
return nil, fmt.Errorf("psql iterate groups rows failed : %s", err) return nil, fmt.Errorf("psql iterate groups rows failed : %s", err)
} }
@ -140,13 +138,8 @@ func (psql PostgresqlStorage) GetGroups(namespaces []string) ([]Group, error) {
func (psql PostgresqlStorage) GetGroupsByIds(ids []string) ([]Group, error) { func (psql PostgresqlStorage) GetGroupsByIds(ids []string) ([]Group, error) {
groups := make([]Group, 0) groups := make([]Group, 0)
stmt, err := psql.DbConnection.Prepare(fmt.Sprintf(`SELECT id, namespace, members, data query := fmt.Sprintf(`SELECT id, namespace, members, data FROM %s WHERE id = ANY($1)`, psql.Tables["groups"])
FROM %s WHERE id = ANY($1)`, psql.Tables["groups"])) rows, err := psql.DbConnection.Query(query, pq.Array(ids))
if err != nil {
return nil, fmt.Errorf("psql prepare select groups query failed : %s", err)
}
defer stmt.Close()
rows, err := stmt.Query(pq.Array(ids))
if err != nil { if err != nil {
return nil, fmt.Errorf("psql select groups query failed: %s", err) return nil, fmt.Errorf("psql select groups query failed: %s", err)
} }
@ -179,14 +172,9 @@ func (psql PostgresqlStorage) UpdateGroup(group Group) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to serialize data field: %s", err) return fmt.Errorf("failed to serialize data field: %s", err)
} }
stmt, err := psql.DbConnection.Prepare(fmt.Sprintf(`UPDATE %s SET namespace=$2, members=$3, data=$4 WHERE id=$1`,
psql.Tables["groups"]))
if err != nil {
return fmt.Errorf("psql prepare update group query failed : %s", err)
}
defer stmt.Close()
_, err = stmt.Exec(group.ID, group.Namespace, members, data) _, err = psql.DbConnection.Exec(fmt.Sprintf(`UPDATE %s SET namespace=$2, members=$3, data=$4 WHERE id=$1`,
psql.Tables["groups"]), group.ID, group.Namespace, members, data)
if err != nil { if err != nil {
return fmt.Errorf("psql update group query failed : %s", err) return fmt.Errorf("psql update group query failed : %s", err)
} }
@ -194,19 +182,15 @@ func (psql PostgresqlStorage) UpdateGroup(group Group) error {
} }
func (psql PostgresqlStorage) CreateGroupMember(member GroupMember) error { func (psql PostgresqlStorage) CreateGroupMember(member GroupMember) error {
stmt, err := psql.DbConnection.Prepare(fmt.Sprintf(`INSERT INTO %s (id, member_id, group_id, data) dataJson, err := json.Marshal(member.Data)
VALUES ($1, $2, $3, $4)`, psql.Tables["group_members"]))
if err != nil {
return fmt.Errorf("psql prepare insert group member query failed: %s", err)
}
defer stmt.Close()
data, err := json.Marshal(member.Data)
if err != nil { if err != nil {
return err return err
} }
_, err = stmt.Exec(member.ID, member.Memberid, member.Groupid, data) _, err = psql.DbConnection.Exec(fmt.Sprintf("INSERT INTO %s (id, member_id, group_id, data)"+
" VALUES ($1, $2, $3, $4)",
psql.Tables["group_members"]), member.ID, member.Memberid, member.Groupid, dataJson)
if err != nil { if err != nil {
return fmt.Errorf("psql insert group member query failed: %s", err) return err
} }
return nil return nil
} }
@ -275,13 +259,9 @@ func (psql PostgresqlStorage) GetGroupsMember(namespaces []string) ([]GroupMembe
func (psql PostgresqlStorage) GetGroupsMemberByIds(ids []string) ([]GroupMember, error) { func (psql PostgresqlStorage) GetGroupsMemberByIds(ids []string) ([]GroupMember, error) {
groupMembers := make([]GroupMember, 0) groupMembers := make([]GroupMember, 0)
stmt, err := psql.DbConnection.Prepare(fmt.Sprintf(`SELECT id, member_id, group_id, data req := fmt.Sprintf(`SELECT id, member_id, group_id, data
FROM %s WHERE group_id = ANY($1)`, psql.Tables["group_members"])) FROM %s WHERE group_id = ANY($1)`, psql.Tables["group_members"])
if err != nil { rows, err := psql.DbConnection.Query(req, pq.Array(ids))
return nil, fmt.Errorf("psql prepare select group members query failed: %s", err)
}
defer stmt.Close()
rows, err := stmt.Query(pq.Array(ids))
if err != nil { if err != nil {
return nil, fmt.Errorf("psql select group members query failed: %s", err) return nil, fmt.Errorf("psql select group members query failed: %s", err)
} }
@ -306,19 +286,13 @@ func (psql PostgresqlStorage) GetGroupsMemberByIds(ids []string) ([]GroupMember,
} }
func (psql PostgresqlStorage) UpdateGroupMember(groupMember GroupMember) error { func (psql PostgresqlStorage) UpdateGroupMember(groupMember GroupMember) error {
stmt, err := psql.DbConnection.Prepare(fmt.Sprintf(`UPDATE %s SET member_id=$1, group_id=$2,
data=$3 WHERE id=$4`, psql.Tables["group_members"]))
if err != nil {
return fmt.Errorf("psql prepare update group member query failed : %s", err)
}
defer stmt.Close()
data, err := json.Marshal(groupMember.Data) data, err := json.Marshal(groupMember.Data)
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal data field: %s", err) return fmt.Errorf("failed to marshal data field: %s", err)
} }
res, err := stmt.Exec(groupMember.Memberid, groupMember.Groupid, data, groupMember.ID) req := fmt.Sprintf(`UPDATE %s SET member_id=$1, group_id=$2, data=$3 WHERE id=$4`, psql.Tables["group_members"])
res, err := psql.DbConnection.Exec(req, groupMember.Memberid, groupMember.Groupid, data, groupMember.ID)
if err != nil { if err != nil {
return fmt.Errorf("psql update group member query failed: %s", err) return fmt.Errorf("psql update group member query failed: %s", err)
} }
@ -335,14 +309,20 @@ func (psql PostgresqlStorage) UpdateGroupMember(groupMember GroupMember) error {
} }
func (psql PostgresqlStorage) DeleteGroupMember(id string) error { func (psql PostgresqlStorage) DeleteGroupMember(id string) error {
stmt, err := psql.DbConnection.Prepare(fmt.Sprintf(`DELETE FROM %s WHERE id=$1`, psql.Tables["group_members"])) res, err := psql.DbConnection.Exec(fmt.Sprintf(`DELETE FROM %s WHERE id=$1`, psql.Tables["group_members"]), id)
if err != nil { if err != nil {
return fmt.Errorf("psql prepare delete group member query failed: %s", err)
}
defer stmt.Close()
if _, err := stmt.Exec(id); err != nil {
return fmt.Errorf("psql delete group member query failed: %s", err) return fmt.Errorf("psql delete group member query failed: %s", err)
} }
rowsAffected, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("failed to get rows affected by delete query: %s", err)
}
if rowsAffected == 0 {
return fmt.Errorf("no rows were deleted for group member with id %s", id)
}
return nil return nil
} }