<commit_before>package main

import (
	"database/sql"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"io/ioutil"
	"net/http"
)

const (
	DATA_HOST = "localhost"
	DATA_PORT = 3306
	PORT      = 8080
)

type Config struct {
	DataHost     string
	DataPort     int
	DataName     string
	DataUsername string
	DataPassword string
	Port         int
}

func AssignConfigDefaultValues(config Config) Config {
	if config.DataHost == "" {
		config.DataHost = DATA_HOST
	}
	if config.DataPort == 0 {
		config.DataPort = DATA_PORT
	}
	if config.Port == 0 {
		config.Port = PORT
	}
	return config
}

func GetConfig(filename string) (Config, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return Config{}, err
	}
	var config Config
	err = json.Unmarshal(data, &config)
	if err != nil {
		return Config{}, err
	}
	return AssignConfigDefaultValues(config), nil
}

func GetQueries(filename string) (map[string]string, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	var queries map[string]string
	err = json.Unmarshal(data, &queries)
	if err != nil {
		return nil, err
	}
	return queries, nil
}

func GetFilenames() (string, string, error) {
	var configFile string
	var queriesFile string
	flag.StringVar(&configFile, "c", "", "config.json file")
	flag.StringVar(&queriesFile, "q", "", "queries.json file")
	flag.Parse()
	if configFile == "" || queriesFile == "" {
		return configFile, queriesFile, errors.New("You must specify a config.json and a queries.json file using -c and -q")
	}
	return configFile, queriesFile, nil
}

func GetDBConnectionAndQueries() (*sql.DB, Config, map[string]string, error) {
	configFile, queriesFile, err := GetFilenames()
	if err != nil {
		return nil, Config{}, nil, err
	}

	config, err := GetConfig(configFile)
	if err != nil {
		return nil, Config{}, nil, err
	}

	connectionString := fmt.Sprintf("%v:%v@/%v", config.DataUsername, config.DataPassword, config.DataName)
	db, err := sql.Open("mysql", connectionString)
	if err != nil {
		return nil, Config{}, nil, err
	}

	queries, err := GetQueries(queriesFile)
	if err != nil {
		return nil, Config{}, nil, err
	}

	return db, config, queries, nil

}

var (
	db, config, queries, startupError = GetDBConnectionAndQueries()
)

func GetQueryAndParams(r *http.Request) (string, []string, error) {
	queryStringMap := r.URL.Query()
	queries := queryStringMap["q"]
	if len(queries) == 0 {
		return "", nil, errors.New("You must provide a query (q=)")
	}
	query := queries[0]
	params := queryStringMap["p"]
	return query, params, nil
}

func Handler(w http.ResponseWriter, r *http.Request) {
	query, params, err := GetQueryAndParams(r)
	if err != nil {
		fmt.Fprintf(w, err.Error())
		return
	}

	storedQuery, queryIsPresent := queries[query]
	if !queryIsPresent {
		fmt.Fprintf(w, "Query is not present")
		return
	}

	paramsInterfaced := make([]interface{}, len(params))
	for i, v := range params {
		paramsInterfaced[i] = v
	}

	rows, err := db.Query(storedQuery, paramsInterfaced...)
	if err != nil {
		fmt.Fprintf(w, "Something went wrong")
		return
	}

	columns, err := rows.Columns()
	if err != nil {
		fmt.Fprintf(w, "Something with columns")
		return
	}

	rawResult := make([][]byte, len(columns))
	result := make([]string, len(columns))

	destination := make([]interface{}, len(columns))
	for i, _ := range rawResult {
		destination[i] = &rawResult[i]
	}

	defer rows.Close()
	for rows.Next() {
		err = rows.Scan(destination...)
		if err != nil {

		}

		for i, raw := range rawResult {
			if raw == nil {
				result[i] = "\\N"
			} else {
				result[i] = string(raw)
			}
		}
		fmt.Printf("%#v\n", result)
	}
	fmt.Fprintf(w, "You pass butter")
}

func main() {
	if startupError != nil {
		fmt.Println(startupError)
		return
	}
	listenPort := fmt.Sprintf(":%d", config.Port)
	http.HandleFunc("/", Handler)
	http.ListenAndServe(listenPort, nil)
}<commit_msg>Converted responses to json and added validation on number of parameters passed<commit_after>package main

import (
	"database/sql"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"io/ioutil"
	"net/http"
	"strings"
)

const (
	DATA_HOST = "localhost"
	DATA_PORT = 3306
	PORT      = 8080
)

type Config struct {
	DataHost     string
	DataPort     int
	DataName     string
	DataUsername string
	DataPassword string
	Port         int
}

