|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright: (c) 2023, Tafsir Thiam (@ttafsir) <ttafsir@gmail.com> |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | +from __future__ import absolute_import, division, print_function |
| 6 | + |
| 7 | +__metaclass__ = type |
| 8 | + |
| 9 | + |
| 10 | +import traceback |
| 11 | +from ansible.module_utils.basic import missing_required_lib |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +IMPORT_ERROR = None |
| 16 | +try: |
| 17 | + from sqlite_utils import Database |
| 18 | + from sqlite_utils.cli import insert_upsert_implementation |
| 19 | + |
| 20 | + HAS_SQLITE_UTILS = True |
| 21 | +except ImportError: |
| 22 | + HAS_SQLITE_UTILS = False |
| 23 | + IMPORT_ERROR = traceback.format_exc() |
| 24 | + |
| 25 | + |
| 26 | +def insert_arg_spec(): |
| 27 | + return dict( |
| 28 | + db_path=dict(type="str", required=True), |
| 29 | + table=dict(type="str", required=True), |
| 30 | + records=dict(type="raw", required=True), |
| 31 | + pk=dict(type="str", default=None), |
| 32 | + column_order=dict(type="list", elements="raw", default=None), |
| 33 | + not_null=dict(type="list", elements="raw", default=None), |
| 34 | + defaults=dict(type="dict", default=None), |
| 35 | + hash_id=dict(type="str", default=None), |
| 36 | + alter=dict(type="bool", default=False), |
| 37 | + ignore=dict(type="bool", default=False), |
| 38 | + replace=dict(type="bool", default=False), |
| 39 | + extracts=dict(type="raw", default=None), |
| 40 | + conversions=dict(type="dict", default=None), |
| 41 | + columns=dict(type="dict", default=None), |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def file_insert_arg_spec(): |
| 46 | + return dict( |
| 47 | + db_path=dict(type="str", required=True), |
| 48 | + table=dict(type="str", required=True), |
| 49 | + file_path=dict(type="str", required=True), |
| 50 | + pk=dict(type="str", default=None), |
| 51 | + empty_null=dict(type="bool", default=False), |
| 52 | + lines=dict(type="bool", default=False), |
| 53 | + text=dict(type="bool", default=False), |
| 54 | + convert=dict(type="dict", default=None), |
| 55 | + imports=dict(type="list", elements="raw", default=None), |
| 56 | + batch_size=dict(type="int", default=100), |
| 57 | + stop_after=dict(type="int", default=None), |
| 58 | + alter=dict(type="bool", default=False), |
| 59 | + upsert=dict(type="bool", default=False), |
| 60 | + ignore=dict(type="bool", default=False), |
| 61 | + replace=dict(type="bool", default=False), |
| 62 | + truncate=dict(type="bool", default=False), |
| 63 | + not_null=dict(type="list", elements="raw", default=None), |
| 64 | + analyze=dict(type="bool", default=False), |
| 65 | + silent=dict(type="bool", default=False), |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +class SqliteUtilsModule: |
| 70 | + def __init__(self, module): |
| 71 | + self.module = module |
| 72 | + self.result = dict(changed=False, message="") |
| 73 | + self.params = module.params |
| 74 | + |
| 75 | + if not HAS_SQLITE_UTILS: |
| 76 | + module.fail_json( |
| 77 | + msg=missing_required_lib("sqlite-utils"), exception=IMPORT_ERROR |
| 78 | + ) |
| 79 | + |
| 80 | + def exit_json(self, **kwargs): |
| 81 | + self.result.update(**kwargs) |
| 82 | + self.module.exit_json(**self.result) |
| 83 | + |
| 84 | + def fail_json(self, msg, **kwargs): |
| 85 | + self.result.update(**kwargs) |
| 86 | + self.module.fail_json(msg=msg, **self.result) |
| 87 | + |
| 88 | + def get_db(self, **kwargs): |
| 89 | + return Database(self.params["db_path"], **kwargs) |
| 90 | + |
| 91 | + |
| 92 | +class SqliteUtilsInsertModule(SqliteUtilsModule): |
| 93 | + def __init__(self, module): |
| 94 | + super(SqliteUtilsInsertModule, self).__init__(module) |
| 95 | + |
| 96 | + file_path = module.params["file_path"] |
| 97 | + if not Path(file_path).exists(): |
| 98 | + module.fail_json( |
| 99 | + msg=f"File path {file_path} does not exist or is not accessible." |
| 100 | + ) |
| 101 | + self.file = Path(file_path) |
| 102 | + |
| 103 | + def insert_upsert(self, *args, **kwargs): |
| 104 | + return insert_upsert_implementation(*args, **kwargs) |
0 commit comments