Esempio n. 1
0
func (ins insert) AppendQuery(b []byte, params ...interface{}) ([]byte, error) {
	table := ins.Table()
	strct := ins.Value()

	b = append(b, "INSERT INTO "...)
	b = types.AppendField(b, table.Name, true)

	b = append(b, " ("...)
	for i, field := range table.Fields {
		if field.Has(PrimaryKeyFlag) && field.IsEmpty(strct) {
			continue
		}
		b = types.AppendField(b, field.SQLName, true)
		if i != len(table.Fields)-1 {
			b = append(b, ", "...)
		}
	}

	b = append(b, ") VALUES ("...)

	for i, field := range table.Fields {
		if field.Has(PrimaryKeyFlag) && field.IsEmpty(strct) {
			continue
		}
		b = field.AppendValue(b, strct, true)
		if i != len(table.Fields)-1 {
			b = append(b, ", "...)
		}
	}
	b = append(b, ")"...)

	b = appendReturning(b, strct, table.PKs)

	return b, nil
}
Esempio n. 2
0
func (upd updateModel) AppendQuery(b []byte, params ...interface{}) ([]byte, error) {
	table := upd.model.Table()
	strct := upd.model.Value()

	for _, pk := range table.PKs {
		if pk.IsEmpty(strct) {
			return nil, errors.New("pg: primary key is empty")
		}
	}

	b = append(b, "UPDATE "...)
	b = types.AppendField(b, table.Name, true)
	b = append(b, " SET "...)

	if len(upd.columns) > 0 {
		for i, v := range upd.columns {
			column, err := v.AppendValue(nil, false)
			if err != nil {
				return nil, err
			}

			field, err := table.GetField(string(column))
			if err != nil {
				return nil, err
			}

			b = types.AppendField(b, field.SQLName, true)
			b = append(b, " = "...)
			b = field.AppendValue(b, strct, true)
			if i != len(upd.columns)-1 {
				b = append(b, ", "...)
			}
		}
	} else {
		for i, field := range table.Fields {
			if field.Has(PrimaryKeyFlag) {
				continue
			}

			b = types.AppendField(b, field.SQLName, true)
			b = append(b, " = "...)
			b = field.AppendValue(b, strct, true)
			if i != len(table.Fields)-1 {
				b = append(b, ", "...)
			}
		}
	}

	b = append(b, " WHERE "...)
	b = appendFieldValue(b, strct, table.PKs)

	if len(upd.returning) > 0 {
		b = append(b, " RETURNING "...)
		b = appendValue(b, ", ", upd.returning...)
	}

	return b, nil
}
Esempio n. 3
0
func TestAppendField(t *testing.T) {
	for _, test := range appendFieldTests {
		got := types.AppendField(nil, test.field, true)
		if string(got) != test.wanted {
			t.Errorf("got %q, wanted %q (field=%q)", got, test.wanted, test.field)
		}
	}
}
Esempio n. 4
0
func (del deleteQuery) AppendQuery(b []byte, params ...interface{}) ([]byte, error) {
	b = append(b, "DELETE FROM "...)
	b = types.AppendField(b, del.model.Table().Name, true)

	b = append(b, " WHERE "...)
	b = appendBytes(b, " AND ", del.wheres...)

	return b, nil
}
Esempio n. 5
0
func appendField(b []byte, ss ...string) []byte {
	for i, field := range ss {
		b = types.AppendField(b, field, true)
		if i != len(ss)-1 {
			b = append(b, ", "...)
		}
	}
	return b
}
Esempio n. 6
0
func columns(prefix string, fields []*Field) []byte {
	var b []byte
	for i, f := range fields {
		b = types.AppendField(b, prefix+f.SQLName, true)
		if i != len(fields)-1 {
			b = append(b, ", "...)
		}
	}
	return b
}
Esempio n. 7
0
func appendFieldValue(b []byte, v reflect.Value, fields []*Field) []byte {
	for i, f := range fields {
		b = types.AppendField(b, f.SQLName, true)
		b = append(b, " = "...)
		b = f.AppendValue(b, v, true)
		if i != len(fields)-1 {
			b = append(b, " AND "...)
		}
	}
	return b
}
Esempio n. 8
0
func (upd updateQuery) AppendQuery(b []byte, params ...interface{}) ([]byte, error) {
	b = append(b, "UPDATE "...)
	b = types.AppendField(b, upd.model.Table().Name, true)
	b = append(b, " SET "...)

	for fieldName, value := range upd.data {
		b = types.AppendField(b, fieldName, true)
		b = append(b, " = "...)
		b = types.Append(b, value, true)
		b = append(b, ", "...)
	}
	b = b[:len(b)-2]

	b = append(b, " WHERE "...)
	b = appendBytes(b, " AND ", upd.wheres...)

	if len(upd.returning) > 0 {
		b = append(b, " RETURNING "...)
		b = appendValue(b, ", ", upd.returning...)
	}

	return b, nil
}
Esempio n. 9
0
func appendReturning(b []byte, v reflect.Value, fields []*Field) []byte {
	var hasReturning bool
	for i, f := range fields {
		if !f.IsEmpty(v) {
			continue
		}
		if !hasReturning {
			b = append(b, " RETURNING "...)
			hasReturning = true
		}
		b = types.AppendField(b, f.SQLName, true)
		if i != len(fields)-1 {
			b = append(b, ", "...)
		}
	}
	return b
}
Esempio n. 10
0
func (m *StructModel) AppendParam(b []byte, name string) ([]byte, error) {
	switch name {
	case "Table":
		return types.AppendField(b, m.table.Name, true), nil
	case "PK":
		return appendPKs(b, m.table.PKs), nil
	}

	if field, ok := m.table.FieldsMap[name]; ok {
		return field.AppendValue(b, m.strct, true), nil
	}

	if method, ok := m.table.Methods[name]; ok {
		return method.AppendValue(b, m.strct.Addr(), true), nil
	}

	return nil, fmt.Errorf("pg: can't map %q on %s", name, m.strct.Type())
}
Esempio n. 11
0
func (del deleteModel) AppendQuery(b []byte, params ...interface{}) ([]byte, error) {
	table := del.Table()
	strct := del.Value()

	for _, pk := range table.PKs {
		if pk.IsEmpty(strct) {
			return nil, errors.New("pg: primary key is empty")
		}
	}

	b = append(b, "DELETE FROM "...)
	b = types.AppendField(b, table.Name, true)

	b = append(b, " WHERE "...)
	b = appendFieldValue(b, strct, table.PKs)

	return b, nil
}
Esempio n. 12
0
func (sel selectQuery) AppendQuery(b []byte, params ...interface{}) ([]byte, error) {
	b = append(b, "SELECT "...)
	if sel.columns == nil {
		b = types.AppendField(b, sel.model.Table().ModelName, true)
		b = append(b, ".*"...)
	} else {
		b = appendValue(b, ", ", sel.columns...)
	}

	b = append(b, " FROM "...)
	b = appendField(b, sel.tables...)

	if sel.joins != nil {
		b = append(b, ' ')
		b = appendBytes(b, " ", sel.joins...)
	}

	if sel.wheres != nil {
		b = append(b, " WHERE "...)
		b = appendBytes(b, " AND ", sel.wheres...)
	}

	if sel.orders != nil {
		b = append(b, " ORDER BY "...)
		b = appendString(b, ", ", sel.orders...)
	}

	if sel.limit != 0 {
		b = append(b, " LIMIT "...)
		b = strconv.AppendInt(b, int64(sel.limit), 10)
	}

	if sel.offset != 0 {
		b = append(b, " OFFSET "...)
		b = strconv.AppendInt(b, int64(sel.offset), 10)
	}

	return b, nil
}