fa9d4c0fc6
Dodano polimorficzny interfejs aby odpowiedzialność za pobranie danych leżała po stronie elementu zależnego, a nie nadrzędnego. Dodano metody rozszerzające do kolekcji modeli, które umożliwiają łatwe tworzenie modeli i dodawanie ich do kolekcji. Pobieranie modeli jeszcze do poprawki.
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
|
|
_ "github.com/denisenkom/go-mssqldb"
|
|
)
|
|
|
|
var connectionString = "server=192.168.1.3;Port=62262;database=DB_s439397;trusted_connection=yes;encrypt=disable"
|
|
|
|
//var connectionString = "server=MARCEL\\SQLEXPRESS;user id=;password="
|
|
var db *sql.DB
|
|
var err error
|
|
|
|
func testConnection() {
|
|
db, err = sql.Open("sqlserver", connectionString)
|
|
if err != nil {
|
|
log.Fatal("Error creating connection pool: ", err.Error())
|
|
}
|
|
//ctx := context.Background()
|
|
err = db.Ping()
|
|
//err = db.PingContext(ctx)
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
fmt.Printf("Connected!\n")
|
|
}
|
|
|
|
type iHasValue interface{}
|
|
type values []iHasValue
|
|
|
|
func readDatabase(tableName string) (iDatabaseModels, error) {
|
|
ctx := context.Background()
|
|
checkIsDatabaseAlive()
|
|
tsql := "SELECT * FROM " + tableName
|
|
rows, _ := db.QueryContext(ctx, tsql)
|
|
defer rows.Close()
|
|
var models iDatabaseModels
|
|
models = nil
|
|
switch tableName {
|
|
case "Lekarze":
|
|
var x doctors
|
|
models = x.readModels(rows)
|
|
case "Pacjenci":
|
|
var x patients
|
|
models = x.readModels(rows)
|
|
case "Operacje":
|
|
var x operations
|
|
models = x.readModels(rows)
|
|
case "Diagnozy":
|
|
var x diagnoses
|
|
models = x.readModels(rows)
|
|
}
|
|
return models, nil
|
|
}
|
|
|
|
func checkIsDatabaseAlive() error {
|
|
err = db.Ping()
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
return nil
|
|
}
|