<commit_before>package main

import (
	"database/sql"
	"fmt"
	"github.com/codegangsta/martini"
	_ "github.com/lib/pq"
)

func SetupDB() *sql.DB {
	db, err := sql.Open("postgres", "dbname=lesson4 sslmode=disable")
	if err != nil {
		panic(err)
	}

	// Does the Database Exist?
	err = db.Ping()
	if err != nil {
		panic(err)
	}

	return db
}

func FormatBook(title, author, description string) string {
	return fmt.Sprintf("Title: %s\nAuthor: %s\nDescription: %s\n\n", title, author, description)
}

func main() {
	m := martini.Classic()

	m.Map(SetupDB())

	m.Get("/", func(db *sql.DB) string {

		var title, author, description, output string

		rows, err := db.Query("SELECT title, author, description FROM books")
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		for rows.Next() {
			err := rows.Scan(&title, &author, &description)
			if err != nil {
				panic(err)
			}
			output += FormatBook(title, author, description)
		}

		if output == "" {
			output = "No Books Found!"
		}

		return output
	})

	m.Get("/:query", func(db *sql.DB, params martini.Params) string {

		var title, author, description, output string

		queryString := "SELECT title, author, description "
		queryString += "FROM books "
		queryString += "WHERE title ILIKE '%" + params["query"] + "%' "
		queryString += "OR author ILIKE '%" + params["query"] + "%' "
		queryString += "OR description ILIKE '%" + params["query"] + "%'"

		rows, err := db.Query(queryString)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		for rows.Next() {
			err := rows.Scan(&title, &author, &description)
			if err != nil {
				panic(err)
			}
			output += FormatBook(title, author, description)
		}

		if output == "" {
			output = "No Books Found!"
		}

		return output
	})

	m.Run()
<commit_msg>Cleaned up the SQL code.<commit_after>package main

import (
	"fmt"
	"database/sql"
	"net/http"
	"github.com/codegangsta/martini"
	_ "github.com/lib/pq"
)

func SetupDB() *sql.DB {
	db, err := sql.Open("postgres", "dbname=lesson4 sslmode=disable")
	if err != nil {
		panic(err)
	}

	return db
}

func main() {
	m := martini.Classic()
	m.Map(SetupDB())

	m.Get("/", func(db *sql.DB, r *http.Request, rw http.ResponseWriter) {
		search := "%" + r.URL.Query().Get("search") + "%"
		rows, err := db.Query(`SELECT title, author, description 
                           FROM books 
                           WHERE title ILIKE $1
                           OR author ILIKE $1
                           OR description ILIKE $1;`, search)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		var title, author, description string
		for rows.Next() {
			err := rows.Scan(&title, &author, &description)
			if err != nil {
				panic(err)
			}
			fmt.Fprintf(rw, "Title: %s\nAuthor: %s\nDescription: %s\n\n", title, author, description)
		}
	})

	m.Run()

{'fraction_non_alphanumeric': 0.16513437057991515, 'fraction_numerical': 0.001768033946251768, 'mean_word_length': 2.8700410396716824, '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': '6387801', 'n_tokens_mistral': 1105, 'n_tokens_neox': 999, 'n_words': 254}