Dodano nowe metody, które będą dostarczały do aplikacji potrzebne informacje o bazie danych. Oprócz tego dalsze przygotowania do połączenia API z Aplikacja desktopową

This commit is contained in:
Marcel Grześ 2018-12-22 15:56:43 +01:00
parent 0aeac10e7b
commit 4e842c5406
7 changed files with 87 additions and 19 deletions

View File

@ -35,7 +35,7 @@ type doctor struct {
type operation struct { type operation struct {
OperationID int64 OperationID int64
Name string Name string
averageTime string averageTime NullTime
operationType operationTypes operationType operationTypes
cost uint8 cost uint8
refoundation int64 refoundation int64
@ -48,11 +48,11 @@ type room struct {
} }
type admission struct { type admission struct {
AdmissionID int64 AdmissionID string
AdmissionDate time.Time AdmissionDate time.Time
EndDate time.Time EndDate NullTime
PatientPesel string PatientPesel sql.NullString
DiagnosisSymbol string DiagnosisSymbol sql.NullString
MainDoctor int64 MainDoctor int64
PlanedOperation int64 PlanedOperation int64
RoomNumber int64 RoomNumber int64

View File

@ -10,11 +10,8 @@ import (
_ "github.com/denisenkom/go-mssqldb" _ "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=192.168.1.3;Port=1433;database=DB_s439397;trusted_connection=yes;encrypt=disable" var connectionString = "server=192.168.1.3;Port=1433;database=DB_s439397;trusted_connection=yes;encrypt=disable"
>>>>>>> Stashed changes
//var connectionString = "server=MARCEL\\SQLEXPRESS;user id=;password="
var db *sql.DB var db *sql.DB
var err error var err error

20
databaseInfo.go Normal file
View File

@ -0,0 +1,20 @@
package main
import (
"context"
_ "github.com/denisenkom/go-mssqldb"
)
func getColumnNames(tableName string) (string, error) {
ctx := context.Background()
checkIsDatabaseAlive()
tsql := "SELECT * FROM " + tableName
rows, _ := db.QueryContext(ctx, tsql)
collection, _ := rows.Columns()
var toReturn string
for _, element := range collection {
toReturn += element + "."
}
return toReturn, nil
}

View File

@ -2,6 +2,7 @@ package main
import ( import (
"database/sql" "database/sql"
"database/sql/driver"
"time" "time"
) )
@ -65,8 +66,7 @@ func (ope operations) readModels(rows *sql.Rows) iDatabaseModels {
var oper operation var oper operation
for rows.Next() { for rows.Next() {
oper = operation{} oper = operation{}
var dummy interface{} rows.Scan(&oper.OperationID, &oper.Name, &oper.averageTime, &oper.operationType,
rows.Scan(&oper.OperationID, &oper.Name, &dummy, &oper.operationType,
&oper.cost, &oper.refoundation) &oper.cost, &oper.refoundation)
models = append(models, oper) models = append(models, oper)
} }
@ -78,15 +78,60 @@ func (doc admissions) readModels(rows *sql.Rows) iDatabaseModels {
var admiss admission var admiss admission
for rows.Next() { for rows.Next() {
admiss = admission{} admiss = admission{}
//var dummy interface{}
/*var valueList [9]iHasValue
rows.Scan(&valueList[0], &valueList[1], &valueList[2], &valueList[3], &valueList[4],
&valueList[5], &valueList[6], &valueList[7], &valueList[8])
timex := NullTime{}
stri, strix := sql.NullString{}, sql.NullString{}
if valueList[2] == nil {
timex.Valid = false
} else {
timex.Time = valueList[2].(time.Time)
timex.Valid = true
}
if valueList[3] == nil {
strix.Valid = false
} else {
strix.String = valueList[4].(string)
strix.Valid = true
}
if valueList[4] == nil {
stri.Valid = false
} else {
stri.String = valueList[4].(string)
stri.Valid = true
}
models = append(models, admission{
valueList[0].(int64), valueList[1].(time.Time), timex, strix,
stri, valueList[5].(int64), valueList[6].(int64), valueList[7].(int64),
valueList[8].(bool)})*/
rows.Scan(&admiss.AdmissionID, &admiss.AdmissionDate, &admiss.EndDate, rows.Scan(&admiss.AdmissionID, &admiss.AdmissionDate, &admiss.EndDate,
&admiss.PatientPesel, &admiss.MainDoctor, &admiss.PlanedOperation, &admiss.PatientPesel, &admiss.MainDoctor, &admiss.PlanedOperation,
&admiss.RoomNumber, &admiss.IsPlanned) &admiss.RoomNumber, &admiss.IsPlanned)
models = append(models, admiss) models = append(models, admiss)
} }
return models return models
} }
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
// Scan implements the Scanner interface.
func (nt *NullTime) Scan(value interface{}) error {
nt.Time, nt.Valid = value.(time.Time)
return nil
}
// Value implements the driver Valuer interface.
func (nt NullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}
func (ro rooms) readModels(rows *sql.Rows) iDatabaseModels { func (ro rooms) readModels(rows *sql.Rows) iDatabaseModels {
var models iDatabaseModels var models iDatabaseModels
var rom room var rom room

View File

@ -32,20 +32,25 @@ func update(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, rowsAffected) fmt.Fprint(w, rowsAffected)
} }
//DO POPRAWY
func insert(w http.ResponseWriter, r *http.Request) { func insert(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r) args := mux.Vars(r)
tsqlCommand := "INSERT INTO " + args["tableName"] + " VALUES (" + args["primaryKey"] // TODO: Dodac walidacje primary key
tsqlCommand := "INSERT INTO " + args["tableName"] + " VALUES ('" + args["primaryKey"]
max, _ := strconv.Atoi(args["count"]) max, _ := strconv.Atoi(args["count"])
for i := 1; i < max; i++ { for i := 1; i < max; i++ {
key := "val" + strconv.Itoa(i) key := "val" + strconv.Itoa(i)
tsqlCommand += ", " + args[key] tsqlCommand += "', '" + args[key]
} }
tsqlCommand += ")" tsqlCommand += "')"
rowsAffected := insertRecord(tsqlCommand) rowsAffected := insertRecord(tsqlCommand)
fmt.Fprint(w, rowsAffected) fmt.Fprint(w, rowsAffected)
} }
func columnNames(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r)
table := args["tableName"]
colNames, _ := getColumnNames(table)
fmt.Fprint(w, colNames)
}
func getIndex(w http.ResponseWriter, r *http.Request) { func getIndex(w http.ResponseWriter, r *http.Request) {
args := mux.Vars(r) args := mux.Vars(r)
pk := args["primaryKey"] pk := args["primaryKey"]

View File

@ -6,13 +6,13 @@ import (
) )
// Zrobione ostatnio: // Zrobione ostatnio:
// // Naprawa insert, Column info,
// Zrobione: // Zrobione:
// Podstawowy serwer; Podstawowe struktury modeli; niezbędne enumy; prosta refraktoryzacja; // Podstawowy serwer; Podstawowe struktury modeli; niezbędne enumy; prosta refraktoryzacja;
// Utworzenie połączenia z bazą danych // Utworzenie połączenia z bazą danych; Funkcje modeli; łączenie z bazą danych;
// Do zrobienia: // Do zrobienia:
// Funkcje modeli; Interfejsy json; łączenie z bazą danych; Rozne metody do tworzenia komend sql; // Rozne metody do tworzenia komend sql;
// Komunikacja z aplikacja desktopowa; Walidacja danych!! ; // Komunikacja z aplikacja desktopowa; Walidacja danych!! ;
func main() { func main() {
router := newRouter() router := newRouter()

View File

@ -23,6 +23,7 @@ var registredRoutes = routes{
update}, update},
route{"InsertRecord", "/insertrec/{tableName}/nr/{count}/pk/{primaryKey}/{val1}/{val2}/{val3}/{val4}/{val5}/{val6}/{val7}", route{"InsertRecord", "/insertrec/{tableName}/nr/{count}/pk/{primaryKey}/{val1}/{val2}/{val3}/{val4}/{val5}/{val6}/{val7}",
insert}, insert},
route{"GetColumnNames", "/getcolumnnames/{tableName}", columnNames},
} }
func newRouter() *mux.Router { func newRouter() *mux.Router {