Esempio n. 1
0
func (tbl *Table) NamedQueryTx(tx sqlx.Ext, cmd string, val interface{}) (*sqlx.Rows, error) {
	if tx == nil {
		tx = tbl.Db.DB
	}
	log.Println("Cmd:", cmd)
	rows, err := sqlx.NamedQuery(tx, cmd, val)
	if err != nil {
		log.Println("NamedQuery: Error:", err)
		return nil, err
	}
	return rows, err
}
Esempio n. 2
0
//Get Entry by using Arbitrary Key
func (tbl *Table) GetBy(field string, val interface{}) error {
	field = tbl.FieldName(field)
	cmd := "select * from " + tbl.Name + " where " + field + "=:" + field
	log.Println("Cmd:", cmd)
	rows, err := sqlx.NamedQuery(tbl.Db.DB, cmd, val)
	if rows != nil {
		defer rows.Close()
	}
	if err == nil && !rows.Next() {
		err = sql.ErrNoRows
	}
	if err != nil {
		log.Println("GetBy: Error:", err)
		return err
	}

	//log.Println(">>>>>>>>>>>>:", rows, RowsDump(rows))
	//row := sqlx.NewRow(&rows.Rows, err)
	return rows.StructScan(val)
}
Esempio n. 3
0
func (tbl *TableCached) ReadChildrenAtUncached(ctIdx int, ent EntryCached, rwp *RWParams) bool {
	ct := tbl.Children[ctIdx]

	if ct.Table.Cache == nil && !ct.Ids {
		cmd := "select * from " + ct.Table.Table.Name + " where " + tbl.Table.PrimKey + "=:" + tbl.Table.PrimKey
		rows, err := sqlx.NamedQuery(ct.Table.Table.Db.DB, cmd, ent)
		if rows == nil || err != nil {
			return false
		}
		defer rows.Close()
		sl := reflect.MakeSlice(ct.SliceType, 0, 32)
		for rows.Next() {
			ent := ct.Table.Table.NewValue().(EntryCached)
			rows.StructScan(ent)
			sl = reflect.Append(sl, reflect.ValueOf(ent))
		}
		refval := reflect.Indirect(reflect.ValueOf(ent))
		refval.Field(ct.VarIdx).Set(sl)

		id := refval.Field(tbl._idxid).Interface()
		log.Println("Cmd:", cmd, "Id:", id, "Res:", sl.Len())
		return true
	}

	keys, err := ct.Table.Table.GetPrimKeys(tbl.Table.PrimKey, ent)
	if err != nil {
		return false
	}

	if ct.Ids {
		sl := reflect.MakeSlice(ct.SliceType, len(keys), len(keys))
		for i, v := range keys {
			sl.Index(i).Set(reflect.ValueOf(v))
		}
		refval := reflect.Indirect(reflect.ValueOf(ent))
		refval.Field(ct.VarIdx).Set(sl)
		return true

	}

	sl := reflect.MakeSlice(ct.SliceType, 0, len(keys))

	for _, v := range keys {
		ent := ct.Table.Table.NewValue().(EntryCached)
		refval := reflect.Indirect(reflect.ValueOf(ent))
		switch ct.Table.Table.PrimType {
		case COLTYPE_STRING:
			refval.Field(ct.Table._idxid).SetString(v.(string))
			break
		case COLTYPE_INT:
			refval.Field(ct.Table._idxid).SetInt(v.(int64))
			break
		case COLTYPE_FLOAT:
			refval.Field(ct.Table._idxid).SetFloat(v.(float64))
			break
		}
		if !ct.Table.ReadParam(ent, rwp) {
			//log.Println("err")
			continue
		}
		sl = reflect.Append(sl, reflect.ValueOf(ent))
	}

	refval := reflect.Indirect(reflect.ValueOf(ent))
	refval.Field(ct.VarIdx).Set(sl)
	return true
}