func (me *postgresTest) TestDelete() { t := me.T() ds := me.db.From("entry") var e entry found, err := ds.Where(goqu.I("int").Eq(9)).Select("id").ScanStruct(&e) assert.NoError(t, err) assert.True(t, found) _, err = ds.Where(goqu.I("id").Eq(e.Id)).Delete().Exec() assert.NoError(t, err) count, err := ds.Count() assert.NoError(t, err) assert.Equal(t, count, 9) var id uint32 found, err = ds.Where(goqu.I("id").Eq(e.Id)).ScanVal(&id) assert.NoError(t, err) assert.False(t, found) e = entry{} found, err = ds.Where(goqu.I("int").Eq(8)).Select("id").ScanStruct(&e) assert.NoError(t, err) assert.True(t, found) assert.NotEqual(t, e.Id, 0) id = 0 _, err = ds.Where(goqu.I("id").Eq(e.Id)).Returning("id").Delete().ScanVal(&id) assert.NoError(t, err) assert.Equal(t, id, e.Id) }
func (me *mysqlTest) TestDelete() { t := me.T() ds := me.db.From("entry") var e entry found, err := ds.Where(goqu.I("int").Eq(9)).Select("id").ScanStruct(&e) assert.NoError(t, err) assert.True(t, found) _, err = ds.Where(goqu.I("id").Eq(e.Id)).Delete().Exec() assert.NoError(t, err) count, err := ds.Count() assert.NoError(t, err) assert.Equal(t, count, 9) var id uint32 found, err = ds.Where(goqu.I("id").Eq(e.Id)).ScanVal(&id) assert.NoError(t, err) assert.False(t, found) e = entry{} found, err = ds.Where(goqu.I("int").Eq(8)).Select("id").ScanStruct(&e) assert.NoError(t, err) assert.True(t, found) assert.NotEqual(t, e.Id, 0) id = 0 _, err = ds.Where(goqu.I("id").Eq(e.Id)).Returning("id").Delete().ScanVal(&id) assert.Equal(t, err.Error(), "goqu: Adapter does not support RETURNING clause") }
func (me *postgresTest) TestQuery() { t := me.T() var entries []entry ds := me.db.From("entry") assert.NoError(t, ds.Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 10) floatVal := float64(0) baseDate, err := time.Parse(time.RFC3339Nano, "2015-02-22T18:19:55.000000000-00:00") assert.NoError(t, err) baseDate = baseDate.UTC() for i, entry := range entries { f := fmt.Sprintf("%f", floatVal) assert.Equal(t, entry.Id, uint32(i+1)) assert.Equal(t, entry.Int, i) assert.Equal(t, fmt.Sprintf("%f", entry.Float), f) assert.Equal(t, entry.String, f) assert.Equal(t, entry.Bytes, []byte(f)) assert.Equal(t, entry.Bool, i%2 == 0) assert.Equal(t, entry.Time.Unix(), baseDate.Add(time.Duration(i)*time.Hour).Unix()) floatVal += float64(0.1) } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("bool").IsTrue()).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 5) assert.NoError(t, err) for _, entry := range entries { assert.True(t, entry.Bool) } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("int").Gt(4)).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 5) assert.NoError(t, err) for _, entry := range entries { assert.True(t, entry.Int > 4) } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("int").Gte(5)).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 5) assert.NoError(t, err) for _, entry := range entries { assert.True(t, entry.Int >= 5) } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("int").Lt(5)).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 5) assert.NoError(t, err) for _, entry := range entries { assert.True(t, entry.Int < 5) } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("int").Lte(4)).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 5) assert.NoError(t, err) for _, entry := range entries { assert.True(t, entry.Int <= 4) } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("string").Eq("0.100000")).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 1) assert.NoError(t, err) for _, entry := range entries { assert.Equal(t, entry.String, "0.100000") } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("string").Like("0.1%")).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 1) assert.NoError(t, err) for _, entry := range entries { assert.Equal(t, entry.String, "0.100000") } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("string").NotLike("0.1%")).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 9) assert.NoError(t, err) for _, entry := range entries { assert.NotEqual(t, entry.String, "0.100000") } entries = entries[0:0] assert.NoError(t, ds.Where(goqu.I("string").IsNull()).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 0) }