This repository was archived by the owner on Apr 14, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2sql-cli
More file actions
Latest commit
executable file
·87 lines (72 loc) · 3.62 KB
/
Copy pathjson2sql-cli
File metadata and controls
executable file
·87 lines (72 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# ######################################################################
# Copyright (C) 2017 Fridolin Pokorny, fridolin.pokorny@gmail.com
# This file is part of json2sql package.
# ######################################################################
"""Convert JSON/YAML/dict to a SQL statement."""
importlogging
importsys
importclick
importdaiquiri
fromjson2sqlimportdelete2sql
fromjson2sqlimportinsert2sql
fromjson2sqlimportjson2sql
fromjson2sqlimportreplace2sql
fromjson2sqlimportselect2sql
fromjson2sqlimportupdate2sql
_logger=daiquiri.getLogger(__name__)
classCliInputError(Exception):
"""Exception raised when wrong CLI arguments are passed."""
@click.command()
@click.option('--delete', '-D', is_flag=True, help='Treat input as DELETE SQL statement.')
@click.option('--insert', '-I', is_flag=True, help='Treat input as INSERT SQL statement.')
@click.option('--replace', '-R', is_flag=True, help='Treat input as REPLACE SQL statement.')
@click.option('--select', '-S', is_flag=True, help='Treat input as SELECT SQL statement.')
@click.option('--update', '-U', is_flag=True, help='Treat input as UPDATE SQL statement.')
@click.option('--output-file', '-o', default=None, help='Specify output file.')
@click.option('--amend-output', '-a', is_flag=True, help='Amend to output file instead of overwriting it.')
@click.option('--input-file', '-i', default=None, help='Specify input file.')
@click.option('--verbose', '-v', count=True, help='Increase logging verbosity, can be used multiple times.')
defmain(delete=False, insert=False, replace=False, select=False, update=False,
output_file=None, amend_output=False, input_file=None, verbose=0):
"""Convert JSON/YAML/dict to a SQL statement."""
ifverbose:
# hack based on num values of logging.DEBUG, logging.INFO, ...
level=max(logging.ERROR-verbose*10, logging.DEBUG)
daiquiri.setup(outputs=(daiquiri.output.STDERR,), level=level)
ifamend_outputandnotoutput_file:
raiseCliInputError("Flag --amend-output specified but no --output-file specified.")
count=int(delete) +int(insert) +int(replace) +int(select) +int(update)
ifcount>1:
raiseCliInputError("Only one of --delete, --insert, --replace, --select, --update "
"can be supplied at once.")
ifdelete:
_logger.debug("Translating DELETE description to SQL statement")
result=delete2sql(input_fileorsys.stdin)
elifinsert:
_logger.debug("Translating INSERT description to SQL statement")
result=insert2sql(input_fileorsys.stdin)
elifreplace:
_logger.debug("Translating REPLACE description to SQL statement")
result=replace2sql(input_fileorsys.stdin)
elifselect:
_logger.debug("Translating SELECT description to SQL statement")
result=select2sql(input_fileorsys.stdin)
elifupdate:
_logger.debug("Translating UPDATE description to SQL statement")
result=update2sql(input_fileorsys.stdin)
else:
_logger.debug("Translating JSON/YAML description to SQL statement")
result=json2sql(input_fileorsys.stdin)
_logger.debug("Result is: %s", result)
ifoutput_fileisnotNone:
_logger.debug("Writing output to file '%s' - content will be %s",
output_file, 'amended'ifamend_outputelse'overwritten')
withopen(output_file, 'a'ifamend_outputelse'w') asf:
f.write(result)
f.write('\n')
else:
_logger.debug("Writing output to stdout")
print(result)
if__name__=='__main__':
main()