<commit_before>package main

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

var db *sql.DB

func setupDatabase( pgPassword string ) {
        var err error

        // set up DB
        db, err := sql.Open("postgres", "password='"+pgPassword+"' user=postgres dbname=postgres host=192.168.127.12 port=5432 sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}

	sqlSetup := `
        CREATE TABLE IF NOT EXISTS keys ( kID BIGINT NOT NULL, kVal bytea NOT NULL ,  oID BIGINT NOT NULL, PRIMARY KEY( kID ) );
        CREATE TABLE IF NOT EXISTS keyUsers ( kID BIGINT NOT NULL, uID BIGINT NOT NULL , PRIMARY KEY( kID,uID ) );
        CREATE TABLE IF NOT EXISTS keyAdmins ( kID BIGINT NOT NULL, uID BIGINT NOT NULL , PRIMARY KEY( kID,uID ) );
        `
        _, err = db.Exec(sqlSetup)
        if err != nil {
                log.Println("sql fatal error in setupDatabase")
                log.Printf("%q\n", err)
        }

	log.Printf("Setup Database\n")
}

func main() {
	var pgPassword string = os.Getenv("SECM_DB_SECRET")
	setupDatabase(pgPassword)
        defer db.Close()

}<commit_msg>fetch key from DB<commit_after>package main

import (
        "database/sql"
	_ "github.com/lib/pq"
        "log"
	"fmt"
	"os"
)

var db *sql.DB

func setupDatabase( pgPassword string ) { // todo pass in hostname, port, username
        var err error

        // set up DB
        db, err = sql.Open("postgres", "password='"+pgPassword+"' user=postgres dbname=postgres host=192.168.127.12 port=5432 sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	
	sqlSetup := `
        CREATE TABLE IF NOT EXISTS keys ( kID BIGINT NOT NULL, kVal bytea NOT NULL ,  oID BIGINT NOT NULL, PRIMARY KEY( kID ) );
        CREATE TABLE IF NOT EXISTS keyUsers ( kID BIGINT NOT NULL, uID BIGINT NOT NULL , PRIMARY KEY( kID,uID ) );
        CREATE TABLE IF NOT EXISTS keyAdmins ( kID BIGINT NOT NULL, uID BIGINT NOT NULL , PRIMARY KEY( kID,uID ) );
        `
        _, err = db.Exec(sqlSetup)
        if err != nil {
                log.Println("sql fatal error in setupDatabase")
                log.Printf("%q\n", err)
        }

	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}
	
	log.Printf("Setup Database\n")
}


func getKey( keyID, userID string ) {
	stmt, err := db.Prepare(
		"SELECT keys.kVal  FROM keyUsers JOIN keys ON  keys.kID = keyUsers.kID WHERE keyUsers.uID = $2 AND keyUsers.kID = $1")
	if err != nil {
		log.Println("sql fatal error in getKey prep")
		log.Fatal(err)
	}
	var keyVal string
	err = stmt.QueryRow(keyID,userID).Scan( &keyVal ) 
	if err != nil {
		log.Println("sql fatal error in getKey querry")
		log.Fatal(err)
	}
	fmt.Println(keyVal)
}


func main() {
	var err error
	
	var pgPassword string = os.Getenv("SECM_DB_SECRET")
	setupDatabase(pgPassword)
        defer db.Close()

	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}
	
	getKey( "101", "1" )
}
{'fraction_non_alphanumeric': 0.12124352331606218, 'fraction_numerical': 0.012435233160621761, 'mean_word_length': 2.5885997521685256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1704021', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1010, 'n_words': 302}