├── .circleci └── config.yml ├── .github └── workflows │ └── python-publish.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.md ├── confs ├── conf.yml └── conf_with_env_vars.yml ├── env_example ├── example.py ├── high_sql ├── __init__.py ├── abstract_highsql.py ├── high_config.py ├── high_mysql.py └── sql_schema.json ├── requirements.txt ├── settings.ini ├── setup.py └── tests ├── __init__.py ├── test_data └── test_high_sql │ └── conf.yml └── test_high_sql.py /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 # use CircleCI 2.0 2 | jobs: # A basic unit of work in a run 3 | build: # runs not using Workflows must have a `build` job as entry point 4 | # directory where steps are run 5 | working_directory: ~/high_sql 6 | docker: # run the steps with Docker 7 | # CircleCI Python images available at: https://hub.docker.com/r/circleci/python/ 8 | - image: circleci/python:3.8 9 | steps: # steps that comprise the `build` job 10 | - checkout # check out source code to working directory 11 | - run: make create_env env=venv 12 | - run: source venv/bin/activate 13 | - run: make dist 14 | - run: make tests 15 | # - run: make release 16 | -------------------------------------------------------------------------------- /.github/workflows/python-publish.yml: -------------------------------------------------------------------------------- 1 | name: Upload Python Package 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | deploy: 9 | runs-on: ubuntu-20.04 10 | 11 | steps: 12 | - uses: actions/checkout@v2 13 | - uses: actions/setup-python@v2 14 | - name: Install dependencies 15 | run: | 16 | python -m pip install --upgrade pip 17 | pip install setuptools wheel twine 18 | - name: Build and publish 19 | env: 20 | TWINE_USERNAME: __token__ 21 | TWINE_PASSWORD: ${{ secrets.GITHUBACTIONS }} 22 | run: | 23 | python setup.py sdist bdist_wheel 24 | twine upload dist/* -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | .gitattributes 3 | .last_checked 4 | .gitconfig 5 | *.bak 6 | *.log 7 | *~ 8 | ~* 9 | _tmp* 10 | tmp* 11 | tags 12 | 13 | # Byte-compiled / optimized / DLL files 14 | __pycache__/ 15 | *.py[cod] 16 | *$py.class 17 | 18 | # C extensions 19 | *.so 20 | 21 | # Distribution / packaging 22 | .Python 23 | env/ 24 | build/ 25 | develop-eggs/ 26 | dist/ 27 | downloads/ 28 | eggs/ 29 | .eggs/ 30 | lib/ 31 | lib64/ 32 | parts/ 33 | sdist/ 34 | var/ 35 | wheels/ 36 | *.egg-info/ 37 | .installed.cfg 38 | *.egg 39 | 40 | # PyInstaller 41 | # Usually these files are written by a python script from a template 42 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 43 | *.manifest 44 | *.spec 45 | 46 | # Installer logs 47 | pip-log.txt 48 | pip-delete-this-directory.txt 49 | 50 | # Unit test / coverage reports 51 | htmlcov/ 52 | .tox/ 53 | .coverage 54 | .coverage.* 55 | .cache 56 | nosetests.xml 57 | coverage.xml 58 | *.cover 59 | .hypothesis/ 60 | 61 | # Translations 62 | *.mo 63 | *.pot 64 | 65 | # Django stuff: 66 | *.log 67 | local_settings.py 68 | 69 | # Flask stuff: 70 | instance/ 71 | .webassets-cache 72 | 73 | # Scrapy stuff: 74 | .scrapy 75 | 76 | # Sphinx documentation 77 | docs/_build/ 78 | 79 | # PyBuilder 80 | target/ 81 | 82 | # Jupyter Notebook 83 | .ipynb_checkpoints 84 | 85 | # pyenv 86 | .python-version 87 | 88 | # celery beat schedule file 89 | celerybeat-schedule 90 | 91 | # SageMath parsed files 92 | *.sage.py 93 | 94 | # dotenv 95 | .env 96 | 97 | # virtualenv 98 | .venv 99 | venv/ 100 | ENV/ 101 | 102 | # Spyder project settings 103 | .spyderproject 104 | .spyproject 105 | 106 | # Rope project settings 107 | .ropeproject 108 | 109 | # mkdocs documentation 110 | /site 111 | 112 | # mypy 113 | .mypy_cache/ 114 | 115 | .vscode 116 | *.swp 117 | 118 | # osx generated files 119 | .DS_Store 120 | .DS_Store? 121 | .Trashes 122 | ehthumbs.db 123 | Thumbs.db 124 | .idea 125 | 126 | # pytest 127 | .pytest_cache 128 | 129 | # tools/trust-doc-nbs 130 | docs_src/.last_checked 131 | 132 | # symlinks to fastai 133 | docs_src/fastai 134 | tools/fastai 135 | 136 | # link checker 137 | checklink/cookies.txt 138 | 139 | # .gitconfig is now autogenerated 140 | .gitconfig 141 | 142 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to contribute 2 | 3 | ## Did you find a bug? 4 | 5 | * Ensure the bug was not already reported by searching on GitHub under Issues. 6 | * If you're unable to find an open issue addressing the problem, open a new one. Be sure to include a title and clear description, as much relevant information as possible, and a code sample or an executable test case demonstrating the expected behavior that is not occurring. 7 | * Be sure to add the complete error messages. 8 | 9 | #### Did you write a patch that fixes a bug? 10 | 11 | * Open a new GitHub pull request with the patch. 12 | * Ensure that your PR includes a test that fails without your patch, and pass with it. 13 | * Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable. 14 | 15 | ## PR submission guidelines 16 | 17 | * Keep each PR focused. While it's more convenient, do not combine several unrelated fixes together. Create as many branches as needing to keep each PR focused. 18 | * Do not mix style changes/fixes with "functional" changes. It's very difficult to review such PRs and it most likely get rejected. 19 | * Do not add/remove vertical whitespace. Preserve the original style of the file you edit as much as you can. 20 | * Do not turn an already submitted PR into your development playground. If after you submitted PR, you discovered that more work is needed - close the PR, do the required work and then submit a new PR. Otherwise each of your commits requires attention from maintainers of the project. 21 | * If, however, you submitted a PR and received a request for changes, you should proceed with commits inside that PR, so that the maintainer can see the incremental fixes and won't need to review the whole PR again. In the exception case where you realize it'll take many many commits to complete the requests, then it's probably best to close the PR, do the work and then submit it again. Use common sense where you'd choose one way over another. 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include settings.ini 2 | include LICENSE 3 | include CONTRIBUTING.md 4 | include README.md 5 | recursive-exclude * __pycache__ 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .ONESHELL: 2 | SHELL:=/bin/bash 3 | PYTHON_VERSION:=3.8 4 | 5 | # You can use either venv (venv) or conda env 6 | # by specifying the correct argument (env=) 7 | ifeq ($(env),venv) 8 | # Use Conda 9 | BASE=venv 10 | BIN=$(BASE)/bin 11 | CREATE_COMMAND="python$(PYTHON_VERSION) -m venv $(BASE)" 12 | DELETE_COMMAND="rm -rf $(BASE)" 13 | ACTIVATE_COMMAND="source venv/bin/activate" 14 | DEACTIVATE_COMMAND="deactivate" 15 | else 16 | # Use Conda 17 | BASE=~/anaconda3/envs/high_sql 18 | BIN=$(BASE)/bin 19 | CREATE_COMMAND="conda create --prefix $(BASE) python=$(PYTHON_VERSION) -y" 20 | DELETE_COMMAND="conda env remove -p $(BASE)" 21 | ACTIVATE_COMMAND="conda activate -p $(BASE)" 22 | DEACTIVATE_COMMAND="conda deactivate" 23 | endif 24 | 25 | # To load a env file use env_file= 26 | # e.g. make release env_file=.env 27 | ifneq ($(env_file),) 28 | include $(env_file) 29 | # export 30 | endif 31 | 32 | all: 33 | $(MAKE) help 34 | help: 35 | @echo 36 | @echo "-------------------------------------------------------------------------------------------" 37 | @echo " DISPLAYING HELP " 38 | @echo "-------------------------------------------------------------------------------------------" 39 | @echo "Run: make [env=] [env_file=]" 40 | @echo 41 | @echo "make help" 42 | @echo " Display this message" 43 | @echo "make release [env=] [env_file=]" 44 | @echo " Run pypi conda_release fastrelease_bump_version" 45 | @echo "make release_requirements [env=] [env_file=]" 46 | @echo " Install fastrelease twine and conda-build" 47 | @echo "make pypi [env=] [env_file=]" 48 | @echo " Run dist and upload using twine" 49 | @echo "make dist [env=] [env_file=]" 50 | @echo " Clean and create bdist and wheel" 51 | @echo "make clean [env=] [env_file=]" 52 | @echo " Delete all './build ./dist ./*.pyc ./*.tgz ./*.egg-info' files" 53 | @echo "make tests [env=] [env_file=]" 54 | @echo " Run all tests" 55 | @echo "make create_env [env=] [env_file=]" 56 | @echo " Create a new conda env or venv for the specified python version" 57 | @echo "make delete_env [env=] [env_file=]" 58 | @echo " Delete the current conda env or venv" 59 | @echo "-------------------------------------------------------------------------------------------" 60 | release: 61 | $(MAKE) release_requirements 62 | $(MAKE) pypi 63 | #$(MAKE) conda_release 64 | #fastrelease_bump_version 65 | release_test: 66 | $(MAKE) release_requirements 67 | $(MAKE) pypi_test 68 | release_requirements: 69 | $(BIN)/pip install fastrelease twine conda-build 70 | pypi: 71 | $(MAKE) dist 72 | twine upload --repository pypi dist/* 73 | pypi_test: 74 | $(MAKE) dist_test 75 | twine upload --repository pypitest dist/* --verbose 76 | conda_release: 77 | fastrelease_conda_package --upload_user drkostas 78 | dist: 79 | $(MAKE) clean 80 | pip install -r requirements.txt 81 | python setup.py sdist bdist_wheel 82 | dist_test: 83 | $(MAKE) clean 84 | pip install -r requirements.txt 85 | python setup.py sdist bdist_wheel --test 86 | clean: 87 | python setup.py clean 88 | tests: 89 | python setup.py test 90 | create_env: 91 | @eval $(CREATE_COMMAND) 92 | delete_env: 93 | @eval $(DELETE_COMMAND) 94 | 95 | .PHONY: all help release release_requirements conda_release pypi clean dist tests create_env delete_env dist_test pypi_test release_test -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # High SQL 2 | 3 | [![Downloads](https://static.pepy.tech/personalized-badge/high-sql?period=total&units=international_system&left_color=grey&right_color=red&left_text=Downloads)](https://pepy.tech/project/high-sql) 4 | [![GitHub license](https://img.shields.io/badge/license-Apache-blue.svg)](https://github.com/drkostas/high-sql/blob/master/LICENSE) 5 | [![CircleCI](https://circleci.com/gh/drkostas/high-sql/tree/master.svg?style=svg)](https://circleci.com/gh/drkostas/high-sql/tree/master) 6 | 7 | ## About 8 | 9 | A high-level sql command utility. Currently, only MySQL is 10 | supported. [PYPI Package](https://pypi.org/project/high-sql/) 11 | 12 | ## Table of Contents 13 | 14 | + [Using the library](#using) 15 | + [Installing and using the library](#install_use) 16 | + [Examples of usage](#examples) 17 | + [Manually install the library](#manual_install) 18 | + [Prerequisites](#prerequisites) 19 | + [Install the requirements](#installing_req) 20 | + [Run the Unit Tests](#unit_tests) 21 | + [Continuous Integration](#ci) 22 | + [Update PyPI package](#pypi) 23 | + [License](#license) 24 | 25 | ## Using the library 26 | 27 | For a detailed usage example see 28 | [example.py](https://github.com/drkostas/high-sql/tree/master/example.py). 29 | 30 | ### Installing and using the library 31 | 32 | First, you need to install the library using pip: 33 | 34 | ```shell 35 | $ pip install high_sql 36 | ``` 37 | 38 | Then, import it and initialize it like so: 39 | 40 | ```python 41 | from high_sql import HighMySQL 42 | 43 | db_conf = {'hostname': 'your hostname', 'username': 'your username', 'password': 'your password', 44 | 'db_name': 'your db name', 'port': 3306} 45 | mysql_obj = HighMySQL(config=db_conf) 46 | ``` 47 | 48 | If you want to use a yml file to load the configuration, you can use the `HighConfig` class: 49 | ```python 50 | from high_sql import HighConfig 51 | import os 52 | 53 | config_path = str(os.path.join('confs', 'conf.yml')) 54 | config = HighConfig(config_src=config_path) 55 | db_conf = config.get_db_config() 56 | ``` 57 | 58 | Two example YAML files can be found in 59 | the [confs folder](https://github.com/drkostas/high-sql/blob/master/confs). 60 | For more details on how to use this YAML configuration loader see 61 | this [Readme](https://github.com/drkostas/yaml-config-wrapper/blob/master/README.md). 62 | 63 | ### Examples of usage 64 | 65 | The currently supported operations are the following: 66 | - Inserts, Updates, Deletes, Select 67 | - Create, Truncate, Drop table 68 | - Show all tables 69 | 70 | **Insert** 71 | ```python 72 | mysql_obj.insert_into_table('test_table', data={'firstname': 'Mr Name', 'lastname': 'surname'}) 73 | ``` 74 | **Update** 75 | ```python 76 | mysql_obj.update_table('test_table', set_data={'lastname': 'New Last Name'}, 77 | where='firstname="Mr Name"') 78 | ``` 79 | **Delete** 80 | ```python 81 | mysql_obj.delete_from_table('test_table', where='firstname="Mr Name"') 82 | ``` 83 | **Select** 84 | ```python 85 | res = mysql_obj.select_from_table('test_table', columns='*', where='firstname="Mr Name"', 86 | order_by='firstname', asc_or_desc='ASC', limit=5) 87 | ``` 88 | **Truncate** 89 | ```python 90 | mysql_obj.truncate_table('test_table') 91 | ``` 92 | **Create** 93 | ```python 94 | mysql_obj.create_table(table='test_table', schema=table_schema) 95 | ``` 96 | **Drop** 97 | ```python 98 | mysql_obj.drop_table('test_table') 99 | ``` 100 | **Show Tables** 101 | ```python 102 | mysql_obj.show_tables() 103 | ``` 104 | 105 | All of these examples can be found 106 | in [example.py](https://github.com/drkostas/high-sql/tree/master/example.py). 107 | 108 | ## Manually install the library 109 | 110 | These instructions will get you a copy of the project up and running on your local machine for 111 | development and testing purposes. 112 | 113 | ### Prerequisites 114 | 115 | You need to have a machine with 116 | [anaconda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html) installed and 117 | any Bash based shell (e.g. zsh) installed. 118 | 119 | ```ShellSession 120 | 121 | $ conda -V 122 | conda 4.10.1 123 | 124 | $ echo $SHELL 125 | /usr/bin/zsh 126 | 127 | ``` 128 | 129 | ### Install the requirements 130 | 131 | All the installation steps are being handled by 132 | the [Makefile](https://github.com/drkostas/high-sql/tree/master/Makefile). 133 | 134 | First, modify the python version (`min_python`) and everything else you need in 135 | the [settings.ini](https://github.com/drkostas/high-sql/tree/master/settings.ini). 136 | 137 | Then, execute the following commands: 138 | 139 | ```ShellSession 140 | $ make create_env 141 | $ conda activate high_sql 142 | $ make dist 143 | ``` 144 | 145 | Now you are ready to use and modify the library. 146 | 147 | ### Run the Unit Tests 148 | 149 | If you want to run the unit tests, execute the following command: 150 | 151 | ```ShellSession 152 | $ make tests 153 | ``` 154 | 155 | ## Continuous Integration 156 | 157 | For the continuous integration, the CircleCI service is being used. For more information you can 158 | check the [setup guide](https://circleci.com/docs/2.0/language-python/). 159 | 160 | For any modifications, edit 161 | the [circleci config](https://github.com/drkostas/high-sql/tree/master/.circleci/config.yml). 162 | 163 | ## Update PyPI package 164 | 165 | This is mainly for future reference for the developers of this project. First, 166 | create a file called `~/.pypirc` with your pypi login details, as follows: 167 | 168 | ``` 169 | [pypi] 170 | username = your_pypi_username 171 | password = your_pypi_password 172 | ``` 173 | 174 | Then, modify the python version (`min_python`), project status (`status`), release version (`version`) 175 | and everything else you need in 176 | the [settings.ini](https://github.com/drkostas/high-sql/tree/master/settings.ini). 177 | 178 | Finally, execute the following commands: 179 | 180 | ```ShellSession 181 | $ make create_env 182 | $ conda activate high_sql 183 | $ make release 184 | ``` 185 | 186 | For a dev release, change the `testing_version` and instead of `make release`, run `make release_test`. 187 | 188 | ## License 189 | 190 | This project is licensed under the Apache License - see 191 | the [LICENSE](https://github.com/drkostas/high-sql/tree/master/LICENSE) file for details. 192 | 193 | Buy Me A Coffee 194 | -------------------------------------------------------------------------------- /confs/conf.yml: -------------------------------------------------------------------------------- 1 | tag: test 2 | high-sql: 3 | - config: 4 | hostname: your_hostname 5 | username: your_username 6 | password: your_password 7 | db_name: your_db_name 8 | port: 3306 9 | type: mysql -------------------------------------------------------------------------------- /confs/conf_with_env_vars.yml: -------------------------------------------------------------------------------- 1 | tag: test 2 | high-sql: 3 | - config: 4 | hostname: !ENV ${MYSQL_HOST} 5 | username: !ENV ${MYSQL_USERNAME} 6 | password: !ENV ${MYSQL_PASSWORD} 7 | db_name: !ENV ${MYSQL_DB_NAME} 8 | port: 3306 9 | type: mysql -------------------------------------------------------------------------------- /env_example: -------------------------------------------------------------------------------- 1 | # You should create a new file with ".env" name or something similar and NOT include it in your git 2 | export MYSQL_HOST='foo.rds.amazonaws.com' 3 | export MYSQL_USERNAME='user' 4 | export MYSQL_PASSWORD='pass' 5 | export MYSQL_DB_NAME='Test_schema' -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | # pip install high-sql 2 | import os 3 | from high_sql import HighMySQL, HighConfig, ColorLogger 4 | 5 | table_schema = """ 6 | id int auto_increment primary key, 7 | firstname varchar(30) not null, 8 | lastname varchar(30) not null, 9 | email varchar(50) null, 10 | reg_date timestamp default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP 11 | """ 12 | # Setup Logger 13 | log = ColorLogger(logger_name='Example', color='yellow') 14 | 15 | # Load config 16 | # db_conf = {'type': 'mysql', 17 | # 'config': {'hostname': 'your hostname', 'username': 'your username', 18 | # 'password': 'your password', 'db_name': 'your db name', 'port': 3306}} 19 | # config_path = os.path.join('confs', 'conf.yml') 20 | config_path = os.path.join('confs', 'conf_with_env_vars.yml') 21 | config = HighConfig(config_src=config_path) 22 | db_conf = config.get_db_config() 23 | # Check for errors 24 | if db_conf['type'] != 'mysql': 25 | raise Exception(f"{db_conf['type']} not yet supported!") 26 | if db_conf['config']['username'] == 'MYSQL_USERNAME': 27 | raise Exception("You are trying to use environmental variables but they are not loaded!") 28 | # Initialize handler 29 | mysql_obj = HighMySQL(config=db_conf['config']) 30 | 31 | # -------- Examples -------- # 32 | # Create Table 33 | mysql_obj.create_table(table='test_table', schema=table_schema) 34 | # Show Tables 35 | log.info(f"List of tables in DB:\n{mysql_obj.show_tables()}") 36 | # Insert into table 37 | mysql_obj.insert_into_table('test_table', data={'firstname': 'Mr Name', 'lastname': 'surname'}) 38 | # Show the record 39 | res = mysql_obj.select_from_table('test_table', columns='*', where='firstname="Mr Name"', 40 | order_by='firstname', asc_or_desc='ASC', limit=5) 41 | log.info(f"Result:\n{res}") 42 | # Update it 43 | mysql_obj.update_table('test_table', set_data={'lastname': 'New Last Name'}, 44 | where='firstname="Mr Name"') 45 | # Show the updated record 46 | res = mysql_obj.select_from_table('test_table', columns='*', where='firstname="Mr Name"') 47 | log.info(f"Result:\n{res}") 48 | # Delete it 49 | mysql_obj.delete_from_table('test_table', where='firstname="Mr Name"') 50 | # Show that it is gone 51 | res = mysql_obj.select_from_table('test_table', columns='*', where='firstname="Mr Name"') 52 | log.info(f"Result:\n{res}") 53 | # Truncate Table 54 | mysql_obj.truncate_table('test_table') 55 | # Drop Table 56 | mysql_obj.drop_table('test_table') 57 | -------------------------------------------------------------------------------- /high_sql/__init__.py: -------------------------------------------------------------------------------- 1 | """high-sql package""" 2 | 3 | from termcolor_logger import ColorLogger 4 | from .high_mysql import HighMySQL 5 | from .high_config import HighConfig 6 | 7 | __author__ = "drkostas" 8 | __email__ = "georgiou.kostas94@gmail.com" 9 | __version__ = "0.1.0" 10 | -------------------------------------------------------------------------------- /high_sql/abstract_highsql.py: -------------------------------------------------------------------------------- 1 | from abc import ABC, abstractmethod 2 | from typing import List, Dict 3 | 4 | 5 | class AbstractHighSQL(ABC): 6 | __slots__ = ('_connection', '_cursor') 7 | 8 | @abstractmethod 9 | def __init__(self, config: Dict) -> None: 10 | """ 11 | Tha basic constructor. Creates a new instance using the specified credentials 12 | 13 | :param config: 14 | """ 15 | 16 | self._connection, self._cursor = self.get_connection(username=config['username'], 17 | password=config['password'], 18 | hostname=config['hostname'], 19 | db_name=config['db_name'], 20 | port=config['port']) 21 | 22 | @staticmethod 23 | @abstractmethod 24 | def get_connection(username: str, password: str, hostname: str, db_name: str, port: int): 25 | pass 26 | 27 | @abstractmethod 28 | def create_table(self, table: str, schema: str): 29 | pass 30 | 31 | @abstractmethod 32 | def drop_table(self, table: str) -> None: 33 | pass 34 | 35 | @abstractmethod 36 | def truncate_table(self, table: str) -> None: 37 | pass 38 | 39 | @abstractmethod 40 | def insert_into_table(self, table: str, data: dict) -> None: 41 | pass 42 | 43 | @abstractmethod 44 | def update_table(self, table: str, set_data: dict, where: str) -> None: 45 | pass 46 | 47 | @abstractmethod 48 | def select_from_table(self, table: str, columns: str = '*', where: str = 'TRUE', 49 | order_by: str = 'NULL', 50 | asc_or_desc: str = 'ASC', limit: int = 1000) -> List: 51 | pass 52 | 53 | @abstractmethod 54 | def delete_from_table(self, table: str, where: str) -> None: 55 | pass 56 | 57 | @abstractmethod 58 | def show_tables(self, *args, **kwargs) -> List: 59 | pass 60 | -------------------------------------------------------------------------------- /high_sql/high_config.py: -------------------------------------------------------------------------------- 1 | from yaml_config_wrapper import Configuration 2 | from typing import * 3 | from io import StringIO, TextIOWrapper 4 | import os 5 | 6 | 7 | class HighConfig(Configuration): 8 | def __init__(self, config_src: Union[TextIOWrapper, StringIO, str], 9 | config_schema_path: str = None): 10 | """ The basic constructor. Creates a new instance of the HighConfig class. 11 | :param config_src: The path, file or StringIO object of the configuration to load 12 | :param config_schema_path: The path, file or StringIO object 13 | of the configuration validation file 14 | """ 15 | if config_schema_path is None: 16 | config_schema_path = os.path.join('high_sql', 'sql_schema.json') 17 | super().__init__(config_src, config_schema_path) 18 | 19 | def get_db_config(self, which: int = 0) -> Dict: 20 | return super().get_config('high-sql')[which] 21 | -------------------------------------------------------------------------------- /high_sql/high_mysql.py: -------------------------------------------------------------------------------- 1 | from typing import List, Tuple, Dict 2 | 3 | from mysql import connector as mysql_connector 4 | import mysql.connector.cursor 5 | 6 | from .abstract_highsql import AbstractHighSQL 7 | from termcolor_logger import ColorLogger 8 | 9 | logger = ColorLogger('MySqlDataStore') 10 | 11 | 12 | class HighMySQL(AbstractHighSQL): 13 | __slots__ = ('_connection', '_cursor') 14 | 15 | _connection: mysql_connector.MySQLConnection 16 | _cursor: mysql_connector.cursor.MySQLCursor 17 | 18 | def __init__(self, config: Dict) -> None: 19 | """ 20 | The basic constructor. Creates a new instance using the specified credentials 21 | 22 | :param config: 23 | """ 24 | 25 | super().__init__(config) 26 | 27 | @staticmethod 28 | def get_connection(username: str, password: str, hostname: str, db_name: str, port: int = 3306) \ 29 | -> Tuple[mysql_connector.MySQLConnection, mysql_connector.cursor.MySQLCursor]: 30 | """ 31 | Creates and returns a connection and a cursor/session to the MySQL DB 32 | 33 | :param username: 34 | :param password: 35 | :param hostname: 36 | :param db_name: 37 | :param port: 38 | :return: 39 | """ 40 | 41 | connection = mysql_connector.connect( 42 | host=hostname, 43 | user=username, 44 | passwd=password, 45 | database=db_name, 46 | use_pure=True 47 | ) 48 | 49 | cursor = connection.cursor() 50 | return connection, cursor 51 | 52 | def create_table(self, table: str, schema: str) -> None: 53 | """ 54 | Creates a table using the specified schema 55 | 56 | :param self: 57 | :param table: 58 | :param schema: 59 | :return: 60 | """ 61 | 62 | query = "CREATE TABLE IF NOT EXISTS {table} ({schema})".format(table=table, schema=schema) 63 | logger.debug("Executing: %s" % query) 64 | self._cursor.execute(query) 65 | self._connection.commit() 66 | 67 | def drop_table(self, table: str) -> None: 68 | """ 69 | Drops the specified table if it exists 70 | 71 | :param self: 72 | :param table: 73 | :return: 74 | """ 75 | 76 | query = "DROP TABLE IF EXISTS {table}".format(table=table) 77 | logger.debug("Executing: %s" % query) 78 | self._cursor.execute(query) 79 | self._connection.commit() 80 | 81 | def truncate_table(self, table: str) -> None: 82 | """ 83 | Truncates the specified table 84 | 85 | :param self: 86 | :param table: 87 | :return: 88 | """ 89 | 90 | query = "TRUNCATE TABLE {table}".format(table=table) 91 | logger.debug("Executing: %s" % query) 92 | self._cursor.execute(query) 93 | self._connection.commit() 94 | 95 | def insert_into_table(self, table: str, data: dict) -> None: 96 | """ 97 | Inserts into the specified table a row based on a column_name: value dictionary 98 | 99 | :param self: 100 | :param table: 101 | :param data: 102 | :return: 103 | """ 104 | 105 | data_str = ", ".join( 106 | list(map(lambda key, val: "{key}='{val}'".format(key=str(key), val=str(val)), data.keys(), 107 | data.values()))) 108 | 109 | query = "INSERT INTO {table} SET {data}".format(table=table, data=data_str) 110 | logger.debug("Executing: %s" % query) 111 | self._cursor.execute(query) 112 | self._connection.commit() 113 | 114 | def update_table(self, table: str, set_data: dict, where: str) -> None: 115 | """ 116 | Updates the specified table using a column_name: value dictionary and a where statement 117 | 118 | :param self: 119 | :param table: 120 | :param set_data: 121 | :param where: 122 | :return: 123 | """ 124 | 125 | set_data_str = ", ".join( 126 | list(map(lambda key, val: "{key}='{val}'".format(key=str(key), val=str(val)), 127 | set_data.keys(), 128 | set_data.values()))) 129 | 130 | query = "UPDATE {table} SET {data} WHERE {where}".format(table=table, data=set_data_str, 131 | where=where) 132 | logger.debug("Executing: %s" % query) 133 | self._cursor.execute(query) 134 | self._connection.commit() 135 | 136 | def select_from_table(self, table: str, columns: str = '*', where: str = 'TRUE', 137 | order_by: str = 'NULL', 138 | asc_or_desc: str = 'ASC', limit: int = 1000) -> List: 139 | """ 140 | Selects from a specified table based on the given columns, where, ordering and limit 141 | 142 | :param self: 143 | :param table: 144 | :param columns: 145 | :param where: 146 | :param order_by: 147 | :param asc_or_desc: 148 | :param limit: 149 | :return results: 150 | """ 151 | 152 | query = "SELECT {columns} FROM {table} WHERE {where} ORDER BY {order_by} {asc_or_desc} LIMIT {limit}".format( 153 | columns=columns, table=table, where=where, order_by=order_by, asc_or_desc=asc_or_desc, 154 | limit=limit) 155 | logger.debug("Executing: %s" % query) 156 | self._cursor.execute(query) 157 | results = self._cursor.fetchall() 158 | 159 | return results 160 | 161 | def delete_from_table(self, table: str, where: str) -> None: 162 | """ 163 | Deletes data from the specified table based on a where statement 164 | 165 | :param self: 166 | :param table: 167 | :param where: 168 | :return: 169 | """ 170 | 171 | query = "DELETE FROM {table} WHERE {where}".format(table=table, where=where) 172 | logger.debug("Executing: %s" % query) 173 | self._cursor.execute(query) 174 | self._connection.commit() 175 | 176 | def show_tables(self) -> List: 177 | """ 178 | Show a list of the tables present in the db 179 | :return: 180 | """ 181 | 182 | query = 'SHOW TABLES' 183 | logger.debug("Executing: %s" % query) 184 | self._cursor.execute(query) 185 | results = self._cursor.fetchall() 186 | results = [result[0].decode() if isinstance(result[0], bytearray) else result[0] 187 | for result in results] 188 | 189 | return results 190 | 191 | def __exit__(self) -> None: 192 | """ 193 | Flushes and closes the connection 194 | 195 | :return: 196 | """ 197 | 198 | self._connection.commit() 199 | self._cursor.close() 200 | -------------------------------------------------------------------------------- /high_sql/sql_schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-04/schema#", 3 | "type": "object", 4 | "properties": { 5 | "tag": { 6 | "type": "string" 7 | }, 8 | "high-sql": { 9 | "$ref": "#/definitions/high-sql" 10 | } 11 | }, 12 | "required": [ 13 | "high-sql" 14 | ], 15 | "definitions": { 16 | "high-sql": { 17 | "type": "array", 18 | "items": { 19 | "type": "object", 20 | "required": [ 21 | "type", 22 | "config" 23 | ], 24 | "config": { 25 | "type": { 26 | "type": "string", 27 | "enum": [ 28 | "mysql", 29 | "mongodb", 30 | "postgres" 31 | ] 32 | }, 33 | "properties": { 34 | "type": "object", 35 | "additionalProperties": false, 36 | "required": [ 37 | "hostname", 38 | "username", 39 | "password", 40 | "db_name", 41 | "port" 42 | ], 43 | "properties": { 44 | "hostname": { 45 | "type": "string" 46 | }, 47 | "username": { 48 | "type": "string" 49 | }, 50 | "password": { 51 | "type": "string" 52 | }, 53 | "db_name": { 54 | "type": "string" 55 | }, 56 | "port": { 57 | "type": "integer" 58 | } 59 | } 60 | } 61 | } 62 | }, 63 | "additionalProperties": false 64 | } 65 | }, 66 | "additionalProperties": true 67 | } -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | mysql-connector-python~=8.0.19 2 | mysql-connector~=2.2.9 3 | yaml-config-wrapper>=1.0.4 4 | termcolor-logger>=1.0.3 -------------------------------------------------------------------------------- /settings.ini: -------------------------------------------------------------------------------- 1 | [DEFAULT] 2 | # All sections below are required unless otherwise specified 3 | host = github 4 | lib_name = high-sql 5 | user = drkostas 6 | description = A high-level sql command utility. Currently only MySQL is supported. 7 | keywords = db, database, wrapper, connection, high-level, commands, sql, mysql, nosql, mongodb, postgresql 8 | author = Kostas Georgiou 9 | author_email = georgiou.kostas94@gmail.com 10 | copyright = Kostas Georgiou 11 | branch = master 12 | version = 1.0.2 13 | testing_version = 0.0.1 14 | min_python = 3.6 15 | audience = Developers 16 | language = English 17 | # Add licenses and see current list in `setup.py` 18 | license = apache2 19 | # From 0-6: Planning Pre-Alpha Alpha Beta Production Mature Inactive 20 | status = 4 21 | 22 | # Optional. Same format as setuptools requirements 23 | requirements = requirements.txt 24 | data_files = high_sql/sql_schema.json 25 | # Optional. Same format as setuptools console_scripts 26 | # console_scripts = 27 | # Optional. Same format as setuptools dependency-links 28 | # dep_links = 29 | 30 | # Values of the form `%(foo)s` are automatically replaced with the value of `foo` 31 | lib_path = %(lib_name)s 32 | 33 | git_url = https://github.com/%(user)s/%(lib_name)s/tree/%(branch)s/ 34 | # For Enterprise Github use: 35 | # repo_name = your-repo 36 | # company_name = your-company 37 | # git_url = https://github.%(company_name)s.com/%(repo_name)s/%(lib_name)s/tree/%(branch)s/ 38 | 39 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | from pkg_resources import parse_version 4 | from configparser import ConfigParser 5 | import setuptools 6 | 7 | assert parse_version(setuptools.__version__) >= parse_version('36.2') 8 | 9 | # For the cases you want a different package to be installed on local and prod environments 10 | LOCAL_ARG = '--test' 11 | if LOCAL_ARG in sys.argv: 12 | index = sys.argv.index(LOCAL_ARG) # Index of the local argument 13 | sys.argv.pop(index) # Removes the local argument in order to prevent the setup() error 14 | testing = True 15 | else: 16 | testing = False 17 | 18 | 19 | class CleanCommand(setuptools.Command): 20 | """Custom clean command to tidy up the project root.""" 21 | user_options = [] 22 | 23 | def initialize_options(self): 24 | pass 25 | 26 | def finalize_options(self): 27 | pass 28 | 29 | def run(self): 30 | os.system('rm -vrf ./build ./dist ./*.pyc ./*.tgz ./*.egg-info') 31 | 32 | 33 | # note: all settings are in settings.ini; edit there, not here 34 | config = ConfigParser(delimiters=['=']) 35 | config.read('settings.ini') 36 | cfg = config['DEFAULT'] 37 | if testing: 38 | lib_version = cfg['testing_version'] 39 | print(f"Testing version: {lib_version}") 40 | else: 41 | lib_version = cfg['version'] 42 | print(f"Non-Testing version: {lib_version}") 43 | 44 | cfg_keys = 'description keywords author author_email'.split() 45 | expected = cfg_keys + "lib_name user branch license status min_python audience language".split() 46 | for o in expected: 47 | assert o in cfg, "missing expected setting: {}".format(o) 48 | setup_cfg = {o: cfg[o] for o in cfg_keys} 49 | licenses = {'apache2': ('Apache Software License 2.0', 'OSI Approved :: Apache Software License')} 50 | statuses = ['1 - Planning', '2 - Pre-Alpha', '3 - Alpha', '4 - Beta', 51 | '5 - Production/Stable', '6 - Mature', '7 - Inactive'] 52 | py_versions = '2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8'.split() 53 | with open(cfg.get('requirements', '')) as f: 54 | requirements = f.readlines() 55 | print(requirements) 56 | data_files = cfg['data_files'].split() 57 | lic = licenses[cfg['license']] 58 | min_python = cfg['min_python'] 59 | 60 | setuptools.setup( 61 | name=cfg['lib_name'], 62 | license=lic[0], 63 | classifiers=['Development Status :: ' + statuses[int(cfg['status'])], 64 | 'Intended Audience :: ' + cfg['audience'].title(), 65 | 'License :: ' + lic[1], 66 | 'Natural Language :: ' + cfg['language'].title()] + 67 | ['Programming Language :: Python :: ' + o for o in 68 | py_versions[py_versions.index(min_python):]], 69 | url=cfg['git_url'], 70 | packages=setuptools.find_packages(), 71 | include_package_data=True, 72 | data_files=[('', data_files)], 73 | test_suite='tests', 74 | install_requires=requirements, 75 | setup_requires=requirements, 76 | tests_require=requirements, 77 | cmdclass={ 78 | 'clean': CleanCommand, 79 | }, 80 | dependency_links=cfg.get('dep_links', '').split(), 81 | python_requires='>=' + cfg['min_python'], 82 | long_description=open('README.md').read(), 83 | long_description_content_type='text/markdown', 84 | zip_safe=False, 85 | entry_points={'console_scripts': cfg.get('console_scripts', '').split()}, 86 | version=lib_version, 87 | **setup_cfg) 88 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | """Unit tests. """ 2 | -------------------------------------------------------------------------------- /tests/test_data/test_high_sql/conf.yml: -------------------------------------------------------------------------------- 1 | tag: test 2 | high-sql: 3 | - config: 4 | hostname: !ENV ${MYSQL_HOST} 5 | username: !ENV ${MYSQL_USERNAME} 6 | password: !ENV ${MYSQL_PASSWORD} 7 | db_name: !ENV ${MYSQL_DB_NAME} 8 | port: 3306 9 | type: mysql -------------------------------------------------------------------------------- /tests/test_high_sql.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import os 3 | import copy 4 | import random 5 | import string 6 | import logging 7 | from typing import List 8 | from mysql.connector.errors import ProgrammingError as MsqlProgrammingError 9 | 10 | from yaml_config_wrapper import Configuration 11 | from high_sql import HighMySQL, ColorLogger 12 | 13 | logger = ColorLogger('TestMysqlDatastore') 14 | 15 | 16 | class TestMysqlDatastore(unittest.TestCase): 17 | __slots__ = ('configuration', 'test_table_schema') 18 | 19 | configuration: Configuration 20 | test_table_schema: str 21 | generated_table_names: List[str] = list() 22 | test_data_path: str = os.path.join('tests', 'test_data', 'test_high_sql') 23 | 24 | def test_connect(self): 25 | # Test the connection with the correct api key 26 | try: 27 | HighMySQL(config=self.configuration.get_config('high-sql')[0]['config']) 28 | except MsqlProgrammingError as e: 29 | logger.error('Error connecting with the correct credentials: %s', e) 30 | self.fail('Error connecting with the correct credentials') 31 | else: 32 | logger.info('Connected with the correct credentials successfully.') 33 | # Test that the connection is failed with the wrong credentials 34 | with self.assertRaises(MsqlProgrammingError): 35 | datastore_conf_copy = copy.deepcopy(self.configuration.get_config('high-sql')[0]['config']) 36 | datastore_conf_copy['password'] = 'wrong_password' 37 | HighMySQL(config=datastore_conf_copy) 38 | logger.info("Loading Mysql with wrong credentials failed successfully.") 39 | 40 | def test_create_drop(self): 41 | data_store = HighMySQL(config=self.configuration.get_config('high-sql')[0]['config']) 42 | # Create table 43 | logger.info('Creating table..') 44 | data_store.create_table(self.table_name, self.test_table_schema) 45 | # Check if it was created 46 | self.assertIn(self.table_name, data_store.show_tables()) 47 | # Drop table 48 | logger.info('Dropping table..') 49 | data_store.drop_table(table=self.table_name) 50 | self.assertNotIn(self.table_name, data_store.show_tables()) 51 | 52 | def test_insert_update_delete(self): 53 | data_store = HighMySQL(config=self.configuration.get_config('high-sql')[0]['config']) 54 | # Create table 55 | logger.info('Creating table..') 56 | data_store.create_table(self.table_name, self.test_table_schema) 57 | # Ensure it is empty 58 | results = data_store.select_from_table(table=self.table_name) 59 | self.assertEqual([], results) 60 | # Insert into table 61 | insert_data = {"order_id": 1, 62 | "order_type": "plain", 63 | "is_delivered": False} 64 | logger.info("Inserting into table..") 65 | data_store.insert_into_table(table=self.table_name, data=insert_data) 66 | # Check if the data was inserted 67 | results = data_store.select_from_table(table=self.table_name) 68 | self.assertEqual([(1, "plain", False)], results) 69 | logger.info("Deleting from table..") 70 | data_store.delete_from_table(table=self.table_name, where='order_id =1 ') 71 | # Check if the data was inserted 72 | results = data_store.select_from_table(table=self.table_name) 73 | self.assertEqual([], results) 74 | 75 | @staticmethod 76 | def _generate_random_filename() -> str: 77 | letters = string.ascii_lowercase 78 | file_name = 'test_table_' + ''.join(random.choice(letters) for _ in range(10)) 79 | return file_name 80 | 81 | @staticmethod 82 | def _setup_log() -> None: 83 | # noinspection PyArgumentList 84 | logging.basicConfig(level=logging.DEBUG, 85 | format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 86 | datefmt='%Y-%m-%d %H:%M:%S', 87 | handlers=[logging.StreamHandler() 88 | ] 89 | ) 90 | 91 | def setUp(self) -> None: 92 | self.table_name = self._generate_random_filename() 93 | self.generated_table_names.append(self.table_name) 94 | 95 | def tearDown(self) -> None: 96 | pass 97 | 98 | @classmethod 99 | def setUpClass(cls): 100 | cls._setup_log() 101 | mysql_os_vars = ['MYSQL_HOST', 'MYSQL_USERNAME', 'MYSQL_PASSWORD', 'MYSQL_DB_NAME'] 102 | if not all(mysql_os_var in os.environ for mysql_os_var in mysql_os_vars): 103 | logger.error('Mysql env variables are not set!') 104 | raise Exception('Mysql env variables are not set!') 105 | logger.info('Loading Configuration..') 106 | cls.configuration = Configuration(config_src=os.path.join(cls.test_data_path, 'conf.yml')) 107 | cls.test_table_schema = """ order_id INT(6) PRIMARY KEY, 108 | order_type VARCHAR(30) NOT NULL, 109 | is_delivered BOOLEAN NOT NULL """ 110 | 111 | @classmethod 112 | def tearDownClass(cls): 113 | data_store = HighMySQL(config=cls.configuration.get_config('high-sql')[0]['config']) 114 | for table in cls.generated_table_names: 115 | logger.info('Dropping table {0}'.format(table)) 116 | data_store.drop_table(table=table) 117 | 118 | 119 | if __name__ == '__main__': 120 | unittest.main() 121 | --------------------------------------------------------------------------------