Пример #1
0
func (b *Backend) DefineField(db *sql.DB, m driver.Model, table *sql.Table, field *sql.Field) (string, []string, error) {
	if field.HasOption(sql.OptionAutoIncrement) {
		if field.Constraint(sql.ConstraintPrimaryKey) == nil {
			return "", nil, fmt.Errorf("%s can only auto increment the primary key", b.Name())
		}
	}
	def, constraints, err := b.SqlBackend.DefineField(db, m, table, field)
	if err == nil {
		def = strings.Replace(strings.Replace(def, "DEFAULT false", "DEFAULT 0", -1), "DEFAULT true", "DEFAULT 1", -1)
	}
	return def, constraints, err
}
Пример #2
0
func (b *Backend) DefineField(db *sql.DB, m driver.Model, table *sql.Table, field *sql.Field) (string, []string, error) {
	def, cons, err := b.SqlBackend.DefineField(db, m, table, field)
	if err != nil {
		return "", nil, err
	}
	if ref := field.Constraint(sql.ConstraintForeignKey); ref != nil {
		if pos := strings.Index(def, " REFERENCES"); pos >= 0 {
			def = def[:pos]
		}
		refTable := ref.References.Table()
		refField := ref.References.Field()
		fkName := db.QuoteIdentifier(fmt.Sprintf("%s_%s_%s_%s", m.Table(), field.Name, refTable, refField))
		cons = append(cons, fmt.Sprintf("FOREIGN KEY %s(%s) REFERENCES %s(%s)", fkName, db.QuoteIdentifier(field.Name),
			db.QuoteIdentifier(refTable), db.QuoteIdentifier(refField)))
	}
	return strings.Replace(def, "AUTOINCREMENT", "AUTO_INCREMENT", -1), cons, nil
}
Пример #3
0
func (b *Backend) canAddField(f *sql.Field) bool {
	// These are a supeset of the actual resctrictions, for
	// simplicity. See https://www.sqlite.org/lang_altertable.html
	// for more details.
	return f.Constraint(sql.ConstraintPrimaryKey) != nil && f.Constraint(sql.ConstraintUnique) == nil && f.Default == ""
}