Skip to content

Commit

Permalink
fix: postgres get records
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Oct 14, 2024
1 parent 213b63b commit 9bbda7b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
1 change: 1 addition & 0 deletions drivers/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func (db *MySQL) GetRecords(database, table, where, sort string, offset, limit i
if err := paginatedRows.Close(); err != nil {
return nil, 0, err
}

countQuery := "SELECT COUNT(*) FROM "
countQuery += fmt.Sprintf("`%s`.", database)
countQuery += fmt.Sprintf("`%s`", table)
Expand Down
59 changes: 22 additions & 37 deletions drivers/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,54 +529,32 @@ func (db *Postgres) GetRecords(database, table, where, sort string, offset, limi
records = append(records, columns)

for paginatedRows.Next() {
nullStringSlice := make([]sql.NullString, len(columns))

rowValues := make([]interface{}, len(columns))
for i := range columns {
rowValues[i] = new(sql.RawBytes)
for i := range nullStringSlice {
rowValues[i] = &nullStringSlice[i]
}

countQuery := "SELECT COUNT(*) FROM "
countQuery += formattedTableName
row := db.Connection.QueryRow(countQuery)
if err := row.Scan(&totalRecords); err != nil {
if err := paginatedRows.Scan(rowValues...); err != nil {
return nil, 0, err
}

columns, columnsError := paginatedRows.Columns()

if columnsError != nil {
err = columnsError
}

records = append(records, columns)

for paginatedRows.Next() {
nullStringSlice := make([]sql.NullString, len(columns))

rowValues := make([]interface{}, len(columns))
for i := range nullStringSlice {
rowValues[i] = &nullStringSlice[i]
}

if err := paginatedRows.Scan(rowValues...); err != nil {
return nil, 0, err
}

var row []string
for _, col := range nullStringSlice {
if col.Valid {
if col.String == "" {
row = append(row, "EMPTY&")
} else {
row = append(row, col.String)
}
var row []string
for _, col := range nullStringSlice {
if col.Valid {
if col.String == "" {
row = append(row, "EMPTY&")
} else {
row = append(row, "NULL&")
row = append(row, col.String)
}
} else {
row = append(row, "NULL&")
}
}

records = append(records, row)
records = append(records, row)

}
}

if err := paginatedRows.Err(); err != nil {
Expand All @@ -587,6 +565,13 @@ func (db *Postgres) GetRecords(database, table, where, sort string, offset, limi
return nil, 0, err
}

countQuery := "SELECT COUNT(*) FROM "
countQuery += formattedTableName
row := db.Connection.QueryRow(countQuery)
if err := row.Scan(&totalRecords); err != nil {
return nil, 0, err
}

return
}

Expand Down

0 comments on commit 9bbda7b

Please sign in to comment.