Exemple #1
0
def get_databases():
    sql_dbs = [
        _SQLFormDb(
            XFormInstanceSQL._meta.db_table,
            lambda id_: XFormInstanceSQL.get_obj_by_id(id_),
            XFormInstanceSQL.__name__
        ),
        _SQLDb(
            CommCareCaseSQL._meta.db_table,
            lambda id_: CommCareCaseSQLRawDocSerializer(CommCareCaseSQL.get_obj_by_id(id_)).data,
            CommCareCaseSQL.__name__
        ),
        _SQLDb(
            SQLLocation._meta.db_table,
            lambda id_: SQLLocation.objects.get(location_id=id_).to_json(),
            SQLLocation.__name__
        ),
    ]

    all_dbs = OrderedDict()
    couchdbs_by_name = couch_config.all_dbs_by_db_name
    for dbname in sorted(couchdbs_by_name):
        all_dbs[dbname] = _CouchDb(couchdbs_by_name[dbname])
    for db in sql_dbs:
        all_dbs[db.dbname] = db
    return all_dbs
Exemple #2
0
def get_databases():
    """Return an ordered dict of (dbname: database). The order is
    according to search preference, the first DB to contain a document
    should be assumed to be the authoritative one."""
    sql_dbs = [
        _SQLDb(
            XFormInstanceSQL._meta.db_table,
            lambda id_: XFormInstanceSQL.get_obj_by_id(id_),
            "XFormInstance",
            lambda doc: XFormInstanceSQLRawDocSerializer(doc).data,
        ),
        _SQLDb(
            CommCareCaseSQL._meta.db_table,
            lambda id_: CommCareCaseSQL.get_obj_by_id(id_),
            "CommCareCase",
            lambda doc: CommCareCaseSQLRawDocSerializer(doc).data,
        ),
        _SQLDb(SQLLocation._meta.db_table,
               lambda id_: SQLLocation.objects.get(location_id=id_),
               'Location', lambda doc: doc.to_json()),
    ]

    all_dbs = OrderedDict()
    for db in sql_dbs:
        all_dbs[db.dbname] = db
    couchdbs_by_name = couch_config.all_dbs_by_db_name
    for dbname in sorted(couchdbs_by_name):
        all_dbs[dbname] = _CouchDb(couchdbs_by_name[dbname])
    return all_dbs
    def get(self, record_id):
        try:
            return self._getter(record_id)
        except (XFormNotFound, CaseNotFound, ObjectDoesNotExist):
            raise ResourceNotFound("missing")


_SQL_DBS = OrderedDict((db.dbname, db) for db in [
    _Db(
        XFormInstanceSQL._meta.db_table, lambda id_:
        XFormInstanceSQLRawDocSerializer(XFormInstanceSQL.get_obj_by_id(id_)
                                         ).data, XFormInstanceSQL.__name__),
    _Db(
        CommCareCaseSQL._meta.db_table, lambda id_:
        CommCareCaseSQLRawDocSerializer(CommCareCaseSQL.get_obj_by_id(id_)
                                        ).data, CommCareCaseSQL.__name__),
    _Db(SQLLocation._meta.db_table, lambda id_: SQLLocation.objects.get(
        location_id=id_).to_json(), SQLLocation.__name__),
])


def get_db_from_db_name(db_name):
    if db_name in _SQL_DBS:
        return _SQL_DBS[db_name]
    elif db_name == couch_config.get_db(None).dbname:  # primary db
        return couch_config.get_db(None)
    else:
        return couch_config.get_db(db_name)


def _lookup_id_in_database(doc_id, db_name=None):
Exemple #4
0
    def get(self, record_id):
        try:
            return self._getter(record_id)
        except (XFormNotFound, CaseNotFound, ObjectDoesNotExist):
            raise ResourceNotFound("missing")


_SQL_DBS = OrderedDict((db.dbname, db) for db in [
    _Db(
        XFormInstanceSQL._meta.db_table,
        lambda id_: XFormInstanceSQLRawDocSerializer(XFormInstanceSQL.get_obj_by_id(id_)).data,
        XFormInstanceSQL.__name__
    ),
    _Db(
        CommCareCaseSQL._meta.db_table,
        lambda id_: CommCareCaseSQLRawDocSerializer(CommCareCaseSQL.get_obj_by_id(id_)).data,
        CommCareCaseSQL.__name__
    ),
    _Db(
        SQLLocation._meta.db_table,
        lambda id_: SQLLocation.objects.get(location_id=id_).to_json(),
        SQLLocation.__name__
    ),
])


def _get_db_from_db_name(db_name):
    if db_name in _SQL_DBS:
        return _SQL_DBS[db_name]
    elif db_name == couch_config.get_db(None).dbname:  # primary db
        return couch_config.get_db(None)