Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Database Factory module 

3""" 

4import importlib 

5import logging 

6 

7logging.basicConfig(format='%(levelname)s: %(asctime)s: ' '%(funcName)s: %(message)s') 

8logger = logging.getLogger(__name__) 

9logger.setLevel(logging.INFO) 

10 

11database_class = lambda x: importlib.import_module(name=available_databases[x]['lib_path']) 

12available_databases = { 

13 'mysql': { 

14 'endpoint': 'MySqlDatabase', 

15 'lib_path': 'sdc_etl_libs.database_helpers.MySqlDatabase' 

16 }, 

17 'nexus': { 

18 'endpoint': 'NexusDatabase', 

19 'lib_path': 'sdc_etl_libs.database_helpers.NexusDatabase' 

20 }, 

21 'odbc': { 

22 'endpoint': 'ODBCDatabase', 

23 'lib_path': 'sdc_etl_libs.database_helpers.ODBCDatabase' 

24 }, 

25 'odbc_netsuite': { 

26 'endpoint': 'ODBCNetsuite', 

27 'lib_path': 'sdc_etl_libs.database_helpers.ODBCNetsuite' 

28 }, 

29 'postgres': { 

30 'endpoint': 'PostgresSqlDatabase', 

31 'lib_path': 'sdc_etl_libs.database_helpers.PostgresSqlDatabase' 

32 }, 

33 'snowflake': { 

34 'endpoint': 'SnowflakeDatabase', 

35 'lib_path': 'sdc_etl_libs.database_helpers.SnowflakeDatabase' 

36 }, 

37 'dynamodb': { 

38 'endpoint': 'DynamoDB', 

39 'lib_path': 'sdc_etl_libs.database_helpers.DynamoDB' 

40 } 

41} 

42 

43 

44class DatabaseFactory(object): 

45 """ 

46 DatabaseFactory class 

47 """ 

48 

49 @staticmethod 

50 def get_database(database_name_: str, schema_: dict = None, endpoint_schema_: dict = None, **kwargs): 

51 """ 

52 Factory for returning the proper Database class. 

53 :param database_name_: database name. 

54 :param schema_: JSON of data schema. 

55 :param endpoint_schema_: JSON of endpoint schema. 

56 :param kwargs: additional custom parameters 

57 :return: SDCDatabase class. 

58 :rtype: SDCDatabase class 

59 :raises Exception: 

60 """ 

61 kva_kwargs = {} 

62 if schema_ is not None: 

63 kva_kwargs['schema_'] = schema_ 

64 if endpoint_schema_ is not None: 

65 kva_kwargs['endpoint_schema_'] = endpoint_schema_ 

66 if kwargs is not None: 

67 kva_kwargs = {**kva_kwargs, **kwargs} 

68 

69 if database_name_ in available_databases.keys(): 

70 database = getattr(database_class(database_name_), 

71 available_databases[database_name_]['endpoint'])(**kva_kwargs) 

72 

73 return database 

74 else: 

75 raise Exception(f"{database_name_} is an invalid database option.")