What is a simple MapRowParser in Anko?

I read the docs for Anko SQLite .

I know that creating a simple RowParser can be done by doing val rowParser = classParser<Person>()

The Parser class is a function defined in the Anko-SQLite source code.

How can I get simple MapRowParser?

+6
source share
1 answer

If you go to Github and do this search , you will see that there are two files where MapRowParser is referenced in some way.

The first file contains the following:

interface MapRowParser<out T> {
    fun parseRow(columns: Map<String, Any?>): T
}

What MapRowParser shows as an interface.

. , , MapRowParser. , , . , , . , Anko , MapRowParser.

, , MapRowParser . , , , , , .


RowParser. . , . RowMapParser.

EDIT:

, . , MapRowParser , . , , , :

private class SingleColumnParser<out T> : RowParser<T> {
    override fun parseRow(columns: Array<Any?>): T {
        if (columns.size != 1)
            throw SQLiteException("Invalid row: row for SingleColumnParser must contain exactly one column")
        @Suppress("UNCHECKED_CAST")
        return columns[0] as T//Right here it just casts the column as the type defined when creating
    }
}

, . , , , , , .

:

private fun readColumnsMap(cursor: Cursor): Map<String, Any?> {
    val count = cursor.columnCount
    val map = hashMapOf<String, Any?>()
    for (i in 0..(count - 1)) {
        map.put(cursor.getColumnName(i), cursor.getColumnValue(i))
    }
    return map
}

, . , - :

Col1 -> Row1col1val
Col2 -> Row1col2val
...

, :

moveToFirst()
while (!isAfterLast) {
    list.add(parser.parseRow(readColumnsMap(this)))//adds the result into a pre-defined list to return
    moveToNext()
}

, , , , , , .

* , , .. , , , MapRowParser, , . , , blob, , .

* , , . , ,   . , , . . Maps , , , .

+3

All Articles