import sqlite3


def mtxdb_matrix_shape(filename, mtxname):
    """Returns the shape of a sparse matrix in the db.

    :param filename: name of the sqlite3 db in which the matrix is stored
    :param mtname: name of the matrix(string)
    :rtype: (rowmin, rowmax, colmin, colmax): min/max of rows and columns
    """
    conn = sqlite3.connect(filename)
    c = conn.cursor()
    query = (mtxname,)

    c.execute("select min(i), max(i) from matrices where matrix = ?", query)
    result = c.fetchall()
    rowmin, rowmax = result[0]

    c.execute("select min(j), max(j) from matrices where matrix = ?", query)
    result = c.fetchall()
    colmin, colmax = result[0]
    c.close()

    return rowmin, rowmax+1, colmin, colmax+1