|
| 1 | +import json |
| 2 | +import mgp |
| 3 | +import mysql.connector as mysql_connector |
| 4 | +import oracledb |
| 5 | +import pyodbc |
| 6 | +import threading |
| 7 | + |
| 8 | +from typing import Any, Dict |
| 9 | + |
| 10 | + |
| 11 | +class Constants: |
| 12 | + I_COLUMN_NAME = 0 |
| 13 | + CURSOR = "cursor" |
| 14 | + COLUMN_NAMES = "column_names" |
| 15 | + CONNECTION = "connection" |
| 16 | + BATCH_SIZE = 1000 |
| 17 | + |
| 18 | + |
| 19 | +##### MYSQL |
| 20 | + |
| 21 | +mysql_dict = {} |
| 22 | + |
| 23 | + |
| 24 | +def init_migrate_mysql( |
| 25 | + table_or_sql: str, |
| 26 | + config: mgp.Map, |
| 27 | + config_path: str = "", |
| 28 | + params: mgp.Nullable[mgp.Any] = None, |
| 29 | +): |
| 30 | + global mysql_dict |
| 31 | + |
| 32 | + if params: |
| 33 | + _check_params_type(params) |
| 34 | + if len(config_path) > 0: |
| 35 | + config = _combine_config(config=config, config_path=config_path) |
| 36 | + |
| 37 | + if _query_is_table(table_or_sql): |
| 38 | + table_or_sql = f"SELECT * FROM {table_or_sql};" |
| 39 | + |
| 40 | + if threading.get_native_id not in mysql_dict: |
| 41 | + mysql_dict[threading.get_native_id] = {} |
| 42 | + |
| 43 | + if Constants.CURSOR not in mysql_dict[threading.get_native_id]: |
| 44 | + mysql_dict[threading.get_native_id][Constants.CURSOR] = None |
| 45 | + |
| 46 | + if mysql_dict[threading.get_native_id][Constants.CURSOR] is None: |
| 47 | + connection = mysql_connector.connect(**config) |
| 48 | + cursor = connection.cursor() |
| 49 | + cursor.execute(table_or_sql, params=params) |
| 50 | + |
| 51 | + mysql_dict[threading.get_native_id][Constants.CONNECTION] = connection |
| 52 | + mysql_dict[threading.get_native_id][Constants.CURSOR] = cursor |
| 53 | + mysql_dict[threading.get_native_id][Constants.COLUMN_NAMES] = [ |
| 54 | + column[Constants.I_COLUMN_NAME] for column in cursor.description |
| 55 | + ] |
| 56 | + |
| 57 | + |
| 58 | +def mysql( |
| 59 | + table_or_sql: str, |
| 60 | + config: mgp.Map, |
| 61 | + config_path: str = "", |
| 62 | + params: mgp.Nullable[mgp.Any] = None, |
| 63 | +) -> mgp.Record(row=mgp.Map): |
| 64 | + """ |
| 65 | + With migrate.mysql you can access MySQL and execute queries. The result table is converted into a stream, |
| 66 | + and returned rows can be used to create or create graph structures. Config must be at least empty map. |
| 67 | + If config_path is passed, every key,value pair from JSON file will overwrite any values in config file. |
| 68 | +
|
| 69 | + :param table_or_sql: Table name or an SQL query |
| 70 | + :param config: Connection configuration parameters (as in mysql.connector.connect), |
| 71 | + :param config_path: Path to the JSON file containing configuration parameters (as in mysql.connector.connect) |
| 72 | + :param params: Optionally, queries may be parameterized. In that case, `params` provides parameter values |
| 73 | + :return: The result table as a stream of rows |
| 74 | + """ |
| 75 | + global mysql_dict |
| 76 | + cursor = mysql_dict[threading.get_native_id][Constants.CURSOR] |
| 77 | + column_names = mysql_dict[threading.get_native_id][Constants.COLUMN_NAMES] |
| 78 | + |
| 79 | + rows = cursor.fetchmany(Constants.BATCH_SIZE) |
| 80 | + |
| 81 | + return [mgp.Record(row=_name_row_cells(row, column_names)) for row in rows] |
| 82 | + |
| 83 | + |
| 84 | +def cleanup_migrate_mysql(): |
| 85 | + global mysql_dict |
| 86 | + mysql_dict[threading.get_native_id][Constants.CURSOR] = None |
| 87 | + mysql_dict[threading.get_native_id][Constants.CONNECTION].close() |
| 88 | + mysql_dict[threading.get_native_id][Constants.CONNECTION].commit() |
| 89 | + mysql_dict[threading.get_native_id][Constants.CONNECTION] = None |
| 90 | + mysql_dict[threading.get_native_id][Constants.COLUMN_NAMES] = None |
| 91 | + |
| 92 | + |
| 93 | +mgp.add_batch_read_proc(mysql, init_migrate_mysql, cleanup_migrate_mysql) |
| 94 | + |
| 95 | +### SQL SERVER |
| 96 | + |
| 97 | +sql_server_dict = {} |
| 98 | + |
| 99 | + |
| 100 | +def init_migrate_sql_server( |
| 101 | + table_or_sql: str, |
| 102 | + config: mgp.Map, |
| 103 | + config_path: str = "", |
| 104 | + params: mgp.Nullable[mgp.Any] = None, |
| 105 | +): |
| 106 | + global sql_server_dict |
| 107 | + |
| 108 | + if params: |
| 109 | + _check_params_type(params, (list, tuple)) |
| 110 | + else: |
| 111 | + params = [] |
| 112 | + |
| 113 | + if len(config_path) > 0: |
| 114 | + config = _combine_config(config=config, config_path=config_path) |
| 115 | + |
| 116 | + if _query_is_table(table_or_sql): |
| 117 | + table_or_sql = f"SELECT * FROM {table_or_sql};" |
| 118 | + |
| 119 | + if threading.get_native_id not in sql_server_dict: |
| 120 | + sql_server_dict[threading.get_native_id] = {} |
| 121 | + |
| 122 | + if Constants.CURSOR not in sql_server_dict[threading.get_native_id]: |
| 123 | + sql_server_dict[threading.get_native_id][Constants.CURSOR] = None |
| 124 | + |
| 125 | + if sql_server_dict[threading.get_native_id][Constants.CURSOR] is None: |
| 126 | + connection = pyodbc.connect(**config) |
| 127 | + cursor = connection.cursor() |
| 128 | + cursor.execute(table_or_sql, *params) |
| 129 | + |
| 130 | + sql_server_dict[threading.get_native_id][Constants.CONNECTION] = connection |
| 131 | + sql_server_dict[threading.get_native_id][Constants.CURSOR] = cursor |
| 132 | + sql_server_dict[threading.get_native_id][Constants.COLUMN_NAMES] = [ |
| 133 | + column[Constants.I_COLUMN_NAME] for column in cursor.description |
| 134 | + ] |
| 135 | + |
| 136 | + |
| 137 | +def sql_server( |
| 138 | + table_or_sql: str, |
| 139 | + config: mgp.Map, |
| 140 | + config_path: str = "", |
| 141 | + params: mgp.Nullable[mgp.Any] = None, |
| 142 | +) -> mgp.Record(row=mgp.Map): |
| 143 | + """ |
| 144 | + With migrate.sql_server you can access SQL Server and execute queries. The result table is converted into a stream, |
| 145 | + and returned rows can be used to create or create graph structures. Config must be at least empty map. |
| 146 | + If config_path is passed, every key,value pair from JSON file will overwrite any values in config file. |
| 147 | +
|
| 148 | + :param table_or_sql: Table name or an SQL query |
| 149 | + :param config: Connection configuration parameters (as in pyodbc.connect), |
| 150 | + :param config_path: Path to the JSON file containing configuration parameters (as in pyodbc.connect) |
| 151 | + :param params: Optionally, queries may be parameterized. In that case, `params` provides parameter values |
| 152 | + :return: The result table as a stream of rows |
| 153 | + """ |
| 154 | + global sql_server_dict |
| 155 | + |
| 156 | + cursor = sql_server_dict[threading.get_native_id][Constants.CURSOR] |
| 157 | + column_names = sql_server_dict[threading.get_native_id][Constants.COLUMN_NAMES] |
| 158 | + rows = cursor.fetchmany(Constants.BATCH_SIZE) |
| 159 | + |
| 160 | + return [mgp.Record(row=_name_row_cells(row, column_names)) for row in rows] |
| 161 | + |
| 162 | + |
| 163 | +def cleanup_migrate_sql_server(): |
| 164 | + global sql_server_dict |
| 165 | + sql_server_dict[threading.get_native_id][Constants.CURSOR] = None |
| 166 | + sql_server_dict[threading.get_native_id][Constants.CONNECTION].close() |
| 167 | + sql_server_dict[threading.get_native_id][Constants.CONNECTION].commit() |
| 168 | + sql_server_dict[threading.get_native_id][Constants.CONNECTION] = None |
| 169 | + sql_server_dict[threading.get_native_id][Constants.COLUMN_NAMES] = None |
| 170 | + |
| 171 | + |
| 172 | +mgp.add_batch_read_proc(sql_server, init_migrate_sql_server, cleanup_migrate_sql_server) |
| 173 | + |
| 174 | +### Oracle DB |
| 175 | + |
| 176 | +oracle_db_dict = {} |
| 177 | + |
| 178 | + |
| 179 | +def init_migrate_oracle_db( |
| 180 | + table_or_sql: str, |
| 181 | + config: mgp.Map, |
| 182 | + config_path: str = "", |
| 183 | + params: mgp.Nullable[mgp.Any] = None, |
| 184 | +): |
| 185 | + global oracle_db_dict |
| 186 | + |
| 187 | + if params: |
| 188 | + _check_params_type(params) |
| 189 | + |
| 190 | + if len(config_path) > 0: |
| 191 | + config = _combine_config(config=config, config_path=config_path) |
| 192 | + |
| 193 | + if _query_is_table(table_or_sql): |
| 194 | + table_or_sql = f"SELECT * FROM {table_or_sql}" |
| 195 | + |
| 196 | + if not config: |
| 197 | + config = {} |
| 198 | + |
| 199 | + # To prevent query execution from hanging |
| 200 | + if "disable_oob" not in config: |
| 201 | + config["disable_oob"] = True |
| 202 | + else: |
| 203 | + config["disable_oob"] = True # overwrite |
| 204 | + |
| 205 | + if threading.get_native_id not in oracle_db_dict: |
| 206 | + oracle_db_dict[threading.get_native_id] = {} |
| 207 | + |
| 208 | + if Constants.CURSOR not in oracle_db_dict[threading.get_native_id]: |
| 209 | + oracle_db_dict[threading.get_native_id][Constants.CURSOR] = None |
| 210 | + |
| 211 | + if oracle_db_dict[threading.get_native_id][Constants.CURSOR] is None: |
| 212 | + connection = oracledb.connect(**config) |
| 213 | + cursor = connection.cursor() |
| 214 | + |
| 215 | + if not params: |
| 216 | + cursor.execute(table_or_sql) |
| 217 | + elif isinstance(params, (list, tuple)): |
| 218 | + cursor.execute(table_or_sql, params) |
| 219 | + else: |
| 220 | + cursor.execute(table_or_sql, **params) |
| 221 | + |
| 222 | + oracle_db_dict[threading.get_native_id][Constants.CONNECTION] = connection |
| 223 | + oracle_db_dict[threading.get_native_id][Constants.CURSOR] = cursor |
| 224 | + oracle_db_dict[threading.get_native_id][Constants.COLUMN_NAMES] = [ |
| 225 | + column[Constants.I_COLUMN_NAME] for column in cursor.description |
| 226 | + ] |
| 227 | + |
| 228 | + |
| 229 | +def oracle_db( |
| 230 | + table_or_sql: str, |
| 231 | + config: mgp.Map, |
| 232 | + config_path: str = "", |
| 233 | + params: mgp.Nullable[mgp.Any] = None, |
| 234 | +) -> mgp.Record(row=mgp.Map): |
| 235 | + """ |
| 236 | + With migrate.oracle_db you can access Oracle DB and execute queries. The result table is converted into a stream, |
| 237 | + and returned rows can be used to create or create graph structures. Config must be at least empty map. |
| 238 | + If config_path is passed, every key,value pair from JSON file will overwrite any values in config file. |
| 239 | +
|
| 240 | + :param table_or_sql: Table name or an SQL query |
| 241 | + :param config: Connection configuration parameters (as in oracledb.connect), |
| 242 | + :param config_path: Path to the JSON file containing configuration parameters (as in oracledb.connect) |
| 243 | + :param params: Optionally, queries may be parameterized. In that case, `params` provides parameter values |
| 244 | + :return: The result table as a stream of rows |
| 245 | + """ |
| 246 | + |
| 247 | + global oracle_db_dict |
| 248 | + cursor = oracle_db_dict[threading.get_native_id][Constants.CURSOR] |
| 249 | + column_names = oracle_db_dict[threading.get_native_id][Constants.COLUMN_NAMES] |
| 250 | + rows = cursor.fetchmany(Constants.BATCH_SIZE) |
| 251 | + |
| 252 | + return [mgp.Record(row=_name_row_cells(row, column_names)) for row in rows] |
| 253 | + |
| 254 | + |
| 255 | +def cleanup_migrate_oracle_db(): |
| 256 | + global oracle_db_dict |
| 257 | + oracle_db_dict[threading.get_native_id][Constants.CURSOR] = None |
| 258 | + oracle_db_dict[threading.get_native_id][Constants.CONNECTION].close() |
| 259 | + oracle_db_dict[threading.get_native_id][Constants.CONNECTION].commit() |
| 260 | + oracle_db_dict[threading.get_native_id][Constants.CONNECTION] = None |
| 261 | + oracle_db_dict[threading.get_native_id][Constants.COLUMN_NAMES] = None |
| 262 | + |
| 263 | + |
| 264 | +mgp.add_batch_read_proc(oracle_db, init_migrate_oracle_db, cleanup_migrate_oracle_db) |
| 265 | + |
| 266 | + |
| 267 | +def _query_is_table(table_or_sql: str) -> bool: |
| 268 | + return len(table_or_sql.split()) == 1 |
| 269 | + |
| 270 | + |
| 271 | +def _load_config(path: str) -> Dict[str, Any]: |
| 272 | + try: |
| 273 | + with open(path, mode="r") as config: |
| 274 | + return json.load(config) |
| 275 | + except Exception: |
| 276 | + raise OSError("Could not open/read file.") |
| 277 | + |
| 278 | + |
| 279 | +def _combine_config(config: mgp.Map, config_path: str) -> Dict[str, Any]: |
| 280 | + assert len(config_path), "Path must not be empty" |
| 281 | + config_items = _load_config(path=config_path) |
| 282 | + |
| 283 | + for key, value in config_items.items(): |
| 284 | + config[key] = value |
| 285 | + return config |
| 286 | + |
| 287 | + |
| 288 | +def _name_row_cells(row_cells, column_names) -> Dict[str, Any]: |
| 289 | + return dict(map(lambda column, value: (column, value), column_names, row_cells)) |
| 290 | + |
| 291 | + |
| 292 | +def _check_params_type(params: Any, types=(dict, list, tuple)) -> None: |
| 293 | + if not isinstance(params, types): |
| 294 | + raise TypeError( |
| 295 | + "Database query parameter values must be passed in a container of type List[Any] (or Map, if migrating from MySQL or Oracle DB)" |
| 296 | + ) |
0 commit comments