Exemplo n.º 1
0
// Convert result of select into Map[] type. Return multiple array map and interface(plural lines)
func (ms *MS) convertRowsToMaps(rows *sql.Rows) ([]map[string]interface{}, []string, error) {
	defer tm.Track(time.Now(), "convertRowsToMaps()")
	//convertRowsToMaps() took 85.191µs

	// Get column name
	columns, err := rows.Columns()
	if err != nil {
		return nil, nil, err
	}

	values := make([]interface{}, len(columns))

	// rows.Scan requires `[]interface{}` as parameter
	scanArgs := make([]interface{}, len(values))

	for i := range values {
		scanArgs[i] = &values[i]
	}

	retMaps := []map[string]interface{}{}
	//
	for rows.Next() { //true or false
		//Get data into scanArgs
		err = rows.Scan(scanArgs...)

		if err != nil {
			return nil, columns, err
		}

		rowData := map[string]interface{}{}

		//var v string
		for i, value := range values {
			if u.CheckInterface(value) == "[]uint8" {
				value = u.ItoBS(value)
			} else if u.CheckInterface(value) == "time.Time" {
				value = u.ItoT(value).Format(tFomt)
			}

			// Here we can check if the value is nil (NULL value)
			//if value == nil {
			//	v = "NULL"
			//} else {
			//	v = string(value)
			//}

			//if b, ok := value.([]byte); ok{
			//	v = string(b)
			//} else {
			//	v = "NULL"
			//}

			//rowdata[columns[i]] = v
			rowData[columns[i]] = value
		}
		retMaps = append(retMaps, rowData)
	}
	return retMaps, columns, nil
}
Exemplo n.º 2
0
// Select is to get all field you set
func (ms *MS) Select(selectSQL string, args ...interface{}) ([]map[string]interface{}, []string, error) {
	defer tm.Track(time.Now(), "SelectSQLAllField()")
	//540.417µs

	//create sql and exec
	rows, err := ms.DB.Query(selectSQL, args...)
	if err != nil {
		return nil, nil, err
	}

	return ms.convertRowsToMaps(rows)
}
Exemplo n.º 3
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
}
Exemplo n.º 4
0
//handle each teacher data
func handleTeachers(si *th.SiteInfo) {
	defer tm.Track(time.Now(), "handleTeachers()")

	wg := &sync.WaitGroup{}
	chanSemaphore := make(chan bool, MaxGoRoutine)

	//si.Teachers
	for _, teacher := range si.Teachers {
		wg.Add(1)
		chanSemaphore <- true

		//chanSemaphore <- true
		go func(t th.Info) {
			defer func() {
				<-chanSemaphore
				wg.Done()
			}()
			//concurrent func
			t.GetHTML(si.URL)
		}(teacher)
	}
	wg.Wait()
}