<commit_before>// Copyright 2015 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
	"database/sql"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"reflect"
	"regexp"

	"strconv"
	"strings"
	"time"

	"github.com/Chronokeeper/anyxml"
	"github.com/xormplus/core"
	"gopkg.in/flosch/pongo2.v3"
)

type ResultBean struct {
	Has    bool
	Result interface{}
	Error  error
}

func (resultBean *ResultBean) Json() (bool, string, error) {
	if resultBean.Error != nil {
		return resultBean.Has, "", resultBean.Error
	}
	if !resultBean.Has {
		return resultBean.Has, "", nil
	}
	result, err := JSONString(resultBean.Result, true)
	return resultBean.Has, result, err
}

func (resultBean *ResultBean) GetResult() (bool, interface{}, error) {
	return resultBean.Has, resultBean.Result, resultBean.Error
}

func (session *Session) GetFirst(bean interface{}) *ResultBean {
	has, err := session.Get(bean)
	r := &ResultBean{Has: has, Result: bean, Error: err}
	return r
}

func (resultBean *ResultBean) Xml() (bool, string, error) {

	if resultBean.Error != nil {
		return false, "", resultBean.Error
	}
	if !resultBean.Has {
		return resultBean.Has, "", nil
	}
	has, result, err := resultBean.Json()
	if err != nil {
		return false, "", err
	}
	if !has {
		return has, "", nil
	}
	var anydata = []byte(result)
	var i interface{}
	err = json.Unmarshal(anydata, &i)
	if err != nil {
		return false, "", err
	}
	resultByte, err := anyxml.Xml(i)
	if err != nil {
		return false, "", err
	}

	return resultBean.Has, string(resultByte), err
}

func (resultBean *ResultBean) XmlIndent(prefix string, indent string, recordTag string) (bool, string, error) {
	if resultBean.Error != nil {
		return false, "", resultBean.Error
	}
	if !resultBean.Has {
		return resultBean.Has, "", nil
	}
	has, result, err := resultBean.Json()
	if err != nil {
		return false, "", err
	}
	if !has {
		return has, "", nil
	}
	var anydata = []byte(result)
	var i interface{}
	err = json.Unmarshal(anydata, &i)
	if err != nil {
		return false, "", err
	}
	resultByte, err := anyxml.XmlIndent(i, prefix, indent, recordTag)
	if err != nil {
		return false, "", err
	}

	return resultBean.Has, string(resultByte), err
}

type ResultMap struct {
	Results []map[string]interface{}
	Error   error
}

func (resultMap *ResultMap) List() ([]map[string]interface{}, error) {
	return resultMap.Results, resultMap.Error
}

func (resultMap *ResultMap) Count() (int, error) {
	if resultMap.Error != nil {
		return 0, resultMap.Error
	}
	if resultMap.Results == nil {
		return 0, nil
	}
	return len(resultMap.Results), nil
}

func (resultMap *ResultMap) ListPage(firstResult int, maxResults int) ([]map[string]interface{}, error) {
	if resultMap.Error != nil {
		return nil, resultMap.Error
	}
	if resultMap.Results == nil {
		return nil, nil
	}
	if firstResult >= maxResults {
		return nil, ErrParamsFormat
	}
	if firstResult < 0 {<commit_msg>bug fixed for ListPage method<commit_after>// Copyright 2015 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
	"database/sql"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"reflect"
	"regexp"

	"strconv"
	"strings"
	"time"

	"github.com/Chronokeeper/anyxml"
	"github.com/xormplus/core"
	"gopkg.in/flosch/pongo2.v3"
)

type ResultBean struct {
	Has    bool
	Result interface{}
	Error  error
}

func (resultBean *ResultBean) Json() (bool, string, error) {
	if resultBean.Error != nil {
		return resultBean.Has, "", resultBean.Error
	}
	if !resultBean.Has {
		return resultBean.Has, "", nil
	}
	result, err := JSONString(resultBean.Result, true)
	return resultBean.Has, result, err
}

func (resultBean *ResultBean) GetResult() (bool, interface{}, error) {
	return resultBean.Has, resultBean.Result, resultBean.Error
}

func (session *Session) GetFirst(bean interface{}) *ResultBean {
	has, err := session.Get(bean)
	r := &ResultBean{Has: has, Result: bean, Error: err}
	return r
}

func (resultBean *ResultBean) Xml() (bool, string, error) {

	if resultBean.Error != nil {
		return false, "", resultBean.Error
	}
	if !resultBean.Has {
		return resultBean.Has, "", nil
	}
	has, result, err := resultBean.Json()
	if err != nil {
		return false, "", err
	}
	if !has {
		return has, "", nil
	}
	var anydata = []byte(result)
	var i interface{}
	err = json.Unmarshal(anydata, &i)
	if err != nil {
		return false, "", err
	}
	resultByte, err := anyxml.Xml(i)
	if err != nil {
		return false, "", err
	}

	return resultBean.Has, string(resultByte), err
}

func (resultBean *ResultBean) XmlIndent(prefix string, indent string, recordTag string) (bool, string, error) {
	if resultBean.Error != nil {
		return false, "", resultBean.Error
	}
	if !resultBean.Has {
		return resultBean.Has, "", nil
	}
	has, result, err := resultBean.Json()
	if err != nil {
		return false, "", err
	}
	if !has {
		return has, "", nil
	}
	var anydata = []byte(result)
	var i interface{}
	err = json.Unmarshal(anydata, &i)
	if err != nil {
		return false, "", err
	}
	resultByte, err := anyxml.XmlIndent(i, prefix, indent, recordTag)
	if err != nil {
		return false, "", err
	}

	return resultBean.Has, string(resultByte), err
}

type ResultMap struct {
	Results []map[string]interface{}
	Error   error
}

func (resultMap *ResultMap) List() ([]map[string]interface{}, error) {
	return resultMap.Results, resultMap.Error
}

func (resultMap *ResultMap) Count() (int, error) {
	if resultMap.Error != nil {
		return 0, resultMap.Error
	}
	if resultMap.Results == nil {
		return 0, nil
	}
	return len(resultMap.Results), nil
}

func (resultMap *ResultMap) ListPage(firstResult int, maxResults int) ([]map[string]interface{}, error) {
	if resultMap.Error != nil {
		return nil, resultMap.Error
	}
	if resultMap.Results == nil {
		return nil, nil
	}
	if firstResult > maxResults {
		return nil, ErrParamsFormat
	}
	if firstResult < 0 {
{'fraction_non_alphanumeric': 0.14023678505919626, 'fraction_numerical': 0.0030015007503751876, 'mean_word_length': 3.9611248966087675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '5166376', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2095, 'n_words': 614}