Example #1
0
// AddTable registers the given interface type with modl. The table name
// will be given the name of the TypeOf(i), lowercased.
//
// This operation is idempotent. If i's type is already mapped, the
// existing *TableMap is returned
func (m *DbMap) AddTable(i interface{}, name ...string) *TableMap {
	Name := ""
	if len(name) > 0 {
		Name = name[0]
	}

	t := reflect.TypeOf(i)
	if len(Name) == 0 {
		Name = sqlx.NameMapper(t.Name())
	}

	// check if we have a table for this type already
	// if so, update the name and return the existing pointer
	for i := range m.tables {
		table := m.tables[i]
		if table.gotype == t {
			table.TableName = Name
			return table
		}
	}

	tmap := &TableMap{gotype: t, TableName: Name, dbmap: m}

	n := t.NumField()
	tmap.columns = make([]*ColumnMap, 0, n)
	for i := 0; i < n; i++ {
		f := t.Field(i)
		columnName := f.Tag.Get("db")
		if columnName == "" {
			columnName = sqlx.NameMapper(f.Name)
		}

		cm := &ColumnMap{
			ColumnName: columnName,
			Transient:  columnName == "-",
			fieldName:  f.Name,
			gotype:     f.Type,
		}
		tmap.columns = append(tmap.columns, cm)
	}
	m.tables = append(m.tables, tmap)
	return tmap
}
Example #2
0
// SetKeys lets you specify the fields on a struct that map to primary
// key columns on the table.  If isAutoIncr is set, result.LastInsertId()
// will be used after INSERT to bind the generated id to the Go struct.
//
// Automatically calls ResetSql() to ensure SQL statements are regenerated.
func (t *TableMap) SetKeys(isAutoIncr bool, fieldNames ...string) *TableMap {
	t.Keys = make([]*ColumnMap, 0)
	for _, name := range fieldNames {
		colmap := t.ColMap(sqlx.NameMapper(name))
		colmap.isPK = true
		colmap.isAutoIncr = isAutoIncr
		t.Keys = append(t.Keys, colmap)
	}
	t.ResetSql()

	return t
}
Example #3
0
// AddTable registers the given interface type with modl. The table name
// will be given the name of the TypeOf(i), lowercased.
//
// This operation is idempotent. If i's type is already mapped, the
// existing *TableMap is returned.
func (m *DbMap) AddTable(i interface{}, name ...string) *TableMap {
	Name := ""
	if len(name) > 0 {
		Name = name[0]
	}

	t := reflect.TypeOf(i)
	// Use sqlx's NameMapper function if no name is supplied
	if len(Name) == 0 {
		Name = TableNameMapper(t.Name())
	}

	// check if we have a table for this type already
	// if so, update the name and return the existing pointer
	for i := range m.tables {
		table := m.tables[i]
		if table.gotype == t {
			table.TableName = Name
			return table
		}
	}

	tmap := &TableMap{gotype: t, TableName: Name, dbmap: m, mapper: m.mapper}
	tmap.setupHooks(i)

	n := t.NumField()
	tmap.Columns = make([]*ColumnMap, 0, n)
	for i := 0; i < n; i++ {
		f := t.Field(i)
		columnName := f.Tag.Get("db")
		if columnName == "" {
			columnName = sqlx.NameMapper(f.Name)
		}

		cm := &ColumnMap{
			ColumnName: columnName,
			Transient:  columnName == "-",
			fieldName:  f.Name,
			gotype:     f.Type,
			table:      tmap,
		}
		tmap.Columns = append(tmap.Columns, cm)
		if cm.fieldName == "Version" {
			tmap.version = tmap.Columns[len(tmap.Columns)-1]
		}
	}
	m.tables = append(m.tables, tmap)

	return tmap

}
Example #4
0
// SetKeys lets you specify the fields on a struct that map to primary
// key columns on the table.  If isAutoIncr is set, result.LastInsertId()
// will be used after INSERT to bind the generated id to the Go struct.
//
// Automatically calls ResetSql() to ensure SQL statements are regenerated.
func (t *TableMap) SetKeys(isAutoIncr bool, fieldNames ...string) *TableMap {
	t.Keys = make([]*ColumnMap, 0)
	for _, name := range fieldNames {
		// FIXME: sqlx.NameMapper is a deprecated API.  modl should have its
		// own API which sets sqlx's mapping funcs as necessary
		colmap := t.ColMap(sqlx.NameMapper(name))
		colmap.isPK = true
		colmap.isAutoIncr = isAutoIncr
		t.Keys = append(t.Keys, colmap)
	}
	t.ResetSql()

	return t
}
Example #5
0
func columnMaps(t reflect.Type, tmap *TableMap, parentFieldIdx []int) []*ColumnMap {
	var cols []*ColumnMap
	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)
		name := f.Tag.Get("db")
		if f.Anonymous {
			cols = append(cols, columnMaps(f.Type, tmap, makeFieldIdx(parentFieldIdx, i))...)
		} else {
			if name == "" {
				name = sqlx.NameMapper(f.Name)
			}
			cols = append(cols, &ColumnMap{
				ColumnName: name,
				Transient:  name == "-",
				fieldName:  f.Name,
				fieldIdx:   makeFieldIdx(parentFieldIdx, i),
				gotype:     f.Type,
				table:      tmap,
			})
		}
	}
	return cols
}
Example #6
0
// QuoteField quotes f with ""
func (d PostgresDialect) QuoteField(f string) string {
	return `"` + sqlx.NameMapper(f) + `"`
}