mardi 22 juin 2021

Is it a better pattern to derive a class or just add to a method to existing class?

This question is specifically related to python but I guess it could be valid for any OO language. I have defined a Query class that imports the pyodbc package and groups in a single class some of its functionality, allowing to simplify the execution of queries against a database (for example, I have a run() method that executes the query and retrieves the resulting data).
pyodbc just allows positional parameter binding (binding each '?' in the source query with the corresponding element of a list of parameters) but I'd also like to have named parameter binding (binding each '@variable' in the source query with the corresponding value for a dict element having @variable as key). So, I need to transform a named parameter query into a positional parameter query and do some rework in the input parameterse. My question is related to the most correct way to achieve this. Is it better to:

  1. Define a new method of the class Query, say run_parsed(), that expects a query with the named parameter notation (@variable), and call that method when you need to run a query that way, or
  2. Define a derived class from Query (say Query_named), and use that class just when you want to run a named parameter query, and the base Query class when you want to run a positional parameter query?

Thanks for your insights

Aucun commentaire:

Enregistrer un commentaire