Example #1
0
// ScanOne is to set a extracted data into parameter variable
func (ms *MS) ScanOne(x interface{}) bool {
	//defer tm.Track(time.Now(), "ScanOne()")
	//ScanOne() took 5.23µs

	if ms.Err != nil {
		lg.Errorf("ScanOne(): ms.Err has error: %s", ms.Err)
		return false
	}

	//e.g. v = person Person
	v := reflect.ValueOf(x)
	if v.Kind() != reflect.Ptr || v.IsNil() {
		ms.Err = errors.New("parameter is not valid. it should be pointer and not nil")
		return false
	}

	// initialize variable
	var values, scanArgs []interface{}
	if v.Elem().Kind() == reflect.Struct {
		//create container to set scaned record on database
		values, scanArgs = makeScanStructArgs(v.Elem().Type())

		//check len(value) and column
		validateStructAndColumns(ms, values)
		if ms.Err != nil {
			return false
		}
	}

	// rows.Next()
	ret := ms.Rows.Next()
	if !ret {
		//data is nothing
		return false
	}

	if v.Elem().Kind() == reflect.Struct {
		// rows.Scan()
		ms.Err = ms.Rows.Scan(scanArgs...)
		if ms.Err != nil {
			return false
		}

		//ms.Err = ms.Rows.Scan(v)
		scanStruct(values, v.Elem())
	} else {
		// rows.Scan()
		ms.Err = ms.Rows.Scan(x)
		if ms.Err != nil {
			return false
		}
		//scanValue(values[0], v.Elem())
	}

	return true
}
Example #2
0
func connection() {
	c := conf.GetConf().Cassa

	hosts := []string{c.Host}
	//port := 9042
	err := New(hosts, int(c.Port), c.KeySpace)
	if err != nil {
		lg.Errorf("New() error: %s", err)
	}
}
Example #3
0
//open browser on PC
func openBrowser(ths []th.Info) {
	for _, t := range ths {
		//during test, it doesn't work.
		//open browser error: exec: "open": executable file not found in $PATH

		//out, err := exec.Command("open /Applications/Google\\ Chrome.app", fmt.Sprintf("http://eikaiwa.dmm.com/teacher/index/%d/", id)).Output()
		err := exec.Command("open", fmt.Sprintf("http://eikaiwa.dmm.com/teacher/index/%d/", t.ID)).Start()
		if err != nil {
			//panic(fmt.Sprintf("open browser error: %v", err))
			lg.Errorf(fmt.Sprintf("open browser error: %v", err))
		}
	}
}
Example #4
0
// SelectIns is to get rows and return db instance
func (ms *MS) SelectIns(selectSQL string, args ...interface{}) *MS {
	defer tm.Track(time.Now(), "SelectIns()")
	//SelectSQLAllFieldIns() took 471.577µs

	//If no args, set nil

	//1. create sql and exec
	//rows, err := self.db.Query("SELECT * FROM t_users WHERE delete_flg=?", "0")
	ms.Rows, ms.Err = ms.DB.Query(selectSQL, args...)
	if ms.Err != nil {
		lg.Errorf("SelectSQLAllFieldIns()->ms.DB.Query():error is %s, \n %s", ms.Err, selectSQL)
	}

	return ms
}
Example #5
0
// Save is to save data on Redis
func (rd *StoreRedis) Save(newData string) bool {
	lg.Debug("Using Redis")

	//close
	//defer rd.RD.Close()

	c := rd.RD.Conn
	val, err := redis.String(c.Do("GET", redisKey))

	if err != nil {
		lg.Errorf("redis error is %s\n", err)
	}
	lg.Debugf("new value is %s, old value is %s\n", newData, val)

	if err != nil || newData != val {
		//save
		c.Do("SET", redisKey, newData)
		return true
	}
	return false
}
Example #6
0
// Scan is to set all extracted data into parameter variable
func (ms *MS) Scan(x interface{}) bool {
	//defer tm.Track(time.Now(), "Scan()")
	//Scan() took 465.971µs

	if ms.Err != nil {
		lg.Errorf("Scan(): ms.Err has error: %s", ms.Err)
		return false
	}

	//e.g. v = persons []Person
	v := reflect.ValueOf(x)

	if v.Kind() != reflect.Ptr || v.IsNil() {
		ms.Err = errors.New("parameter is not valid. it should be pointer and not nil")
		return false
	}

	if v.Elem().Kind() == reflect.Slice || v.Elem().Kind() == reflect.Array {
		elemType := v.Elem().Type().Elem() //reflects_test.TeacherInfo
		newElem := reflect.New(elemType).Elem()

		var values, scanArgs []interface{}
		if newElem.Kind() == reflect.Struct {
			//create container to set scaned record on database
			values, scanArgs = makeScanStructArgs(newElem.Type())
		} else {
			// Note! Only one filed
			values = make([]interface{}, 1)
			scanArgs = make([]interface{}, 1)
			scanArgs[0] = &values[0]
		}

		//check len(value) and column
		validateStructAndColumns(ms, values)
		if ms.Err != nil {
			return false
		}

		// rows.Next()
		cnt := 0
		for ms.Rows.Next() {
			ms.Err = ms.Rows.Scan(scanArgs...)
			if ms.Err != nil {
				return false
			}

			if newElem.Kind() == reflect.Struct {
				scanStruct(values, newElem)
			} else {
				scanValue(values[0], newElem)
			}
			v.Elem().Set(reflect.Append(v.Elem(), newElem))
			cnt++
		}
		if cnt == 0 {
			return false
		}
		return true
	}

	ms.Err = errors.New("parameter is not valid. it should be pointer and not nil")
	return false

}