import motor
import error
from tornado import gen
from bson.objectid import ObjectId


class Connection(object):
    def __init__(self, host='localhost', port=None, db=None):
        self.host = host
        self.port = port
        self.db = db


class Database(object):
    def __init__(self, connection):
        if not isinstance(connection, Connection):
            raise error.StormError('connection must be instance of storm.db.Connection')

        self.connection = connection
        self.is_connected = False


class MongoDb(Database):
    def connect(self):
        if self.is_connected:
            return

        self.motor_client = motor.MotorClient(
            self.connection.host,
            self.connection.port
        ).open_sync()

        self.db = self.motor_client[self.connection.db]
        self.is_connected = True

    @gen.coroutine
    def select_one(self, table, **kwargs):
        self.connect()

        if '_id' in kwargs:
            kwargs['_id'] = ObjectId(kwargs['_id'])

        result = yield motor.Op(getattr(self.db, table).find_one, kwargs)

        if result is None:
            raise error.StormNotFoundError("Object of type: %s not found with args: %s" % (table, kwargs))

        callback = kwargs.get('callback')

        if callback is None:
            raise gen.Return(result)

        callback(result)

    @gen.coroutine
    def insert(self, table, data, callback=None):
        result = yield self.update(table, data, callback)

        if callback is None:
            raise gen.Return(result)

        callback(result)

    @gen.coroutine
    def update(self, table, data, callback=None):
        self.connect()

        if '_id' in data:
            data['_id'] = ObjectId(data['_id'])

        result = yield motor.Op(self.db[table].save, data)

        if callback is None:
            raise gen.Return(result)

        callback(result)

    @gen.coroutine
    def delete(self, table, primary_key_field, primary_key_value, callback=None):
        self.connect()

        to_delete = {
            primary_key_field: ObjectId(primary_key_value)
        }

        result = yield motor.Op(self.db[table].remove, to_delete)

        if callback is None:
            raise gen.Return(result)

        callback(result)

@@ -1345,32 +1345,848 @@
 llback(result)%0A%0A
+    @gen.coroutine%0A    def select_multiple(self, table, data, **kwargs):%0A        cursor = getattr(self.db, table).find(data)%0A%0A        # handle pagination%0A        if 'page' in kwargs:%0A            page = kwargs%5B'page'%5D%0A            page_size = kwargs.get('page_size', 10)%0A            offset = (page - 1) * page_size%0A            cursor.limit(page_size)%0A            cursor.skip(offset)%0A%0A        # default sort to newest first%0A        sort = kwargs.get('sort', %5B('_id', 0)%5D)%0A        cursor.sort(sort)%0A%0A        data = %5B%5D%0A        while (yield cursor.fetch_next):%0A            data.append(cursor.next_object())%0A%0A        total_count = yield motor.Op(cursor.count)%0A%0A        callback = kwargs.get('callback')%0A%0A        if callback is None:%0A            raise gen.Return(%5Bdata, total_count%5D)%0A%0A        callback(%5Bdata, total_count%5D)%0A%0A
     @gen.corouti

Add db method to select_multiple rows