コード例 #1
0
 def getFirst(self):
     if (self.__convertMethod is None
         ):  #Se não houver forma de conversão, retorne um dicionário
         return create_dictionary(
             self.__atributes,
             execute(self.__commandToDB + self.__returning))
     return self.__convertMethod(
         create_dictionary(
             self.__atributes,
             execute(self.__commandToDB + self.__returning)))
コード例 #2
0
def create_types():  #<- Adicionar tratamento de exceção depois ****
    commands = ''
    databaseFile = open(
        os.path.dirname(os.path.abspath(__file__)) + "\\scripts\\types.sql",
        "r")
    for line in databaseFile:
        commands += line
    databaseFile.close()
    commands = (
        f"""
        {commands}
        """

        #is_active BOOLEAN DEFAULT TRUE <- Desnecessário
    )
    execute(commands)
コード例 #3
0
    def _save(self,
              atributeEValue: dict,
              toInsert=True,
              convertMethod=False,
              toJoinOnUpdate: str = None
              ):  #Tratar atributos inexistentes depois ********
        self._aspas = "******"
        commandToDB = ''
        atributesLength = len(atributeEValue)
        count = 0
        atributesToSave = ''  #atributo, atributo
        tablesNamesToSave = ''  #'valor', 'valor'
        AtributesEtablesNamesToUpdate = ''  #atributo = 'valor'
        for atribute, value in atributeEValue.items():
            value = str(value)
            count += 1
            value = f"'{value}'" if (
                value is not None
            ) else "NULL"  #<-- Necessário pois o banco não interpreta None como Null
            if (count == atributesLength):
                atributesToSave += atribute
                tablesNamesToSave += value
                AtributesEtablesNamesToUpdate += f"{atribute} = {value}"
            else:
                atributesToSave += atribute + ','
                tablesNamesToSave += f"{value},"
                AtributesEtablesNamesToUpdate += f"{atribute} = {value},"
        toReturn = self._lstrListToStr(list(self.__typesAcceptables.keys()),
                                       ",")
        #Se for pra inserir
        if (toInsert == True):  #Se o objeto ainda não está no banco
            commandToDB = f"INSERT INTO {self.__tableName}({atributesToSave}) VALUES ({tablesNamesToSave}) RETURNING {toReturn}"
            if (not convertMethod):
                return create_dictionary(list(self.__typesAcceptables.keys()),
                                         execute(commandToDB))
            return self._convertToSchema(
                create_dictionary(list(self.__typesAcceptables.keys()),
                                  execute(commandToDB)))

        #Se for pra atualizar:
        if ("updated_on" in self.__typesAcceptables):
            AtributesEtablesNamesToUpdate += ',updated_on = NOW()'

        if (toJoinOnUpdate is not None):
            self._aspas = ""
            return self._FROM(
                WHERE=self._WHERE,
                commandToDB=
                f"UPDATE {toJoinOnUpdate} SET {AtributesEtablesNamesToUpdate}",
                convertCommand=self._convertCommand,
                storageCommand=self.__storageCommand,
                atributes=list(self.__typesAcceptables.keys()),
                returning=f" RETURNING {toReturn}",  #O espaço antes é importante
                convertMethod=self._convertToSchema if
                (convertMethod == True) else None,
                executeCommand=self._executeCommand,
                lstrListToStr=self._lstrListToStr)

        return self._WHERE(
            commandToDB=
            f"UPDATE {self.__tableName} SET {AtributesEtablesNamesToUpdate}",
            convertCommand=self._convertCommand,
            storageCommand=self.__storageCommand,
            atributes=list(self.__typesAcceptables.keys()),
            returning=f" RETURNING {toReturn}",  #O espaço antes é importante
            convertMethod=self._convertToSchema if
            (convertMethod == True) else None,
            executeCommand=self._executeCommand)