# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import psycopg2

with psycopg2.connect('host=localhost dbname=Test user=postgres password=hemmeligt') as con:
    c = con.cursor()
    c.execute('DROP TABLE chessopening')
    c.execute('CREATE TABLE chessopening (id INTEGER NOT NULL, ' +
                                         'names VARCHAR(255), ' +
                                         'moves TEXT, PRIMARY KEY(id))')
    c.execute("CREATE INDEX names_ix ON chessopening USING GIN(TO_TSVECTOR('english', names))")
    c.execute("CREATE INDEX moves_ix ON chessopening USING GIN(TO_TSVECTOR('english',moves))")
    c.execute("CREATE INDEX names_moves_ix ON chessopening USING GIN(TO_TSVECTOR('english',names || ' ' || moves))")
    c.execute("INSERT INTO chessopening VALUES(1, 'Spanish defense, Ruy Lopez', '1. e4, e5, 2. Nf3, Nc6, 3. Bb5,')")
    c.execute("INSERT INTO chessopening VALUES(2, 'French defense', '1. e4, e6, 2. d4, d5,')")
    c.execute("INSERT INTO chessopening VALUES(3, 'Sicilian defense', '1. e4, c5,')")
    c.execute("INSERT INTO chessopening VALUES(4, 'Queens gambit', '1. d4, d5, 2. c4,')")
    c.execute("INSERT INTO chessopening VALUES(5, 'Dutch defense', '1. d4, f5,')")
    c.execute("INSERT INTO chessopening VALUES(6, 'Reti opening', '1. Nf3, d5, 2. c4,')")
    c.close()
    con.commit()
