Ejemplo n.º 1
0
func (attr Bytes) Encode(w *pgx.WriteBuf, oid pgx.Oid) error {
	if oid != pgx.ByteaOid {
		return pgx.SerializationError(fmt.Sprintf("Bytes.Encode cannot encode into OID %d", oid))
	}

	if attr.Status != Present {
		w.WriteInt32(-1)
		return nil
	}

	w.WriteBytes(attr.Value)
	return nil
}
Ejemplo n.º 2
0
func (p *NullPoint) Scan(vr *pgx.ValueReader) error {
	if vr.Type().DataTypeName != "point" {
		return pgx.SerializationError(fmt.Sprintf("NullPoint.Scan cannot decode %s (OID %d)", vr.Type().DataTypeName, vr.Type().DataType))
	}

	if vr.Len() == -1 {
		p.X, p.Y, p.Valid = 0, 0, false
		return nil
	}

	switch vr.Type().FormatCode {
	case pgx.TextFormatCode:
		s := vr.ReadString(vr.Len())
		match := pointRegexp.FindStringSubmatch(s)
		if match == nil {
			return pgx.SerializationError(fmt.Sprintf("Received invalid point: %v", s))
		}

		var err error
		p.X, err = strconv.ParseFloat(match[1], 64)
		if err != nil {
			return pgx.SerializationError(fmt.Sprintf("Received invalid point: %v", s))
		}
		p.Y, err = strconv.ParseFloat(match[2], 64)
		if err != nil {
			return pgx.SerializationError(fmt.Sprintf("Received invalid point: %v", s))
		}
	case pgx.BinaryFormatCode:
		return errors.New("binary format not implemented")
	default:
		return fmt.Errorf("unknown format %v", vr.Type().FormatCode)
	}

	p.Valid = true
	return vr.Err()
}
Ejemplo n.º 3
0
func (attr *IPNet) Encode(w *pgx.WriteBuf, oid pgx.Oid) error {
	if oid != pgx.InetOid && oid != pgx.CidrOid {
		return pgx.SerializationError(fmt.Sprintf("IPNet.Encode cannot encode into OID %d", oid))
	}

	switch attr.Status {
	case Present:
		return pgx.Encode(w, oid, attr.Value)
	case Null:
		w.WriteInt32(-1)
		return nil
	case Undefined:
		return errors.New("cannot encode undefined attr")
	default:
		panic("unreachable")
	}
}
Ejemplo n.º 4
0
func (attr *Int64) Scan(vr *pgx.ValueReader) error {
	if vr.Type().DataType != pgx.Int8Oid {
		return pgx.SerializationError(fmt.Sprintf("Int64.Scan cannot decode OID %d", vr.Type().DataType))
	}

	if vr.Len() == -1 {
		attr.Value = 0
		attr.Status = Null
		return nil
	}

	err := pgx.Decode(vr, &attr.Value)
	if err != nil {
		return err
	}
	attr.Status = Present

	return vr.Err()
}
Ejemplo n.º 5
0
func (attr *IPNet) Scan(vr *pgx.ValueReader) error {
	oid := vr.Type().DataType
	if oid != pgx.InetOid && oid != pgx.CidrOid {
		return pgx.SerializationError(fmt.Sprintf("IPNet.Scan cannot decode OID %d", vr.Type().DataType))
	}

	if vr.Len() == -1 {
		attr.Value = net.IPNet{}
		attr.Status = Null
		return nil
	}

	attr.Status = Present
	err := pgx.Decode(vr, &attr.Value)
	if err != nil {
		return err
	}

	return vr.Err()
}
Ejemplo n.º 6
0
func (attr *Time) Scan(vr *pgx.ValueReader) error {
	oid := vr.Type().DataType
	if oid != pgx.TimestampTzOid && oid != pgx.TimestampOid && oid != pgx.DateOid {
		return pgx.SerializationError(fmt.Sprintf("Time.Scan cannot decode OID %d", vr.Type().DataType))
	}

	if vr.Len() == -1 {
		attr.Value = time.Time{}
		attr.Status = Null
		return nil
	}

	err := pgx.Decode(vr, &attr.Value)
	if err != nil {
		return err
	}
	attr.Status = Present

	return vr.Err()
}