from fw import rds

class Channel:
    def __init__(self,record):
        self.id=record['id']
        self.c_id=record['c_id']
        self.c_name=record['c_name']
        self.c_type=record['c_type']
        self.c_category_id=record['c_category_id']
        self.categorized=record['categorized']
        self.except_flg=record['except_flg']
        self.slowmode_flg=record['slowmode_flg']
        self.status=record['status']
        return

class Channels:
    def __init__(self):
        return

    def get(self, c_id):
        sql="SELECT * FROM channels WHERE c_id=%s and status=0;"
        params=(str(c_id),)
        records=rds.executeSQL(sql,params)
        
        if len(records)==0:
            return None
        
        dataset=[]
        for record in records:
            data=Channel(record)
            dataset.append(data)
        
        return dataset
        
    def first(self,c_id=-1, channel_id=-1):
        sql="SELECT * FROM channels WHERE c_id=%s and status=0;"
        params=(str(c_id),)
        if channel_id != -1:
            sql="SELECT * FROM channels WHERE id=%s and status=0;"
            params=(str(channel_id),)
        records=rds.executeSQL(sql,params)
        
        if len(records)==0:
            return None
        
        record=records[0]
        data=Channel(record)

        return data
        
    def new(self,c_id,c_category_id,c_name,c_type):
        sql="INSERT INTO channels (c_id,c_category_id,c_name,c_type) VALUES(%s,%s,%s,%s);"
        params=(str(c_id),str(c_category_id),str(c_name),str(c_type))
        result=rds.executeSQL(sql,params,True)
        
        return result

    def set(self,id,c_category_id,c_name):
        sql="UPDATE channels SET c_category_id=%s,c_name=%s where id=%s;"
        params=(str(c_category_id),str(c_name),str(id))
        result=rds.executeSQL(sql,params,True)
        
        return result
