Exemple #1
0
func InitDb(dst string) (*gorp.DbMap, error) {
	db, err := sql.Open("sqlite3", dst)
	if err != nil {
		return nil, err
	}
	dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}

	var t *gorp.TableMap
	t = dbmap.AddTable(EquipmentInfo{}).SetKeys(true, "ID")
	t.SetUniqueTogether(
		"CertifiedName",
		"EquipmentType",
		"Model",
		"AuthNumber",
		"RadioType",
		"AuthDate",
	)
	t = dbmap.AddTable(RadioAccessTechnology{}).SetKeys(true, "ID")
	t.ColMap("EquipmentType").SetUnique(true)

	if err := dbmap.CreateTablesIfNotExists(); err != nil {
		return nil, err
	}

	for k, v := range ratDef {
		obj := RadioAccessTechnology{EquipmentType: k, Description: v}
		if err := dbmap.Insert(&obj); err != nil {
			if !strings.HasPrefix(err.Error(), "UNIQUE constraint failed") {
				return nil, err
			}
		}
	}

	return dbmap, nil
}
Exemple #2
0
// mapColumns creates a list of field addresses and column maps, to
// make looking up the column for a field address easier.  Note that
// it doesn't do any special handling for overridden fields, because
// passing the address of a field that has been overridden is
// difficult to do accidentally.
func (plan *QueryPlan) mapColumns(table *gorp.TableMap, value reflect.Value) (err error) {
	value = value.Elem()
	valueType := value.Type()
	if plan.colMap == nil {
		plan.colMap = make(structColumnMap, 0, value.NumField())
	}
	queryableFields := 0
	quotedTableName := plan.dbMap.Dialect.QuotedTableForQuery(table.SchemaName, table.TableName)
	for i := 0; i < value.NumField(); i++ {
		fieldType := valueType.Field(i)
		fieldVal := value.Field(i)
		if fieldType.Anonymous {
			if fieldVal.Kind() != reflect.Ptr {
				fieldVal = fieldVal.Addr()
			} else if fieldVal.IsNil() {
				// embedded types must be initialized for querying
				fieldVal.Set(reflect.New(fieldVal.Type().Elem()))
			}
			plan.mapColumns(table, fieldVal)
		} else if fieldType.PkgPath == "" {
			col := table.ColMap(fieldType.Name)
			quotedCol := plan.dbMap.Dialect.QuoteField(col.ColumnName)
			fieldMap := fieldColumnMap{
				addr:         fieldVal.Addr().Interface(),
				column:       col,
				quotedTable:  quotedTableName,
				quotedColumn: quotedCol,
			}
			plan.colMap = append(plan.colMap, fieldMap)
			if !col.Transient {
				queryableFields++
			}
		}
	}
	if queryableFields == 0 {
		return errors.New("No fields in the target struct are mappable.")
	}
	return
}
Exemple #3
0
func InitDB() *gorp.DbMap {
	//db, err := sql.Open("mssql", "server=127.0.0.1;user id=sa;password=sa;database=DB_DEMO;encrypt=disable")
	db, err := sql.Open("mysql", "root:root@/db_hygl?charset=utf8")
	checkErr(err, "sql.Open failed")

	// construct a gorp DbMap
	dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "utf8"}}

	//*kazarus:important
	dbmap.AddTableWithName(TUSER{}, "TBL_USER_GO")
	dbmap.AddTableWithName(TDICT{}, "TBL_DICT")

	var t *gorp.TableMap
	t = dbmap.AddTableWithName(TDICT{}, "TBL_DICT")
	fmt.Println(t.SqlForCreate(false))
	fmt.Println(t.Columns[0].ColumnName)

	//create the table. in a production system you'd generally
	//use a migration tool, or create the tables via scripts
	//err = dbmap.CreateTablesIfNotExists()
	//checkErr(err, "Create tables failed")

	return dbmap
}
Exemple #4
0
func SetNotNull(cm *gorp.TableMap, colName ...string) {
	for _, n := range colName {
		cm.ColMap(n).SetNotNull(true)
	}
}