'''
Created on 10/nov/2010

@author: giorgiodefelice
'''
import psycopg2

class dataBase(object):
    '''
    classdocs
    '''
    user = "postgres"
    pwd = "northern"
    host = "localhost"
    port = "5432"
    db = "postgres"
    
    dbConn = None

    def __init__(self):
        #connString =  "pq://%s:%s,@%s:%s/%s" % (self.user,self.pwd,self.host,self.port,self.db)
        #print(connString)
        #self.dbConn = postgresql.open(connString)
        self.conn = psycopg2.connect(database=self.db, user=self.user, password=self.pwd, host=self.host, port=self.port)
        self.cur = self.conn.cursor()
     

        
    def printConn(self):
        print(self.dbConn)
        
    def getCalculiByType(self, Type):
    	query = "select \"Name\" from \"Calculi\" where \"Type\" = %s" % (Type)
      	self.cur.execute(query)
        #return self.dbConn.prepare(query)
        return self.cur
        


    
    def getCalculusIdByName(self, Name):
        query = "select \"ID\" from \"Calculi\" where \"Name\" = '%s'" % (Name)
        self.cur.execute(query)
        id = self.cur.fetchone()
        return id
        
    def getRelationsById(self, Id):
        if Id == None:
            Id = 0
        query = "select \"Relname\" from \"Relations\" where \"Calculi_ID\" = %s" % (Id)
        self.cur.execute(query)
        return self.cur
        
