1
I am refactoring a code, where I process some data and send it to a database. I have two versions. One where I use Mongodb and the other where I use Mysql. But I’d like to put the two versions together (there’s a lot of duplicate code) and I was wondering how I could do that. Below is a piece of code where I create an Update class that I inherit either from Bancodedadosmongodb or Bancodedadossql. My idea was to put this scheme in all classes that connect to the database.
Is there a better way to do this? What do you think ? Would it be better to make two separate versions? One pro Mongodb and one pro Mysql ?
class Update(BancoDeDadosMongoDB):
def __init__():
self.db = super().__init__()
def check_update_date():
#Checa se o banco de dados escolhido
if issuclass(Update,BancoDeDadosMongoDB):
#Faz a busca na coleção atualização
result = self.db.atualizacaofind(
{'ultima_atualizacao':{'$regex': '\d\d/\d\d/\d\d\d\d'}},
{'_id':0}
)
return result
if issubclass(Update,BancoDeDadosSQL):
#Preciso ajeitar essa busca para buscar pela data de atualização.
#Primeiro preciso ver como ficará organizado a tabela no BD relacional
empresa =self.session.query(Config).filter(Config.ultima_atualizacao.like('\d\d/\d\d/\d\d\d\d')):
return result
Hi - I would answer basically the same thing - I took the liberty of editing your pseudo-code to get "pseudo code in Python". (The biggest difference is that Python does not have native "interfaces" - the most common substitute is multiple inheritance even though in this example it is not even necessary)
– jsbueno
What can be added is that in Python, being very quiet the introspection is easy to make the itnerfaces for SQL and Mongodb be "generic" and introduce the fields (and table name/Collection) in the parent class (and then to avoid a very magical layer the best is that the repository methods explicitly receive the parent object)
– jsbueno
Its edition was super welcome, I lacked Skills in Python to build an example in it :D.
– Leandro Souza
Thanks for the reply, @jsbueno !!! Helped yes , and a lot!
– brunocpradom