예제 #1
0
// Open a go routine to run this source iteration until signal/complete
func SourceIterChannel(iter schema.Iterator, sigCh <-chan bool) <-chan schema.Message {

	out := make(chan schema.Message, 100)

	go func() {
		defer func() {
			if r := recover(); r != nil {
				u.Errorf("recover panic: %v", r)
			}
			// Can we safely close this?
			close(out)
		}()
		for item := iter.Next(); item != nil; item = iter.Next() {

			//u.Infof("In source Scanner iter %#v", item)
			select {
			case <-sigCh:
				u.Warnf("got signal quit")

				return
			case out <- item:
				// continue
			}
		}
	}()
	return out
}
예제 #2
0
func IntrospectTable(tbl *schema.Table, iter schema.Iterator) error {

	nameIndex := make(map[int]string, len(tbl.Columns()))
	for i, colName := range tbl.Columns() {
		nameIndex[i] = colName
	}
	//u.Infof("s:%s INTROSPECT SCHEMA name %q", s, name)
	ct := 0
	for {
		msg := iter.Next()
		//u.Debugf("msg %#v", msg)
		if msg == nil || ct > IntrospectCount {
			break
		}
		switch mt := msg.Body().(type) {
		case []driver.Value:
			for i, v := range mt {

				k := nameIndex[i]
				_, exists := tbl.FieldMap[k]

				//u.Debugf("i:%v k:%s  v: %T %v", i, k, v, v)
				switch val := v.(type) {
				case int, int64, int16, int32, uint16, uint64, uint32:
					tbl.AddFieldType(k, value.IntType)
				case time.Time, *time.Time:
					tbl.AddFieldType(k, value.TimeType)
				case bool:
					tbl.AddFieldType(k, value.BoolType)
				case float32, float64:
					tbl.AddFieldType(k, value.NumberType)
				case string:
					valType := guessValueType(val)
					if !exists {
						tbl.AddFieldType(k, valType)
						//fld := tbl.FieldMap[k]
						//u.Debugf("add field? %+v", fld)
						//u.Debugf("%s = %v   type: %T   vt:%s new? %v", k, val, val, valType, !exists)
					}
				default:
					u.Warnf("not implemented: %T", val)
				}
			}
		case *SqlDriverMessageMap:
			for i, v := range mt.Vals {

				k := nameIndex[i]
				_, exists := tbl.FieldMap[k]

				//u.Debugf("i:%v k:%s  v: %T %v", i, k, v, v)
				switch val := v.(type) {
				case int, int64, int16, int32, uint16, uint64, uint32:
					tbl.AddFieldType(k, value.IntType)
				case time.Time, *time.Time:
					tbl.AddFieldType(k, value.TimeType)
				case bool:
					tbl.AddFieldType(k, value.BoolType)
				case float32, float64:
					tbl.AddFieldType(k, value.NumberType)
				case string:
					valType := guessValueType(val)
					if !exists {
						tbl.AddFieldType(k, valType)
						//fld := tbl.FieldMap[k]
						//u.Debugf("add field? %+v", fld)
						//u.Debugf("%s = %v   type: %T   vt:%s new? %v", k, val, val, valType, !exists)
					}
				default:
					u.Warnf("not implemented: %T", val)
				}
			}
		default:
			u.Warnf("not implemented: %T", mt)
		}

		ct++
	}
	return nil
}