func AssignConfigDefaultValues(config Config) Config {
	if config.DataHost == "" {
		config.DataHost = DATA_HOST
	}
	if config.DataPort == 0 {
		config.DataPort = DATA_PORT
	}
	if config.Port == 0 {
		config.Port = PORT
	}
	return config
}

func GetConfig(filename string) (Config, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return Config{}, err
	}
	var config Config
	err = json.Unmarshal(data, &config)
	if err != nil {
		return Config{}, err
	}
	return AssignConfigDefaultValues(config), nil
}

func GetQueries(filename string) (map[string]string, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	var queries map[string]string
	err = json.Unmarshal(data, &queries)
	if err != nil {
		return nil, err
	}
	return queries, nil
}

func GetFilenames() (string, string, error) {
	var configFile string
	var queriesFile string
	flag.StringVar(&configFile, "c", "", "config.json file")
	flag.StringVar(&queriesFile, "q", "", "queries.json file")
	flag.Parse()
	if configFile == "" || queriesFile == "" {
		return configFile, queriesFile, errors.New("You must specify a config.json and a queries.json file using -c and -q")
	}
	return configFile, queriesFile, nil
}

func GetDBConnectionAndQueries() (*sql.DB, Config, map[string]string, error) {
	configFile, queriesFile, err := GetFilenames()
	if err != nil {
		return nil, Config{}, nil, err
	}

	config, err := GetConfig(configFile)
	if err != nil {
		return nil, Config{}, nil, err
	}

	connectionString := fmt.Sprintf("%v:%v@/%v", config.DataUsername, config.DataPassword, config.DataName)
	db, err := sql.Open("mysql", connectionString)
	if err != nil {
		return nil, Config{}, nil, err
	}

	queries, err := GetQueries(queriesFile)
	if err != nil {
		return nil, Config{}, nil, err
	}

	return db, config, queries, nil

}

var (
	db, config, queries, startupError = GetDBConnectionAndQueries()
)

func GetQueryAndParams(r *http.Request) (string, []string, error) {
	queryStringMap := r.URL.Query()
	queries := queryStringMap["q"]
	if len(queries) == 0 {
		return "", nil, errors.New("You must provide a query (q=)")
	}
	query := queries[0]
	params := queryStringMap["p"]
	return query, params, nil
}

type ErrorJson struct {
	ErrorMessage string
}

func WriteErrorMessage(w http.ResponseWriter, errorString string) {
	w.WriteHeader(http.StatusBadRequest)
	errorJson := ErrorJson{errorString}
	json, err := json.Marshal(errorJson)
	if err != nil {
		fmt.Fprintf(w, "{\"ErrorMessage\":\"Something went wrong\"")
		return
	}
	w.Write(json)
}

func Handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Content-Type", "application/json")

	query, params, err := GetQueryAndParams(r)
	if err != nil {
		WriteErrorMessage(w, err.Error())
		return
	}

	storedQuery, queryIsPresent := queries[query]
	if !queryIsPresent {
		WriteErrorMessage(w, "Passed query is not present in queries.json")
		return
	}

	parameterCount := strings.Count(storedQuery, "?")
	if parameterCount != len(params) {
		WriteErrorMessage(w, "Count of passed parameters are unequal to expected parameters")
		return
	}

	paramsInterfaced := make([]interface{}, len(params))
	for i, v := range params {
		paramsInterfaced[i] = v
	}

	rows, err := db.Query(storedQuery, paramsInterfaced...)
	if err != nil {
		WriteErrorMessage(w, err.Error())
		return
	}

	columns, err := rows.Columns()
	if err != nil {
		WriteErrorMessage(w, err.Error())
		return
	}

	rawResult := make([][]byte, len(columns))
	result := make([]string, len(columns))

	destination := make([]interface{}, len(columns))
	for i, _ := range rawResult {
		destination[i] = &rawResult[i]
	}

	defer rows.Close()
	for rows.Next() {
		err = rows.Scan(destination...)
		if err != nil {
			WriteErrorMessage(w, err.Error())
			return
		}

		for i, raw := range rawResult {
			if raw == nil {
				result[i] = "\\N"
			} else {
				result[i] = string(raw)
			}
		}
		fmt.Printf("%#v\n", result)
	}
	WriteErrorMessage(w, "You pass butter")
}

func main() {
	if startupError != nil {
		fmt.Println(startupError)
		return
	}
	listenPort := fmt.Sprintf(":%d", config.Port)
	http.HandleFunc("/", Handler)
	http.ListenAndServe(listenPort, nil)
}
{'fraction_non_alphanumeric': 0.14688152098307442, 'fraction_numerical': 0.0027822861117551586, 'mean_word_length': 3.9838243789716925, 'pattern_counts': {'":': 3, '<': 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': '4179419', 'n_tokens_mistral': 3136, 'n_tokens_neox': 2988, 'n_words': 792}