func usersIdHandler(rw http.ResponseWriter, r *http.Request) { rw.Header().Set("Content-Type", "application/json") switch r.Method { case "POST": norm.NewSelect(normConnection.NewSession(nil), &User{}, nil) case "GET": user := &User{} norm.NewSelect(normConnection.NewSession(nil), &User{}, nil).Where("id = ?", 1).LoadStruct(user) default: rw.Write([]byte("WHAT?")) } }
func usersHandler(rw http.ResponseWriter, r *http.Request) { rw.Header().Set("Content-Type", "application/json") switch r.Method { case "POST": norm.NewSelect(normConnection.NewSession(nil), &User{}, nil) case "GET": users := make([]*User, 0) selectQ := norm.NewSelect(normConnection.NewSession(nil), &User{}, nil) _, err := selectQ.LoadStructs(&users) if err != nil { handleError(rw, err, http.StatusInternalServerError) return } handleJSON(rw, users) default: rw.Write([]byte("WHAT?")) } //norm.NewSelect(dbrConnection.NewSession(), User{}, nil).Where(Where("id = ?")) }
func ExampleNewSelect() { cnx := norm.NewConnection(nil, "norm_mysql", nil) dbrSess := cnx.NewSession(nil) user := &User{} user.Id.Scan(1) selectBuilder := norm.NewSelect(dbrSess, user, nil) selectSql, selectArgs := selectBuilder.ToSql() fmt.Println("DML:", selectSql) fmt.Println("ARGS:", selectArgs) // Output: // DML: SELECT `id`, `first_name`, `last_name`, `email` FROM norm_mysql.norm_users // ARGS: [] }
func ExampleConnection_newSelect() { user := &User{} // Mock Database Connection to expect a query and return row data db, mock, err := sqlmock.New() if err != nil { log.Fatal("an error'%s' was not expected while open mock database", err) } rows := sqlmock.NewRows([]string{"id", "email"}).AddRow(5432, "*****@*****.**") // ExpectQuery is a regexp, escape regex tokens like `\\(` and `\\)` mock.ExpectQuery("SELECT `id`, `email` FROM norm_database.norm_users WHERE \\(id = 5432\\)").WillReturnRows(rows) // Create norm Connection and Session for sqlmock connection := norm.NewConnection(db, "norm_database", nil) session := connection.NewSession(nil) // Perform and load mocked result into Model err = norm.NewSelect(session, user, field.Names{"id", "email"}).Where("id = ?", 5432).LoadStruct(user) if err != nil { log.Fatal(err.Error()) } // Compare query executed versus query provided err = mock.ExpectationsWereMet() if err != nil { fmt.Println(err.Error()) } outputJson(user) // Output: // {"id":5432,"first_name":"","last_name":"","email":"*****@*****.**"} }