@@ -0,0 +1,3241 @@
+%22%22%22%0A    pgadapter.py%0A    PostgreSQL connection helper class.%0A    Author: Kenta Yamada%0A%22%22%22%0Aimport psycopg2%0Aimport psycopg2.extras%0A%0A%0Aclass PgAdapter():%0A    def __init__(self, config):%0A        self.__config = config%0A        self.__con = None%0A        self.__cur = None%0A%0A    def __create_cursor(self):%0A        %22%22%22%0A            attach database and create session%0A        %22%22%22%0A        if self.__config is None:%0A            raise ValueError()%0A        if self.__con is None or self.__con.closed:%0A            self.__con = psycopg2.connect(**self.__config)%0A        self.__cur = self.__con.cursor(cursor_factory=psycopg2.extras.DictCursor)%0A%0A%0A    def save(self, command, data):%0A        %22%22%22%0A            run save or change procedure%0A        %22%22%22%0A        if not command or data is None:%0A            raise ValueError()%0A        self.__create_cursor()%0A        self.__cur.callproc(command, data)%0A%0A    def remove(self, command, data=None):%0A        %22%22%22%0A            run remove procedure%0A        %22%22%22%0A        self.save(command, data)%0A%0A    def find(self, command, condition=None):%0A        %22%22%22%0A            run find rows procedure%0A        %22%22%22%0A        if not command:%0A            raise ValueError()%0A        self.__create_cursor()%0A        self.__cur.callproc(command, condition)%0A        return self.__cur.fetchall()%0A%0A    def find_one(self, command, condition):%0A        %22%22%22%0A            run find row procedure%0A        %22%22%22%0A        if not command:%0A            raise ValueError()%0A        self.__create_cursor()%0A        self.__cur.callproc(command, condition)%0A        return self.__cur.fetchone()%0A%0A    def execute(self, query, data=None):%0A        %22%22%22%0A            run sql command%0A        %22%22%22%0A        if not query:%0A            raise ValueError()%0A        self.__create_cursor()%0A        self.__cur.execute(query, data)%0A%0A    def fetch_rowcount(self, tablename):%0A        %22%22%22%0A            fetch row count by specific table%0A        %22%22%22%0A        if not tablename:%0A            raise ValueError()%0A        query = 'SELECT COUNT(id) AS rowcount FROM %7B0%7D;'.format(tablename)%0A        self.__create_cursor()%0A        self.__cur.execute(query)%0A        return self.__cur.fetchone()%5B'rowcount'%5D%0A%0A    def fetch_last_row_id(self):%0A        %22%22%22%0A            fetch last row id%0A        %22%22%22%0A        self.__create_cursor()%0A        self.__cur.execute('SELECT LASTVAL();')%0A        return self.__cur.fetchone()%5B'lastval'%5D%0A%0A    def bulk_insert(self, query, values):%0A        %22%22%22%0A            run bulk insert values %0A        %22%22%22%0A        if not query:%0A            raise ValueError()%0A        if values is None or len(values) %3C 1:%0A            raise ValueError()%0A        self.__create_cursor()%0A        self.__cur.executemany(query, values)%0A        return len(values) == self.__cur.rowcount%0A%0A    def commit(self):%0A        %22%22%22%0A            commit transaction%0A        %22%22%22%0A        if self.__con is not None and not self.__con.closed:%0A            self.__con.commit()%0A            self.__cur.close()%0A            self.__con.close()%0A            self.__con = None%0A%0A    def rollback(self):%0A        %22%22%22%0A            rollback transaction%0A        %22%22%22%0A        if self.__con is not None and not self.__con.closed:%0A            self.__con.rollback()%0A            self.__cur.close()%0A            self.__con.close()%0A            self.__con = None%0A

implement db helper.