Skip to content

Commit

Permalink
Merge pull request #54 from SUSE/postgres_dialect_enhancements
Browse files Browse the repository at this point in the history
Switch all DB table definitions to TableSpec
  • Loading branch information
gbuenodevsuse authored Oct 31, 2024
2 parents e95cc91 + 300f774 commit 14c07c4
Show file tree
Hide file tree
Showing 14 changed files with 952 additions and 384 deletions.
11 changes: 3 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (a *App) Initialize() error {
return err
}

if err := a.StagingDB.EnsureTablesExist(dbTablesStaging); err != nil {
if err := a.StagingDB.EnsureTableSpecsExist(stagingTables); err != nil {
slog.Error("Staging DB tables setup failed", slog.String("error", err.Error()))
return err
}
Expand All @@ -250,13 +250,8 @@ func (a *App) Initialize() error {
return err
}

if err := a.TelemetryDB.EnsureTablesExist(dbTablesTelemetry); err != nil {
slog.Error("Telemetry DB standard tables setup failed", slog.String("error", err.Error()))
return err
}

if err := a.TelemetryDB.EnsureTablesExist(dbTablesXform); err != nil {
slog.Error("Telemetry DB transform tables exist", slog.String("error", err.Error()))
if err := a.TelemetryDB.EnsureTableSpecsExist(telemetryTables); err != nil {
slog.Error("Telemetry DB tables setup failed", slog.String("error", err.Error()))
return err
}

Expand Down
216 changes: 101 additions & 115 deletions app/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ type ClientsRow struct {
// include common table row fields
TableRowCommon

// table specific query handlers
instIdExists *sql.Stmt

Id int64 `json:"id"`
ClientInstanceId types.ClientInstanceId `json:"clientInstanceId"`
RegistrationDate string `json:"registrationDate"`
Expand Down Expand Up @@ -63,119 +60,33 @@ func (c *ClientsRow) String() string {

func (c *ClientsRow) SetupDB(db *DbConnection) (err error) {
c.tableSpec = &clientsTableSpec
err = c.TableRowCommon.SetupDB(db)
if err != nil {
slog.Error("SetupDB() failed", slog.String("table", c.tableSpec.Name))
return err
}

var ph Placeholder
var stmt string

// prepare clientId exists check statement
ph = c.db.Placeholder(1)
stmt = `SELECT ` +
c.tableSpec.Columns[1].Name + `, ` +
c.tableSpec.Columns[2].Name + `, ` +
c.tableSpec.Columns[3].Name +
` FROM ` + c.TableName() +
` WHERE ` +
c.tableSpec.Columns[0].Name + ` = ` + ph.Next()

c.exists, err = c.db.Conn.Prepare(stmt)
if err != nil {
slog.Error(
"exists statement prep failed",
slog.String("table", c.TableName()),
slog.String("statement", stmt),
slog.String("error", err.Error()),
)
return
}

// prepare clientInstanceId exists check statement
ph = c.db.Placeholder(1)
stmt = `SELECT ` +
c.tableSpec.Columns[0].Name + `, ` +
c.tableSpec.Columns[2].Name + `, ` +
c.tableSpec.Columns[3].Name +
` FROM ` + c.TableName() +
` WHERE ` +
c.tableSpec.Columns[1].Name + ` = ` + ph.Next()

c.instIdExists, err = c.db.Conn.Prepare(stmt)
if err != nil {
slog.Error(
"exists statement prep failed",
slog.String("table", c.TableName()),
slog.String("statement", stmt),
slog.String("error", err.Error()),
)
return
}

// prepare insert statement
ph = c.db.Placeholder(3)
stmt = `INSERT INTO ` + c.TableName() +
`(` + c.tableSpec.Columns[1].Name +
`, ` + c.tableSpec.Columns[2].Name +
`, ` + c.tableSpec.Columns[3].Name + `) ` +
`VALUES(` +
ph.Next() + `, ` +
ph.Next() + `, ` +
ph.Next() + `) ` +
`RETURNING ` + c.tableSpec.Columns[0].Name
c.insert, err = c.db.Conn.Prepare(stmt)
if err != nil {
slog.Error(
"insert statement prep failed",
slog.String("table", c.TableName()),
slog.String("statement", stmt),
slog.String("error", err.Error()),
)
return
}

// prepare update statement
ph = c.db.Placeholder(4)
stmt = `UPDATE ` + c.TableName() +
` SET ` +
c.tableSpec.Columns[1].Name + ` = ` + ph.Next() + ", " +
c.tableSpec.Columns[2].Name + ` = ` + ph.Next() + ", " +
c.tableSpec.Columns[3].Name + ` = ` + ph.Next() +
` WHERE ` +
c.tableSpec.Columns[0].Name + ` = ` + ph.Next()
c.update, err = c.db.Conn.Prepare(stmt)
if err != nil {
slog.Error(
"update statement prep failed",
slog.String("table", c.TableName()),
slog.String("statement", stmt),
slog.String("error", err.Error()),
)
return
}
return c.TableRowCommon.SetupDB(db)
}

// prepare delete statement
ph = c.db.Placeholder(1)
stmt = `DELETE FROM ` + c.TableName() + ` WHERE ` +
c.tableSpec.Columns[0].Name + ` = ` + ph.Next()
c.delete, err = c.db.Conn.Prepare(stmt)
func (c *ClientsRow) Exists() bool {
stmt, err := c.SelectStmt(
// select columns
[]string{
"clientInstanceId",
"registrationDate",
"authToken",
},
// match columns
[]string{
"id",
},
SelectOpts{}, // no special options
)
if err != nil {
slog.Error(
"delete statement prep failed",
"exists statement generation failed",
slog.String("table", c.TableName()),
slog.String("statement", stmt),
slog.String("error", err.Error()),
)
return
panic(err)
}

return
}

func (c *ClientsRow) Exists() bool {
row := c.exists.QueryRow(c.Id)
row := c.DB().QueryRow(stmt, c.Id)
// if the entry was found, all fields not used to find the entry will have
// been updated to match what is in the DB
if err := row.Scan(
Expand All @@ -197,7 +108,29 @@ func (c *ClientsRow) Exists() bool {
}

func (c *ClientsRow) InstIdExists() bool {
row := c.instIdExists.QueryRow(c.ClientInstanceId)
stmt, err := c.SelectStmt(
// select columns
[]string{
"id",
"registrationDate",
"authToken",
},
// match columns
[]string{
"clientInstanceId",
},
SelectOpts{}, // no special options
)
if err != nil {
slog.Error(
"instIdExists statement generation failed",
slog.String("table", c.TableName()),
slog.String("error", err.Error()),
)
panic(err)
}

row := c.DB().QueryRow(stmt, c.ClientInstanceId)
// if the entry was found, all fields not used to find the entry will have
// been updated to match what is in the DB
if err := row.Scan(
Expand All @@ -219,8 +152,27 @@ func (c *ClientsRow) InstIdExists() bool {
}

func (c *ClientsRow) Insert() (err error) {
row := c.insert.QueryRow(
c.ClientInstanceId, c.RegistrationDate, c.AuthToken,
stmt, err := c.InsertStmt(
[]string{
"clientInstanceId",
"registrationDate",
"authToken",
},
"id",
)
if err != nil {
slog.Error(
"insert statement generation failed",
slog.String("table", c.TableName()),
slog.String("error", err.Error()),
)
return
}
row := c.DB().QueryRow(
stmt,
c.ClientInstanceId,
c.RegistrationDate,
c.AuthToken,
)
if err = row.Scan(
&c.Id,
Expand All @@ -237,7 +189,26 @@ func (c *ClientsRow) Insert() (err error) {
}

func (c *ClientsRow) Update() (err error) {
_, err = c.update.Exec(
stmt, err := c.UpdateStmt(
[]string{
"clientInstanceId",
"registrationDate",
"authToken",
},
[]string{
"id",
},
)
if err != nil {
slog.Error(
"update statement generation failed",
slog.String("table", c.TableName()),
slog.String("error", err.Error()),
)
return
}
_, err = c.DB().Exec(
stmt,
c.ClientInstanceId,
c.RegistrationDate,
c.AuthToken,
Expand All @@ -246,7 +217,7 @@ func (c *ClientsRow) Update() (err error) {
if err != nil {
slog.Error(
"update failed",
slog.String("table", c.table),
slog.String("table", c.TableName()),
slog.Int64("id", c.Id),
slog.String("error", err.Error()),
)
Expand All @@ -255,13 +226,28 @@ func (c *ClientsRow) Update() (err error) {
}

func (c *ClientsRow) Delete() (err error) {
_, err = c.delete.Exec(
stmt, err := c.DeleteStmt(
[]string{
"id",
},
)
if err != nil {
slog.Error(
"delete statement generation failed",
slog.String("table", c.TableName()),
slog.String("error", err.Error()),
)
return
}

_, err = c.DB().Exec(
stmt,
c.Id,
)
if err != nil {
slog.Error(
"delete failed",
slog.String("table", c.table),
slog.String("table", c.TableName()),
slog.Int64("id", c.Id),
slog.String("error", err.Error()),
)
Expand Down
Loading

0 comments on commit 14c07c4

Please sign in to comment.