From 4e842c5406a45b80bb35569b884f07cfb6e485b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Grze=C5=9B?= Date: Sat, 22 Dec 2018 15:56:43 +0100 Subject: [PATCH] =?UTF-8?q?Dodano=20nowe=20metody,=20kt=C3=B3re=20b=C4=99d?= =?UTF-8?q?=C4=85=20dostarcza=C5=82y=20do=20aplikacji=20potrzebne=20inform?= =?UTF-8?q?acje=20o=20bazie=20danych.=20Opr=C3=B3cz=20tego=20dalsze=20przy?= =?UTF-8?q?gotowania=20do=20po=C5=82=C4=85czenia=20API=20z=20Aplikacja=20d?= =?UTF-8?q?esktopow=C4=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basicModels.go | 10 ++++----- databaseConnection.go | 3 --- databaseInfo.go | 20 +++++++++++++++++ databaseRead.go | 51 ++++++++++++++++++++++++++++++++++++++++--- handlers.go | 15 ++++++++----- index.go | 6 ++--- routes.go | 1 + 7 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 databaseInfo.go diff --git a/basicModels.go b/basicModels.go index 5a16406..8675e18 100644 --- a/basicModels.go +++ b/basicModels.go @@ -35,7 +35,7 @@ type doctor struct { type operation struct { OperationID int64 Name string - averageTime string + averageTime NullTime operationType operationTypes cost uint8 refoundation int64 @@ -48,11 +48,11 @@ type room struct { } type admission struct { - AdmissionID int64 + AdmissionID string AdmissionDate time.Time - EndDate time.Time - PatientPesel string - DiagnosisSymbol string + EndDate NullTime + PatientPesel sql.NullString + DiagnosisSymbol sql.NullString MainDoctor int64 PlanedOperation int64 RoomNumber int64 diff --git a/databaseConnection.go b/databaseConnection.go index 7d30ca8..8d0213f 100644 --- a/databaseConnection.go +++ b/databaseConnection.go @@ -10,11 +10,8 @@ import ( _ "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" ->>>>>>> Stashed changes -//var connectionString = "server=MARCEL\\SQLEXPRESS;user id=;password=" var db *sql.DB var err error diff --git a/databaseInfo.go b/databaseInfo.go new file mode 100644 index 0000000..0b88ebf --- /dev/null +++ b/databaseInfo.go @@ -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 +} diff --git a/databaseRead.go b/databaseRead.go index fd01f2e..1afe13a 100644 --- a/databaseRead.go +++ b/databaseRead.go @@ -2,6 +2,7 @@ package main import ( "database/sql" + "database/sql/driver" "time" ) @@ -65,8 +66,7 @@ func (ope operations) readModels(rows *sql.Rows) iDatabaseModels { var oper operation for rows.Next() { oper = operation{} - var dummy interface{} - rows.Scan(&oper.OperationID, &oper.Name, &dummy, &oper.operationType, + rows.Scan(&oper.OperationID, &oper.Name, &oper.averageTime, &oper.operationType, &oper.cost, &oper.refoundation) models = append(models, oper) } @@ -78,15 +78,60 @@ func (doc admissions) readModels(rows *sql.Rows) iDatabaseModels { var admiss admission for rows.Next() { 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, &admiss.PatientPesel, &admiss.MainDoctor, &admiss.PlanedOperation, &admiss.RoomNumber, &admiss.IsPlanned) - models = append(models, admiss) } 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 { var models iDatabaseModels var rom room diff --git a/handlers.go b/handlers.go index 10a9475..d5e2889 100644 --- a/handlers.go +++ b/handlers.go @@ -32,20 +32,25 @@ func update(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, rowsAffected) } -//DO POPRAWY func insert(w http.ResponseWriter, r *http.Request) { 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"]) for i := 1; i < max; i++ { key := "val" + strconv.Itoa(i) - tsqlCommand += ", " + args[key] + tsqlCommand += "', '" + args[key] } - tsqlCommand += ")" + tsqlCommand += "')" rowsAffected := insertRecord(tsqlCommand) 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) { args := mux.Vars(r) pk := args["primaryKey"] diff --git a/index.go b/index.go index f6127c8..3e34452 100644 --- a/index.go +++ b/index.go @@ -6,13 +6,13 @@ import ( ) // Zrobione ostatnio: -// +// Naprawa insert, Column info, // Zrobione: // 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: -// 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!! ; func main() { router := newRouter() diff --git a/routes.go b/routes.go index 12899d5..9d97c5e 100644 --- a/routes.go +++ b/routes.go @@ -23,6 +23,7 @@ var registredRoutes = routes{ update}, route{"InsertRecord", "/insertrec/{tableName}/nr/{count}/pk/{primaryKey}/{val1}/{val2}/{val3}/{val4}/{val5}/{val6}/{val7}", insert}, + route{"GetColumnNames", "/getcolumnnames/{tableName}", columnNames}, } func newRouter() *mux.Router {