from MyUtils.MetadataWebUtil import *

class ZillionUtil():
    def __init__(self, url):
        self.metadata = MetadataWebUtil(url)

    ##列出所有数据库
    def database_list(self):
        param = {
            "QueryType": "database_list"
        }
        databases = self.metadata.get_hbase(param)
        return databases["Content"]

        # TODO  其他zillion语句

    ##列出所有数据库下的表（不包含分表）
    def table_list_strict(self,table):
        param = {
                "QueryType":"table_list_strict",
                    "Database":table
        }
        databases = self.metadata.get_hbase(param)
        return databases["Content"]


    ##列出所有数据库
    # def query(self, database, table, Criteria):
    #     param = {
    #         "QueryType": "select",
    #         "Database": database,
    #         "Datatable": table,
    #         "Criteria": Criteria
    #     }
    #     print(param)
    #     databases = self.metadata.get_hbase(param)
    #     return databases["Content"]

    def select(self, database, table, Criteria):
        param = {
            "QueryType": "select",
            "Database": database,
            "Datatable": table,
            "Criteria": Criteria
        }
        databases = self.metadata.get_hbase(param)
        return databases["Content"]

    def table_key(self, database, table):
        param = {
            "QueryType": "table_detail",
            "Database": database,
            "Datatable": table
        }
        databases = self.metadata.get_hbase(param)
        key = databases["Content"]["Key"]
        return key



    def select_count(self, database, table, Criteria):
        param = {
            "QueryType": "select_count",
            "Database": database,
            "Datatable": table,
            "Criteria": Criteria
        }
        databases = self.metadata.get_hbase(param)
        return databases["Count"]

    def query_data(self, sql):
        databases = self.metadata.get_hbase(sql)
        return databases["Content"]

    def insert(self,database,table,InsertObject):
        param = {
            "QueryType": "batch_insert",
            "Database": database,
            "Datatable": table,
            "InsertObjects": InsertObject
        }
        databases = self.metadata.get_hbase(param)
        return databases["Count"]

    def remove(self,database,table,key):
        param = {
            "QueryType": "remove",
            "Database": database,
            "Datatable": table,
            "Key": key
        }
        databases = self.metadata.get_hbase(param)
        return databases["Count"]

    def delete(self,database,table,Criteria):
        param = {
            "QueryType": "delete",
            "Database": database,
            "Datatable": table,
            "Criteria": Criteria
        }
        databases = self.metadata.get_hbase(param)
        return databases["Count"]


    def put(self,database,table,InsertObject):
        param = {
            "QueryType": "batch_put",
            "Database": database,
            "Datatable": table,
            "InsertObjects": InsertObject
        }
        databases = self.metadata.get_hbase(param)
        return databases["Result"]
