

class BaseModel():

    def __init__(self):
        raise NotImplementedError()

    def getPrimary_keys(self):
        raise NotImplementedError()

    def getProperties(self):
        return self.__dict__

    def setValue(self,k,v,isColumn=False):
        if "all_columns" not in self.__dict__:
            self.all_columns = []
        self.__dict__[k] = v
        if isColumn:
            if k not in (set(self.all_columns)):
                self.all_columns.append(k)

    def insert_row(self,conn):
        cursor = conn.cursor()
        sql = "insert into %s"%(self.table_name)
        s_columns = "("
        s_values = "values("
        _set_columns = set(self.all_columns)
        for k,v in self.__dict__.items():
            if k in _set_columns:
                if v is not None and str(v)!="":
                    s_columns += "%s,"%k
                    if isinstance(v,str):
                        s_values += "'%s',"%v
                    else:
                        s_values += "%d,"%v
        s_columns = "%s)"%s_columns[:-1]
        s_values = "%s)"%s_values[:-1]
        sql = "%s%s%s"%(sql,s_columns,s_values)
        cursor.execute(sql)
        conn.commit()


    def update_row(self,conn,conditions=[]):
        cursor = conn.cursor()
        sql = "update %s set "%(self.table_name)
        s_columns = ""
        s_where = " where "
        _set_columns = set(self.all_columns)
        _set_keys = set(self.getPrimary_keys())
        for k,v in self.__dict__.items():
            if k in _set_columns and k not in _set_keys:
                if v is not None and str(v)!="":
                    s_columns += "%s="%k
                    if isinstance(v,str):
                        s_columns += "'%s',"%v
                    else:
                        s_columns += "%d,"%v
            elif k in _set_keys:
                if v is None or str(v)=="":
                    raise RuntimeError("主键%s为空"%k)
                s_where += "%s="%k
                if isinstance(v,str):
                    s_where += "'%s' and "%v
                else:
                    s_where += "%d and "%v
        for k,v in conditions:
            s_where += "%s="%k
            if isinstance(v,str):
                s_where += "'%s' and "%v
            else:
                s_where += "%d and "%v
        s_columns = "%s"%s_columns[:-1]
        s_where = "%s"%s_where[:-1]
        sql = "%s%s%s"%(sql,s_columns,s_where)
        update_rows = cursor.execute(sql)
        conn.commit()
        return update_rows

    @staticmethod
    def exists(conn,table_name,conditions):
        s_where = ""
        for k,v in conditions:
            s_where += "%s="%k
            if isinstance(v,str):
                s_where += "'%s' and "%v
            else:
                s_where += "%d and "%v
        cursor = conn.cursor()
        sql = "select count(1) from %s %s"%(table_name,s_where)
        cursor.execute(sql)
        rows = cursor.fetchall()
        if rows[0][0]==0:
            return False
        return True

    @staticmethod
    def select_rows(conn,cls,table_name,conditions,limit=None):
        list_result = []
        s_limit = ""
        if limit is not None:
            s_limit = "limit %d"%limit
        s_where = ""
        for k,v in conditions:
            s_where += "%s="%k
            if isinstance(v,str):
                s_where += "'%s' and "%v
            else:
                s_where += "%d and "%v

        cursor = conn.cursor()
        sql = "select * from %s %s %s"%(table_name,s_where,s_limit)
        cursor.execute(sql)

        vol = cursor.description
        rows = cursor.fetchall()
        for row in rows:
            _dict = {}
            for _vol,_val in zip(vol,row):
                _name = _vol[0]
                _dict[_name] = _val
            list_result.append(cls(_dict))
        return list_result



