Skip to content

Commit

Permalink
add saving to db
Browse files Browse the repository at this point in the history
  • Loading branch information
gagarinfan committed Dec 20, 2018
1 parent e7c70ff commit a1098af
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions SQL4/sqljson.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ func getEnv(key, defaultValue string) string {
}

var people []Person
var cnstr = fmt.Sprintf("server=%s;user id=%s;password=%s;port=%s;database=%s;", server, user, password, port, database)

func main() {
cnstr := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%s;database=%s;", server, user, password, port, database)

fmt.Println(cnstr)
var err error

Expand Down Expand Up @@ -84,13 +85,13 @@ func czytajdb(cnstr string) error {
return errq
}

func dodaj(cnstr string, name string, location string) (int64, error) {
func dodaj(cnstr string, name string, location string, tableName string) (int64, error) {
var err error
db, ctx, errdb := connopen(cnstr)
if errdb != nil {
log.Println("Problem z db prepare: ", err.Error())
}
var zapins = "insert into TestSchema.Employees (name, location) values (@name,@location); select @@identity;"
var zapins = "insert into TestSchema.EmployeesTMP (name, location) values (@name,@location); select @@identity;"

skladnia, err := db.Prepare(zapins)
if err != nil {
Expand All @@ -99,6 +100,7 @@ func dodaj(cnstr string, name string, location string) (int64, error) {
defer skladnia.Close()
//var name, location string
row := skladnia.QueryRowContext(ctx,
//sql.Named("tableName", tableName),
sql.Named("name", name),
sql.Named("location", location))

Expand Down Expand Up @@ -208,6 +210,23 @@ func connclose(db *sql.DB) {
defer db.Close()
}

func wypelnijTempTable() {
//var sessionid string
//sessionid = "tg1"
nazwatb := "TestSchema.EmployeesTMP"

for _, item := range people {
{
name := item.Name
location := item.Location
_, err := dodaj(cnstr, name, location, nazwatb)
if err != nil {
fmt.Println("Nie dodałem, bo: ", err.Error())
}
}
}
}

//<import json>
func GetPersonEndpoint(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
Expand All @@ -223,7 +242,6 @@ func GetPeopleEndpoint(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(people)
}
func CreatePersonEndpoint(w http.ResponseWriter, r *http.Request) {
cnstr := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%s;database=%s;", server, user, password, port, database)
params := mux.Vars(r)
var person Person
_ = json.NewDecoder(r.Body).Decode(&person)
Expand All @@ -232,7 +250,11 @@ func CreatePersonEndpoint(w http.ResponseWriter, r *http.Request) {
person.Location = params["location"]
people = append(people, person)
json.NewEncoder(w).Encode(people)
_, _ = dodaj(cnstr, person.Name, person.Location)

//wywołanie dodania do temptable
wypelnijTempTable()

_, _ = dodaj(cnstr, person.Name, person.Location, "TestSchema.EmployeesTMP")
}
func DeletePersonEndpoint(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
Expand Down

0 comments on commit a1098af

Please sign in to comment.