Esempio n. 1
0
func (q *Query) generateIndex(filters []*dbox.Filter) (output []int, e error) {
	var n int = 0
	for {
		tdread, e := q.reader.Read()
		if e != nil && e != io.EOF {
			break
		}
		n++

		tm := toolkit.M{}

		for i, v := range tdread {
			tolower := strings.ToLower(q.headerColumn[i].name)
			tm.Set(tolower, v)
			if q.headerColumn[i].dataType == "int" {
				tm[tolower] = cast.ToInt(v, cast.RoundingAuto)
			} else if q.headerColumn[i].dataType == "float" {
				tm[tolower] = cast.ToF64(v, (len(v) - (strings.IndexAny(v, "."))), cast.RoundingAuto)
			}
		}

		match := dbox.MatchM(tm, filters)
		if (len(filters) == 0 || match) && len(tm) > 0 {
			output = append(output, n)
		}

		if e == io.EOF {
			break
		}
	}

	e = q.resetReader()
	return
}
Esempio n. 2
0
func (q *Query) execQueryPartDelete(Cond QueryCondition) error {

	writer := q.Connection().(*Connection).writer
	reader := q.Connection().(*Connection).reader
	tempHeader := []string{}

	for _, val := range q.Connection().(*Connection).headerColumn {
		tempHeader = append(tempHeader, val.name)
	}

	for {
		foundDelete := true
		recData := toolkit.M{}

		dataTemp, e := reader.Read()
		for i, val := range dataTemp {
			recData.Set(tempHeader[i], val)
			if q.Connection().(*Connection).headerColumn[i].dataType == "int" {
				recData[tempHeader[i]] = cast.ToInt(val, cast.RoundingAuto)
			} else if q.Connection().(*Connection).headerColumn[i].dataType == "float" {
				recData[tempHeader[i]] = cast.ToF64(val, (len(val) - (strings.IndexAny(val, "."))), cast.RoundingAuto)
			}
		}

		foundDelete = Cond.getCondition(recData)

		if e == io.EOF {
			if !foundDelete && dataTemp != nil {
				writer.Write(dataTemp)
				writer.Flush()
			}
			break
		} else if e != nil {
			return errorlib.Error(packageName, modQuery, "Delete", e.Error())
		}

		if !foundDelete && dataTemp != nil {
			writer.Write(dataTemp)
			writer.Flush()
		}
	}

	return nil

}
Esempio n. 3
0
func (c *Cursor) generateIndexes() error {
	var e error
	output := []int{}
	var n int = 0
	for {
		tdread, e := c.reader.Read()
		if e != nil && e != io.EOF {
			break
		}
		n++
		tm := toolkit.M{}

		for i, v := range tdread {
			lowername := strings.ToLower(c.headerColumn[i].name)
			tm.Set(lowername, v)
			if c.headerColumn[i].dataType == "int" {
				tm[lowername] = cast.ToInt(v, cast.RoundingAuto)
			} else if c.headerColumn[i].dataType == "float" {
				tm[lowername] = cast.ToF64(v, (len(v) - (strings.IndexAny(v, "."))), cast.RoundingAuto)
			}
		}

		if c.ConditionVal.getCondition(tm) && len(tm) > 0 { //len(c.ConditionVal.where) == 0 || dbox.MatchM(tm, c.ConditionVal.where) {
			output = append(output, n)
		}

		if e == io.EOF {
			break
		}
	}
	c.ConditionVal.indexes = output

	e = c.resetConnection()
	if e != nil {
		return errorlib.Error(packageName, modCursor, "Reset Fetch", e.Error())
	}

	return e
}
Esempio n. 4
0
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {

	if closeWhenDone {
		defer c.Close()
	}

	e := c.prepIter()
	if e != nil {
		return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	}

	if !toolkit.IsPointer(m) {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer")
	}

	if n != 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer of slice")
	}

	var v reflect.Type

	if n == 1 {
		v = reflect.TypeOf(m).Elem()
	} else {
		v = reflect.TypeOf(m).Elem().Elem()
	}

	ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

	lineCount := 0

	//=============================
	// fmt.Println("Qursor 133 : ", c.ConditionVal.Find)
	for {
		iv := reflect.New(v).Interface()

		isAppend := true
		c.count += 1
		recData := toolkit.M{}
		appendData := toolkit.M{}

		dataTemp, e := c.reader.Read()

		for i, val := range dataTemp {
			orgname := c.headerColumn[i].name
			lowername := strings.ToLower(c.headerColumn[i].name)

			switch c.headerColumn[i].dataType {
			case "int":
				recData[lowername] = cast.ToInt(val, cast.RoundingAuto)
			case "float":
				recData[lowername] = cast.ToF64(val, 2, cast.RoundingAuto)
			default:
				recData[lowername] = val
			}

			if len(c.ConditionVal.Select) == 0 || c.ConditionVal.Select.Get("*", 0).(int) == 1 {
				appendData[orgname] = recData[lowername]
			} else {
				if c.ConditionVal.Select.Get(strings.ToLower(c.headerColumn[i].name), 0).(int) == 1 {
					appendData[orgname] = recData[lowername]
				}
			}
		}

		isAppend = c.ConditionVal.getCondition(recData)

		if c.count < c.ConditionVal.skip || (c.count > (c.ConditionVal.skip+c.ConditionVal.limit) && c.ConditionVal.limit > 0) {
			isAppend = false
		}

		if v.Kind() == reflect.Struct {
			for i := 0; i < v.NumField(); i++ {
				if appendData.Has(v.Field(i).Name) {
					switch v.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(v.Field(i).Name, cast.ToInt(appendData[v.Field(i).Name], cast.RoundingAuto))
					}
				}
			}
		}

		if e == io.EOF {
			if isAppend && len(appendData) > 0 {
				toolkit.Serde(appendData, iv, "json")
				ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
				lineCount += 1
			}
			break
		} else if e != nil {
			return errorlib.Error(packageName, modCursor,
				"Fetch", e.Error())
		}

		if isAppend && len(appendData) > 0 {
			toolkit.Serde(appendData, iv, "json")
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
			lineCount += 1
		}

		if n > 0 {
			if lineCount >= n {
				break
			}
		}
	}

	if e != nil {
		return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	}

	if n == 1 {
		if ivs.Len() > 0 {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	} else {
		reflect.ValueOf(m).Elem().Set(ivs)
	}

	return nil
}
Esempio n. 5
0
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {
	fmt.Println(c.QueryString)

	rows, e := c.session.Query(c.QueryString)

	var valueType reflect.Type

	if n == 1 {
		valueType = reflect.TypeOf(m).Elem()
	} else {
		valueType = reflect.TypeOf(m).Elem().Elem()
	}

	if e != nil {
		return e
	}
	defer rows.Close()
	columns, e := rows.Columns()
	if e != nil {
		return e
	}

	count := len(columns)

	tableData := []toolkit.M{}
	values := make([]interface{}, count)
	valuePtrs := make([]interface{}, count)

	// valueint := values
	for rows.Next() {
		for i := 0; i < count; i++ {
			valuePtrs[i] = &values[i]
		}
		// rows.Scan(valuePtrs...)

		rows.Scan(valuePtrs...)
		entry := toolkit.M{}

		for i, col := range columns {
			var v interface{}
			val := values[i]

			// fmt.Println("Nilai val : ", val)
			b, ok := val.([]byte)
			if ok {
				v = string(b)
			} else {
				v = val
			}
			entry.Set(strings.ToLower(col), v)
			// entry.Set(col, values[i])
			// e = toolkit.DecodeByte(val.([]byte), v)
			// toolkit.FromBytes(toolkit.ToBytes(val, ""), "", v)

			// entry.Set(col, v)
		}

		if valueType.Kind() == reflect.Struct {
			for i := 0; i < valueType.NumField(); i++ {
				namaField := strings.ToLower(valueType.Field(i).Name)
				dataType := strings.ToLower(valueType.Field(i).Type.String())

				if entry.Has(namaField) {
					fmt.Println("isi entry : ", entry[namaField], dataType)
					if strings.Contains(dataType, "int") {
						entry.Set(namaField,
							cast.ToInt(entry[namaField], cast.RoundingAuto))
					} else if strings.Contains(dataType, "time.time") {
						entry.Set(namaField,
							cast.String2Date(cast.ToString(entry[namaField]), "2006-01-02 15:04:05"))
					}
				}
			}
		}

		tableData = append(tableData, entry)
	}

	fmt.Println("Nilai table data : ", tableData)
	if e != nil {
		return e
	}
	if n == 0 {
		// *m.(*[]map[string]interface{}) = tableData
		// toolkit.Unjson(toolkit.Jsonify(tableData), m)
		e = toolkit.Serde(tableData, m, "json")
		fmt.Println("Nilai Model : ", m)
	} else {
		end := c.start + n
		if end > len(tableData) {
			e = errors.New("index out of range")
		} else {
			// *m.(*[]map[string]interface{}) = tableData[0:n]
			//toolkit.Unjson(toolkit.Jsonify(tableData[0:n]), m)
			e = toolkit.Serde(tableData[0:n], m, "json")
		}
	}
	return e
}
Esempio n. 6
0
func Parse(header []string, in interface{}, m interface{}, outputType string, dateFormat string) (e error) {
	// log.Printf("start parse:\n")
	if !toolkit.IsPointer(m) {
		// log.Printf("not pointer\n")
		return errorlib.Error("", "", "Fetch", "Model object should be pointer")
	}
	// log.Printf("pointer\n")
	slice := false
	var ins []string
	if reflect.ValueOf(m).Elem().Kind() == reflect.Slice || toolkit.TypeName(in) == "[]string" {
		slice = true
		ins = in.([]string)
	} else {
		ins = append(ins, in.(string))
	}

	// log.Printf("outputType: %v\n", outputType)

	if outputType == CSV {
		var v reflect.Type

		if slice {
			v = reflect.TypeOf(m).Elem().Elem()
		} else {
			v = reflect.TypeOf(m).Elem()
		}

		ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)
		for _, data := range ins {
			appendData := toolkit.M{}
			iv := reflect.New(v).Interface()
			reader := csv.NewReader(strings.NewReader(""))
			if strings.Contains(data, "','") {
				reader = csv.NewReader(strings.NewReader("\"" + strings.Trim(strings.Replace(data, "','", "\",\"", -1), "'") + "\""))
			} else {
				reader = csv.NewReader(strings.NewReader(data))
			}
			record, e := reader.Read()

			if e != nil {
				return e
			}

			if v.Kind() == reflect.Struct {
				for i := 0; i < v.NumField(); i++ {
					appendData[v.Field(i).Name] = strings.TrimSpace(record[i])
				}

				for i := 0; i < v.NumField(); i++ {
					tag := v.Field(i).Tag

					if appendData.Has(v.Field(i).Name) || appendData.Has(tag.Get("tag_name")) {
						valthis := appendData[v.Field(i).Name]
						if valthis == nil {
							valthis = appendData[tag.Get("tag_name")]
						}

						switch v.Field(i).Type.Kind() {
						case reflect.Int:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int16:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int32:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int64:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Float32:
							valf, _ := strconv.ParseFloat(valthis.(string), 32)
							appendData.Set(v.Field(i).Name, valf)
						case reflect.Float64:
							valf, _ := strconv.ParseFloat(valthis.(string), 64)
							appendData.Set(v.Field(i).Name, valf)
						}

						dtype := DetectFormat(valthis.(string), dateFormat)
						if dtype == "date" {
							valf := cast.String2Date(valthis.(string), dateFormat)
							appendData.Set(v.Field(i).Name, valf)
						} else if dtype == "bool" {
							valf, _ := strconv.ParseBool(valthis.(string))
							appendData.Set(v.Field(i).Name, valf)
						}
					}
				}
			} else {
				for i, val := range header {
					appendData[val] = strings.TrimSpace(record[i])
				}

				if len(header) == 0 {
					e = errorlib.Error("", "", "Parse Out", "Header cant be null because object is not struct")
					return e
				}

				for _, val := range header {
					valthis := appendData[val]
					dtype := DetectFormat(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
			}

			toolkit.Serde(appendData, iv, JSON)
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
		}
		if slice {
			reflect.ValueOf(m).Elem().Set(ivs)
		} else {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	} else if outputType == JSON {
		var temp interface{}
		ins = InspectJson(ins)

		//for catch multi json in one line
		if JsonPart != "" && slice {
			for {
				tempjsonpart := JsonPart
				JsonPart = ""
				tempIn := InspectJson([]string{tempjsonpart})
				if len(tempIn) == 0 {
					break
				} else {
					for _, tin := range tempIn {
						ins = append(ins, tin)
					}
				}
			}
		}

		forSerde := strings.Join(ins, ",")
		if slice {
			forSerde = fmt.Sprintf("[%s]", strings.Join(ins, ","))
		}
		if len(ins) > 0 {
			e := json.Unmarshal([]byte(forSerde), &temp)
			if e != nil {
				return e
			}
			e = toolkit.Serde(temp, m, "json")
			if e != nil {
				return e
			}
		}
	} else {
		var v reflect.Type

		if slice {
			v = reflect.TypeOf(m).Elem().Elem()
		} else {
			v = reflect.TypeOf(m).Elem()
		}

		// log.Printf("v: %v\n", v)

		ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

		// log.Printf("ivs: %v\n", ivs)

		for _, data := range ins {
			appendData := toolkit.M{}
			iv := reflect.New(v).Interface()

			/*log.Printf("data: %v\n", data)
			log.Printf("iv: %v\n", iv)*/

			splitted := strings.Split(data, "\t")

			/*log.Printf("appendData: %v\n", appendData)
			log.Printf("kind: %v\n", v.Kind())
			log.Printf("test: %v", fmt.Sprintf("%v", v))
			//log.Printf("v.Name: %T\n", v)

			if fmt.Sprintf("%v", v) == "reflect.Value" {
				log.Printf("else: %v\n", "reflect.Value")
				for _, val := range header {
					log.Printf("val: %v\n", val)
					valthis := appendData[val]
					dtype := DetectFormat(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
				log.Printf("appendData: %v\n", appendData)
			} else */
			if v.Kind() == reflect.Struct {
				for i := 0; i < v.NumField(); i++ {
					appendData[v.Field(i).Name] = strings.TrimSpace(strings.Trim(splitted[i], " '"))
				}

				// log.Printf("struct: %v\n", v.Kind())
				for i := 0; i < v.NumField(); i++ {
					tag := v.Field(i).Tag
					// log.Printf("i: %v\n", i)

					// log.Printf("name: (%v) tag: (%v)\n", appendData.Has(v.Field(i).Name), appendData.Has(tag.Get("tag_name")))

					if appendData.Has(v.Field(i).Name) || appendData.Has(tag.Get("tag_name")) {
						valthis := appendData[v.Field(i).Name]
						if valthis == nil {
							valthis = appendData[tag.Get("tag_name")]
						}
						// log.Printf("valthis: %v\n", valthis)
						switch v.Field(i).Type.Kind() {
						case reflect.Int:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int16:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int32:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int64:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Float32:
							valf, _ := strconv.ParseFloat(valthis.(string), 32)
							appendData.Set(v.Field(i).Name, valf)
						case reflect.Float64:
							valf, _ := strconv.ParseFloat(valthis.(string), 64)
							appendData.Set(v.Field(i).Name, valf)
						}
						dtype := DetectFormat(valthis.(string), dateFormat)
						if dtype == "date" {
							valf := cast.String2Date(valthis.(string), dateFormat)
							appendData.Set(v.Field(i).Name, valf)
						} else if dtype == "bool" {
							valf, _ := strconv.ParseBool(valthis.(string))
							appendData.Set(v.Field(i).Name, valf)
						}
					}
				}

			} else {
				for i, val := range header {
					appendData[val] = strings.TrimSpace(strings.Trim(splitted[i], " '"))
				}

				if len(header) == 0 {
					e = errorlib.Error("", "", "Parse Out", "Header cant be null because object is not struct")
					return e
				}

				// log.Printf("else: %v\n", v.Kind())
				for _, val := range header {
					// log.Printf("val: %v\n", val)
					valthis := appendData[val]
					dtype := DetectFormat(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
			}

			toolkit.Serde(appendData, iv, JSON)
			// log.Printf("iv result: %v\n", iv)
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
			// log.Printf("ivs result: %v\n", ivs)
		}

		if slice {
			reflect.ValueOf(m).Elem().Set(ivs)
		} else {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
		// log.Printf("result: %v\n", m)
	}
	return nil
}
Esempio n. 7
0
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {
	if closeWhenDone {
		defer c.Close()
	}

	if len(c.indexes) == 0 {
		return nil
	}

	var lower, upper int

	lower = c.currentIndex
	if lower < c.skip {
		lower = c.skip
	}

	upper = lower + n
	if upper > c.limit && c.limit > 0 {
		upper = c.limit
	}

	if !toolkit.IsPointer(m) {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer")
	}

	if n != 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer of slice")
	}

	var v reflect.Type

	if n == 1 {
		v = reflect.TypeOf(m).Elem()
	} else {
		v = reflect.TypeOf(m).Elem().Elem()
	}

	ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

	for {
		appendData := toolkit.M{}
		iv := reflect.New(v).Interface()

		datatemp, ef := c.q.reader.Read()
		c.realIndex += 1
		if c.indexes[c.currentIndex] != c.realIndex {
			continue
		}

		c.currentIndex += 1
		for i, val := range datatemp {
			if len(c.fields) == 0 || c.fields.Has("*") || c.fields.Has(c.q.headerColumn[i].name) {
				appendData[c.q.headerColumn[i].name] = val
			}
		}

		if v.Kind() == reflect.Struct {
			for i := 0; i < v.NumField(); i++ {
				if appendData.Has(v.Field(i).Name) {
					switch v.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(v.Field(i).Name, cast.ToInt(appendData[v.Field(i).Name], cast.RoundingAuto))
					}
				}
			}
		}

		isAppend := lower < c.currentIndex && upper >= c.currentIndex

		if (ef == io.EOF || ef == nil) && isAppend && len(appendData) > 0 {
			toolkit.Serde(appendData, iv, "json")
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
		}

		if ef != nil && ef != io.EOF {
			return errorlib.Error(packageName, modCursor, "Fetch", ef.Error())
		} else if ef == io.EOF || (ivs.Len() >= n && n > 0) {
			break
		}
	}

	// if e != nil {
	// 	return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	// }

	if n == 1 {
		reflect.ValueOf(m).Elem().Set(ivs.Index(0))
	} else {
		reflect.ValueOf(m).Elem().Set(ivs)
	}

	return nil
}
Esempio n. 8
0
func Parse(header []string, in interface{}, m interface{}, outputType string, dateFormat string) (e error) {
	if !toolkit.IsPointer(m) {
		return errorlib.Error("", "", "Fetch", "Model object should be pointer")
	}
	slice := false
	var ins []string
	if reflect.ValueOf(m).Elem().Kind() == reflect.Slice || toolkit.TypeName(in) == "[]string" {
		slice = true
		ins = in.([]string)
	} else {
		ins = append(ins, in.(string))
	}

	if outputType == CSV {
		var v reflect.Type

		if slice && toolkit.TypeName(m) != "*interface {}" {
			v = reflect.TypeOf(m).Elem().Elem()
		} else {
			v = reflect.TypeOf(m).Elem()
		}

		ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)
		for _, data := range ins {
			appendData := toolkit.M{}
			iv := reflect.New(v).Interface()
			reader := csv.NewReader(strings.NewReader(""))
			if strings.Contains(data, "','") {
				reader = csv.NewReader(strings.NewReader("\"" + strings.Trim(strings.Replace(data, "','", "\",\"", -1), "'") + "\""))
			} else {
				reader = csv.NewReader(strings.NewReader(data))
			}
			record, e := reader.Read()

			if e != nil {
				return e
			}

			if v.Kind() == reflect.Struct {
				for i := 0; i < v.NumField(); i++ {
					appendData[v.Field(i).Name] = strings.TrimSpace(record[i])
					valthis := appendData[v.Field(i).Name]
					switch v.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int16:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int32:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int64:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Float32:
						valf, _ := strconv.ParseFloat(valthis.(string), 32)
						appendData.Set(v.Field(i).Name, valf)
					case reflect.Float64:
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(v.Field(i).Name, valf)
					case reflect.Bool:
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(v.Field(i).Name, valf)
					default:
						dtype := DetectDataType(valthis.(string), dateFormat)
						if dtype == "date" {
							valf := cast.String2Date(valthis.(string), dateFormat)
							appendData.Set(v.Field(i).Name, valf)
						}
					}

				}

			} else {
				if len(header) == 0 {
					e = errorlib.Error("", "", "Parse Out", "Header cant be null because object is not struct")
					return e
				}

				for i, val := range header {
					appendData[val] = strings.TrimSpace(record[i])
					valthis := appendData[val]
					dtype := DetectDataType(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
			}

			toolkit.Serde(appendData, iv, JSON)
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
		}
		if slice {
			reflect.ValueOf(m).Elem().Set(ivs)
		} else {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	} else if outputType == JSON {
		var temp interface{}
		ins = InspectJson(ins)

		//for catch multi json in one line
		if JsonPart != "" && slice {
			for {
				tempjsonpart := JsonPart
				JsonPart = ""
				tempIn := InspectJson([]string{tempjsonpart})
				if len(tempIn) == 0 {
					break
				} else {
					for _, tin := range tempIn {
						ins = append(ins, tin)
					}
				}
			}
		}

		forSerde := strings.Join(ins, ",")
		if slice {
			forSerde = fmt.Sprintf("[%s]", strings.Join(ins, ","))
		}
		if len(ins) > 0 {
			e := json.Unmarshal([]byte(forSerde), &temp)
			if e != nil {
				return e
			}
			e = toolkit.Serde(temp, m, "json")
			if e != nil {
				return e
			}
		}
	} else {
		var v reflect.Type

		if slice && toolkit.TypeName(m) != "*interface {}" {
			v = reflect.TypeOf(m).Elem().Elem()
		} else {
			v = reflect.TypeOf(m).Elem()
		}

		ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

		for _, data := range ins {
			appendData := toolkit.M{}
			iv := reflect.New(v).Interface()

			splitted := strings.Split(data, "\t")

			if v.Kind() == reflect.Struct {
				for i := 0; i < v.NumField(); i++ {
					appendData[v.Field(i).Name] = strings.TrimSpace(strings.Trim(splitted[i], " '"))
					valthis := appendData[v.Field(i).Name]
					switch v.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int16:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int32:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int64:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Float32:
						valf, _ := strconv.ParseFloat(valthis.(string), 32)
						appendData.Set(v.Field(i).Name, valf)
					case reflect.Float64:
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(v.Field(i).Name, valf)
					case reflect.Bool:
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(v.Field(i).Name, valf)
					default:
						dtype := DetectDataType(valthis.(string), dateFormat)
						if dtype == "date" {
							valf := cast.String2Date(valthis.(string), dateFormat)
							appendData.Set(v.Field(i).Name, valf)
						}
					}

				}

			} else {

				if len(header) == 0 {
					e = errorlib.Error("", "", "Parse Out", "Header cant be null because object is not struct")
					return e
				}

				for i, val := range header {
					appendData[val] = strings.TrimSpace(strings.Trim(splitted[i], " '"))
					valthis := appendData[val]
					dtype := DetectDataType(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
			}

			toolkit.Serde(appendData, iv, JSON)
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
		}

		if slice {
			reflect.ValueOf(m).Elem().Set(ivs)
		} else {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	}
	return nil
}
Esempio n. 9
0
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {
	tableData := []toolkit.M{}
	// var e error

	rows, e := c.session.Query(c.QueryString)
	var valueType reflect.Type

	if n == 1 {
		valueType = reflect.TypeOf(m).Elem()
	} else {
		valueType = reflect.TypeOf(m).Elem().Elem()
	}

	if e != nil {
		return e
	}
	defer rows.Close()
	columns, e := rows.Columns()

	if e != nil {
		return e
	}

	count := len(columns)

	values := make([]interface{}, count)
	valuePtrs := make([]interface{}, count)

	for rows.Next() {
		for i := 0; i < count; i++ {
			valuePtrs[i] = &values[i]
		}

		rows.Scan(valuePtrs...)
		entry := toolkit.M{}

		for i, col := range columns {
			var v interface{}
			val := values[i]
			// b, ok := val.([]byte)

			// // toolkit.Println("i : ", i, " :col: ", col, " :val: ", val, " :b : ", b, " :type data : ", toolkit.Value(val))

			// var out interface{}
			// e = toolkit.Unjson(b, &out)
			// // toolkit.Println("i : ", i, "b : ", b, " :out: ", v, " :: error : ", e)

			// if e != nil {
			// 	ok = false
			// }
			// if ok {
			// 	v = out
			// 	toolkit.Println("error OK :: ", ok, " :v :", v)
			// } else {
			// 	toolkit.Println("error OK :: ", ok, " :b :", b)
			// 	v = string(b)
			// }
			v = val
			entry.Set(strings.ToLower(col), v)
		}

		if valueType.Kind() == reflect.Struct {
			for i := 0; i < valueType.NumField(); i++ {
				namaField := strings.ToLower(valueType.Field(i).Name)
				dataType := strings.ToLower(valueType.Field(i).Type.String())

				if entry.Has(namaField) {
					if strings.Contains(dataType, "int") {
						entry.Set(namaField,
							cast.ToInt(entry[namaField], cast.RoundingAuto))
					} else if strings.Contains(dataType, "time.time") {
						entry.Set(namaField,
							cast.String2Date(cast.ToString(entry[namaField]), "2006-01-02 15:04:05"))
					}
				}
			}
		}

		tableData = append(tableData, entry)
	}
	// toolkit.Println("... ::: ", tableData)

	maxIndex := toolkit.SliceLen(tableData)

	var e2 error
	if e2 != nil {
		return e2
	}
	end := c.start + n

	if end > maxIndex || n == 0 {
		end = maxIndex
	}

	if c.start >= maxIndex {
		e2 = errors.New("No more data to fetched!")
	} else {
		e2 = toolkit.Serde(tableData[c.start:end], m, "json")
	}
	c.start = end

	return e2
}
Esempio n. 10
0
func generateFilterQuerySQL(strwhere string) (fb *Filter) {
	strwhere = strings.TrimSpace(strwhere)
	rbracket := regexp.MustCompile(`^\(.*\)$`)
	if rbracket.MatchString(strwhere) {
		if cond, _ := regexp.MatchString(`^\(.*\) ([Oo][Rr]|[Aa][Nn][Dd]) (.*)$`, strwhere); !cond {
			strwhere = strings.TrimSuffix(strings.TrimPrefix(strwhere, "("), ")")
		}
	}
	// toolkit.Printf("Connection 161 : %#v\n", strwhere)
	r := regexp.MustCompile(`^(?P<lastprocess01>(.*))(?P<firstprocess>(\(.*([Aa][Nn][Dd]|[Oo][Rr]).*\)))(?P<lastprocess02>(.*))$`)
	if !r.MatchString(strwhere) {
		r = regexp.MustCompile(`(.*) (?P<oprandor>([Oo][Rr])) (.*)`)
	}

	if !r.MatchString(strwhere) {
		r = regexp.MustCompile(`(.*) (?P<oprandor>([Aa][Nn][Dd])) (.*)`)
	}

	if !r.MatchString(strwhere) {
		r = regexp.MustCompile(`(?P<field>([a-zA-Z][_a-zA-Z]+[_a-zA-Z0-1]*))(?P<opr>((\s)*(=|<>|[><](=)?)|(\s(I|i)(N|n)\s)|(\s(L|l)(I|i)(K|k)(E|e)\s)))(?P<value>(.*))`)
	}

	temparray := r.FindStringSubmatch(strwhere)
	condpart := toolkit.M{}

	for i, val := range r.SubexpNames() {
		if val != "" {
			condpart.Set(val, temparray[i])
		}
	}

	arstrwhere := make([]string, 0, 0)
	oprstr := "or"
	if condpart.Has("firstprocess") {
		oprstr, arstrwhere = generateWhereCondition(strwhere)
		// for _, val := range generateWhereCondition(strwhere) {
		// 	arstrwhere = append(arstrwhere, val)
		// }

		// next01 := condpart.Get("lastprocess01", "").(string)
		// next02 := condpart.Get("lastprocess01", "").(string)
		// if condition {

		// }
		// //parsing check operator to and insert to arstwhere
	} else if condpart.Has("oprandor") {
		arstrwhere = strings.Split(strwhere, condpart["oprandor"].(string))
		if strings.ToLower(condpart["oprandor"].(string)) == "and" {
			oprstr = "and"
		}
	}

	if len(arstrwhere) > 0 {
		var arfilter []*Filter

		for _, swhere := range arstrwhere {
			arfilter = append(arfilter, generateFilterQuerySQL(swhere))
		}

		if oprstr == "and" {
			fb = And(arfilter...)
		} else {
			fb = Or(arfilter...)
		}
		// //Check Bracket if regexp.MatchString(`^(.*)\(.*([Aa][Nn][Dd]|[Oo][Rr]).*\)(.*)$`, strwhere) {
		// if strings.TrimSpace(condpart.Get("oprandor", "").(string)) == "and" {
		// 	fb = And(generateFilterQuerySQL(condpart.Get("currcond", "").(string)), generateFilterQuerySQL(condpart.Get("nextcond", "").(string)))
		// } else {
		// 	fb = Or(generateFilterQuerySQL(condpart.Get("currcond", "").(string)), generateFilterQuerySQL(condpart.Get("nextcond", "").(string)))
		// }
	} else {
		var (
			asv []interface{}
			iv  interface{}
		)

		c1 := strings.TrimSpace(condpart.Get("field", "").(string))
		tv := strings.TrimSpace(condpart.Get("value", "").(string))
		opr := strings.ToLower(strings.TrimSpace(condpart.Get("opr", "").(string)))
		// if condition {
		tv = strings.TrimSuffix(tv, ")")
		// }

		if opr != "in" {
			if strings.Contains(tv, `'`) || strings.Contains(tv, `"`) {
				sv := strings.Replace(strings.Replace(tv, `'`, "", -1), `"`, "", -1)
				if opr == "like" {
					sv = strings.Replace(strings.Replace(sv, "%", "(.)*", -1), "_", "(.)?", -1)
				}
				iv = sv
			} else if strings.Contains(tv, `.`) {
				iv = cast.ToF64(tv, (len(tv) - (strings.IndexAny(tv, "."))), cast.RoundingAuto)
			} else {
				iv = cast.ToInt(tv, cast.RoundingAuto)
			}
		} else {
			tv = strings.Replace(strings.Replace(tv, "(", "", 1), ")", "", 1)
			// asv = strings.Split(tv, ",")
			for _, val := range strings.Split(tv, ",") {
				var tiv interface{}
				ttv := strings.TrimSpace(val)
				if strings.Contains(ttv, `'`) || strings.Contains(ttv, `"`) {
					tiv = strings.Replace(strings.Replace(ttv, `'`, "", -1), `"`, "", -1)
				} else if strings.Contains(ttv, `.`) {
					tiv = cast.ToF64(ttv, (len(ttv) - (strings.IndexAny(ttv, "."))), cast.RoundingAuto)
				} else {
					tiv = cast.ToInt(ttv, cast.RoundingAuto)
				}
				asv = append(asv, tiv)
			}
		}

		switch opr {
		case "=":
			fb = Eq(c1, iv)
		case "<>":
			fb = Ne(c1, iv)
		case ">":
			fb = Lt(c1, iv)
		case "<":
			fb = Gt(c1, iv)
		case ">=":
			fb = Lte(c1, iv)
		case "<=":
			fb = Gte(c1, iv)
		case "like":
			fb = Contains(c1, iv.(string))
		case "in":
			fb = In(c1, asv...)
		}
	}

	return
}
Esempio n. 11
0
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {

	if closeWhenDone {
		defer c.Close()
	}

	e := c.prepIter()
	if e != nil {
		return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	}

	if !toolkit.IsPointer(m) {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer")
	}

	if n != 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer of slice")
	}

	var v reflect.Type

	if n == 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		v = reflect.TypeOf(m).Elem()
	} else {
		v = reflect.TypeOf(m).Elem().Elem()
	}

	ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

	lineCount := 0

	//=============================
	// fmt.Println("Qursor 191 : ", c.headerColumn)
	for {
		iv := reflect.New(v).Interface()

		isAppend := true
		// c.count += 1
		recData := toolkit.M{}
		appendData := toolkit.M{}

		dataTemp, e := c.reader.Read()

		for i, val := range dataTemp {
			orgname := c.headerColumn[i].name
			lowername := strings.ToLower(c.headerColumn[i].name)

			switch c.headerColumn[i].dataType {
			case "int":
				recData[lowername] = cast.ToInt(val, cast.RoundingAuto)
			case "float":
				decimalPoint := len(val) - (strings.Index(val, ".") + 1)
				recData[lowername] = cast.ToF64(val, decimalPoint, cast.RoundingAuto)
			case "date":
				recData[lowername] = toolkit.String2Date(val, c.headerColumn[i].format) // Just for test
				// fmt.Printf("FOR DEBUG : %v \n", c.headerColumn[i].format)
			default:
				recData[lowername] = val
			}

			if len(c.ConditionVal.Select) == 0 || c.ConditionVal.Select.Get("*", 0).(int) == 1 {
				appendData[orgname] = recData[lowername]
			} else {
				if c.ConditionVal.Select.Get(strings.ToLower(c.headerColumn[i].name), 0).(int) == 1 {
					appendData[orgname] = recData[lowername]
				}
			}
		}

		isAppend = c.ConditionVal.getCondition(recData)

		if isAppend {
			c.count += 1
		}

		if c.count <= c.ConditionVal.skip || (c.count > (c.ConditionVal.skip+c.ConditionVal.limit) && c.ConditionVal.limit > 0) {
			isAppend = false
		}
		// fmt.Printf("%v - %v \n", v.Elem().Kind(), toolkit.TypeName(v))
		if v.Kind() == reflect.Struct || v.Elem().Kind() == reflect.Struct {
			tv := v
			if v.Elem().Kind() == reflect.Struct {
				tv = v.Elem()
			}

			for i := 0; i < tv.NumField(); i++ {
				str := tv.Field(i).Name
				fcond := false
				if appendData.Has(str) {
					fcond = true
				} else if appendData.Has(strings.ToLower(str)) {
					fcond = true
					str = strings.ToLower(str)
				} else if strings.ToLower(str) == "id" && appendData.Has("_id") {
					str = "_id"
					fcond = true
				}

				if fcond {
					switch tv.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(str, cast.ToInt(appendData[str], cast.RoundingAuto))
					case reflect.String:
						appendData.Set(str, toolkit.ToString(appendData[str]))
					case reflect.Float64:
						tstr := toolkit.ToString(appendData[str])
						decimalPoint := len(tstr) - (strings.Index(tstr, ".") + 1)
						appendData.Set(str, toolkit.ToFloat64(tstr, decimalPoint, toolkit.RoundingAuto))
					}
				}
			}
		}

		if e == io.EOF {
			if isAppend && len(appendData) > 0 {
				toolkit.Serde(appendData, iv, "json")
				ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
				lineCount += 1
			}
			break
		} else if e != nil {
			return errorlib.Error(packageName, modCursor,
				"Fetch", e.Error())
		}

		if isAppend && len(appendData) > 0 {
			toolkit.Serde(appendData, iv, "json")
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
			lineCount += 1
		}

		if n > 0 {
			if lineCount >= n {
				break
			}
		}
	}

	if e != nil {
		return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	}

	if n == 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		if ivs.Len() > 0 {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	} else {
		reflect.ValueOf(m).Elem().Set(ivs)
	}

	return nil
}
Esempio n. 12
0
func (q *Query) execQueryPartUpdate(dt toolkit.M, Cond QueryCondition) error {

	if len(dt) == 0 {
		return errorlib.Error(packageName, "Query", modQuery, "data to update is not found")
	}

	writer := q.Connection().(*Connection).writer
	reader := q.Connection().(*Connection).reader
	tempHeader := []string{}

	for _, val := range q.Connection().(*Connection).headerColumn {
		tempHeader = append(tempHeader, val.name)
	}

	for {
		foundChange := false

		recData := toolkit.M{}
		dataTemp, e := reader.Read()
		for i, val := range dataTemp {
			recData.Set(tempHeader[i], val)
			if q.Connection().(*Connection).headerColumn[i].dataType == "int" {
				recData[tempHeader[i]] = cast.ToInt(val, cast.RoundingAuto)
			} else if q.Connection().(*Connection).headerColumn[i].dataType == "float" {
				recData[tempHeader[i]] = cast.ToF64(val, (len(val) - (strings.IndexAny(val, "."))), cast.RoundingAuto)
			}
		}

		if len(Cond.where) > 0 { //|| (len(Cond.where) == 0 && toolkit.IdField(dt) == "") {
			foundChange = Cond.getCondition(recData)
		}

		// Check ID IF Condition Not Found
		if nameid := toolkit.IdField(dt); nameid != "" && !foundChange {
			if recData.Has(nameid) && dt[nameid] == recData[nameid] {
				foundChange = true
			}
		}

		if foundChange && len(dataTemp) > 0 {
			for n, v := range tempHeader {
				if dt.Has(v) {
					dataTemp[n] = cast.ToString(dt[v])
				}
			}
		}

		if e == io.EOF {
			if dataTemp != nil {
				writer.Write(dataTemp)
				writer.Flush()
			}
			break
		} else if e != nil {
			return errorlib.Error(packageName, modQuery, "Update", e.Error())
		}
		if dataTemp != nil {
			writer.Write(dataTemp)
			writer.Flush()
		}
	}

	return nil
}