Created
March 23, 2016 02:46
-
-
Save jinto/329468c2161741ecb6d3 to your computer and use it in GitHub Desktop.
Refinement from decorator to plain old fashioned functions Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# problem is code duplication. | |
from django.db import connections | |
class Dao: | |
@classmethod | |
def get_list(cls): | |
try: | |
conn = connections["db_name"] | |
cursor = conn.cursor() | |
result = cursor.callproc("our_internal_stored_procedure") | |
result_set = next(cursor.stored_results()) | |
field_names = result_set.column_names | |
rows = result_set.fetchall() | |
finally: | |
cursor.close() | |
conn.close() | |
return rows, field_names | |
@classmethod | |
def get(cls, some_arg): | |
if not some_arg: | |
raise ValueError("Argument not exists") | |
args = [some_arg] | |
ret_info = None | |
try: | |
conn = connections["db_name"] | |
cursor = conn.cursor() | |
result = cursor.callproc("other_stored_procedure", args) | |
result_set = next(cursor.stored_results()) | |
if result_set.rowcount > 0: | |
row = result_set.fetchall() | |
.. | |
finally: | |
cursor.close() | |
conn.close() | |
return ret_info | |
# django view | |
class SomeView(View): | |
def get(self, request, **kwargs): | |
response_code = 200 | |
d = dict() | |
arg = ... | |
ret_info = Dao.get(arg) | |
return JsonResponse(ret_info) | |
def get_list(self, request, **kwargs): | |
response_code = 200 | |
d = dict() | |
arg = ... | |
ret_info = Dao.get_list(arg) | |
return JsonResponse(ret_info) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# argument "cursor" is automatically transfered, but it's little bit weird. | |
import functools | |
import inspect | |
from django.db import connections | |
def data_accessor_method(function): | |
def wrapper(*args, **kwargs): | |
conn = None | |
cursor = None | |
try: | |
conn = connections["db_name"] | |
cursor = conn.cursor() | |
res = function(cursor, *args, **kwargs) | |
finally: | |
if cursor: | |
cursor.close() | |
if conn: | |
conn.close() | |
return res | |
return functools.update_wrapper(wrapper, function) | |
class Account: | |
@data_accessor_method | |
def get(cls, cursor, arg): | |
if not user_id: | |
raise ValueError("Argument not exists") | |
cursor.callproc("some_sp", [arg]) | |
result_set = next(cursor.stored_results()) | |
field_names = result_set.column_names | |
row = result_set.fetchall() | |
return row, field_names | |
@data_accessor_method | |
def get_list(cls): | |
result = cursor.callproc("other_sp", args) | |
result_set = next(cursor.stored_results()) | |
field_names = result_set.column_names | |
total_row_count = result[-1] | |
rows = result_set.fetchall() | |
return rows, field_names, total_row_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# "with ClassName() as instance" is much more simple. | |
# but caller side code is not clean. | |
class Dao: | |
def __init__(self): | |
self.conn = connections["db_name"] | |
self.cursor = self.conn.cursor() | |
def __enter__(self): | |
return self | |
def __exit__(self, exc_type, exc_value, traceback): | |
if self.cursor: | |
self.cursor.close() | |
if self.conn: | |
self.conn.close() | |
class SomeView(View): | |
def show(self, request, **kwargs): | |
arg = kwargs.get("arg_name") | |
if not arg: | |
raise ValueError("Argument not exists") | |
with Dao() as dao: | |
result, fields = dao.get(arg) | |
return JsonResponse(.. something...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# old fashioned plain top level functions. | |
from django.db import connections | |
def fetch_record(sp, args): | |
conn = None | |
cursor = None | |
try: | |
conn = connections["db_name"] | |
cursor = conn.cursor() | |
cursor.callproc(sp, args) | |
result_set = next(cursor.stored_results()) | |
field_names = result_set.column_names | |
row = result_set.fetchall() | |
return row, field_names | |
finally: | |
if cursor: | |
cursor.close() | |
if conn: | |
conn.close() | |
def fetch_code(sp, args): | |
conn = None | |
cursor = None | |
try: | |
conn = connections["db_name"] | |
cursor = conn.cursor() | |
result = cursor.callproc(sp, args) | |
error_code = result[-1] | |
return error_code | |
finally: | |
if cursor: | |
cursor.close() | |
if conn: | |
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# much more clean codes. (i think) | |
from last_data_module import fetch_record, fetch_code, fetch_list | |
class Dao: | |
@staticmethod | |
def get(self, arg): | |
return self.fetch_record("some_sp", [arg]) | |
@staticmethod | |
def get_list(self, page): | |
return self.fetch_list("other_sp", (page, 0)) | |
@staticmethod | |
def close(self, arg): | |
return self.fetch_code("other_sp2", (arg, 0)) | |
class SomeView(View): | |
def show(self, request, **kwargs): | |
arg = ... | |
result, fields = Dao.get(arg) | |
return JsonResponse(...) | |
def list(self, request, **kwargs): | |
arg = ... | |
result, fields, total = Dao.get_list(arg) | |
return JsonResponse(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment