Skip to content

Instantly share code, notes, and snippets.

@ogaere
Created March 23, 2020 11:41
Show Gist options
  • Save ogaere/26da8f08b97b53c28b8935424b11eb0e to your computer and use it in GitHub Desktop.
Save ogaere/26da8f08b97b53c28b8935424b11eb0e to your computer and use it in GitHub Desktop.
package getstudentinfo
//You enter a student id
// You get back the student result
//This is a rewrite of the original API in Delphi
// I am using Fiebirdsql
import (
"context"
"golang.org/x/sync/singleflight"
"database/sql"
_ "github.com/nakagami/firebirdsql"
)
func main() {
writeLn("The magic happens here.")
//Forgive me for mixing Pascal code with Go afterall am using pascal IDE called Element from RemObjects to code
}
type StudentInfoParams struct {
StudentID string // Matriculation No
}
type StudentResult struct {
GPA float32 // Grade point average
Grade string // "A", "B", "C" etc
}
var group singleflight.Group
// Student info does the magic
// After all this is the public API
func Student(ctx context.Context, params *StudentInfoParams) (*StudentResult, error) {
res, err, _ := group.Do(params.Name, func() (interface{}, error) {
stdresult, err := queryDB(ctx, params.Name)
return stdresult, err
})
if err != nil {
return nil, err
}
return res.(*StudentResult), nil
}
func queryDB(ctx context.Context, studentid string) (*StudentResult, error) {
var stdresult StudentResult
conn, _ := sql.Open("firebirdsql", "user:password@servername/ereSchoolERP.fdb")
defer conn.Close()
err := conn.QueryRow(ctx, `
SELECT g.grades, g.gpa
FROM grades g
WHERE a.studentid = $1
`, studentid).Scan(&stdresult.GPA, &stdresult.Grade)
return &stdresult, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment