# -*- coding: UTF-8 -*-
#    This file is part of arena.
#
#    arena is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by the
#    Free Software Foundation; either version 3 of the License or, at your
#    option, any later version.
#
#    arena is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLITY
#    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
#    License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with arena.  If not, see <http://www.gnu.org/licenses/>.

## @file
#  This file defines a class that extends MySQLCursor to return query rows
#  as dictionary objects. 

import connector

## This class extends the base MySQLCursor class defined by the MySQL
#  connector library to return query rows as dictionary objects instead of
#  lists. 
class AMySQLDictionaryCursor(connector.cursor.MySQLCursor):
    ## Retrieve a row from the cursor.
    def fetchone(Self):
        result = None

        row = super(AMySQLDictionaryCursor, Self).fetchone()

        if row is not None:
            result = {}
            fieldNames = Self.column_names
            for i in xrange(0, len(fieldNames)):
                result[fieldNames[i]] = row[i]

            del row

        return result

    ## Retrieve all rows from the query.
    def fetchall(Self):
        result = None

        rows = super(AMySQLDictionaryCursor, Self).fetchall()

        if rows is not None:
            result = []
            fieldNames = Self.column_names

            for thisRow in rows:
                row = {}
                for i in xrange(0, len(fieldNames)):
                    row[fieldNames[i]] = thisRow[i]
                    result.append(row)

            del rows

        return result

## This class extends the base MySQLCursor class defined by the MySQL
#  connector library to return query rows as dictionary objects instead of
#  lists.
class AMySQLBufferedDictionaryCursor(connector.cursor.MySQLCursorBuffered):
    ## Retrieve a row from the cursor.
    def fetchone(Self):
        result = None

        row = super(AMySQLBufferedDictionaryCursor, Self).fetchone()

        if row is not None:
            result = {}
            fieldNames = Self.column_names
            for i in xrange(0, len(fieldNames)):
                result[fieldNames[i]] = row[i]

            del row

        return result

    ## Retrieve all rows from the query.
    def fetchall(Self):
        result = None

        rows = super(AMySQLBufferedDictionaryCursor).fetchall()

        if rows is not None:
            result = []
            fieldNames = Self.column_names

            for thisRow in rows:
                row = {}
                for i in xrange(0, len(fieldNames)):
                    row[fieldNames[i]] = thisRow[i]
                    result.append(row)

            del rows

        return result

