def update(self, context, table_info, field_list=None):
        if not field_list:
            field_list = self.__field_list

        if 'last_update_date_time' not in field_list:
            field_list.append('last_update_date_time')
        table_info.last_update_date_time = datetime.now()

        enc = encoder.Encoder()

        query_builder = collections.deque()
        query_builder.append("UPDATE {} SET ".format(
            self.SYSTEM_TABLE_TABLE_INFO))

        query_builder.append(",".join([
            '"{}"={}'.format(
                field, enc.cql_encode_all_types(getattr(table_info, field)))
            for field in field_list
        ]))

        query_builder.append(
            " WHERE tenant='{}' AND name='{}' IF exists=1".format(
                context.tenant, table_info.name))

        result = self.__cluster_handler.execute_query("".join(query_builder),
                                                      consistent=True)

        if not result[0]['[applied]']:
            raise exception.TableNotExistsException(
                "Table {} is not exists".format(table_info.name))
        self._remove_table_info_from_cache(context, table_info.name)
        return True
Beispiel #2
0
    def save(self, context, table_info):
        enc = encoder.Encoder()

        query_builder = collections.deque()
        query_builder.append(
            'INSERT INTO {} '
            '(exists, tenant, name, id, "schema", status,'
            ' internal_name, last_update_date_time, creation_date_time)'
            "VALUES(1, '{}', '{}', {}".format(
                self.SYSTEM_TABLE_TABLE_INFO, context.tenant, table_info.name,
                enc.cql_encode_all_types(table_info.id)
            )
        )

        if table_info.schema:
            query_builder.append(",'{}'".format(table_info.schema.to_json()))
        else:
            query_builder.append(",null")

        if table_info.status:
            query_builder.append(",'{}'".format(table_info.status))
        else:
            query_builder.append(",null")

        try:
            internal_name = table_info.internal_name
        except AttributeError:
            internal_name = None

        if internal_name:
            query_builder.append(",'{}'".format(table_info.internal_name))
        else:
            query_builder.append(",null")

        table_info.last_update_date_time = timeutils.utcnow()
        query_builder.append(", {}".format(
            enc.cql_encode_datetime(table_info.last_update_date_time)))

        table_info.creation_date_time = table_info.last_update_date_time
        query_builder.append(", {}".format(
            enc.cql_encode_datetime(table_info.creation_date_time)))

        query_builder.append(") IF NOT EXISTS")

        result = self.__cluster_handler.execute_query(
            "".join(query_builder), consistent=True
        )

        if not result[0]['[applied]']:
            raise exception.TableAlreadyExistsException(
                "Table {} already exists".format(table_info.name)
            )

        self._save_table_info_to_cache(context, copy.copy(table_info))
        return True
 def __init__(self, cluster_handler):
     self.__cluster_handler = cluster_handler
     self.__enc = encoder.Encoder()