<commit_before>package main

import (
	"fmt"
	"reflect"
	"strconv"

	"database/sql"
	_ "github.com/lib/pq"
	"log"
)

var db *sql.DB

//Book is on the table
type Book struct {
	ID    int
	Name  string
	Pages int
}

func initDB() {
	tmpDB, err := sql.Open("postgres", "user=docker password=<PASSWORD> dbname=docker sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	err = tmpDB.Ping()
	if err != nil {
		log.Fatal(err)
	}

	db = tmpDB
}

func save(table interface{}) {

	typeOfTable := reflect.TypeOf(table)
	valueOfTable := reflect.ValueOf(table)

	tableName := typeOfTable.Name()

	fields := make(map[string]string)
	for i := 0; i < typeOfTable.NumField(); i++ {
		fieldName := typeOfTable.Field(i).Name

		if fieldName == "ID" && valueOfTable.Field(i).Int() <= 0 {
			continue
		}

		if typeOfTable.Field(i).Type.Name() == "int" {
			fields[fieldName] = strconv.Itoa(int(valueOfTable.Field(i).Int()))
		}
		if typeOfTable.Field(i).Type.Name() == "string" {
			fields[fieldName] = "'" + valueOfTable.Field(i).String() + "'"
		}
	}

	sqlInstruction := "insert into " + tableName + "("
	sqlFields := ""
	sqlValues := ""
	for fieldName, value := range fields {
		sqlFields = sqlFields + fieldName + ", "
		sqlValues = sqlValues + value + ", "
	}
	sqlFields = sqlFields[:len(sqlFields)-2]
	sqlValues = sqlValues[:len(sqlValues)-2]
	sqlInstruction = sqlInstruction + sqlFields + ") values (" + sqlValues + ");"

	result, err := db.Exec(sqlInstruction)
	fmt.Printf("sqlInstruction: %v\n", sqlInstruction)
	fmt.Printf("result: %v\n", result)
	fmt.Printf("err: %v\n", err)
}

func createTable(table interface{}) {

	typeOfTable := reflect.TypeOf(table)

	tableName := typeOfTable.Name()

	fieldsList := ""
	for i := 0; i < typeOfTable.NumField(); i++ {
		fieldName := typeOfTable.Field(i).Name
		fieldType := ""
		if typeOfTable.Field(i).Type.Name() == "int" {
			fieldType = "integer"
		}
		if typeOfTable.Field(i).Type.Name() == "string" {
			fieldType = "character varying"
		}

		if fieldName == "ID" {
			fieldsList = fieldsList + fieldName + " " + fieldType + " NOT NULL, "
		} else {
			fieldsList = fieldsList + fieldName + " " + fieldType + ", "
		}
	}

	primaryKey := "constraint " + tableName + "_pkey primary key (id)"
	sqlInstruction := "create table " + tableName + " (" + fieldsList + " " + primaryKey + ");\n"

	result, err := db.Exec(sqlInstruction)
	fmt.Printf("result: %v\n", result)
	fmt.Printf("err: %v\n", err)
}

func main() {
	initDB()
	createTable(Book{})
	save(Book{Name: "<NAME>", Pages: 199})
}
<commit_msg>Change ID field type to serial when creating new tables, this way new records will have the ID automatically assigned.<commit_after>package main

import (
	"fmt"
	"reflect"
	"strconv"

	"database/sql"
	_ "github.com/lib/pq"
	"log"
)

var db *sql.DB

//Book is on the table
type Book struct {
	ID    int
	Name  string
	Pages int
}

func initDB() {
	tmpDB, err := sql.Open("postgres", "user=docker password=<PASSWORD> dbname=docker sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	err = tmpDB.Ping()
	if err != nil {
		log.Fatal(err)
	}

	db = tmpDB
}

func save(table interface{}) {

	typeOfTable := reflect.TypeOf(table)
	valueOfTable := reflect.ValueOf(table)

	tableName := typeOfTable.Name()

	fields := make(map[string]string)
	for i := 0; i < typeOfTable.NumField(); i++ {
		fieldName := typeOfTable.Field(i).Name

		if fieldName == "ID" {
			fields[fieldName] = "serial"
		} else {
			if typeOfTable.Field(i).Type.Name() == "int" {
				fields[fieldName] = strconv.Itoa(int(valueOfTable.Field(i).Int()))
			}
			if typeOfTable.Field(i).Type.Name() == "string" {
				fields[fieldName] = "'" + valueOfTable.Field(i).String() + "'"
			}
		}
	}
	sqlInstruction := "select 1"
	id, _ := strconv.Atoi(fields["ID"])
	if id > 0 {
		//update
	} else {
		sqlInstruction = "insert into " + tableName + "("
		sqlFields := ""
		sqlValues := ""
		for fieldName, value := range fields {
			if fieldName == "ID" {
				continue
			}
			sqlFields = sqlFields + fieldName + ", "
			sqlValues = sqlValues + value + ", "
		}
		sqlFields = sqlFields[:len(sqlFields)-2]
		sqlValues = sqlValues[:len(sqlValues)-2]
		sqlInstruction = sqlInstruction + sqlFields + ") values (" + sqlValues + ");"
	}

	result, err := db.Exec(sqlInstruction)
	fmt.Printf("sqlInstruction: %v\n", sqlInstruction)
	fmt.Printf("result: %v\n", result)
	fmt.Printf("err: %v\n", err)
}

func createTable(table interface{}) {

	typeOfTable := reflect.TypeOf(table)

	tableName := typeOfTable.Name()

	fieldsList := ""
	for i := 0; i < typeOfTable.NumField(); i++ {
		fieldName := typeOfTable.Field(i).Name
		fieldType := ""
		if typeOfTable.Field(i).Type.Name() == "int" {
			fieldType = "integer"
		}
		if typeOfTable.Field(i).Type.Name() == "string" {
			fieldType = "character varying"
		}

		if fieldName == "ID" {
			fieldsList = fieldsList + fieldName + " serial NOT NULL, "
		} else {
			fieldsList = fieldsList + fieldName + " " + fieldType + ", "
		}
	}

	primaryKey := "constraint " + tableName + "_pkey primary key (id)"
	sqlInstruction := "create table " + tableName + " (" + fieldsList + " " + primaryKey + ");\n"

	result, err := db.Exec(sqlInstruction)
	fmt.Printf("result: %v\n", result)
	fmt.Printf("err: %v\n", err)
}

func main() {
	initDB()
	createTable(Book{})
	save(Book{Name: "<NAME>", Pages: 199})
}

{'fraction_non_alphanumeric': 0.17557395558901015, 'fraction_numerical': 0.00319909672563041, 'mean_word_length': 3.732858414959929, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 8, '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': '1702317', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1939, 'n_words': 419}