<commit_before>package pg

import (
	"database/sql"
	"fmt"
	_ "github.com/lib/pq"
	"github.com/vokalinteractive/vip/fetch"
	"github.com/vokalinteractive/vip/store"
	"log"
	"net/http"
	"strings"
)

type Postgres struct {
	db *sql.DB
}

const (
	tableCreate = `CREATE TABLE vip_serving_keys (
                    id integer primary key, 
                    key varchar(30) NOT NULL, 
                    bucket varchar(30) NOT NULL, 
                    mime varchar(30) NOT NULL)`
	checkCreate = "SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name=vip_serving_keys)"
	keyQuery    = "SELECT key, bucket, mime FROM vip_serving_keys WHERE key = $1"
	keyInsert   = "INSERT INTO vip_serving_keys (key, bucket, mime) VALUES (?, ?, ?)"
)

func Dial(conn string) (*Postgres, error) {
	db, err := sql.Open("postgres", conn)
	if err != nil {
		return nil, err
	}

	rows, err := db.Query(checkCreate)
	if !rows.Next() {
		_, err = db.Exec(tableCreate)
	}

	return &Postgres{db}, err
}

func (p *Postgres) FindOriginal(storage store.ImageStore, c *fetch.CacheContext) ([]byte, string, error) {
	var key, bucket, mime string
	err := p.db.QueryRow(keyQuery, c.ImageId).Scan(&key, &bucket, &mime)

	if err == nil {
		data, err := storage.Get(bucket, key)
		if err != nil {
			log.Printf("s3 download: %s", err.Error())
			return nil, "", err
		}

		return data, mime, err
	}

	return nil, "", err
}
<commit_msg>Fix Postgres INSERT query<commit_after>package pg

import (
	"database/sql"
	"fmt"
	_ "github.com/lib/pq"
	"github.com/vokalinteractive/vip/fetch"
	"github.com/vokalinteractive/vip/store"
	"log"
	"net/http"
	"strings"
)

type Postgres struct {
	db *sql.DB
}

const (
	tableCreate = `CREATE TABLE vip_serving_keys (
                    id integer primary key, 
                    key varchar(30) NOT NULL, 
                    bucket varchar(30) NOT NULL, 
                    mime varchar(30) NOT NULL)`
	checkCreate = "SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name='vip_serving_keys')"
	keyQuery    = "SELECT key, bucket, mime FROM vip_serving_keys WHERE key = $1"
	keyInsert   = "INSERT INTO vip_serving_keys (key, bucket, mime) VALUES ($1, $2, $3)"
)

func Dial(conn string) (*Postgres, error) {
	db, err := sql.Open("postgres", conn)
	if err != nil {
		return nil, err
	}

	rows, err := db.Query(checkCreate)
	if err != nil {
		return nil, err
	}

	if !rows.Next() {
		_, err = db.Exec(tableCreate)
	}

	return &Postgres{db}, err
}

func (p *Postgres) FindOriginal(storage store.ImageStore, c *fetch.CacheContext) ([]byte, string, error) {
	var key, bucket, mime string
	err := p.db.QueryRow(keyQuery, c.ImageId).Scan(&key, &bucket, &mime)

	if err == nil {
		data, err := storage.Get(bucket, key)
		if err != nil {
			log.Printf("s3 download: %s", err.Error())
			return nil, "", err
		}

		return data, mime, err
	}

	return nil, "", err
}

{'fraction_non_alphanumeric': 0.13885041551246538, 'fraction_numerical': 0.006578947368421052, 'mean_word_length': 3.138968481375358, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1249653', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1028, 'n_words': 274}