<commit_before>package model

import (
	"errors"
	"log"
	"reflect"
	"strconv"
	"strings"
	"sync"
	"time"

	"encoding/json"

	"github.com/DanielRenne/GoCore/core/serverSettings"
	stacktrace "github.com/go-errors/errors"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/dbServices"
	"github.com/DanielRenne/GoCore/core/extensions"
	golog "github.com/DanielRenne/GoCore/core/log"
	dateformatter "github.com/altipla-consulting/i18n-dateformatter"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

const (
	ERROR_INVALID_ID_VALUE = "Invalid Id value for query."
	JOIN_ALL               = "All"
)

type queryError func() error

type Range struct {
	Min interface{}
	Max interface{}
}

type join struct {
	collectionName       string
	joinFieldRefName     string
	joinFieldName        string
	joinSchemaName       string
	joinSpecified        string
	joinType             string
	isMany               bool
	joinForeignFieldName string
}

type joinType struct {
	Type string
}

type view struct {
	fieldName string
	ref       string
	format    string
}

type Query struct {
	q           *mgo.Query
	m           bson.M
	stopLog     bool
	limit       int
	skip        int
	sort        []string
	collection  *mgo.Collection
	e           error
	joins       map[string]joinType
	format      DataFormat
	renderViews bool
}

type DataFormat struct {
	Language      string `json:"Language"`
	DateFormat    string `json:"DateFormat"`
	LocalTimeZone string `json:"LocalTimeZone"`
}

func (obj *DataFormat) JSONString() (string, error) {
	bytes, err := json.Marshal(obj)
	return string(bytes), err
}

func (self *DataFormat) Parse(data string) (err error) {
	err = json.Unmarshal([]byte(data), &self)
	return
}

func Criteria(key string, value interface{}) map[string]interface{} {
	criteria := make(map[string]interface{}, 1)
	criteria[key] = value
	return criteria
}

func (self *Query) ById(val interface{}, x interface{}) error {

	objId, err := self.getIdHex(val)
	if err != nil {
		return err
	}

	if !serverSettings.WebConfig.Application.LogQueries && serverSettings.WebConfig.Application.LogQueryTimes {
		defer golog.TimeTrackQuery(time.Now(), "q.ById("+objId.Hex()+")", self.collection, self.m, self.q)
	} else if serverSettings.WebConfig.Application.LogQueries {
		defer golog.TimeTrack(time.Now(), "q.ById("+objId.Hex()+")")
	}

	if !self.stopLog && serverSettings.WebConfig.Application.LogQueries {
		self.LogQuery("q.ById(" + objId.Hex() + ")")
	}
	q := self.collection.FindId(objId)

	err = q.One(x)

	if err != nil {
		// This callback is used for if the Ethernet port is unplugged
		callback := func() error {
			err = q.One(x)
			if err != nil {
				return err
			}
			return self.processJoinsAndViews(x)
		}

		return self.handleQueryError(err, callback)
	}

	return self.processJoinsAndViews(x)

}

func (self *Query) Join(criteria string) *Query {

	if self.joins == nil {
		self.joins = make(map[string]joinType)
	}

	_, ok := self.joins[JOIN_ALL]
	if ok {
		return self
	}

	self.joins[criteria] = joinType{Type: "Inner"}

	return self
}

func (self *Query) LeftJoin(criteria string) *Query {

	if self.joins == nil {
		self.joins = make(map[string]joinType)
	}

	_, ok := self.joins[JOIN_ALL]
	if ok {
		return self
	}

	self.joins[criteria] = joinType{Type: "Left"}

	return self
}

func (self *Query) ToggleLogFlag(toggle bool) *Query {
	self.stopLog = toggle
	return self
}

func (self *Query) Filter(criteria map[string]interface{}) *Query {

	val, hasId := criteria["Id"]
	if hasId {

		//objId, err := self.getIdHex(val)
		//if err != nil {
		//	self.e = err
		//	return self
		//}

		//self.q = self.collection.FindId(objId)
		self.m["_id"] = self.CheckForObjectId(val)
		return self
	} else {

		if self.m == nil {
			self.m = make(bson.M)<commit_msg>Fixed Bug.<commit_after>package model

import (
	"errors"
	"log"
	"reflect"
	"strconv"
	"strings"
	"sync"
	"time"

	"encoding/json"

	"github.com/DanielRenne/GoCore/core/serverSettings"
	stacktrace "github.com/go-errors/errors"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/dbServices"
	"github.com/DanielRenne/GoCore/core/extensions"
	golog "github.com/DanielRenne/GoCore/core/log"
	dateformatter "github.com/altipla-consulting/i18n-dateformatter"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

const (
	ERROR_INVALID_ID_VALUE = "Invalid Id value for query."
	JOIN_ALL               = "All"
)

type queryError func() error

type Range struct {
	Min interface{}
	Max interface{}
}

type join struct {
	collectionName       string
	joinFieldRefName     string
	joinFieldName        string
	joinSchemaName       string
	joinSpecified        string
	joinType             string
	isMany               bool
	joinForeignFieldName string
}

type joinType struct {
	Type string
}

type view struct {
	fieldName string
	ref       string
	format    string
}

type Query struct {
	q           *mgo.Query
	m           bson.M
	stopLog     bool
	limit       int
	skip        int
	sort        []string
	collection  *mgo.Collection
	e           error
	joins       map[string]joinType
	format      DataFormat
	renderViews bool
}

type DataFormat struct {
	Language      string `json:"Language"`
	DateFormat    string `json:"DateFormat"`
	LocalTimeZone string `json:"LocalTimeZone"`
}

func (obj *DataFormat) JSONString() (string, error) {
	bytes, err := json.Marshal(obj)
	return string(bytes), err
}

func (self *DataFormat) Parse(data string) (err error) {
	err = json.Unmarshal([]byte(data), &self)
	return
}

func Criteria(key string, value interface{}) map[string]interface{} {
	criteria := make(map[string]interface{}, 1)
	criteria[key] = value
	return criteria
}

func (self *Query) ById(val interface{}, x interface{}) error {

	objId, err := self.getIdHex(val)
	if err != nil {
		return err
	}

	if !serverSettings.WebConfig.Application.LogQueries && serverSettings.WebConfig.Application.LogQueryTimes {
		defer golog.TimeTrackQuery(time.Now(), "q.ById("+objId.Hex()+")", self.collection, self.m, self.q)
	} else if serverSettings.WebConfig.Application.LogQueries {
		defer golog.TimeTrack(time.Now(), "q.ById("+objId.Hex()+")")
	}

	if !self.stopLog && serverSettings.WebConfig.Application.LogQueries {
		self.LogQuery("q.ById(" + objId.Hex() + ")")
	}
	q := self.collection.FindId(objId)

	err = q.One(x)

	if err != nil {
		// This callback is used for if the Ethernet port is unplugged
		callback := func() error {
			err = q.One(x)
			if err != nil {
				return err
			}
			return self.processJoinsAndViews(x)
		}

		return self.handleQueryError(err, callback)
	}

	return self.processJoinsAndViews(x)

}

func (self *Query) Join(criteria string) *Query {

	if self.joins == nil {
		self.joins = make(map[string]joinType)
	}

	_, ok := self.joins[JOIN_ALL]
	if ok {
		return self
	}

	self.joins[criteria] = joinType{Type: "Inner"}

	return self
}

func (self *Query) LeftJoin(criteria string) *Query {

	if self.joins == nil {
		self.joins = make(map[string]joinType)
	}

	_, ok := self.joins[JOIN_ALL]
	if ok {
		return self
	}

	self.joins[criteria] = joinType{Type: "Left"}

	return self
}

func (self *Query) ToggleLogFlag(toggle bool) *Query {
	self.stopLog = toggle
	return self
}

func (self *Query) Filter(criteria map[string]interface{}) *Query {

	val, hasId := criteria["Id"]
	if hasId {

		objId, err := self.getIdHex(val)
		if err != nil {
			self.e = err
			return self
		}

		self.m["_id"] = objId
		return self
	} else {

		if self.m == nil {
			self.m = make(bson.M)
{'fraction_non_alphanumeric': 0.1277844471121782, 'fraction_numerical': 0.0013338668800853674, 'mean_word_length': 3.4002347417840375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '804122', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2663, 'n_words': 612}