Beispiel #1
0
class PDO(Tweak("Database")):
    
    def __init__(self):
        
        self.PROVIDER = Parameter()
        self.LOCATION = Parameter()
        self.USER = Parameter()
        self.PASSWD = Parameter()
        self.CONNECT_WITH = Parameter() 
        self.tweak()
        self.models = []
        self.builder = Builder()
        self.connection = None        
        self.preload()
        
    def connect(self):
        _args = []
        for arg in self.CONNECT_WITH:
            try:
                _args.append(getattr(self, arg))
            except AttributeError as err:
                raise err
            print(_args)
            self.connection = self.PROVIDER.connect(*_args)
        if self.connection:             
            print("connected")
            
    def regiserModel(self, model):
        model.PDO = self
        self.models.append(model)
        
    def preload(self):
        QueryCreate.builder = self.builder.create(Names.Create.definition)
        QueryInsert.builder = self.builder.create(Names.Insert.definition)
        QuerySelect.builder = self.builder.create(Names.Select.definition)
        QueryUpdate.builder = self.builder.create(Names.Update.definition)
        QueryDelete.builder = self.builder.create(Names.Delete.definition)
        Expression.builder = self.builder.create(Names.Expression.definition)
        
    def gosql(self, function):
        if self.connection:
            return self.connection.execute(function())
        else:
            raise Exception("No connection")
    
    def execute(self, query):
        if self.connection:
            return self.connection.execute(query)
        else:
            raise Exception("No connection")            
Beispiel #2
0
class PDO():
    
    def __init__(self, config=None):
        
        #поля конфигурационного файла
        self.PROVIDER = Parameter()
        self.LOCATION = Parameter()
        self.USER = Parameter()
        self.PASSWD = Parameter()
        self.CONNECT_WITH = Parameter() 
        self.DATA_TYPE = Parameter(Data)
        
        tweak(self, "Database", config)

        self.connection = None   

    def connect(self):
        _args = []
        for arg in self.CONNECT_WITH:
            try:
                _args.append(getattr(self, arg))
            except AttributeError as err:
                raise err
            self.connection = self.PROVIDER.connect(*_args)
        return self.connection           
               
    def gosql(self, function):
        if self.connection:
            cursor = self.connection.execute(function())
            return self.DATA_TYPE(cursor) if self.DATA_TYPE else cursor
        else:
            raise Exception("No connection")
    
    def execute(self, query):
        if self.connection:
            cursor = self.connection.execute(query)
            return self.DATA_TYPE(cursor) if self.DATA_TYPE else cursor
        else:
            raise Exception("No connection")