//ToSingleTableSQL will convert the query to sql if the query has only one table
ToSingleTableSQL will convert the query to sql if the query has only one table
// ToSingleTableSQL will convert the query to sql if the query has only one table
func (q Query) ToSingleTableSQL() (*SQLQuery, error) {
	if len(q.Tables) != 1 {
		return nil, fmt.Errorf("couldn't find any table. Length of the tables to which the query belong to is %d", len(q.Tables))
	}
	var tableNode TableNode
	for _, v := range q.Tables {
		tableNode = v
	}
	var queryB strings.Builder
	queryB.WriteString("SELECT ")
	hasGroupBy := false
	for _, v := range q.GroupBy {
		if len(v.Name) > 0 {
			hasGroupBy = true
		}
	}
	count := 0
	for _, v := range q.Select {
		if len(v.Name) == 0 {
			continue
		}
		addColumnString(count, v, &queryB, hasGroupBy)
		count++
	}
	for _, v := range q.GroupBy {
		if len(v.Name) == 0 {
			continue
		}
		addColumnString(count, v, &queryB, false)
		count++
	}
	queryB.WriteString(" FROM \"" + tableNode.Name + "\"")
	count = 0
	values := []interface{}{}
	done := false
	index := 0
	for _, v := range q.Filters {
		if v.Column == nil || len(v.Column.Name) == 0 || (v.Value == nil && v.Unknown == nil && v.Time == nil) {
			continue
		}
		if v.Unknown == nil && v.Time == nil && len(v.Value.Name) == 0 {
			continue
		}
		if v.Value == nil && v.Time == nil && len(v.Unknown.Word) == 0 {
			continue
		}
		if v.Value == nil && v.Unknown == nil && (!v.Time.Value.IsValid() || !v.Time.Value.From.IsValid()) {
			continue
		}
		if (v.Column.DataType == DataTypeInt || v.Column.DataType == DataTypeFloat || v.Column.DataType == DataTypeDate) &&
			(v.Operation != EqOperator && v.Operation != NotEqOperator && v.Operation != GreaterOperator && v.Operation != LessOperator) {
			continue
		}
		var convertedVal interface{}
		if v.Value != nil {
			vl, ok := getValue(v.Column.DataType, v.Value.Name)
			if !ok {
				continue
			}
			convertedVal = vl
		} else if v.Unknown != nil {
			vl, ok := getValue(v.Column.DataType, string(v.Unknown.Word))
			if !ok {
				continue
			}
			convertedVal = vl
		} else if v.Time != nil {
			if v.Column.DataType != DataTypeDate {
				continue
			}
			convertedVal = v.Time.Value.From.Time
		}
		if len(q.Filters) > 0 && !done {
			queryB.WriteString(" WHERE ")
			done = true
		}
		if count != 0 {
			queryB.WriteString(" AND ")
		}
		count++
		columnName := "\"" + v.Column.Name + "\""
		value := ""
		if v.Column.DataType == DataTypeString {
			value += "'"
		}
		if (v.Operation == LikeOperator || v.Operation == ContainsOperator) && v.Column.DataType == DataTypeString {
			value += "%"
		}
		if v.Column.DataType == DataTypeString {
			value += convertedVal.(string)
		}
		if (v.Operation == LikeOperator || v.Operation == ContainsOperator) && v.Column.DataType == DataTypeString {
			value += "%"
		}
		if v.Column.DataType == DataTypeString {
			value += "'"
		}
		var finalValue interface{} = value
		if v.Column.DataType != DataTypeString {
			finalValue = convertedVal
		}
		index++
		queryB.WriteString(columnName + " " + v.Operation + " $" + strconv.Itoa(index))
		values = append(values, finalValue)
	}
	result := &SQLQuery{Args: values}
	if !hasGroupBy {
		result.Query = queryB.String()
		return result, nil
	}
	count = 0
	queryB.WriteString(" GROUP BY ")
	for _, v := range q.GroupBy {
		if len(v.Name) == 0 {
			continue
		}
		if count != 0 {
			queryB.WriteString(", ")
		}
		count++
		queryB.WriteString("\"" + v.Name + "\"")
	}
	result.Query = queryB.String()
	return result, nil
}
func ( q Query ) ToSingleTableSQL ( ) ( * SQLQuery , error ) { if len ( q . Tables ) != 1 { return nil , fmt . Errorf ( " " , len ( q . Tables ) ) } var tableNode TableNode for _ , v := range q . Tables { tableNode = v } var queryB strings . Builder queryB . WriteString ( " " ) hasGroupBy := false for _ , v := range q . GroupBy { if len ( v . Name ) > 0 { hasGroupBy = true } } count := 0 for _ , v := range q . Select { if len ( v . Name ) == 0 { continue } addColumnString ( count , v , & queryB , hasGroupBy ) count ++ } for _ , v := range q . GroupBy { if len ( v . Name ) == 0 { continue } addColumnString ( count , v , & queryB , false ) count ++ } queryB . WriteString ( " \" " + tableNode . Name + " \" " ) count = 0 values := [ ] interface { } { } done := false index := 0 for _ , v := range q . Filters { if v . Column == nil || len ( v . Column . Name ) == 0 || ( v . Value == nil && v . Unknown == nil && v . Time == nil ) { continue } if v . Unknown == nil && v . Time == nil && len ( v . Value . Name ) == 0 { continue } if v . Value == nil && v . Time == nil && len ( v . Unknown . Word ) == 0 { continue } if v . Value == nil && v . Unknown == nil && ( ! v . Time . Value . IsValid ( ) || ! v . Time . Value . From . IsValid ( ) ) { continue } if ( v . Column . DataType == DataTypeInt || v . Column . DataType == DataTypeFloat || v . Column . DataType == DataTypeDate ) && ( v . Operation != EqOperator && v . Operation != NotEqOperator && v . Operation != GreaterOperator && v . Operation != LessOperator ) { continue } var convertedVal interface { } if v . Value != nil { vl , ok := getValue ( v . Column . DataType , v . Value . Name ) if ! ok { continue } convertedVal = vl } else if v . Unknown != nil { vl , ok := getValue ( v . Column . DataType , string ( v . Unknown . Word ) ) if ! ok { continue } convertedVal = vl } else if v . Time != nil { if v . Column . DataType != DataTypeDate { continue } convertedVal = v . Time . Value . From . Time } if len ( q . Filters ) > 0 && ! done { queryB . WriteString ( " " ) done = true } if count != 0 { queryB . WriteString ( " " ) } count ++ columnName := " \" " + v . Column . Name + " \" " value := " " if v . Column . DataType == DataTypeString { value += " " } if ( v . Operation == LikeOperator || v . Operation == ContainsOperator ) && v . Column . DataType == DataTypeString { value += " " } if v . Column . DataType == DataTypeString { value += convertedVal . ( string ) } if ( v . Operation == LikeOperator || v . Operation == ContainsOperator ) && v . Column . DataType == DataTypeString { value += " " } if v . Column . DataType == DataTypeString { value += " " } var finalValue interface { } = value if v . Column . DataType != DataTypeString { finalValue = convertedVal } index ++ queryB . WriteString ( columnName + " " + v . Operation + " " + strconv . Itoa ( index ) ) values = append ( values , finalValue ) } result := & SQLQuery { Args : values } if ! hasGroupBy { result . Query = queryB . String ( ) return result , nil } count = 0 queryB . WriteString ( " " ) for _ , v := range q . GroupBy { if len ( v . Name ) == 0 { continue } if count != 0 { queryB . WriteString ( " " ) } count ++ queryB . WriteString ( " \" " + v . Name + " \" " ) } result . Query = queryB . String ( ) return result , nil }
ToSingleTableSQL will convert the query to sql if the query has only one table
ToSingleTableSQL will convert the query to sql if the query has only one table
/*
	 * We will add a table check
	 * Then we will get the table
	 * Then we will iterate through the select fields
	 * If group by fields are there, will add them to be selected
	 * Then we will add the filters
	 * Then we will add the group by if any
	 */ //adding the table number check //getting the table //iterating through the fields to be selected //if group fields are there, then add then for selection //adding the filters //add the group by if required