Repository: mtxr/SQLTools
Branch: master
Commit: 1af1303dff8f
Files: 66
Total size: 270.1 KB
Directory structure:
gitextract_blnxvnte/
├── .bumpversion.cfg
├── .gitignore
├── .mention-bot
├── .no-sublime-package
├── Default (Linux).sublime-keymap
├── Default (OSX).sublime-keymap
├── Default (Windows).sublime-keymap
├── Default.sublime-commands
├── ISSUE_TEMPLATE.md
├── LICENSE.md
├── Main.sublime-menu
├── README.md
├── SQLTools.py
├── SQLTools.sublime-settings
├── SQLToolsAPI/
│ ├── Command.py
│ ├── Completion.py
│ ├── Connection.py
│ ├── History.py
│ ├── ParseUtils.py
│ ├── README.md
│ ├── Storage.py
│ ├── Utils.py
│ ├── __init__.py
│ └── lib/
│ └── sqlparse/
│ ├── __init__.py
│ ├── __main__.py
│ ├── cli.py
│ ├── compat.py
│ ├── engine/
│ │ ├── __init__.py
│ │ ├── filter_stack.py
│ │ ├── grouping.py
│ │ └── statement_splitter.py
│ ├── exceptions.py
│ ├── filters/
│ │ ├── __init__.py
│ │ ├── aligned_indent.py
│ │ ├── others.py
│ │ ├── output.py
│ │ ├── reindent.py
│ │ ├── right_margin.py
│ │ └── tokens.py
│ ├── formatter.py
│ ├── keywords.py
│ ├── lexer.py
│ ├── sql.py
│ ├── tokens.py
│ └── utils.py
├── SQLToolsConnections.sublime-settings
├── SQLToolsSavedQueries.sublime-settings
├── messages/
│ ├── install.md
│ ├── v0.1.6.md
│ ├── v0.2.0.md
│ ├── v0.3.0.md
│ ├── v0.8.2.md
│ ├── v0.9.0.md
│ ├── v0.9.1.md
│ ├── v0.9.10.md
│ ├── v0.9.11.md
│ ├── v0.9.12.md
│ ├── v0.9.2.md
│ ├── v0.9.3.md
│ ├── v0.9.4.md
│ ├── v0.9.5.md
│ ├── v0.9.6.md
│ ├── v0.9.7.md
│ ├── v0.9.8.md
│ └── v0.9.9.md
└── messages.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .bumpversion.cfg
================================================
[bumpversion]
current_version = 0.9.12
files = SQLTools.py
tag = True
commit = True
================================================
FILE: .gitignore
================================================
.DS_Store
*.pyc
*.py~
*.sublime-workspace
.idea/
================================================
FILE: .mention-bot
================================================
{
"maxReviewers": 2,
"skipCollaboratorPR": true
}
================================================
FILE: .no-sublime-package
================================================
================================================
FILE: Default (Linux).sublime-keymap
================================================
[
{ "keys": ["ctrl+alt+e"], "command": "st_select_connection" },
{ "keys": ["ctrl+e", "ctrl+e"], "command": "st_execute" },
{ "keys": ["ctrl+e", "ctrl+a"], "command": "st_execute_all" },
{ "keys": ["ctrl+e", "ctrl+x"], "command": "st_explain_plan" },
{ "keys": ["ctrl+e", "ctrl+h"], "command": "st_history" },
{ "keys": ["ctrl+e", "ctrl+s"], "command": "st_show_records" },
{ "keys": ["ctrl+e", "ctrl+d"], "command": "st_desc_table" },
{ "keys": ["ctrl+e", "ctrl+f"], "command": "st_desc_function" },
{ "keys": ["ctrl+e", "ctrl+b"], "command": "st_format" },
{ "keys": ["ctrl+e", "ctrl+q"], "command": "st_save_query" },
{ "keys": ["ctrl+e", "ctrl+r"], "command": "st_remove_saved_query" },
{ "keys": ["ctrl+e", "ctrl+l"], "command": "st_list_queries", "args": {"mode" : "run"}},
{ "keys": ["ctrl+e", "ctrl+o"], "command": "st_list_queries", "args": {"mode" : "open"}},
{ "keys": ["ctrl+e", "ctrl+i"], "command": "st_list_queries", "args": {"mode" : "insert"}}
]
================================================
FILE: Default (OSX).sublime-keymap
================================================
[
{ "keys": ["ctrl+super+e"], "command": "st_select_connection" },
{ "keys": ["ctrl+e", "ctrl+e"], "command": "st_execute" },
{ "keys": ["ctrl+e", "ctrl+a"], "command": "st_execute_all" },
{ "keys": ["ctrl+e", "ctrl+x"], "command": "st_explain_plan" },
{ "keys": ["ctrl+e", "ctrl+h"], "command": "st_history" },
{ "keys": ["ctrl+e", "ctrl+s"], "command": "st_show_records" },
{ "keys": ["ctrl+e", "ctrl+d"], "command": "st_desc_table" },
{ "keys": ["ctrl+e", "ctrl+f"], "command": "st_desc_function" },
{ "keys": ["ctrl+e", "ctrl+b"], "command": "st_format" },
{ "keys": ["ctrl+e", "ctrl+q"], "command": "st_save_query" },
{ "keys": ["ctrl+e", "ctrl+r"], "command": "st_remove_saved_query" },
{ "keys": ["ctrl+e", "ctrl+l"], "command": "st_list_queries", "args": {"mode" : "run"}},
{ "keys": ["ctrl+e", "ctrl+o"], "command": "st_list_queries", "args": {"mode" : "open"}},
{ "keys": ["ctrl+e", "ctrl+i"], "command": "st_list_queries", "args": {"mode" : "insert"}}
]
================================================
FILE: Default (Windows).sublime-keymap
================================================
[
{ "keys": ["ctrl+alt+e"], "command": "st_select_connection" },
{ "keys": ["ctrl+e", "ctrl+e"], "command": "st_execute" },
{ "keys": ["ctrl+e", "ctrl+a"], "command": "st_execute_all" },
{ "keys": ["ctrl+e", "ctrl+x"], "command": "st_explain_plan" },
{ "keys": ["ctrl+e", "ctrl+h"], "command": "st_history" },
{ "keys": ["ctrl+e", "ctrl+s"], "command": "st_show_records" },
{ "keys": ["ctrl+e", "ctrl+d"], "command": "st_desc_table" },
{ "keys": ["ctrl+e", "ctrl+f"], "command": "st_desc_function" },
{ "keys": ["ctrl+e", "ctrl+b"], "command": "st_format" },
{ "keys": ["ctrl+e", "ctrl+q"], "command": "st_save_query" },
{ "keys": ["ctrl+e", "ctrl+r"], "command": "st_remove_saved_query" },
{ "keys": ["ctrl+e", "ctrl+l"], "command": "st_list_queries", "args": {"mode" : "run"}},
{ "keys": ["ctrl+e", "ctrl+o"], "command": "st_list_queries", "args": {"mode" : "open"}},
{ "keys": ["ctrl+e", "ctrl+i"], "command": "st_list_queries", "args": {"mode" : "insert"}}
]
================================================
FILE: Default.sublime-commands
================================================
[
{
"caption": "ST: Select Connection",
"command": "st_select_connection"
},
{
"caption": "ST: Execute",
"command": "st_execute"
},
{
"caption": "ST: Execute All File",
"command": "st_execute_all"
},
{
"caption": "ST: Explain Plan",
"command": "st_explain_plan"
},
{
"caption": "ST: History",
"command": "st_history"
},
{
"caption": "ST: Show Table Records",
"command": "st_show_records"
},
{
"caption": "ST: Table Description",
"command": "st_desc_table"
},
{
"caption": "ST: Function Description",
"command": "st_desc_function"
},
{
"caption": "ST: Refresh Connection Data",
"command": "st_refresh_connection_data"
},
{
"caption": "ST: Save Query",
"command": "st_save_query"
},
{
"caption": "ST: Remove Saved Query",
"command": "st_remove_saved_query"
},
{
"caption": "ST: List and Run Saved Queries",
"command": "st_list_queries",
"args" : {"mode": "run"}
},
{
"caption": "ST: List and Open Saved Queries",
"command": "st_list_queries",
"args" : {"mode": "open"}
},
{
"caption": "ST: List and Insert Saved Queries",
"command": "st_list_queries",
"args" : {"mode": "insert"}
},
{
"caption": "ST: Format SQL",
"command": "st_format"
},
{
"caption": "ST: Format SQL All File",
"command": "st_format_all"
},
{
"caption": "ST: About",
"command": "st_version"
},
{
"caption": "ST: Setup Connections",
"command": "edit_settings", "args":
{
"base_file": "${packages}/SQLTools/SQLToolsConnections.sublime-settings",
"default": "// List all your connections to DBs here\n{\n\t\"connections\": {\n\t\t$0\n\t},\n\t\"default\": null\n}"
}
},
{
"caption": "ST: Settings",
"command": "edit_settings", "args":
{
"base_file": "${packages}/SQLTools/SQLTools.sublime-settings",
"default": "// Settings in here override those in \"SQLTools/SQLTools.sublime-settings\"\n{\n\t$0\n}\n"
}
}
]
================================================
FILE: ISSUE_TEMPLATE.md
================================================
> This issue template helps us understand your SQLTools issues better.
>
> You don't need to stick to this template, but please try to guide us to reproduce the errors or understand your feature requests.
>
> Before submitting an issue, please consider these things first:
> * Are you running the latest version? If not, try to upgrade.
> * Did you check the [Setup Guide](https://code.mteixeira.dev/SublimeText-SQLTools/)?
> * Did you check the logs in console (``Ctrl+` `` or select *View → Show Console*)?
### Issue Type
Feature Request |
Bug/Error |
Question |
Other
### Description
[Description of the bug / feature / question]
### Version
- *SQLTools Version*: vX.Y.Z
- *OS*: (Windows, Mac, Linux)
- *RDBMS*: (MySQL, PostgreSQL, Oracle, MSSQL, SQLite, Vertica, ...)
> You can get this information by executing `ST: About` from Sublime `Command Palette`.
### Steps to Reproduce (For bugfixes)
1. [First Step]
2. [Second Step]
3. [and so on...]
**Expected behavior:** [What you expected to happen]
**Actual behavior:** [What actually happened]
================================================
FILE: LICENSE.md
================================================
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
Copyright (C)
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
================================================
FILE: Main.sublime-menu
================================================
[
{
"caption": "Preferences",
"mnemonic": "n",
"id": "preferences",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "SQLTools",
"children":
[
{
"caption": "Connections",
"command": "edit_settings", "args":
{
"base_file": "${packages}/SQLTools/SQLToolsConnections.sublime-settings",
"default": "// List all your connections to DBs here\n{\n\t\"connections\": {\n\t\t$0\n\t},\n\t\"default\": null\n}"
}
},
{
"caption": "Settings",
"command": "edit_settings", "args":
{
"base_file": "${packages}/SQLTools/SQLTools.sublime-settings",
"default": "// Settings in here override those in \"SQLTools/SQLTools.sublime-settings\"\n{\n\t$0\n}\n"
}
},
{
"caption": "Key Bindings",
"command": "edit_settings", "args":
{
"base_file": "${packages}/SQLTools/Default ($platform).sublime-keymap",
"default": "[\n\t$0\n]\n"
}
}
]
}
]
}
]
}
]
================================================
FILE: README.md
================================================
 SQLTools
===============
> Looking for maintainers! I'm currently using VSCode as my editor, so I'm not actively maintaining this project anymore.
>
> If you are interested in maintaining this project, contact me.
>
> If you are interested in checking VSCode version, see [https://github.com/mtxr/vscode-sqltools](https://github.com/mtxr/vscode-sqltools).
[](https://gitter.im/SQLTools/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Your swiss knife SQL for Sublime Text.
Write your SQL with smart completions and handy table and function definitions, execute SQL and explain queries, format your queries and save them in history.
Project website: [https://code.mteixeira.dev/SublimeText-SQLTools/](https://code.mteixeira.dev/SublimeText-SQLTools/)
> If you are looking for VSCode version go to [https://github.com/mtxr/vscode-sqltools](https://github.com/mtxr/vscode-sqltools).
## Donate
SQLTools was developed with ♥ to save us time during our programming journey. But It also takes me time and efforts to develop SQLTools.
SQLTools will save you (for sure) a lot of time and help you to increase your productivity so, I hope you can donate and help SQLTools to become more awesome than ever.
## Features
* Works with PostgreSQL, MySQL, Oracle, MSSQL, SQLite, Vertica, Firebird and Snowflake
* Smart completions (except SQLite)
* Run SQL Queries CTRL+e, CTRL+e

* View table description CTRL+e, CTRL+d

* Show table records CTRL+e, CTRL+s

* Show explain plan for queries CTRL+e, CTRL+x
* Formatting SQL Queries CTRL+e, CTRL+b

* View Queries history CTRL+e, CTRL+h
* Save queries CTRL+e, CTRL+q
* List and Run saved queries CTRL+e, CTRL+l
* Remove saved queries CTRL+e, CTRL+r
* Threading support to prevent lockups
* Query timeout (kill thread if query takes too long)
## Installing
### Using Sublime Package Control
If you are using [Sublime Package Control](https://packagecontrol.io/packages/SQLTools), you can easily install SQLTools via the `Package Control: Install Package` menu item.
1. Press CTRL+SHIFT+p
2. Type *`Install Package`*
3. Find *`SQLTools`*
4. Wait & Done!
### Download Manually
I strongly recommend you to use Package Control. It helps you to keep the package updated with the last version.
1. Download the latest released zip file [here](https://github.com/mtxr/SublimeText-SQLTools/releases/latest)
2. Unzip the files and rename the folder to `SQLTools`
3. Find your `Packages` directory using the menu item `Preferences -> Browse Packages...`
4. Copy the folder into your Sublime Text `Packages` directory
### Using SQLTools with Mac OS X
Sublime Text has it's environment variable `PATH` set from launchctl, not by your shell. Binaries installed by packages such as homebrew, for instance `psql` DB CLI for `PostgreSQL`, cannot be found by Sublime Text and results in error in Sublime Text console by `SQLTools`. Installing the package `Fix Mac Path` or setting the full path to your DB CLI binary in `SQLTools.sublime-settings` resolves this issue. Package can be downloaded via [PackageControl](https://packagecontrol.io/packages/Fix%20Mac%20Path) or [github](https://github.com/int3h/SublimeFixMacPath).
## Contributors
This project exists thanks to all the people who [contribute](https://github.com/mtxr/SublimeText-SQLTools/graphs/contributors).
## Configuration
Documentation: [https://code.mteixeira.dev/SublimeText-SQLTools/](https://code.mteixeira.dev/SublimeText-SQLTools/)
================================================
FILE: SQLTools.py
================================================
__version__ = "v0.9.12"
import sys
import os
import re
import logging
from collections import OrderedDict
import sublime
from sublime_plugin import WindowCommand, EventListener, TextCommand
from Default.paragraph import expand_to_paragraph
from .SQLToolsAPI import Utils
from .SQLToolsAPI.Storage import Storage, Settings
from .SQLToolsAPI.Connection import Connection
from .SQLToolsAPI.History import History
from .SQLToolsAPI.Completion import Completion
MESSAGE_RUNNING_CMD = 'Executing SQL command...'
SYNTAX_PLAIN_TEXT = 'Packages/Text/Plain text.tmLanguage'
SYNTAX_SQL = 'Packages/SQL/SQL.tmLanguage'
SQLTOOLS_SETTINGS_FILE = 'SQLTools.sublime-settings'
SQLTOOLS_CONNECTIONS_FILE = 'SQLToolsConnections.sublime-settings'
SQLTOOLS_QUERIES_FILE = 'SQLToolsSavedQueries.sublime-settings'
USER_FOLDER = None
DEFAULT_FOLDER = None
SETTINGS_FILENAME = None
SETTINGS_FILENAME_DEFAULT = None
CONNECTIONS_FILENAME = None
CONNECTIONS_FILENAME_DEFAULT = None
QUERIES_FILENAME = None
QUERIES_FILENAME_DEFAULT = None
settingsStore = None
queriesStore = None
connectionsStore = None
historyStore = None
# create pluggin logger
DEFAULT_LOG_LEVEL = logging.WARNING
plugin_logger = logging.getLogger(__package__)
# some plugins are not playing by the rules and configure the root loger
plugin_logger.propagate = False
if not plugin_logger.handlers:
plugin_logger_handler = logging.StreamHandler()
plugin_logger_formatter = logging.Formatter("[{name}] {levelname}: {message}", style='{')
plugin_logger_handler.setFormatter(plugin_logger_formatter)
plugin_logger.addHandler(plugin_logger_handler)
plugin_logger.setLevel(DEFAULT_LOG_LEVEL)
logger = logging.getLogger(__name__)
def getSublimeUserFolder():
return os.path.join(sublime.packages_path(), 'User')
def startPlugin():
global USER_FOLDER, DEFAULT_FOLDER
global SETTINGS_FILENAME, SETTINGS_FILENAME_DEFAULT
global CONNECTIONS_FILENAME, CONNECTIONS_FILENAME_DEFAULT
global QUERIES_FILENAME, QUERIES_FILENAME_DEFAULT
global settingsStore, queriesStore, connectionsStore, historyStore
USER_FOLDER = getSublimeUserFolder()
DEFAULT_FOLDER = os.path.dirname(__file__)
SETTINGS_FILENAME = os.path.join(USER_FOLDER, SQLTOOLS_SETTINGS_FILE)
SETTINGS_FILENAME_DEFAULT = os.path.join(DEFAULT_FOLDER, SQLTOOLS_SETTINGS_FILE)
CONNECTIONS_FILENAME = os.path.join(USER_FOLDER, SQLTOOLS_CONNECTIONS_FILE)
CONNECTIONS_FILENAME_DEFAULT = os.path.join(DEFAULT_FOLDER, SQLTOOLS_CONNECTIONS_FILE)
QUERIES_FILENAME = os.path.join(USER_FOLDER, SQLTOOLS_QUERIES_FILE)
QUERIES_FILENAME_DEFAULT = os.path.join(DEFAULT_FOLDER, SQLTOOLS_QUERIES_FILE)
try:
settingsStore = Settings(SETTINGS_FILENAME, default=SETTINGS_FILENAME_DEFAULT)
except Exception as e:
msg = '{0}: Failed to parse {1} file'.format(__package__, SQLTOOLS_SETTINGS_FILE)
logging.exception(msg)
Window().status_message(msg)
try:
connectionsStore = Settings(CONNECTIONS_FILENAME, default=CONNECTIONS_FILENAME_DEFAULT)
except Exception as e:
msg = '{0}: Failed to parse {1} file'.format(__package__, SQLTOOLS_CONNECTIONS_FILE)
logging.exception(msg)
Window().status_message(msg)
queriesStore = Storage(QUERIES_FILENAME, default=QUERIES_FILENAME_DEFAULT)
historyStore = History(settingsStore.get('history_size', 100))
if settingsStore.get('debug', False):
plugin_logger.setLevel(logging.DEBUG)
else:
plugin_logger.setLevel(DEFAULT_LOG_LEVEL)
Connection.setTimeout(settingsStore.get('thread_timeout', 15))
Connection.setHistoryManager(historyStore)
logger.info('plugin (re)loaded')
logger.info('version %s', __version__)
def readConnections():
mergedConnections = {}
# fixes #39 and #45
if not connectionsStore:
startPlugin()
# global connections
globalConnectionsDict = connectionsStore.get('connections', {})
# project-specific connections
projectConnectionsDict = {}
projectData = Window().project_data()
if projectData:
projectConnectionsDict = projectData.get('connections', {})
# merge connections
mergedConnections = globalConnectionsDict.copy()
mergedConnections.update(projectConnectionsDict)
ordered = OrderedDict(sorted(mergedConnections.items()))
return ordered
def getDefaultConnectionName():
default = connectionsStore.get('default', False)
if not default:
return
return default
def createOutput(panel=None, syntax=None, prependText=None):
onInitialOutput = None
if not panel:
panel, onInitialOutput = getOutputPlace(syntax)
if prependText:
panel.run_command('append', {'characters': str(prependText)})
initial = True
def append(outputContent):
nonlocal initial
if initial:
initial = False
if onInitialOutput:
onInitialOutput()
# append content
panel.set_read_only(False)
panel.run_command('append', {'characters': outputContent})
panel.set_read_only(True)
return append
def toNewTab(content, name="", suffix="SQLTools Saved Query"):
resultContainer = Window().new_file()
resultContainer.set_name(
((name + " - ") if name != "" else "") + suffix)
resultContainer.set_syntax_file(SYNTAX_SQL)
resultContainer.run_command('append', {'characters': content})
def insertContent(content):
view = View()
# getting the settings local to this view/tab
viewSettings = view.settings()
# saving the original settings for "auto_indent", or True if none set
autoIndent = viewSettings.get('auto_indent', True)
# turn off automatic indenting otherwise the tabbing of the original
# string is not respected after a newline is encountered
viewSettings.set('auto_indent', False)
view.run_command('insert', {'characters': content})
# restore "auto_indent" setting
viewSettings.set('auto_indent', autoIndent)
def getOutputPlace(syntax=None, name="SQLTools Result"):
showResultOnWindow = settingsStore.get('show_result_on_window', False)
if not showResultOnWindow:
resultContainer = Window().find_output_panel(name)
if resultContainer is None:
resultContainer = Window().create_output_panel(name)
else:
resultContainer = None
views = Window().views()
for view in views:
if view.name() == name:
resultContainer = view
break
if not resultContainer:
resultContainer = Window().new_file()
resultContainer.set_name(name)
resultContainer.set_scratch(True) # avoids prompting to save
resultContainer.set_read_only(True)
resultContainer.settings().set("word_wrap", "false")
def onInitialOutputCallback():
if settingsStore.get('clear_output', False):
resultContainer.set_read_only(False)
resultContainer.run_command('select_all')
resultContainer.run_command('left_delete')
resultContainer.set_read_only(True)
# set custom syntax highlight, only if one was passed explicitly,
# otherwise use Plain Text syntax
if syntax:
# if custom and SQL related, use that, otherwise defaults to SQL
if 'sql' in syntax.lower():
resultContainer.set_syntax_file(syntax)
else:
resultContainer.set_syntax_file(SYNTAX_SQL)
else:
resultContainer.set_syntax_file(SYNTAX_PLAIN_TEXT)
# hide previously set command running message (if any)
Window().status_message('')
if not showResultOnWindow:
# if case this is an output pannel, show it
Window().run_command("show_panel", {"panel": "output." + name})
if settingsStore.get('focus_on_result', False):
Window().focus_view(resultContainer)
return resultContainer, onInitialOutputCallback
def getSelectionText():
text = []
selectionRegions = getSelectionRegions()
if not selectionRegions:
return text
for region in selectionRegions:
text.append(View().substr(region))
return text
def getSelectionRegions():
expandedRegions = []
if not View().sel():
return None
# If we would need to expand the empty selection, then which type:
# 'file', 'view' = use text of current view
# 'paragraph' = paragraph(s) (text between newlines)
# 'line' = current line(s)
expandTo = settingsStore.get('expand_to', 'file')
if not expandTo:
expandTo = 'file'
# keep compatibility with previous settings
expandToParagraph = settingsStore.get('expand_to_paragraph')
if expandToParagraph is True:
expandTo = 'paragraph'
expandTo = str(expandTo).strip()
if expandTo not in ['file', 'view', 'paragraph', 'line']:
expandTo = 'file'
for region in View().sel():
# if user did not select anything - expand selection,
# otherwise use the currently selected region
if region.empty():
if expandTo in ['file', 'view']:
region = sublime.Region(0, View().size())
# no point in further iterating over selections, just use entire file
return [region]
elif expandTo == 'paragraph':
region = expand_to_paragraph(View(), region.b)
else:
# expand to line
region = View().line(region)
# even if we could not expand, avoid adding empty regions
if not region.empty():
expandedRegions.append(region)
return expandedRegions
def getCurrentSyntax():
view = View()
currentSyntax = None
if view:
currentSyntax = view.settings().get('syntax')
return currentSyntax
class ST(EventListener):
connectionDict = None
conn = None
tables = []
columns = []
functions = []
completion = None
@staticmethod
def bootstrap():
ST.connectionDict = readConnections()
ST.setDefaultConnection()
@staticmethod
def setDefaultConnection():
default = getDefaultConnectionName()
if not default:
return
if default not in ST.connectionDict:
logger.error('connection "%s" set as default, but it does not exists', default)
return
logger.info('default connection is set to "%s"', default)
ST.setConnection(default)
@staticmethod
def setConnection(connectionName, callback=None):
if not connectionName:
return
if connectionName not in ST.connectionDict:
return
settings = settingsStore.all()
config = ST.connectionDict.get(connectionName)
promptKeys = [key for key, value in config.items() if value is None]
promptDict = {}
logger.info('[setConnection] prompt keys {}'.format(promptKeys))
def mergeConfig(config, promptedKeys=None):
merged = config.copy()
if promptedKeys:
merged.update(promptedKeys)
return merged
def createConnection(connectionName, config, settings, callback=None):
# if DB cli binary could not be found in path a FileNotFoundError is thrown
try:
ST.conn = Connection(connectionName, config, settings=settings)
except FileNotFoundError as e:
# use only first line of the Exception in status message
Window().status_message(__package__ + ": " + str(e).splitlines()[0])
raise e
ST.loadConnectionData(callback)
if not promptKeys:
createConnection(connectionName, config, settings, callback)
return
def setMissingKey(key, value):
nonlocal promptDict
if value is None:
return
promptDict[key] = value
if promptKeys:
promptNext()
else:
merged = mergeConfig(config, promptDict)
createConnection(connectionName, merged, settings, callback)
def promptNext():
nonlocal promptKeys
if not promptKeys:
merged = mergeConfig(config, promptDict)
createConnection(connectionName, merged, settings, callback)
key = promptKeys.pop();
Window().show_input_panel(
'Connection ' + key,
'',
lambda userInput: setMissingKey(key, userInput),
None,
None)
promptNext()
@staticmethod
def loadConnectionData(callback=None):
# clear the list of identifiers (in case connection is changed)
ST.tables = []
ST.columns = []
ST.functions = []
ST.completion = None
objectsLoaded = 0
if not ST.conn:
return
def afterAllDataHasLoaded():
ST.completion = Completion(ST.tables, ST.columns, ST.functions, settings=settingsStore)
logger.info('completions loaded')
if (callback):
callback()
def tablesCallback(tables):
ST.tables = tables
nonlocal objectsLoaded
objectsLoaded += 1
logger.info('loaded tables : "{0}"'.format(tables))
if objectsLoaded == 3:
afterAllDataHasLoaded()
def columnsCallback(columns):
ST.columns = columns
nonlocal objectsLoaded
objectsLoaded += 1
logger.info('loaded columns : "{0}"'.format(columns))
if objectsLoaded == 3:
afterAllDataHasLoaded()
def functionsCallback(functions):
ST.functions = functions
nonlocal objectsLoaded
objectsLoaded += 1
logger.info('loaded functions: "{0}"'.format(functions))
if objectsLoaded == 3:
logger.info('all objects loaded')
afterAllDataHasLoaded()
ST.conn.getTables(tablesCallback)
ST.conn.getColumns(columnsCallback)
ST.conn.getFunctions(functionsCallback)
@staticmethod
def selectConnectionQuickPanel(callback=None):
ST.connectionDict = readConnections()
if len(ST.connectionDict) == 0:
sublime.message_dialog('You need to setup your connections first.')
return
def connectionMenuList(connDictionary):
menuItemsList = []
template = '{dbtype}://{user}{host}{port}{db}'
for name, config in ST.connectionDict.items():
dbtype = config.get('type', '')
user = '{}@'.format(config.get('username', '')) if 'username' in config else ''
# user = config.get('username', '')
host=config.get('host', '')
port = ':{}'.format(config.get('port', '')) if 'port' in config else ''
db = '/{}'.format(config.get('database', '')) if 'database' in config else ''
connectionInfo = template.format(
dbtype=dbtype,
user=user,
host=host,
port=port,
db=db)
menuItemsList.append([name, connectionInfo])
menuItemsList.sort()
return menuItemsList
def onConnectionSelected(index, callback):
menuItemsList = connectionMenuList(ST.connectionDict)
if index < 0 or index >= len(menuItemsList):
return
connectionName = menuItemsList[index][0]
ST.setConnection(connectionName, callback)
logger.info('Connection "{0}" selected'.format(connectionName))
menu = connectionMenuList(ST.connectionDict)
# show pannel with callback above
Window().show_quick_panel(menu, lambda index: onConnectionSelected(index, callback))
@staticmethod
def showTablesQuickPanel(callback):
if len(ST.tables) == 0:
sublime.message_dialog('Your database has no tables.')
return
ST.showQuickPanelWithSelection(ST.tables, callback)
@staticmethod
def showFunctionsQuickPanel(callback):
if len(ST.functions) == 0:
sublime.message_dialog('Your database has no functions.')
return
ST.showQuickPanelWithSelection(ST.functions, callback)
@staticmethod
def showQuickPanelWithSelection(arrayOfValues, callback):
w = Window();
view = w.active_view()
selection = view.sel()[0]
initialText = ''
# ignore obvious non-identifier selections
if selection.size() <= 128:
(row_begin,_) = view.rowcol(selection.begin())
(row_end,_) = view.rowcol(selection.end())
# only consider selections within same line
if row_begin == row_end:
initialText = view.substr(selection)
w.show_quick_panel(arrayOfValues, callback)
w.run_command('insert', {'characters': initialText})
w.run_command("select_all")
@staticmethod
def on_query_completions(view, prefix, locations):
# skip completions, if no connection
if ST.conn is None:
return None
if ST.completion is None:
return None
if ST.completion.isDisabled():
return None
if not len(locations):
return None
ignoreSelectors = ST.completion.getIgnoreSelectors()
if ignoreSelectors:
for selector in ignoreSelectors:
if view.match_selector(locations[0], selector):
return None
activeSelectors = ST.completion.getActiveSelectors()
if activeSelectors:
for selector in activeSelectors:
if view.match_selector(locations[0], selector):
break
else:
return None
# sublimePrefix = prefix
# sublimeCompletions = view.extract_completions(sublimePrefix, locations[0])
# preferably get prefix ourselves instead of using default sublime "prefix".
# Sublime will return only last portion of this preceding text. Given:
# SELECT table.col|
# sublime will return: "col", and we need: "table.col"
# to know more precisely which completions are more appropriate
# get a Region that starts at the beginning of current line
# and ends at current cursor position
currentPoint = locations[0]
lineStartPoint = view.line(currentPoint).begin()
lineStartToLocation = sublime.Region(lineStartPoint, currentPoint)
try:
lineStr = view.substr(lineStartToLocation)
prefix = re.split('[^`\"\w.\$]+', lineStr).pop()
except Exception as e:
logger.debug(e)
# use current paragraph as sql text to parse
sqlRegion = expand_to_paragraph(view, currentPoint)
sql = view.substr(sqlRegion)
sqlToCursorRegion = sublime.Region(sqlRegion.begin(), currentPoint)
sqlToCursor = view.substr(sqlToCursorRegion)
# get completions
autoCompleteList, inhibit = ST.completion.getAutoCompleteList(prefix, sql, sqlToCursor)
# safe check here, so even if we return empty completions and inhibit is true
# we return empty completions to show default sublime completions
if autoCompleteList is None or len(autoCompleteList) == 0:
return None
if inhibit:
return (autoCompleteList, sublime.INHIBIT_WORD_COMPLETIONS)
return autoCompleteList
# #
# # Commands
# #
# Usage for old keybindings defined by users
class StShowConnectionMenu(WindowCommand):
@staticmethod
def run():
Window().run_command('st_select_connection')
class StSelectConnection(WindowCommand):
@staticmethod
def run():
ST.selectConnectionQuickPanel()
class StShowRecords(WindowCommand):
@staticmethod
def run():
if not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_show_records'))
return
def onTableSelected(index):
if index < 0:
return None
Window().status_message(MESSAGE_RUNNING_CMD)
tableName = ST.tables[index]
prependText = 'Table "{tableName}"\n'.format(tableName=tableName)
return ST.conn.getTableRecords(
tableName,
createOutput(prependText=prependText))
ST.showTablesQuickPanel(callback=onTableSelected)
class StDescTable(WindowCommand):
@staticmethod
def run():
currentSyntax = getCurrentSyntax()
if not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_desc_table'))
return
def onTableSelected(index):
if index < 0:
return None
Window().status_message(MESSAGE_RUNNING_CMD)
return ST.conn.getTableDescription(ST.tables[index], createOutput(syntax=currentSyntax))
ST.showTablesQuickPanel(callback=onTableSelected)
class StDescFunction(WindowCommand):
@staticmethod
def run():
currentSyntax = getCurrentSyntax()
if not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_desc_function'))
return
def onFunctionSelected(index):
if index < 0:
return None
Window().status_message(MESSAGE_RUNNING_CMD)
functionName = ST.functions[index].split('(', 1)[0]
return ST.conn.getFunctionDescription(functionName, createOutput(syntax=currentSyntax))
# get everything until first occurrence of "(", e.g. get "function_name"
# from "function_name(int)"
ST.showFunctionsQuickPanel(callback=onFunctionSelected)
class StRefreshConnectionData(WindowCommand):
@staticmethod
def run():
if not ST.conn:
return
ST.loadConnectionData()
class StExplainPlan(WindowCommand):
@staticmethod
def run():
if not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_explain_plan'))
return
Window().status_message(MESSAGE_RUNNING_CMD)
ST.conn.explainPlan(getSelectionText(), createOutput())
class StExecute(WindowCommand):
@staticmethod
def run():
if not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_execute'))
return
Window().status_message(MESSAGE_RUNNING_CMD)
ST.conn.execute(getSelectionText(), createOutput())
class StExecuteAll(WindowCommand):
@staticmethod
def run():
if not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_execute_all'))
return
Window().status_message(MESSAGE_RUNNING_CMD)
allText = View().substr(sublime.Region(0, View().size()))
ST.conn.execute(allText, createOutput())
class StFormat(TextCommand):
@staticmethod
def run(edit):
selectionRegions = getSelectionRegions()
if not selectionRegions:
return
for region in selectionRegions:
textToFormat = View().substr(region)
View().replace(edit, region, Utils.formatSql(textToFormat, settingsStore.get('format', {})))
class StFormatAll(TextCommand):
@staticmethod
def run(edit):
region = sublime.Region(0, View().size())
textToFormat = View().substr(region)
View().replace(edit, region, Utils.formatSql(textToFormat, settingsStore.get('format', {})))
class StVersion(WindowCommand):
@staticmethod
def run():
sublime.message_dialog('Using {0} {1}'.format(__package__, __version__))
class StHistory(WindowCommand):
@staticmethod
def run():
if not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_history'))
return
if len(historyStore.all()) == 0:
sublime.message_dialog('History is empty.')
return
def cb(index):
if index < 0:
return None
return ST.conn.execute(historyStore.get(index), createOutput())
Window().show_quick_panel(historyStore.all(), cb)
class StSaveQuery(WindowCommand):
@staticmethod
def run():
query = getSelectionText()
def cb(alias):
queriesStore.add(alias, query)
Window().show_input_panel('Query alias', '', cb, None, None)
class StListQueries(WindowCommand):
@staticmethod
def run(mode="run"):
if mode == "run" and not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_list_queries',
{'mode': mode}))
return
queriesList = queriesStore.all()
if len(queriesList) == 0:
sublime.message_dialog('No saved queries.')
return
options = []
for alias, query in queriesList.items():
options.append([str(alias), str(query)])
options.sort()
def cb(index):
if index < 0:
return None
alias, query = options[index]
if mode == "run":
ST.conn.execute(query, createOutput())
elif mode == "insert":
insertContent(query)
else:
toNewTab(query, alias)
return
try:
Window().show_quick_panel(options, cb)
except Exception:
pass
class StRemoveSavedQuery(WindowCommand):
@staticmethod
def run():
if not ST.conn:
ST.selectConnectionQuickPanel(callback=lambda: Window().run_command('st_remove_saved_query'))
return
queriesList = queriesStore.all()
if len(queriesList) == 0:
sublime.message_dialog('No saved queries.')
return
options = []
for alias, query in queriesList.items():
options.append([str(alias), str(query)])
options.sort()
def cb(index):
if index < 0:
return None
return queriesStore.delete(options[index][0])
try:
Window().show_quick_panel(options, cb)
except Exception:
pass
def Window():
return sublime.active_window()
def View():
return Window().active_view()
def reload():
try:
# python 3.0 to 3.3
import imp
imp.reload(sys.modules[__package__ + ".SQLToolsAPI"])
imp.reload(sys.modules[__package__ + ".SQLToolsAPI.Utils"])
imp.reload(sys.modules[__package__ + ".SQLToolsAPI.Completion"])
imp.reload(sys.modules[__package__ + ".SQLToolsAPI.Storage"])
imp.reload(sys.modules[__package__ + ".SQLToolsAPI.History"])
imp.reload(sys.modules[__package__ + ".SQLToolsAPI.Command"])
imp.reload(sys.modules[__package__ + ".SQLToolsAPI.Connection"])
except Exception as e:
raise (e)
try:
ST.bootstrap()
except Exception:
pass
def plugin_loaded():
try:
from package_control import events
if events.install(__name__):
logger.info('Installed %s!' % events.install(__name__))
elif events.post_upgrade(__name__):
logger.info('Upgraded to %s!' % events.post_upgrade(__name__))
sublime.message_dialog(('{0} was upgraded.' +
'If you have any problem,' +
'just restart your Sublime Text.'
).format(__name__)
)
except Exception:
pass
startPlugin()
reload()
def plugin_unloaded():
if plugin_logger.handlers:
plugin_logger.handlers.pop()
================================================
FILE: SQLTools.sublime-settings
================================================
// NOTE: it is strongly advised to override ONLY those settings
// that you wish to change in your Users/SQLTools.sublime-settings.
// Don't copy-paste the entire config.
{
/**
* If DB cli binary is not in PATH, set the full path in "cli" section.
* Note: forward slashes ("/") should be used in path. Example:
* "mysql" : "c:/Program Files/MySQL/MySQL Server 5.7/bin/mysql.exe"
*/
"cli": {
"mysql" : "mysql",
"pgsql" : "psql",
"mssql" : "sqlcmd",
"oracle" : "sqlplus",
"sqlite" : "sqlite3",
"vertica" : "vsql",
"firebird": "isql",
"sqsh" : "sqsh",
"snowsql" : "snowsql"
},
// If there is no SQL selected, use "expanded" region.
// Possible options:
// "file" - entire file contents
// "paragraph" - text between newlines relative to cursor(s)
// "line" - current line of cursor(s)
"expand_to": "file",
// puts results either in output panel or new window
"show_result_on_window": false,
// focus on result panel
"focus_on_result": false,
// clears the output of previous query
"clear_output": true,
// query timeout in seconds
"thread_timeout": 15,
// stream the output line by line
"use_streams": false,
// number of queries to save in the history
"history_size": 100,
"show_records": {
"limit": 50
},
// unless false, appends LIMIT clause to SELECT statements (not compatible with all DB's)
"safe_limit": false,
"debug": false,
/**
* Print the queries that were executed to the output.
* Possible values for show_query: "top", "bottom", true ("top"), or false (disable)
* When using regular output, this will determine where query details is displayed.
* In stream output mode, any option that isn't false will print query details at
* the bottom of result.
*/
"show_query": false,
/**
* Possible values for autocompletion: "basic", "smart", true ("smart"),
* or false (disable)
* Completion keywords case is controlled by format.keyword_case (see below)
*/
"autocompletion": "smart",
// Settings used for formatting the queries and autocompletions
//
// "keyword_case" , "upper", "lower", "capitalize" and null (leaves case intact)
// "identifier_case", "upper", "lower", "capitalize" and null (leaves case intact)
// "strip_comments" , formatting removes comments
// "indent_tabs" , use tabs instead of spaces
// "indent_width" , indentation width
// "reindent" , reindent code
"format": {
"keyword_case" : "upper",
"identifier_case" : null,
"strip_comments" : false,
"indent_tabs" : false,
"indent_width" : 4,
"reindent" : true
},
/**
* The list of syntax selectors for which the autocompletion will be active.
* An empty list means autocompletion always active.
*/
"autocomplete_selectors_active": [
"source.sql",
"source.pgsql",
"source.plpgsql.postgres",
"source.plsql.oracle",
"source.tsql"
],
/**
* The list of syntax selectors for which the autocompletion will be disabled.
*/
"autocomplete_selectors_ignore": [
"string.quoted.single.sql",
"string.quoted.single.pgsql",
"string.quoted.single.postgres",
"string.quoted.single.oracle",
"string.group.quote.oracle"
],
/**
* Command line interface options for each RDBMS.
* In this file, the section `cli` above has the names you can use here.
* E.g.: "mysql", "pgsql", "oracle"
*
* Names in the curly brackets (e.g. `{host}`) in sections `args`, `args_optional`,
* `env`, `env_optional` are replaced by the values specified in the connection.
*/
"cli_options": {
"pgsql": {
"options": ["--no-password"],
"before": [],
"after": [],
"args": "-d {database}",
"args_optional": [
"-h {host}",
"-p {port}",
"-U {username}"
],
"env_optional": {
"PGPASSWORD": "{password}"
},
"queries": {
"execute": {
"options": []
},
"show records": {
"query": "select * from {0} limit {1};",
"options": []
},
"desc table": {
"query": "\\d+ {0}",
"options": []
},
"desc function": {
"query": "\\sf {0}",
"options": []
},
"explain plan": {
"query": "explain analyze {0};",
"options": []
},
"desc": {
"query": "select quote_ident(table_schema) || '.' || quote_ident(table_name) as tblname from information_schema.tables where table_schema not in ('pg_catalog', 'information_schema') order by table_schema = current_schema() desc, table_schema, table_name;",
"options": ["--tuples-only", "--no-psqlrc"]
},
"columns": {
"query": "select distinct quote_ident(table_name) || '.' || quote_ident(column_name) from information_schema.columns where table_schema not in ('pg_catalog', 'information_schema');",
"options": ["--tuples-only", "--no-psqlrc"]
},
"functions": {
"query": "select quote_ident(n.nspname) || '.' || quote_ident(f.proname) || '(' || pg_get_function_identity_arguments(f.oid) || ')' as funname from pg_catalog.pg_proc as f inner join pg_catalog.pg_namespace as n on n.oid = f.pronamespace where n.nspname not in ('pg_catalog', 'information_schema');",
"options": ["--tuples-only", "--no-psqlrc"]
}
}
},
"mysql": {
"options": ["--no-auto-rehash", "--compress"],
"before": [],
"after": [],
"args": "-D\"{database}\"",
"args_optional": [
"--login-path=\"{login-path}\"",
"--defaults-extra-file=\"{defaults-extra-file}\"",
"--default-character-set=\"{default-character-set}\"",
"-h\"{host}\"",
"-P{port}",
"-u\"{username}\""
],
"env_optional": {
"MYSQL_PWD": "{password}"
},
"queries": {
"execute": {
"options": ["--table"]
},
"show records": {
"query": "select * from {0} limit {1};",
"options": ["--table"]
},
"desc table": {
"query": "desc {0};",
"options": ["--table"]
},
"desc function": {
"query": "select routine_definition from information_schema.routines where concat(case when routine_schema REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_schema,'`') else routine_schema end, '.', case when routine_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_name,'`') else routine_name end) = '{0}';",
"options": ["--silent", "--raw"]
},
"explain plan": {
"query": "explain {0};",
"options": ["--table"]
},
"desc" : {
"query": "select concat(case when table_schema REGEXP '[^0-9a-zA-Z$_]' then concat('`',table_schema,'`') else table_schema end, '.', case when table_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',table_name,'`') else table_name end) as obj from information_schema.tables where table_schema = database() order by table_name;",
"options": ["--silent", "--raw", "--skip-column-names"]
},
"columns": {
"query": "select concat(case when table_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',table_name,'`') else table_name end, '.', case when column_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',column_name,'`') else column_name end) as obj from information_schema.columns where table_schema = database() order by table_name, ordinal_position;",
"options": ["--silent", "--raw", "--skip-column-names"]
},
"functions": {
"query": "select concat(case when routine_schema REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_schema,'`') else routine_schema end, '.', case when routine_name REGEXP '[^0-9a-zA-Z$_]' then concat('`',routine_name,'`') else routine_name end, '()') as obj from information_schema.routines where routine_schema = database();",
"options": ["--silent", "--raw", "--skip-column-names"]
}
}
},
"mssql": {
"options": [],
"before": [],
"after": ["go", "quit"],
"args": "-d \"{database}\"",
"args_optional": ["-S \"{host},{port}\"", "-S \"{host}\\{instance}\"", "-U \"{username}\"", "-P \"{password}\""],
"queries": {
"execute": {
"options": ["-k"]
},
"show records": {
"query": "select top {1} * from {0};",
"options": []
},
"desc table": {
"query": "exec sp_help N'{0}';",
"options": ["-y30", "-Y30"]
},
"desc function": {
"query": "exec sp_helptext N'{0}';",
"options": ["-h-1"]
},
"explain plan": {
"query": "{0};",
"options": ["-k"],
"before": [
"SET STATISTICS PROFILE ON",
"SET STATISTICS IO ON",
"SET STATISTICS TIME ON"
]
},
"desc": {
"query": "set nocount on; select concat(table_schema, '.', table_name) as obj from information_schema.tables order by table_schema, table_name;",
"options": ["-h-1", "-r1"]
},
"columns": {
"query": "set nocount on; select distinct concat(table_name, '.', column_name) as obj from information_schema.columns;",
"options": ["-h-1", "-r1"]
},
"functions": {
"query": "set nocount on; select concat(routine_schema, '.', routine_name) as obj from information_schema.routines order by routine_schema, routine_name;",
"options": ["-h-1", "-r1"]
}
}
},
"oracle": {
"options": ["-S"],
"before": [
"SET LINESIZE 32767",
"SET WRAP OFF",
"SET PAGESIZE 0",
"SET EMBEDDED ON",
"SET TRIMOUT ON",
"SET TRIMSPOOL ON",
"SET TAB OFF",
"SET SERVEROUT ON",
"SET NULL '@'",
"SET COLSEP '|'",
"SET SQLBLANKLINES ON"
],
"after": [],
"env_optional": {
"NLS_LANG": "{nls_lang}"
},
"args": "{username}/{password}@\"(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={host})(PORT={port})))(CONNECT_DATA=(SERVICE_NAME={service})))\"",
"queries": {
"execute": {
"options": [],
"before": [
// "SET TIMING ON",
"SET FEEDBACK ON"
]
},
"show records": {
"query": "select * from {0} where rownum <= {1};",
"options": [],
"before": [
"SET FEEDBACK ON"
]
},
"desc table": {
"query": "desc {0};",
"options": [],
"before": [
"SET LINESIZE 80", // override for readability
"SET WRAP ON", // override for readability
"SET FEEDBACK ON"
]
},
"desc function": {
"query": "select text from all_source where type in ('FUNCTION', 'PROCEDURE', 'PACKAGE', 'PACKAGE BODY') and name = nvl(substr(ltrim('{0}', sys_context('USERENV', 'CURRENT_SCHEMA') || '.' ), 0, instr(ltrim('{0}', sys_context('USERENV', 'CURRENT_SCHEMA') || '.' ), '.')-1), ltrim('{0}', sys_context('USERENV', 'CURRENT_SCHEMA') || '.' )) and owner = sys_context('USERENV', 'CURRENT_SCHEMA') order by type, line;",
"options": [],
"before": [
"SET SERVEROUT OFF", // override
"SET NULL ''", // override
"SET HEADING OFF",
"SET FEEDBACK OFF"
]
},
"explain plan": {
"query": "explain plan for {0};\nselect plan_table_output from table(dbms_xplan.display());",
"options": [],
"before": [
"SET FEEDBACK ON"
]
},
"desc" : {
"query": "select owner || '.' || case when upper(name) = name then name else chr(34) || name || chr(34) end as obj from (select owner, table_name as name from all_tables union all select owner, view_name as name from all_views) o where owner not in ('ANONYMOUS','APPQOSSYS','CTXSYS','DBSNMP','EXFSYS', 'LBACSYS', 'MDSYS','MGMT_VIEW','OLAPSYS','OWBSYS','ORDPLUGINS', 'ORDSYS','OUTLN', 'SI_INFORMTN_SCHEMA','SYS','SYSMAN','SYSTEM', 'TSMSYS','WK_TEST','WKSYS', 'WKPROXY','WMSYS','XDB','APEX_040000', 'APEX_PUBLIC_USER','DIP', 'FLOWS_30000','FLOWS_FILES','MDDATA', 'ORACLE_OCM','SPATIAL_CSW_ADMIN_USR', 'SPATIAL_WFS_ADMIN_USR', 'XS$NULL','PUBLIC');",
"options": [],
"before": [
"SET SERVEROUT OFF", // override
"SET NULL ''", // override
"SET HEADING OFF",
"SET FEEDBACK OFF"
]
},
"columns": {
"query": "select case when upper(table_name) = table_name then table_name else chr(34) || table_name || chr(34) end || '.' || case when upper(column_name) = column_name then column_name else chr(34) || column_name || chr(34) end as obj from (select c.table_name, c.column_name, t.owner from all_tab_columns c inner join all_tables t on c.owner = t.owner and c.table_name = t.table_name union all select c.table_name, c.column_name, t.owner from all_tab_columns c inner join all_views t on c.owner = t.owner and c.table_name = t.view_name) o where owner not in ('ANONYMOUS','APPQOSSYS','CTXSYS','DBSNMP','EXFSYS', 'LBACSYS', 'MDSYS','MGMT_VIEW','OLAPSYS','OWBSYS','ORDPLUGINS', 'ORDSYS','OUTLN', 'SI_INFORMTN_SCHEMA','SYS','SYSMAN','SYSTEM', 'TSMSYS','WK_TEST','WKSYS', 'WKPROXY','WMSYS','XDB','APEX_040000', 'APEX_PUBLIC_USER','DIP', 'FLOWS_30000','FLOWS_FILES','MDDATA', 'ORACLE_OCM','SPATIAL_CSW_ADMIN_USR', 'SPATIAL_WFS_ADMIN_USR', 'XS$NULL','PUBLIC');",
"options": [],
"before": [
"SET SERVEROUT OFF", // override
"SET NULL ''", // override
"SET HEADING OFF",
"SET FEEDBACK OFF"
]
},
"functions": {
"query": "select case when object_type = 'PACKAGE' then object_name||'.'||procedure_name else owner || '.' || object_name end || '()' as obj from all_procedures where object_type in ('FUNCTION','PROCEDURE','PACKAGE') and not (object_type = 'PACKAGE' and procedure_name is null) and owner = sys_context('USERENV', 'CURRENT_SCHEMA');",
"options": [],
"before": [
"SET SERVEROUT OFF", // override
"SET NULL ''", // override
"SET HEADING OFF",
"SET FEEDBACK OFF"
]
}
}
},
"sqlite": {
"options": [],
"before": [],
"after": [],
"args": "\"{database}\"",
"queries": {
"execute": {
"options": ["-column", "-header", "-bail"]
},
"show records": {
"query": "select * from \"{0}\" limit {1};",
"options": ["-column", "-header"]
},
"explain plan": {
"query": "EXPLAIN QUERY PLAN {0};",
"options": ["-column", "-header", "-bail"]
},
"desc table": {
"query": ".schema \"{0}\"",
"options": ["-column", "-header"]
},
"desc" : {
"query": "SELECT name FROM sqlite_master WHERE type='table';",
"options": ["-noheader"],
"before": [
".headers off"
]
}
}
},
"vertica": {
"options": [],
"before" : [],
"after": [],
"args": "-h \"{host}\" -p {port} -U \"{username}\" -w \"{password}\" -d \"{database}\"",
"queries": {
"execute": {
"options": []
},
"show records": {
"query": "select * from {0} limit {1};",
"options": []
},
"desc table": {
"query": "\\d {0}",
"options": []
},
"explain plan": {
"query": "explain {0};",
"options": []
},
"desc" : {
"query": "select quote_ident(table_schema) || '.' || quote_ident(table_name) as obj from v_catalog.tables where is_system_table = false;",
"options": ["--tuples-only", "--no-vsqlrc"]
},
"columns": {
"query": "select quote_ident(table_name) || '.' || quote_ident(column_name) as obj from v_catalog.columns where is_system_table = false order by table_name, ordinal_position;",
"options": ["--tuples-only", "--no-vsqlrc"]
}
}
},
"firebird": {
"options": [],
"before": [],
"after": [],
"args": "-pagelength 10000 -u \"{username}\" -p \"{password}\" \"{host}/{port}:{database}\"",
"queries": {
"execute": {
"options": [],
"before": [
"set bail on;",
"set count on;"
]
},
"show records": {
"query": "select first {1} * from {0};",
"options": [],
"before": [
"set bail off;",
"set count on;"
]
},
"desc table": {
"query": "show table {0}; \n show view {0};",
"before": [
"set bail off;"
]
},
"desc function": {
"query": "show procedure {0};",
"before": [
"set bail off;"
]
},
"explain plan": {
"query": "{0};",
"before": [
"set bail on;",
"set count on;",
"set plan on;",
"set stats on;"
]
},
"desc" : {
"query": "select case when upper(rdb$relation_name) = rdb$relation_name then trim(rdb$relation_name) else '\"' || trim(rdb$relation_name) || '\"' end as obj from rdb$relations where (rdb$system_flag is null or rdb$system_flag = 0);",
"before": [
"set bail off;",
"set heading off;",
"set count off;",
"set stats off;"
]
},
"columns": {
"query": "select case when upper(f.rdb$relation_name) = f.rdb$relation_name then trim(f.rdb$relation_name) else '\"' || trim(f.rdb$relation_name) || '\"' end || '.' || case when upper(f.rdb$field_name) = f.rdb$field_name then trim(f.rdb$field_name) else '\"' || trim(f.rdb$field_name) || '\"' end as obj from rdb$relation_fields f join rdb$relations r on f.rdb$relation_name = r.rdb$relation_name where (r.rdb$system_flag is null or r.rdb$system_flag = 0) order by f.rdb$relation_name, f.rdb$field_position;",
"before": [
"set bail off;",
"set heading off;",
"set count off;",
"set stats off;"
]
},
"functions": {
"query": "select case when upper(rdb$procedure_name) = rdb$procedure_name then trim(rdb$procedure_name) else '\"' || trim(rdb$procedure_name) || '\"' end || '()' as obj from rdb$procedures where (rdb$system_flag is null or rdb$system_flag = 0);",
"before": [
"set bail off;",
"set heading off;",
"set count off;",
"set stats off;"
]
}
}
},
"sqsh": {
"options": [],
"before": [],
"after": [],
"args": "-S {host}:{port} -U\"{username}\" -P\"{password}\" -D{database}",
"queries": {
"execute": {
"options": [],
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l\""]
},
"desc": {
"query": "select concat(table_schema, '.', table_name) from information_schema.tables order by table_name;",
"options": [],
"before" :["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
},
"columns": {
"query": "select concat(table_name, '.', column_name) from information_schema.columns order by table_name, ordinal_position;",
"options": [],
"before" :["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
},
"desc table": {
"query": "exec sp_columns \"{0}\";",
"options": [],
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l -h -f\""]
},
"show records": {
"query": "select top {1} * from \"{0}\";",
"options": [],
"before": ["\\set semicolon_cmd=\"\\go -mpretty -l\""]
}
}
},
"snowsql": {
"options": [],
"env_optional": {
"SNOWSQL_PWD": "{password}"
},
"args": "-u {user} -a {account} -d {database} --authenticator {auth} -K",
"queries": {
"execute": {
"options": [],
},
"show records": {
"query": "SELECT * FROM {0} LIMIT {1};"
},
"desc table": {
"query": "DESC TABLE {0};",
"options": ["-o", "output_format=plain",
"-o", "timing=False"]
},
"desc": {
"query": "SELECT TABLE_SCHEMA || '.' || TABLE_NAME AS tbl FROM INFORMATION_SCHEMA.TABLES ORDER BY tbl;",
"options": ["-o", "output_format=plain",
"-o", "header=False",
"-o", "timing=False"]
},
"columns": {
"query": "SELECT TABLE_NAME || '.' || COLUMN_NAME AS col FROM INFORMATION_SCHEMA.COLUMNS ORDER BY col;",
"options": ["-o", "output_format=plain",
"-o", "header=False",
"-o", "timing=False"]
}
}
}
}
}
================================================
FILE: SQLToolsAPI/Command.py
================================================
import os
import signal
import subprocess
import time
import logging
from threading import Thread, Timer
logger = logging.getLogger(__name__)
class Command(object):
timeout = 15
def __init__(self, args, env, callback, query=None, encoding='utf-8',
options=None, timeout=15, silenceErrors=False, stream=False):
if options is None:
options = {}
self.args = args
self.env = env
self.callback = callback
self.query = query
self.encoding = encoding
self.options = options
self.timeout = timeout
self.silenceErrors = silenceErrors
self.stream = stream
self.process = None
if 'show_query' not in self.options:
self.options['show_query'] = False
elif self.options['show_query'] not in ['top', 'bottom']:
self.options['show_query'] = 'top' if (isinstance(self.options['show_query'], bool) and
self.options['show_query']) else False
def run(self):
if not self.query:
return
self.args = map(str, self.args)
si = None
if os.name == 'nt':
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# select appropriate file handle for stderr
# usually we want to redirect stderr to stdout, so erros are shown
# in the output in the right place (where they actually occurred)
# only if silenceErrors=True, we separate stderr from stdout and discard it
stderrHandle = subprocess.STDOUT
if self.silenceErrors:
stderrHandle = subprocess.PIPE
# set the environment
modifiedEnvironment = os.environ.copy()
if (self.env):
modifiedEnvironment.update(self.env)
queryTimerStart = time.time()
self.process = subprocess.Popen(self.args,
stdout=subprocess.PIPE,
stderr=stderrHandle,
stdin=subprocess.PIPE,
env=modifiedEnvironment,
startupinfo=si)
if self.stream:
self.process.stdin.write(self.query.encode(self.encoding))
self.process.stdin.close()
hasWritten = False
for line in self.process.stdout:
self.callback(line.decode(self.encoding, 'replace').replace('\r', ''))
hasWritten = True
queryTimerEnd = time.time()
# we are done with the output, terminate the process
if self.process:
self.process.terminate()
else:
if hasWritten:
self.callback('\n')
if self.options['show_query']:
formattedQueryInfo = self._formatShowQuery(self.query, queryTimerStart, queryTimerEnd)
self.callback(formattedQueryInfo + '\n')
return
# regular mode is handled with more reliable Popen.communicate
# which also terminates the process afterwards
results, errors = self.process.communicate(input=self.query.encode(self.encoding))
queryTimerEnd = time.time()
resultString = ''
if results:
resultString += results.decode(self.encoding,
'replace').replace('\r', '')
if errors and not self.silenceErrors:
resultString += errors.decode(self.encoding,
'replace').replace('\r', '')
if self.process is None and resultString != '':
resultString += '\n'
if self.options['show_query']:
formattedQueryInfo = self._formatShowQuery(self.query, queryTimerStart, queryTimerEnd)
queryPlacement = self.options['show_query']
if queryPlacement == 'top':
resultString = "{0}\n{1}".format(formattedQueryInfo, resultString)
elif queryPlacement == 'bottom':
resultString = "{0}{1}\n".format(resultString, formattedQueryInfo)
self.callback(resultString)
@staticmethod
def _formatShowQuery(query, queryTimeStart, queryTimeEnd):
resultInfo = "/*\n-- Executed querie(s) at {0} took {1:.3f} s --".format(
str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(queryTimeStart))),
(queryTimeEnd - queryTimeStart))
resultLine = "-" * (len(resultInfo) - 3)
resultString = "{0}\n{1}\n{2}\n{3}\n*/".format(
resultInfo, resultLine, query, resultLine)
return resultString
@staticmethod
def createAndRun(args, env, callback, query=None, encoding='utf-8',
options=None, timeout=15, silenceErrors=False, stream=False):
if options is None:
options = {}
command = Command(args=args,
env=env,
callback=callback,
query=query,
encoding=encoding,
options=options,
timeout=timeout,
silenceErrors=silenceErrors,
stream=stream)
command.run()
class ThreadCommand(Command, Thread):
def __init__(self, args, env, callback, query=None, encoding='utf-8',
options=None, timeout=Command.timeout, silenceErrors=False, stream=False):
if options is None:
options = {}
Command.__init__(self,
args=args,
env=env,
callback=callback,
query=query,
encoding=encoding,
options=options,
timeout=timeout,
silenceErrors=silenceErrors,
stream=stream)
Thread.__init__(self)
def stop(self):
if not self.process:
return
# if poll returns None - proc still running, otherwise returns process return code
if self.process.poll() is not None:
return
try:
# Windows does not provide SIGKILL, go with SIGTERM
sig = getattr(signal, 'SIGKILL', signal.SIGTERM)
os.kill(self.process.pid, sig)
self.process = None
logger.info("command execution exceeded timeout (%s s), process killed", self.timeout)
self.callback(("Command execution time exceeded 'thread_timeout' ({0} s).\n"
"Process killed!\n\n"
).format(self.timeout))
except Exception:
logger.info("command execution exceeded timeout (%s s), process could not be killed", self.timeout)
self.callback(("Command execution time exceeded 'thread_timeout' ({0} s).\n"
"Process could not be killed!\n\n"
).format(self.timeout))
pass
@staticmethod
def createAndRun(args, env, callback, query=None, encoding='utf-8',
options=None, timeout=Command.timeout, silenceErrors=False, stream=False):
# Don't allow empty dicts or lists as defaults in method signature,
# cfr http://nedbatchelder.com/blog/200806/pylint.html
if options is None:
options = {}
command = ThreadCommand(args=args,
env=env,
callback=callback,
query=query,
encoding=encoding,
options=options,
timeout=timeout,
silenceErrors=silenceErrors,
stream=stream)
command.start()
killTimeout = Timer(command.timeout, command.stop)
killTimeout.start()
================================================
FILE: SQLToolsAPI/Completion.py
================================================
import re
import logging
from collections import namedtuple
from .ParseUtils import extractTables
JOIN_COND_PATTERN = r"\s+?JOIN\s+?[\w\.`\"]+\s+?(?:AS\s+)?(\w+)\s+?ON\s+?(?:[\w\.]+)?$"
JOIN_COND_REGEX = re.compile(JOIN_COND_PATTERN, re.IGNORECASE)
keywords_list = [
'SELECT', 'UPDATE', 'DELETE', 'INSERT', 'INTO', 'FROM',
'WHERE', 'GROUP BY', 'ORDER BY', 'HAVING', 'JOIN',
'INNER JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'USING',
'LIMIT', 'DISTINCT', 'SET'
]
logger = logging.getLogger(__name__)
# this function is generously used in completions code to get rid
# of all sorts of leading and trailing quotes in RDBMS identifiers
def _stripQuotes(ident):
return ident.strip('"\'`')
# used for formatting output
def _stripQuotesOnDemand(ident, doStrip=True):
if doStrip:
return _stripQuotes(ident)
return ident
def _startsWithQuote(ident):
# ident is matched against any of the possible ident quotes
quotes = ('`', '"')
return ident.startswith(quotes)
def _stripPrefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text
# escape $ sign when formatting output
def _escapeDollarSign(ident):
return ident.replace("$", "\$")
class CompletionItem(namedtuple('CompletionItem', ['type', 'ident'])):
"""Represents a potential or actual completion item.
* type - type of item (Table, Function, Column)
* ident - identifier (table.column, schema.table, alias)
"""
__slots__ = ()
@property
def parent(self):
"""Parent of identifier, e.g. "table" from "table.column" """
if self.ident.count('.') == 0:
return None
else:
return self.ident.partition('.')[0]
@property
def name(self):
"""Name of identifier, e.g. "column" from "table.column" """
return self.ident.split('.').pop()
# for functions - strip open bracket "(" and everything after that
# e.g: mydb.myAdd(int, int) --> mydb.myadd
def _matchIdent(self):
if self.type == 'Function':
return self.ident.partition('(')[0].lower()
return self.ident.lower()
# Helper method for string matching
# When exactly is true:
# matches search string to target exactly, but empty search string matches anything
# When exactly is false:
# if only one char given in search string match this single char with start
# of target string, otherwise match search string anywhere in target string
@staticmethod
def _stringMatched(target, search, exactly):
if exactly:
return target == search or search == ''
else:
if (len(search) == 1):
return target.startswith(search)
return search in target
# Method to match completion item against search string (prefix).
# Lower score means a better match.
# If completion item matches prefix with parent identifier, e.g.:
# table_name.column ~ table_name.co, then score = 1
# If completion item matches prefix without parent identifier, e.g.:
# table_name.column ~ co, then score = 2
# If completion item matches, but prefix has no parent, e.g.:
# table ~ tab, then score = 3
def prefixMatchScore(self, search, exactly=False):
target = self._matchIdent()
search = search.lower()
# match parent exactly and partially match name
if '.' in target and '.' in search:
searchList = search.split('.')
searchObject = _stripQuotes(searchList.pop())
searchParent = _stripQuotes(searchList.pop())
targetList = target.split('.')
targetObject = _stripQuotes(targetList.pop())
targetParent = _stripQuotes(targetList.pop())
if (searchParent == targetParent):
if self._stringMatched(targetObject, searchObject, exactly):
return 1 # highest score
return 0
# second part matches ?
if '.' in target:
targetObjectNoQuote = _stripQuotes(target.split('.').pop())
searchNoQuote = _stripQuotes(search)
if self._stringMatched(targetObjectNoQuote, searchNoQuote, exactly):
return 2
else:
targetNoQuote = _stripQuotes(target)
searchNoQuote = _stripQuotes(search)
if self._stringMatched(targetNoQuote, searchNoQuote, exactly):
return 3
else:
return 0
return 0
def prefixMatchListScore(self, searchList, exactly=False):
for item in searchList:
score = self.prefixMatchScore(item, exactly)
if score:
return score
return 0
# format completion item according to sublime text completions format
def format(self, stripQuotes=False):
typeDisplay = ''
if self.type == 'Table':
typeDisplay = self.type
elif self.type == 'Keyword':
typeDisplay = self.type
elif self.type == 'Alias':
typeDisplay = self.type
elif self.type == 'Function':
typeDisplay = 'Func'
elif self.type == 'Column':
typeDisplay = 'Col'
if not typeDisplay:
return (self.ident, _stripQuotesOnDemand(self.ident, stripQuotes))
part = self.ident.split('.')
if len(part) > 1:
return ("{0}\t({1} {2})".format(part[1], part[0], typeDisplay),
_stripQuotesOnDemand(_escapeDollarSign(part[1]), stripQuotes))
return ("{0}\t({1})".format(self.ident, typeDisplay),
_stripQuotesOnDemand(_escapeDollarSign(self.ident), stripQuotes))
class Completion:
def __init__(self, allTables, allColumns, allFunctions, settings=None):
self.allTables = [CompletionItem('Table', table) for table in allTables]
self.allColumns = [CompletionItem('Column', column) for column in allColumns]
self.allFunctions = [CompletionItem('Function', func) for func in allFunctions]
# we don't save the settings (we don't need them after init)
if settings is None:
settings = {}
# check old setting name ('selectors') first for compatibility
activeSelectors = settings.get('selectors', None)
if not activeSelectors:
activeSelectors = settings.get(
'autocomplete_selectors_active',
['source.sql'])
self.activeSelectors = activeSelectors
self.ignoreSelectors = settings.get(
'autocomplete_selectors_ignore',
['string.quoted.single.sql'])
# determine type of completions
self.completionType = settings.get('autocompletion', 'smart')
if not self.completionType:
self.completionType = None # autocompletion disabled
else:
self.completionType = str(self.completionType).strip()
if self.completionType not in ['basic', 'smart']:
self.completionType = 'smart'
# determine desired keywords case from settings
formatSettings = settings.get('format', {})
keywordCase = formatSettings.get('keyword_case', 'upper')
uppercaseKeywords = keywordCase.lower().startswith('upper')
self.allKeywords = []
for keyword in keywords_list:
if uppercaseKeywords:
keyword = keyword.upper()
else:
keyword = keyword.lower()
self.allKeywords.append(CompletionItem('Keyword', keyword))
def getActiveSelectors(self):
return self.activeSelectors
def getIgnoreSelectors(self):
return self.ignoreSelectors
def isDisabled(self):
return self.completionType is None
def getAutoCompleteList(self, prefix, sql, sqlToCursor):
if self.isDisabled():
return None
autocompleteList = []
inhibit = False
if self.completionType == 'smart':
autocompleteList, inhibit = self._getAutoCompleteListSmart(prefix, sql, sqlToCursor)
else:
autocompleteList = self._getAutoCompleteListBasic(prefix)
if not autocompleteList:
return None, False
# return completions with or without quotes?
# determined based on ident after last dot
startsWithQuote = _startsWithQuote(prefix.split(".").pop())
autocompleteList = [item.format(startsWithQuote) for item in autocompleteList]
return autocompleteList, inhibit
def _getAutoCompleteListBasic(self, prefix):
prefix = prefix.lower()
autocompleteList = []
# columns, tables and functions that match the prefix
for item in self.allColumns:
score = item.prefixMatchScore(prefix)
if score:
autocompleteList.append(item)
for item in self.allTables:
score = item.prefixMatchScore(prefix)
if score:
autocompleteList.append(item)
for item in self.allFunctions:
score = item.prefixMatchScore(prefix)
if score:
autocompleteList.append(item)
if len(autocompleteList) == 0:
return None
return autocompleteList
def _getAutoCompleteListSmart(self, prefix, sql, sqlToCursor):
"""
Generally, we recognize 3 different variations in prefix:
* ident| // no dots (.) in prefix
In this case we show completions for all available identifiers (tables, columns,
functions) that have "ident" text in them. Identifiers relevant to current
statement shown first.
* parent.ident| // single dot in prefix
In this case, if "parent" matches on of parsed table aliases we show column
completions for them, as well as we do prefix search for all other identifiers.
If something is matched, we return results as well as set a flag to suppress
Sublime completions.
If we don't find any objects using prefix search or we know that "parent" is
a query alias, we don't return anything and allow Sublime to do it's job by
showing most relevant completions.
* database.table.col| // multiple dots in prefix
In this case we only show columns for "table" column, as there is nothing else
that could be referenced that way.
Since it's too complicated to handle the specifics of identifiers case sensitivity
as well as all nuances of quoting of those identifiers for each RDBMS, we always
match against lower-cased and stripped quotes of both prefix and our internal saved
identifiers (tables, columns, functions). E.g. "MyTable"."myCol" --> mytable.mycol
"""
# TODO: add completions of function out fields
prefix = prefix.lower()
prefixDots = prefix.count('.')
# continue with empty identifiers list, even if we failed to parse identifiers
identifiers = []
try:
identifiers = extractTables(sql)
except Exception as e:
logger.debug('Failed to extact the list identifiers from SQL:\n {}'.format(sql),
exc_info=True)
# joinAlias is set only if user is editing join condition with alias. E.g.
# SELECT a.* from tbl_a a inner join tbl_b b ON |
joinAlias = None
if prefixDots <= 1:
try:
joinCondMatch = JOIN_COND_REGEX.search(sqlToCursor, re.IGNORECASE)
if joinCondMatch:
joinAlias = joinCondMatch.group(1)
except Exception as e:
logger.debug('Failed search of join condition, SQL:\n {}'.format(sqlToCursor),
exc_info=True)
autocompleteList = []
inhibit = False
if prefixDots == 0:
autocompleteList, inhibit = self._noDotsCompletions(prefix, identifiers, joinAlias)
elif prefixDots == 1:
autocompleteList, inhibit = self._singleDotCompletions(prefix, identifiers, joinAlias)
else:
autocompleteList, inhibit = self._multiDotCompletions(prefix, identifiers)
if not autocompleteList:
return None, False
return autocompleteList, inhibit
def _noDotsCompletions(self, prefix, identifiers, joinAlias=None):
"""
Method handles most generic completions when prefix does not contain any dots.
In this case completions can be anything: cols, tables, functions that have this name.
Still we try to predict users needs and output aliases, tables, columns and function
that are used in currently parsed statement first, then show everything else that
could be related.
Order: statement aliases -> statement cols -> statement tables -> statement functions,
then: other cols -> other tables -> other functions that match the prefix in their names
"""
# use set, as we are interested only in unique identifiers
sqlAliases = set()
sqlTables = []
sqlColumns = []
sqlFunctions = []
otherTables = []
otherColumns = []
otherFunctions = []
otherKeywords = []
otherJoinConditions = []
# utilitary temp lists
identTables = set()
identColumns = set()
identFunctions = set()
for ident in identifiers:
if ident.has_alias():
aliasItem = CompletionItem('Alias', ident.alias)
score = aliasItem.prefixMatchScore(prefix)
if score and aliasItem.ident != prefix:
sqlAliases.add(aliasItem)
if ident.is_function:
identFunctions.add(ident.full_name)
elif ident.is_table_alias:
identTables.add(ident.full_name)
identColumns.add(ident.name + '.' + prefix)
for table in self.allTables:
score = table.prefixMatchScore(prefix, exactly=False)
if score:
if table.prefixMatchListScore(identTables, exactly=True) > 0:
sqlTables.append(table)
else:
otherTables.append(table)
for col in self.allColumns:
score = col.prefixMatchScore(prefix, exactly=False)
if score:
if col.prefixMatchListScore(identColumns, exactly=False) > 0:
sqlColumns.append(col)
else:
otherColumns.append(col)
for fun in self.allFunctions:
score = fun.prefixMatchScore(prefix, exactly=False)
if score:
if fun.prefixMatchListScore(identFunctions, exactly=True) > 0:
sqlColumns.append(fun)
else:
otherColumns.append(fun)
# keywords
for item in self.allKeywords:
score = item.prefixMatchScore(prefix)
if score:
otherKeywords.append(item)
# join conditions
if joinAlias:
joinConditions = self._joinConditionCompletions(identifiers, joinAlias)
for condition in joinConditions:
if condition.ident.lower().startswith(prefix):
otherJoinConditions.append(condition)
# collect the results in prefered order
autocompleteList = []
# first of all list join conditions (if applicable)
autocompleteList.extend(otherJoinConditions)
# then aliases and identifiers related to currently parsed statement
autocompleteList.extend(sqlAliases)
# then cols, tables, functions related to current statement
autocompleteList.extend(sqlColumns)
autocompleteList.extend(sqlTables)
autocompleteList.extend(sqlFunctions)
# then other matching cols, tables, functions
autocompleteList.extend(otherKeywords)
autocompleteList.extend(otherColumns)
autocompleteList.extend(otherTables)
autocompleteList.extend(otherFunctions)
return autocompleteList, False
def _singleDotCompletions(self, prefix, identifiers, joinAlias=None):
"""
More intelligent completions can be shown if we have single dot in prefix in certain cases.
"""
prefixList = prefix.split(".")
prefixObject = prefixList.pop()
prefixParent = prefixList.pop()
# get join conditions
joinConditions = []
if joinAlias:
joinConditions = self._joinConditionCompletions(identifiers, joinAlias)
sqlTableAliases = set() # set of CompletionItem
sqlQueryAliases = set() # set of strings
# we use set, as we are interested only in unique identifiers
for ident in identifiers:
if ident.has_alias() and ident.alias.lower() == prefixParent:
if ident.is_query_alias:
sqlQueryAliases.add(ident.alias)
if ident.is_table_alias:
tables = [
table
for table in self.allTables
if table.prefixMatchScore(ident.full_name, exactly=True) > 0
]
sqlTableAliases.update(tables)
autocompleteList = []
for condition in joinConditions:
aliasPrefix = prefixParent + '.'
if condition.ident.lower().startswith(aliasPrefix):
autocompleteList.append(CompletionItem(condition.type,
_stripPrefix(condition.ident, aliasPrefix)))
# first of all expand table aliases to real table names and try
# to match their columns with prefix of these expanded identifiers
# e.g. select x.co| from tab x // "x.co" will expland to "tab.co"
for table_item in sqlTableAliases:
prefix_to_match = table_item.name + '.' + prefixObject
for item in self.allColumns:
score = item.prefixMatchScore(prefix_to_match)
if score:
autocompleteList.append(item)
# try to match all our other objects (tables, columns, functions) with prefix
for item in self.allColumns:
score = item.prefixMatchScore(prefix)
if score:
autocompleteList.append(item)
for item in self.allTables:
score = item.prefixMatchScore(prefix)
if score:
autocompleteList.append(item)
for item in self.allFunctions:
score = item.prefixMatchScore(prefix)
if score:
autocompleteList.append(item)
inhibit = len(autocompleteList) > 0
# in case prefix parent is a query alias we simply don't know what those
# columns might be so, set inhibit = False to allow sublime default completions
if prefixParent in sqlQueryAliases:
inhibit = False
return autocompleteList, inhibit
# match only columns if prefix contains multiple dots (db.table.col)
def _multiDotCompletions(self, prefix, identifiers):
autocompleteList = []
for item in self.allColumns:
score = item.prefixMatchScore(prefix)
if score:
autocompleteList.append(item)
if len(autocompleteList) > 0:
return autocompleteList, True
return None, False
def _joinConditionCompletions(self, identifiers, joinAlias=None):
if not joinAlias:
return None
# use set, as we are interested only in unique identifiers
sqlTableAliases = set()
joinAliasColumns = set()
sqlOtherColumns = set()
for ident in identifiers:
if ident.has_alias() and not ident.is_function:
sqlTableAliases.add(CompletionItem('Alias', ident.alias))
prefixForColumnMatch = ident.name + '.'
columns = [
(ident.alias, col)
for col in self.allColumns
if (col.prefixMatchScore(prefixForColumnMatch, exactly=True) > 0 and
_stripQuotes(col.name).lower().endswith('id'))
]
if ident.alias == joinAlias:
joinAliasColumns.update(columns)
else:
sqlOtherColumns.update(columns)
joinCandidatesCompletions = []
for joinAlias, joinColumn in joinAliasColumns:
# str.endswith can be matched against a tuple
columnsToMatch = None
if _stripQuotes(joinColumn.name).lower() == 'id':
columnsToMatch = (
_stripQuotes(joinColumn.parent).lower() + _stripQuotes(joinColumn.name).lower(),
_stripQuotes(joinColumn.parent).lower() + '_' + _stripQuotes(joinColumn.name).lower()
)
else:
columnsToMatch = (
_stripQuotes(joinColumn.name).lower(),
_stripQuotes(joinColumn.parent).lower() + _stripQuotes(joinColumn.name).lower(),
_stripQuotes(joinColumn.parent).lower() + '_' + _stripQuotes(joinColumn.name).lower()
)
for otherAlias, otherColumn in sqlOtherColumns:
if _stripQuotes(otherColumn.name).lower().endswith(columnsToMatch):
sideA = joinAlias + '.' + joinColumn.name
sideB = otherAlias + '.' + otherColumn.name
joinCandidatesCompletions.append(CompletionItem('Condition', sideA + ' = ' + sideB))
joinCandidatesCompletions.append(CompletionItem('Condition', sideB + ' = ' + sideA))
return joinCandidatesCompletions
================================================
FILE: SQLToolsAPI/Connection.py
================================================
import shutil
import shlex
import codecs
import logging
import sqlparse
from . import Utils as U
from . import Command as C
logger = logging.getLogger(__name__)
def _encoding_exists(enc):
try:
codecs.lookup(enc)
except LookupError:
return False
return True
class Connection(object):
DB_CLI_NOT_FOUND_MESSAGE = """DB CLI '{0}' could not be found.
Please set the path to DB CLI '{0}' binary in your SQLTools settings before continuing.
Example of "cli" section in SQLTools.sublime-settings:
/* ... (note the use of forward slashes) */
"cli" : {{
"mysql" : "c:/Program Files/MySQL/MySQL Server 5.7/bin/mysql.exe",
"pgsql" : "c:/Program Files/PostgreSQL/9.6/bin/psql.exe"
}}
You might need to restart the editor for settings to be refreshed."""
name = None
options = None
settings = None
type = None
host = None
port = None
database = None
username = None
password = None
encoding = None
safe_limit = None
show_query = None
rowsLimit = None
history = None
timeout = None
def __init__(self, name, options, settings=None, commandClass='ThreadCommand'):
self.Command = getattr(C, commandClass)
self.name = name
self.options = {k: v for k, v in options.items() if v is not None}
if settings is None:
settings = {}
self.settings = settings
self.type = self.options.get('type', None)
self.host = self.options.get('host', None)
self.port = self.options.get('port', None)
self.database = self.options.get('database', None)
self.username = self.options.get('username', None)
self.password = self.options.get('password', None)
self.encoding = self.options.get('encoding', 'utf-8')
self.encoding = self.encoding or 'utf-8' # defaults to utf-8
if not _encoding_exists(self.encoding):
self.encoding = 'utf-8'
self.safe_limit = settings.get('safe_limit', None)
self.show_query = settings.get('show_query', False)
self.rowsLimit = settings.get('show_records', {}).get('limit', 50)
self.useStreams = settings.get('use_streams', False)
self.cli = settings.get('cli')[self.options['type']]
cli_path = shutil.which(self.cli)
if cli_path is None:
logger.info(self.DB_CLI_NOT_FOUND_MESSAGE.format(self.cli))
raise FileNotFoundError(self.DB_CLI_NOT_FOUND_MESSAGE.format(self.cli))
def __str__(self):
return self.name
def info(self):
return 'DB: {0}, Connection: {1}@{2}:{3}'.format(
self.database, self.username, self.host, self.port)
def runInternalNamedQueryCommand(self, queryName, callback):
query = self.getNamedQuery(queryName)
if not query:
emptyList = []
callback(emptyList)
return
queryToRun = self.buildNamedQuery(queryName, query)
args = self.buildArgs(queryName)
env = self.buildEnv()
def cb(result):
callback(U.getResultAsList(result))
self.Command.createAndRun(args=args,
env=env,
callback=cb,
query=queryToRun,
encoding=self.encoding,
timeout=60,
silenceErrors=True,
stream=False)
def getTables(self, callback):
self.runInternalNamedQueryCommand('desc', callback)
def getColumns(self, callback):
self.runInternalNamedQueryCommand('columns', callback)
def getFunctions(self, callback):
self.runInternalNamedQueryCommand('functions', callback)
def runFormattedNamedQueryCommand(self, queryName, formatValues, callback):
query = self.getNamedQuery(queryName)
if not query:
return
# added for compatibility with older format string
query = query.replace("%s", "{0}", 1)
query = query.replace("%s", "{1}", 1)
if isinstance(formatValues, tuple):
query = query.format(*formatValues) # unpack the tuple
else:
query = query.format(formatValues)
queryToRun = self.buildNamedQuery(queryName, query)
args = self.buildArgs(queryName)
env = self.buildEnv()
self.Command.createAndRun(args=args,
env=env,
callback=callback,
query=queryToRun,
encoding=self.encoding,
timeout=self.timeout,
silenceErrors=False,
stream=False)
def getTableRecords(self, tableName, callback):
# in case we expect multiple values pack them into tuple
formatValues = (tableName, self.rowsLimit)
self.runFormattedNamedQueryCommand('show records', formatValues, callback)
def getTableDescription(self, tableName, callback):
self.runFormattedNamedQueryCommand('desc table', tableName, callback)
def getFunctionDescription(self, functionName, callback):
self.runFormattedNamedQueryCommand('desc function', functionName, callback)
def explainPlan(self, queries, callback):
queryName = 'explain plan'
explainQuery = self.getNamedQuery(queryName)
if not explainQuery:
return
strippedQueries = [
explainQuery.format(query.strip().strip(";"))
for rawQuery in queries
for query in filter(None, sqlparse.split(rawQuery))
]
queryToRun = self.buildNamedQuery(queryName, strippedQueries)
args = self.buildArgs(queryName)
env = self.buildEnv()
self.Command.createAndRun(args=args,
env=env,
callback=callback,
query=queryToRun,
encoding=self.encoding,
timeout=self.timeout,
silenceErrors=False,
stream=self.useStreams)
def execute(self, queries, callback, stream=None):
queryName = 'execute'
# if not explicitly overriden, use the value from settings
if stream is None:
stream = self.useStreams
if isinstance(queries, str):
queries = [queries]
# add original (umodified) queries to the history
if self.history:
self.history.add('\n'.join(queries))
processedQueriesList = []
for rawQuery in queries:
for query in sqlparse.split(rawQuery):
if self.safe_limit:
parsedTokens = sqlparse.parse(query.strip().replace("'", "\""))
if ((parsedTokens[0][0].ttype in sqlparse.tokens.Keyword and
parsedTokens[0][0].value == 'select')):
applySafeLimit = True
for parse in parsedTokens:
for token in parse.tokens:
if token.ttype in sqlparse.tokens.Keyword and token.value == 'limit':
applySafeLimit = False
if applySafeLimit:
if (query.strip()[-1:] == ';'):
query = query.strip()[:-1]
query += " LIMIT {0};".format(self.safe_limit)
processedQueriesList.append(query)
queryToRun = self.buildNamedQuery(queryName, processedQueriesList)
args = self.buildArgs(queryName)
env = self.buildEnv()
logger.debug("Query: %s", str(queryToRun))
self.Command.createAndRun(args=args,
env=env,
callback=callback,
query=queryToRun,
encoding=self.encoding,
options={'show_query': self.show_query},
timeout=self.timeout,
silenceErrors=False,
stream=stream)
def getNamedQuery(self, queryName):
if not queryName:
return None
cliOptions = self.getOptionsForSgdbCli()
return cliOptions.get('queries', {}).get(queryName, {}).get('query')
def buildNamedQuery(self, queryName, queries):
if not queryName:
return None
if not queries:
return None
cliOptions = self.getOptionsForSgdbCli()
beforeCli = cliOptions.get('before')
afterCli = cliOptions.get('after')
beforeQuery = cliOptions.get('queries', {}).get(queryName, {}).get('before')
afterQuery = cliOptions.get('queries', {}).get(queryName, {}).get('after')
# sometimes we preprocess the raw queries from user, in that case we already have a list
if type(queries) is not list:
queries = [queries]
builtQueries = []
if beforeCli is not None:
builtQueries.extend(beforeCli)
if beforeQuery is not None:
builtQueries.extend(beforeQuery)
if queries is not None:
builtQueries.extend(queries)
if afterQuery is not None:
builtQueries.extend(afterQuery)
if afterCli is not None:
builtQueries.extend(afterCli)
# remove empty list items
builtQueries = list(filter(None, builtQueries))
return '\n'.join(builtQueries)
def buildArgs(self, queryName=None):
cliOptions = self.getOptionsForSgdbCli()
args = [self.cli]
# append otional args (if any) - could be a single value or a list
optionalArgs = cliOptions.get('args_optional')
if optionalArgs: # only if we have optional args
if isinstance(optionalArgs, list):
for item in optionalArgs:
formattedItem = self.formatOptionalArgument(item, self.options)
if formattedItem:
args = args + shlex.split(formattedItem)
else:
formattedItem = self.formatOptionalArgument(optionalArgs, self.options)
if formattedItem:
args = args + shlex.split(formattedItem)
# append generic options
options = cliOptions.get('options', None)
if options:
args = args + options
# append query specific options (if present)
if queryName:
queryOptions = cliOptions.get('queries', {}).get(queryName, {}).get('options')
if queryOptions:
if len(queryOptions) > 0:
args = args + queryOptions
# append main args - could be a single value or a list
mainArgs = cliOptions['args']
if isinstance(mainArgs, list):
mainArgs = ' '.join(mainArgs)
mainArgs = mainArgs.format(**self.options)
args = args + shlex.split(mainArgs)
logger.debug('CLI args (%s): %s', str(queryName), ' '.join(args))
return args
def buildEnv(self):
cliOptions = self.getOptionsForSgdbCli()
env = dict()
# append **optional** environment variables dict (if any)
optionalEnv = cliOptions.get('env_optional')
if optionalEnv: # only if we have optional args
if isinstance(optionalEnv, dict):
for var, value in optionalEnv.items():
formattedValue = self.formatOptionalArgument(value, self.options)
if formattedValue:
env.update({var: formattedValue})
# append environment variables dict (if any)
staticEnv = cliOptions.get('env')
if staticEnv: # only if we have optional args
if isinstance(staticEnv, dict):
for var, value in staticEnv.items():
formattedValue = value.format(**self.options)
if formattedValue:
env.update({var: formattedValue})
logger.debug('CLI environment: %s', str(env))
return env
def getOptionsForSgdbCli(self):
return self.settings.get('cli_options', {}).get(self.type)
@staticmethod
def formatOptionalArgument(argument, formatOptions):
try:
formattedArg = argument.format(**formatOptions)
except (KeyError, IndexError):
return None
if argument == formattedArg: # string not changed after format
return None
return formattedArg
@staticmethod
def setTimeout(timeout):
Connection.timeout = timeout
logger.info('Connection timeout set to {0} seconds'.format(timeout))
@staticmethod
def setHistoryManager(manager):
Connection.history = manager
size = manager.getMaxSize()
logger.info('Connection history size is {0}'.format(size))
================================================
FILE: SQLToolsAPI/History.py
================================================
__version__ = "v0.1.0"
class SizeException(Exception):
pass
class NotFoundException(Exception):
pass
class History:
def __init__(self, maxSize=100):
self.items = []
self.maxSize = maxSize
def add(self, query):
if self.getSize() >= self.getMaxSize():
self.items.pop(0)
self.items.insert(0, query)
def get(self, index):
if index < 0 or index > (len(self.items) - 1):
raise NotFoundException("No query selected")
return self.items[index]
def setMaxSize(self, size=100):
if size < 1:
raise SizeException("Size can't be lower than 1")
self.maxSize = size
return self.maxSize
def getMaxSize(self):
return self.maxSize
def getSize(self):
return len(self.items)
def all(self):
return self.items
def clear(self):
self.items = []
return self.items
================================================
FILE: SQLToolsAPI/ParseUtils.py
================================================
import os
import sys
import itertools
from collections import namedtuple
dirpath = os.path.join(os.path.dirname(__file__), 'lib')
if dirpath not in sys.path:
sys.path.append(dirpath)
import sqlparse
from sqlparse.sql import IdentifierList, Identifier, Function
from sqlparse.tokens import Keyword, DML
class Reference(namedtuple('Reference', ['schema', 'name', 'alias', 'is_function'])):
__slots__ = ()
def has_alias(self):
return self.alias is not None
@property
def is_query_alias(self):
return self.name is None and self.alias is not None
@property
def is_table_alias(self):
return self.name is not None and self.alias is not None and not self.is_function
@property
def full_name(self):
if self.schema is None:
return self.name
else:
return self.schema + '.' + self.name
def _is_subselect(parsed):
if not parsed.is_group:
return False
for item in parsed.tokens:
if item.ttype is DML and item.value.upper() in ('SELECT', 'INSERT',
'UPDATE', 'CREATE', 'DELETE'):
return True
return False
def _identifier_is_function(identifier):
return any(isinstance(t, Function) for t in identifier.tokens)
def _extract_from_part(parsed):
tbl_prefix_seen = False
for item in parsed.tokens:
if item.is_group:
for x in _extract_from_part(item):
yield x
if tbl_prefix_seen:
if _is_subselect(item):
for x in _extract_from_part(item):
yield x
# An incomplete nested select won't be recognized correctly as a
# sub-select. eg: 'SELECT * FROM (SELECT id FROM user'. This causes
# the second FROM to trigger this elif condition resulting in a
# StopIteration. So we need to ignore the keyword if the keyword
# FROM.
# Also 'SELECT * FROM abc JOIN def' will trigger this elif
# condition. So we need to ignore the keyword JOIN and its variants
# INNER JOIN, FULL OUTER JOIN, etc.
elif item.ttype is Keyword and (
not item.value.upper() == 'FROM') and (
not item.value.upper().endswith('JOIN')):
tbl_prefix_seen = False
else:
yield item
elif item.ttype is Keyword or item.ttype is Keyword.DML:
item_val = item.value.upper()
if (item_val in ('COPY', 'FROM', 'INTO', 'UPDATE', 'TABLE') or
item_val.endswith('JOIN')):
tbl_prefix_seen = True
# 'SELECT a, FROM abc' will detect FROM as part of the column list.
# So this check here is necessary.
elif isinstance(item, IdentifierList):
for identifier in item.get_identifiers():
if (identifier.ttype is Keyword and
identifier.value.upper() == 'FROM'):
tbl_prefix_seen = True
break
def _extract_table_identifiers(token_stream):
for item in token_stream:
if isinstance(item, IdentifierList):
for ident in item.get_identifiers():
try:
alias = ident.get_alias()
schema_name = ident.get_parent_name()
real_name = ident.get_real_name()
except AttributeError:
continue
if real_name:
yield Reference(schema_name, real_name,
alias, _identifier_is_function(ident))
elif isinstance(item, Identifier):
yield Reference(item.get_parent_name(), item.get_real_name(),
item.get_alias(), _identifier_is_function(item))
elif isinstance(item, Function):
yield Reference(item.get_parent_name(), item.get_real_name(),
item.get_alias(), _identifier_is_function(item))
def extractTables(sql):
# let's handle multiple statements in one sql string
extracted_tables = []
statements = list(sqlparse.parse(sql))
for statement in statements:
stream = _extract_from_part(statement)
extracted_tables.append(list(_extract_table_identifiers(stream)))
return list(itertools.chain(*extracted_tables))
================================================
FILE: SQLToolsAPI/README.md
================================================
## SQLTools API for plugins - v0.2.5
Docs will be ready soon
================================================
FILE: SQLToolsAPI/Storage.py
================================================
import os
import shutil
from . import Utils as U
__version__ = "v0.1.0"
class Storage:
def __init__(self, filename, default=None):
self.storageFile = filename
self.defaultFile = default
self.items = {}
# copy entire file, to keep comments
# if not os.path.isfile(filename) and default and os.path.isfile(default):
# shutil.copyfile(default, filename)
self.all()
def all(self):
userFile = self.getFilename()
if os.path.exists(userFile):
self.items = U.parseJson(self.getFilename())
else:
self.items = {}
return U.merge(self.items, self.defaults())
def write(self):
return U.saveJson(self.items if isinstance(self.items, dict) else {}, self.getFilename())
def add(self, key, value):
if len(key) <= 0:
return
self.all()
if isinstance(value, str):
value = [value]
self.items[key] = '\n'.join(value)
self.write()
def delete(self, key):
if len(key) <= 0:
return
self.all()
self.items.pop(key)
self.write()
def get(self, key, default=None):
if len(key) <= 0:
return
items = self.all()
return items[key] if key in items else default
def getFilename(self):
return self.storageFile
def defaults(self):
if self.defaultFile and os.path.isfile(self.defaultFile):
return U.parseJson(self.defaultFile)
return {}
class Settings(Storage):
pass
================================================
FILE: SQLToolsAPI/Utils.py
================================================
__version__ = "v0.2.0"
import json
import os
import re
import sys
dirpath = os.path.join(os.path.dirname(__file__), 'lib')
if dirpath not in sys.path:
sys.path.append(dirpath)
import sqlparse
# Regular expression for comments
comment_re = re.compile(
'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
re.DOTALL | re.MULTILINE
)
def parseJson(filename):
""" Parse a JSON file
First remove comments and then use the json module package
Comments look like :
// ...
or
/*
...
*/
"""
with open(filename, mode='r', encoding='utf-8') as f:
content = ''.join(f.readlines())
# Looking for comments
match = comment_re.search(content)
while match:
# single line comment
content = content[:match.start()] + content[match.end():]
match = comment_re.search(content)
# remove trailing commas
content = re.sub(r',([ \t\r\n]+)}', r'\1}', content)
content = re.sub(r',([ \t\r\n]+)\]', r'\1]', content)
# Return json file
return json.loads(content, encoding='utf-8')
def saveJson(content, filename):
with open(filename, mode='w', encoding='utf-8') as outfile:
json.dump(content, outfile,
sort_keys=True, indent=2, separators=(',', ': '))
def getResultAsList(results):
resultList = []
for result in results.splitlines():
lineResult = ''
for element in result.strip('|').split('|'):
lineResult += element.strip()
if lineResult:
resultList.append(lineResult)
return resultList
def formatSql(raw, settings):
try:
result = sqlparse.format(raw, **settings)
return result
except Exception:
return None
def merge(source, destination):
"""
run me with nosetests --with-doctest file.py
>>> a = { 'first' : { 'all_rows' : { 'pass' : 'dog', 'number' : '1' } } }
>>> b = { 'first' : { 'all_rows' : { 'fail' : 'cat', 'number' : '5' } } }
>>> merge(b, a) == { 'first' : { 'all_rows' : { 'pass' : 'dog', 'fail' : 'cat', 'number' : '5' } } }
True
"""
for key, value in source.items():
if isinstance(value, dict):
# get node or create one
node = destination.setdefault(key, {})
merge(value, node)
else:
destination[key] = value
return destination
================================================
FILE: SQLToolsAPI/__init__.py
================================================
__version__ = "v0.3.0"
__all__ = [
'Utils',
'Completion',
'Command',
'Connection',
'History',
'Storage',
'Settings'
]
================================================
FILE: SQLToolsAPI/lib/sqlparse/__init__.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""Parse SQL statements."""
# Setup namespace
from sqlparse import sql
from sqlparse import cli
from sqlparse import engine
from sqlparse import tokens
from sqlparse import filters
from sqlparse import formatter
from sqlparse.compat import text_type
__version__ = '0.2.3'
__all__ = ['engine', 'filters', 'formatter', 'sql', 'tokens', 'cli']
def parse(sql, encoding=None):
"""Parse sql and return a list of statements.
:param sql: A string containing one or more SQL statements.
:param encoding: The encoding of the statement (optional).
:returns: A tuple of :class:`~sqlparse.sql.Statement` instances.
"""
return tuple(parsestream(sql, encoding))
def parsestream(stream, encoding=None):
"""Parses sql statements from file-like object.
:param stream: A file-like object.
:param encoding: The encoding of the stream contents (optional).
:returns: A generator of :class:`~sqlparse.sql.Statement` instances.
"""
stack = engine.FilterStack()
stack.enable_grouping()
return stack.run(stream, encoding)
def format(sql, encoding=None, **options):
"""Format *sql* according to *options*.
Available options are documented in :ref:`formatting`.
In addition to the formatting options this function accepts the
keyword "encoding" which determines the encoding of the statement.
:returns: The formatted SQL statement as string.
"""
stack = engine.FilterStack()
options = formatter.validate_options(options)
stack = formatter.build_filter_stack(stack, options)
stack.postprocess.append(filters.SerializerUnicode())
return u''.join(stack.run(sql, encoding))
def split(sql, encoding=None):
"""Split *sql* into single statements.
:param sql: A string containing one or more SQL statements.
:param encoding: The encoding of the statement (optional).
:returns: A list of strings.
"""
stack = engine.FilterStack()
return [text_type(stmt).strip() for stmt in stack.run(sql, encoding)]
================================================
FILE: SQLToolsAPI/lib/sqlparse/__main__.py
================================================
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""Entrypoint module for `python -m sqlparse`.
Why does this file exist, and why __main__? For more info, read:
- https://www.python.org/dev/peps/pep-0338/
- https://docs.python.org/2/using/cmdline.html#cmdoption-m
- https://docs.python.org/3/using/cmdline.html#cmdoption-m
"""
import sys
from sqlparse.cli import main
if __name__ == '__main__':
sys.exit(main())
================================================
FILE: SQLToolsAPI/lib/sqlparse/cli.py
================================================
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""Module that contains the command line app.
Why does this file exist, and why not put this in __main__?
You might be tempted to import things from __main__ later, but that will
cause problems: the code will get executed twice:
- When you run `python -m sqlparse` python will execute
``__main__.py`` as a script. That means there won't be any
``sqlparse.__main__`` in ``sys.modules``.
- When you import __main__ it will get executed again (as a module) because
there's no ``sqlparse.__main__`` in ``sys.modules``.
Also see (1) from http://click.pocoo.org/5/setuptools/#setuptools-integration
"""
import argparse
import sys
from io import TextIOWrapper
from codecs import open, getreader
import sqlparse
from sqlparse.compat import PY2
from sqlparse.exceptions import SQLParseError
# TODO: Add CLI Tests
# TODO: Simplify formatter by using argparse `type` arguments
def create_parser():
_CASE_CHOICES = ['upper', 'lower', 'capitalize']
parser = argparse.ArgumentParser(
prog='sqlformat',
description='Format FILE according to OPTIONS. Use "-" as FILE '
'to read from stdin.',
usage='%(prog)s [OPTIONS] FILE, ...',
)
parser.add_argument('filename')
parser.add_argument(
'-o', '--outfile',
dest='outfile',
metavar='FILE',
help='write output to FILE (defaults to stdout)')
parser.add_argument(
'--version',
action='version',
version=sqlparse.__version__)
group = parser.add_argument_group('Formatting Options')
group.add_argument(
'-k', '--keywords',
metavar='CHOICE',
dest='keyword_case',
choices=_CASE_CHOICES,
help='change case of keywords, CHOICE is one of {0}'.format(
', '.join('"{0}"'.format(x) for x in _CASE_CHOICES)))
group.add_argument(
'-i', '--identifiers',
metavar='CHOICE',
dest='identifier_case',
choices=_CASE_CHOICES,
help='change case of identifiers, CHOICE is one of {0}'.format(
', '.join('"{0}"'.format(x) for x in _CASE_CHOICES)))
group.add_argument(
'-l', '--language',
metavar='LANG',
dest='output_format',
choices=['python', 'php'],
help='output a snippet in programming language LANG, '
'choices are "python", "php"')
group.add_argument(
'--strip-comments',
dest='strip_comments',
action='store_true',
default=False,
help='remove comments')
group.add_argument(
'-r', '--reindent',
dest='reindent',
action='store_true',
default=False,
help='reindent statements')
group.add_argument(
'--indent_width',
dest='indent_width',
default=2,
type=int,
help='indentation width (defaults to 2 spaces)')
group.add_argument(
'-a', '--reindent_aligned',
action='store_true',
default=False,
help='reindent statements to aligned format')
group.add_argument(
'-s', '--use_space_around_operators',
action='store_true',
default=False,
help='place spaces around mathematical operators')
group.add_argument(
'--wrap_after',
dest='wrap_after',
default=0,
type=int,
help='Column after which lists should be wrapped')
group.add_argument(
'--comma_first',
dest='comma_first',
default=False,
type=bool,
help='Insert linebreak before comma (default False)')
group.add_argument(
'--encoding',
dest='encoding',
default='utf-8',
help='Specify the input encoding (default utf-8)')
return parser
def _error(msg):
"""Print msg and optionally exit with return code exit_."""
sys.stderr.write(u'[ERROR] {0}\n'.format(msg))
return 1
def main(args=None):
parser = create_parser()
args = parser.parse_args(args)
if args.filename == '-': # read from stdin
if PY2:
data = getreader(args.encoding)(sys.stdin).read()
else:
data = TextIOWrapper(
sys.stdin.buffer, encoding=args.encoding).read()
else:
try:
data = ''.join(open(args.filename, 'r', args.encoding).readlines())
except IOError as e:
return _error(
u'Failed to read {0}: {1}'.format(args.filename, e))
if args.outfile:
try:
stream = open(args.outfile, 'w', args.encoding)
except IOError as e:
return _error(u'Failed to open {0}: {1}'.format(args.outfile, e))
else:
stream = sys.stdout
formatter_opts = vars(args)
try:
formatter_opts = sqlparse.formatter.validate_options(formatter_opts)
except SQLParseError as e:
return _error(u'Invalid options: {0}'.format(e))
s = sqlparse.format(data, **formatter_opts)
stream.write(s)
stream.flush()
return 0
================================================
FILE: SQLToolsAPI/lib/sqlparse/compat.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""Python 2/3 compatibility.
This module only exists to avoid a dependency on six
for very trivial stuff. We only need to take care of
string types, buffers and metaclasses.
Parts of the code is copied directly from six:
https://bitbucket.org/gutworth/six
"""
import sys
from io import TextIOBase
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY3:
def unicode_compatible(cls):
return cls
bytes_type = bytes
text_type = str
string_types = (str,)
from io import StringIO
file_types = (StringIO, TextIOBase)
elif PY2:
def unicode_compatible(cls):
cls.__unicode__ = cls.__str__
cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
return cls
bytes_type = str
text_type = unicode
string_types = (str, unicode,)
from StringIO import StringIO
file_types = (file, StringIO, TextIOBase)
from StringIO import StringIO
================================================
FILE: SQLToolsAPI/lib/sqlparse/engine/__init__.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse.engine import grouping
from sqlparse.engine.filter_stack import FilterStack
from sqlparse.engine.statement_splitter import StatementSplitter
__all__ = [
'grouping',
'FilterStack',
'StatementSplitter',
]
================================================
FILE: SQLToolsAPI/lib/sqlparse/engine/filter_stack.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""filter"""
from sqlparse import lexer
from sqlparse.engine import grouping
from sqlparse.engine.statement_splitter import StatementSplitter
class FilterStack(object):
def __init__(self):
self.preprocess = []
self.stmtprocess = []
self.postprocess = []
self._grouping = False
def enable_grouping(self):
self._grouping = True
def run(self, sql, encoding=None):
stream = lexer.tokenize(sql, encoding)
# Process token stream
for filter_ in self.preprocess:
stream = filter_.process(stream)
stream = StatementSplitter().process(stream)
# Output: Stream processed Statements
for stmt in stream:
if self._grouping:
stmt = grouping.group(stmt)
for filter_ in self.stmtprocess:
filter_.process(stmt)
for filter_ in self.postprocess:
stmt = filter_.process(stmt)
yield stmt
================================================
FILE: SQLToolsAPI/lib/sqlparse/engine/grouping.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse import sql
from sqlparse import tokens as T
from sqlparse.utils import recurse, imt
T_NUMERICAL = (T.Number, T.Number.Integer, T.Number.Float)
T_STRING = (T.String, T.String.Single, T.String.Symbol)
T_NAME = (T.Name, T.Name.Placeholder)
def _group_matching(tlist, cls):
"""Groups Tokens that have beginning and end."""
opens = []
tidx_offset = 0
for idx, token in enumerate(list(tlist)):
tidx = idx - tidx_offset
if token.is_whitespace:
# ~50% of tokens will be whitespace. Will checking early
# for them avoid 3 comparisons, but then add 1 more comparison
# for the other ~50% of tokens...
continue
if token.is_group and not isinstance(token, cls):
# Check inside previously grouped (ie. parenthesis) if group
# of differnt type is inside (ie, case). though ideally should
# should check for all open/close tokens at once to avoid recursion
_group_matching(token, cls)
continue
if token.match(*cls.M_OPEN):
opens.append(tidx)
elif token.match(*cls.M_CLOSE):
try:
open_idx = opens.pop()
except IndexError:
# this indicates invalid sql and unbalanced tokens.
# instead of break, continue in case other "valid" groups exist
continue
close_idx = tidx
tlist.group_tokens(cls, open_idx, close_idx)
tidx_offset += close_idx - open_idx
def group_brackets(tlist):
_group_matching(tlist, sql.SquareBrackets)
def group_parenthesis(tlist):
_group_matching(tlist, sql.Parenthesis)
def group_case(tlist):
_group_matching(tlist, sql.Case)
def group_if(tlist):
_group_matching(tlist, sql.If)
def group_for(tlist):
_group_matching(tlist, sql.For)
def group_begin(tlist):
_group_matching(tlist, sql.Begin)
def group_typecasts(tlist):
def match(token):
return token.match(T.Punctuation, '::')
def valid(token):
return token is not None
def post(tlist, pidx, tidx, nidx):
return pidx, nidx
valid_prev = valid_next = valid
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)
def group_period(tlist):
def match(token):
return token.match(T.Punctuation, '.')
def valid_prev(token):
sqlcls = sql.SquareBrackets, sql.Identifier
ttypes = T.Name, T.String.Symbol
return imt(token, i=sqlcls, t=ttypes)
def valid_next(token):
# issue261, allow invalid next token
return True
def post(tlist, pidx, tidx, nidx):
# next_ validation is being performed here. issue261
sqlcls = sql.SquareBrackets, sql.Function
ttypes = T.Name, T.String.Symbol, T.Wildcard
next_ = tlist[nidx] if nidx is not None else None
valid_next = imt(next_, i=sqlcls, t=ttypes)
return (pidx, nidx) if valid_next else (pidx, tidx)
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)
def group_as(tlist):
def match(token):
return token.is_keyword and token.normalized == 'AS'
def valid_prev(token):
return token.normalized == 'NULL' or not token.is_keyword
def valid_next(token):
ttypes = T.DML, T.DDL
return not imt(token, t=ttypes) and token is not None
def post(tlist, pidx, tidx, nidx):
return pidx, nidx
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)
def group_assignment(tlist):
def match(token):
return token.match(T.Assignment, ':=')
def valid(token):
return token is not None
def post(tlist, pidx, tidx, nidx):
m_semicolon = T.Punctuation, ';'
snidx, _ = tlist.token_next_by(m=m_semicolon, idx=nidx)
nidx = snidx or nidx
return pidx, nidx
valid_prev = valid_next = valid
_group(tlist, sql.Assignment, match, valid_prev, valid_next, post)
def group_comparison(tlist):
sqlcls = (sql.Parenthesis, sql.Function, sql.Identifier,
sql.Operation)
ttypes = T_NUMERICAL + T_STRING + T_NAME
def match(token):
return token.ttype == T.Operator.Comparison
def valid(token):
if imt(token, t=ttypes, i=sqlcls):
return True
elif token and token.is_keyword and token.normalized == 'NULL':
return True
else:
return False
def post(tlist, pidx, tidx, nidx):
return pidx, nidx
valid_prev = valid_next = valid
_group(tlist, sql.Comparison, match,
valid_prev, valid_next, post, extend=False)
@recurse(sql.Identifier)
def group_identifier(tlist):
ttypes = (T.String.Symbol, T.Name)
tidx, token = tlist.token_next_by(t=ttypes)
while token:
tlist.group_tokens(sql.Identifier, tidx, tidx)
tidx, token = tlist.token_next_by(t=ttypes, idx=tidx)
def group_arrays(tlist):
sqlcls = sql.SquareBrackets, sql.Identifier, sql.Function
ttypes = T.Name, T.String.Symbol
def match(token):
return isinstance(token, sql.SquareBrackets)
def valid_prev(token):
return imt(token, i=sqlcls, t=ttypes)
def valid_next(token):
return True
def post(tlist, pidx, tidx, nidx):
return pidx, tidx
_group(tlist, sql.Identifier, match,
valid_prev, valid_next, post, extend=True, recurse=False)
def group_operator(tlist):
ttypes = T_NUMERICAL + T_STRING + T_NAME
sqlcls = (sql.SquareBrackets, sql.Parenthesis, sql.Function,
sql.Identifier, sql.Operation)
def match(token):
return imt(token, t=(T.Operator, T.Wildcard))
def valid(token):
return imt(token, i=sqlcls, t=ttypes)
def post(tlist, pidx, tidx, nidx):
tlist[tidx].ttype = T.Operator
return pidx, nidx
valid_prev = valid_next = valid
_group(tlist, sql.Operation, match,
valid_prev, valid_next, post, extend=False)
def group_identifier_list(tlist):
m_role = T.Keyword, ('null', 'role')
sqlcls = (sql.Function, sql.Case, sql.Identifier, sql.Comparison,
sql.IdentifierList, sql.Operation)
ttypes = (T_NUMERICAL + T_STRING + T_NAME +
(T.Keyword, T.Comment, T.Wildcard))
def match(token):
return token.match(T.Punctuation, ',')
def valid(token):
return imt(token, i=sqlcls, m=m_role, t=ttypes)
def post(tlist, pidx, tidx, nidx):
return pidx, nidx
valid_prev = valid_next = valid
_group(tlist, sql.IdentifierList, match,
valid_prev, valid_next, post, extend=True)
@recurse(sql.Comment)
def group_comments(tlist):
tidx, token = tlist.token_next_by(t=T.Comment)
while token:
eidx, end = tlist.token_not_matching(
lambda tk: imt(tk, t=T.Comment) or tk.is_whitespace, idx=tidx)
if end is not None:
eidx, end = tlist.token_prev(eidx, skip_ws=False)
tlist.group_tokens(sql.Comment, tidx, eidx)
tidx, token = tlist.token_next_by(t=T.Comment, idx=tidx)
@recurse(sql.Where)
def group_where(tlist):
tidx, token = tlist.token_next_by(m=sql.Where.M_OPEN)
while token:
eidx, end = tlist.token_next_by(m=sql.Where.M_CLOSE, idx=tidx)
if end is None:
end = tlist._groupable_tokens[-1]
else:
end = tlist.tokens[eidx - 1]
# TODO: convert this to eidx instead of end token.
# i think above values are len(tlist) and eidx-1
eidx = tlist.token_index(end)
tlist.group_tokens(sql.Where, tidx, eidx)
tidx, token = tlist.token_next_by(m=sql.Where.M_OPEN, idx=tidx)
@recurse()
def group_aliased(tlist):
I_ALIAS = (sql.Parenthesis, sql.Function, sql.Case, sql.Identifier,
sql.Operation)
tidx, token = tlist.token_next_by(i=I_ALIAS, t=T.Number)
while token:
nidx, next_ = tlist.token_next(tidx)
if isinstance(next_, sql.Identifier):
tlist.group_tokens(sql.Identifier, tidx, nidx, extend=True)
tidx, token = tlist.token_next_by(i=I_ALIAS, t=T.Number, idx=tidx)
@recurse(sql.Function)
def group_functions(tlist):
has_create = False
has_table = False
for tmp_token in tlist.tokens:
if tmp_token.value == 'CREATE':
has_create = True
if tmp_token.value == 'TABLE':
has_table = True
if has_create and has_table:
return
tidx, token = tlist.token_next_by(t=T.Name)
while token:
nidx, next_ = tlist.token_next(tidx)
if isinstance(next_, sql.Parenthesis):
tlist.group_tokens(sql.Function, tidx, nidx)
tidx, token = tlist.token_next_by(t=T.Name, idx=tidx)
def group_order(tlist):
"""Group together Identifier and Asc/Desc token"""
tidx, token = tlist.token_next_by(t=T.Keyword.Order)
while token:
pidx, prev_ = tlist.token_prev(tidx)
if imt(prev_, i=sql.Identifier, t=T.Number):
tlist.group_tokens(sql.Identifier, pidx, tidx)
tidx = pidx
tidx, token = tlist.token_next_by(t=T.Keyword.Order, idx=tidx)
@recurse()
def align_comments(tlist):
tidx, token = tlist.token_next_by(i=sql.Comment)
while token:
pidx, prev_ = tlist.token_prev(tidx)
if isinstance(prev_, sql.TokenList):
tlist.group_tokens(sql.TokenList, pidx, tidx, extend=True)
tidx = pidx
tidx, token = tlist.token_next_by(i=sql.Comment, idx=tidx)
def group(stmt):
for func in [
group_comments,
# _group_matching
group_brackets,
group_parenthesis,
group_case,
group_if,
group_for,
group_begin,
group_functions,
group_where,
group_period,
group_arrays,
group_identifier,
group_order,
group_typecasts,
group_operator,
group_as,
group_aliased,
group_assignment,
group_comparison,
align_comments,
group_identifier_list,
]:
func(stmt)
return stmt
def _group(tlist, cls, match,
valid_prev=lambda t: True,
valid_next=lambda t: True,
post=None,
extend=True,
recurse=True
):
"""Groups together tokens that are joined by a middle token. ie. x < y"""
tidx_offset = 0
pidx, prev_ = None, None
for idx, token in enumerate(list(tlist)):
tidx = idx - tidx_offset
if token.is_whitespace:
continue
if recurse and token.is_group and not isinstance(token, cls):
_group(token, cls, match, valid_prev, valid_next, post, extend)
if match(token):
nidx, next_ = tlist.token_next(tidx)
if prev_ and valid_prev(prev_) and valid_next(next_):
from_idx, to_idx = post(tlist, pidx, tidx, nidx)
grp = tlist.group_tokens(cls, from_idx, to_idx, extend=extend)
tidx_offset += to_idx - from_idx
pidx, prev_ = from_idx, grp
continue
pidx, prev_ = tidx, token
================================================
FILE: SQLToolsAPI/lib/sqlparse/engine/statement_splitter.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse import sql, tokens as T
class StatementSplitter(object):
"""Filter that split stream at individual statements"""
def __init__(self):
self._reset()
def _reset(self):
"""Set the filter attributes to its default values"""
self._in_declare = False
self._is_create = False
self._begin_depth = 0
self.consume_ws = False
self.tokens = []
self.level = 0
def _change_splitlevel(self, ttype, value):
"""Get the new split level (increase, decrease or remain equal)"""
# ANSI
# if normal token return
# wouldn't parenthesis increase/decrease a level?
# no, inside a paranthesis can't start new statement
if ttype not in T.Keyword:
return 0
# Everything after here is ttype = T.Keyword
# Also to note, once entered an If statement you are done and basically
# returning
unified = value.upper()
# three keywords begin with CREATE, but only one of them is DDL
# DDL Create though can contain more words such as "or replace"
if ttype is T.Keyword.DDL and unified.startswith('CREATE'):
self._is_create = True
return 0
# can have nested declare inside of being...
if unified == 'DECLARE' and self._is_create and self._begin_depth == 0:
self._in_declare = True
return 1
if unified == 'BEGIN':
self._begin_depth += 1
if self._is_create:
# FIXME(andi): This makes no sense.
return 1
return 0
# Should this respect a preceeding BEGIN?
# In CASE ... WHEN ... END this results in a split level -1.
# Would having multiple CASE WHEN END and a Assigment Operator
# cause the statement to cut off prematurely?
if unified == 'END':
self._begin_depth = max(0, self._begin_depth - 1)
return -1
if (unified in ('IF', 'FOR', 'WHILE') and
self._is_create and self._begin_depth > 0):
return 1
if unified in ('END IF', 'END FOR', 'END WHILE'):
return -1
# Default
return 0
def process(self, stream):
"""Process the stream"""
EOS_TTYPE = T.Whitespace, T.Comment.Single
# Run over all stream tokens
for ttype, value in stream:
# Yield token if we finished a statement and there's no whitespaces
# It will count newline token as a non whitespace. In this context
# whitespace ignores newlines.
# why don't multi line comments also count?
if self.consume_ws and ttype not in EOS_TTYPE:
yield sql.Statement(self.tokens)
# Reset filter and prepare to process next statement
self._reset()
# Change current split level (increase, decrease or remain equal)
self.level += self._change_splitlevel(ttype, value)
# Append the token to the current statement
self.tokens.append(sql.Token(ttype, value))
# Check if we get the end of a statement
if self.level <= 0 and ttype is T.Punctuation and value == ';':
self.consume_ws = True
# Yield pending statement (if any)
if self.tokens:
yield sql.Statement(self.tokens)
================================================
FILE: SQLToolsAPI/lib/sqlparse/exceptions.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""Exceptions used in this package."""
class SQLParseError(Exception):
"""Base class for exceptions in this module."""
================================================
FILE: SQLToolsAPI/lib/sqlparse/filters/__init__.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse.filters.others import SerializerUnicode
from sqlparse.filters.others import StripCommentsFilter
from sqlparse.filters.others import StripWhitespaceFilter
from sqlparse.filters.others import SpacesAroundOperatorsFilter
from sqlparse.filters.output import OutputPHPFilter
from sqlparse.filters.output import OutputPythonFilter
from sqlparse.filters.tokens import KeywordCaseFilter
from sqlparse.filters.tokens import IdentifierCaseFilter
from sqlparse.filters.tokens import TruncateStringFilter
from sqlparse.filters.reindent import ReindentFilter
from sqlparse.filters.right_margin import RightMarginFilter
from sqlparse.filters.aligned_indent import AlignedIndentFilter
__all__ = [
'SerializerUnicode',
'StripCommentsFilter',
'StripWhitespaceFilter',
'SpacesAroundOperatorsFilter',
'OutputPHPFilter',
'OutputPythonFilter',
'KeywordCaseFilter',
'IdentifierCaseFilter',
'TruncateStringFilter',
'ReindentFilter',
'RightMarginFilter',
'AlignedIndentFilter',
]
================================================
FILE: SQLToolsAPI/lib/sqlparse/filters/aligned_indent.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse import sql, tokens as T
from sqlparse.compat import text_type
from sqlparse.utils import offset, indent
class AlignedIndentFilter(object):
join_words = (r'((LEFT\s+|RIGHT\s+|FULL\s+)?'
r'(INNER\s+|OUTER\s+|STRAIGHT\s+)?|'
r'(CROSS\s+|NATURAL\s+)?)?JOIN\b')
split_words = ('FROM',
join_words, 'ON',
'WHERE', 'AND', 'OR',
'GROUP', 'HAVING', 'LIMIT',
'ORDER', 'UNION', 'VALUES',
'SET', 'BETWEEN', 'EXCEPT')
def __init__(self, char=' ', n='\n'):
self.n = n
self.offset = 0
self.indent = 0
self.char = char
self._max_kwd_len = len('select')
def nl(self, offset=1):
# offset = 1 represent a single space after SELECT
offset = -len(offset) if not isinstance(offset, int) else offset
# add two for the space and parens
indent = self.indent * (2 + self._max_kwd_len)
return sql.Token(T.Whitespace, self.n + self.char * (
self._max_kwd_len + offset + indent + self.offset))
def _process_statement(self, tlist):
if tlist.tokens[0].is_whitespace and self.indent == 0:
tlist.tokens.pop(0)
# process the main query body
self._process(sql.TokenList(tlist.tokens))
def _process_parenthesis(self, tlist):
# if this isn't a subquery, don't re-indent
_, token = tlist.token_next_by(m=(T.DML, 'SELECT'))
if token is not None:
with indent(self):
tlist.insert_after(tlist[0], self.nl('SELECT'))
# process the inside of the parantheses
self._process_default(tlist)
# de-indent last parenthesis
tlist.insert_before(tlist[-1], self.nl())
def _process_identifierlist(self, tlist):
# columns being selected
identifiers = list(tlist.get_identifiers())
identifiers.pop(0)
[tlist.insert_before(token, self.nl()) for token in identifiers]
self._process_default(tlist)
def _process_case(self, tlist):
offset_ = len('case ') + len('when ')
cases = tlist.get_cases(skip_ws=True)
# align the end as well
end_token = tlist.token_next_by(m=(T.Keyword, 'END'))[1]
cases.append((None, [end_token]))
condition_width = [len(' '.join(map(text_type, cond))) if cond else 0
for cond, _ in cases]
max_cond_width = max(condition_width)
for i, (cond, value) in enumerate(cases):
# cond is None when 'else or end'
stmt = cond[0] if cond else value[0]
if i > 0:
tlist.insert_before(stmt, self.nl(
offset_ - len(text_type(stmt))))
if cond:
ws = sql.Token(T.Whitespace, self.char * (
max_cond_width - condition_width[i]))
tlist.insert_after(cond[-1], ws)
def _next_token(self, tlist, idx=-1):
split_words = T.Keyword, self.split_words, True
tidx, token = tlist.token_next_by(m=split_words, idx=idx)
# treat "BETWEEN x and y" as a single statement
if token and token.normalized == 'BETWEEN':
tidx, token = self._next_token(tlist, tidx)
if token and token.normalized == 'AND':
tidx, token = self._next_token(tlist, tidx)
return tidx, token
def _split_kwds(self, tlist):
tidx, token = self._next_token(tlist)
while token:
# joins are special case. only consider the first word as aligner
if token.match(T.Keyword, self.join_words, regex=True):
token_indent = token.value.split()[0]
else:
token_indent = text_type(token)
tlist.insert_before(token, self.nl(token_indent))
tidx += 1
tidx, token = self._next_token(tlist, tidx)
def _process_default(self, tlist):
self._split_kwds(tlist)
# process any sub-sub statements
for sgroup in tlist.get_sublists():
idx = tlist.token_index(sgroup)
pidx, prev_ = tlist.token_prev(idx)
# HACK: make "group/order by" work. Longer than max_len.
offset_ = 3 if (prev_ and prev_.match(T.Keyword, 'BY')) else 0
with offset(self, offset_):
self._process(sgroup)
def _process(self, tlist):
func_name = '_process_{cls}'.format(cls=type(tlist).__name__)
func = getattr(self, func_name.lower(), self._process_default)
func(tlist)
def process(self, stmt):
self._process(stmt)
return stmt
================================================
FILE: SQLToolsAPI/lib/sqlparse/filters/others.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse import sql, tokens as T
from sqlparse.utils import split_unquoted_newlines
class StripCommentsFilter(object):
@staticmethod
def _process(tlist):
def get_next_comment():
# TODO(andi) Comment types should be unified, see related issue38
return tlist.token_next_by(i=sql.Comment, t=T.Comment)
tidx, token = get_next_comment()
while token:
pidx, prev_ = tlist.token_prev(tidx, skip_ws=False)
nidx, next_ = tlist.token_next(tidx, skip_ws=False)
# Replace by whitespace if prev and next exist and if they're not
# whitespaces. This doesn't apply if prev or next is a paranthesis.
if (prev_ is None or next_ is None or
prev_.is_whitespace or prev_.match(T.Punctuation, '(') or
next_.is_whitespace or next_.match(T.Punctuation, ')')):
tlist.tokens.remove(token)
else:
tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ')
tidx, token = get_next_comment()
def process(self, stmt):
[self.process(sgroup) for sgroup in stmt.get_sublists()]
StripCommentsFilter._process(stmt)
return stmt
class StripWhitespaceFilter(object):
def _stripws(self, tlist):
func_name = '_stripws_{cls}'.format(cls=type(tlist).__name__)
func = getattr(self, func_name.lower(), self._stripws_default)
func(tlist)
@staticmethod
def _stripws_default(tlist):
last_was_ws = False
is_first_char = True
for token in tlist.tokens:
if token.is_whitespace:
token.value = '' if last_was_ws or is_first_char else ' '
last_was_ws = token.is_whitespace
is_first_char = False
def _stripws_identifierlist(self, tlist):
# Removes newlines before commas, see issue140
last_nl = None
for token in list(tlist.tokens):
if last_nl and token.ttype is T.Punctuation and token.value == ',':
tlist.tokens.remove(last_nl)
last_nl = token if token.is_whitespace else None
# next_ = tlist.token_next(token, skip_ws=False)
# if (next_ and not next_.is_whitespace and
# token.ttype is T.Punctuation and token.value == ','):
# tlist.insert_after(token, sql.Token(T.Whitespace, ' '))
return self._stripws_default(tlist)
def _stripws_parenthesis(self, tlist):
if tlist.tokens[1].is_whitespace:
tlist.tokens.pop(1)
if tlist.tokens[-2].is_whitespace:
tlist.tokens.pop(-2)
self._stripws_default(tlist)
def process(self, stmt, depth=0):
[self.process(sgroup, depth + 1) for sgroup in stmt.get_sublists()]
self._stripws(stmt)
if depth == 0 and stmt.tokens and stmt.tokens[-1].is_whitespace:
stmt.tokens.pop(-1)
return stmt
class SpacesAroundOperatorsFilter(object):
@staticmethod
def _process(tlist):
ttypes = (T.Operator, T.Comparison)
tidx, token = tlist.token_next_by(t=ttypes)
while token:
nidx, next_ = tlist.token_next(tidx, skip_ws=False)
if next_ and next_.ttype != T.Whitespace:
tlist.insert_after(tidx, sql.Token(T.Whitespace, ' '))
pidx, prev_ = tlist.token_prev(tidx, skip_ws=False)
if prev_ and prev_.ttype != T.Whitespace:
tlist.insert_before(tidx, sql.Token(T.Whitespace, ' '))
tidx += 1 # has to shift since token inserted before it
# assert tlist.token_index(token) == tidx
tidx, token = tlist.token_next_by(t=ttypes, idx=tidx)
def process(self, stmt):
[self.process(sgroup) for sgroup in stmt.get_sublists()]
SpacesAroundOperatorsFilter._process(stmt)
return stmt
# ---------------------------
# postprocess
class SerializerUnicode(object):
@staticmethod
def process(stmt):
lines = split_unquoted_newlines(stmt)
return '\n'.join(line.rstrip() for line in lines)
================================================
FILE: SQLToolsAPI/lib/sqlparse/filters/output.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse import sql, tokens as T
from sqlparse.compat import text_type
class OutputFilter(object):
varname_prefix = ''
def __init__(self, varname='sql'):
self.varname = self.varname_prefix + varname
self.count = 0
def _process(self, stream, varname, has_nl):
raise NotImplementedError
def process(self, stmt):
self.count += 1
if self.count > 1:
varname = u'{f.varname}{f.count}'.format(f=self)
else:
varname = self.varname
has_nl = len(text_type(stmt).strip().splitlines()) > 1
stmt.tokens = self._process(stmt.tokens, varname, has_nl)
return stmt
class OutputPythonFilter(OutputFilter):
def _process(self, stream, varname, has_nl):
# SQL query asignation to varname
if self.count > 1:
yield sql.Token(T.Whitespace, '\n')
yield sql.Token(T.Name, varname)
yield sql.Token(T.Whitespace, ' ')
yield sql.Token(T.Operator, '=')
yield sql.Token(T.Whitespace, ' ')
if has_nl:
yield sql.Token(T.Operator, '(')
yield sql.Token(T.Text, "'")
# Print the tokens on the quote
for token in stream:
# Token is a new line separator
if token.is_whitespace and '\n' in token.value:
# Close quote and add a new line
yield sql.Token(T.Text, " '")
yield sql.Token(T.Whitespace, '\n')
# Quote header on secondary lines
yield sql.Token(T.Whitespace, ' ' * (len(varname) + 4))
yield sql.Token(T.Text, "'")
# Indentation
after_lb = token.value.split('\n', 1)[1]
if after_lb:
yield sql.Token(T.Whitespace, after_lb)
continue
# Token has escape chars
elif "'" in token.value:
token.value = token.value.replace("'", "\\'")
# Put the token
yield sql.Token(T.Text, token.value)
# Close quote
yield sql.Token(T.Text, "'")
if has_nl:
yield sql.Token(T.Operator, ')')
class OutputPHPFilter(OutputFilter):
varname_prefix = '$'
def _process(self, stream, varname, has_nl):
# SQL query asignation to varname (quote header)
if self.count > 1:
yield sql.Token(T.Whitespace, '\n')
yield sql.Token(T.Name, varname)
yield sql.Token(T.Whitespace, ' ')
if has_nl:
yield sql.Token(T.Whitespace, ' ')
yield sql.Token(T.Operator, '=')
yield sql.Token(T.Whitespace, ' ')
yield sql.Token(T.Text, '"')
# Print the tokens on the quote
for token in stream:
# Token is a new line separator
if token.is_whitespace and '\n' in token.value:
# Close quote and add a new line
yield sql.Token(T.Text, ' ";')
yield sql.Token(T.Whitespace, '\n')
# Quote header on secondary lines
yield sql.Token(T.Name, varname)
yield sql.Token(T.Whitespace, ' ')
yield sql.Token(T.Operator, '.=')
yield sql.Token(T.Whitespace, ' ')
yield sql.Token(T.Text, '"')
# Indentation
after_lb = token.value.split('\n', 1)[1]
if after_lb:
yield sql.Token(T.Whitespace, after_lb)
continue
# Token has escape chars
elif '"' in token.value:
token.value = token.value.replace('"', '\\"')
# Put the token
yield sql.Token(T.Text, token.value)
# Close quote
yield sql.Token(T.Text, '"')
yield sql.Token(T.Punctuation, ';')
================================================
FILE: SQLToolsAPI/lib/sqlparse/filters/reindent.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse import sql, tokens as T
from sqlparse.compat import text_type
from sqlparse.utils import offset, indent
class ReindentFilter(object):
def __init__(self, width=2, char=' ', wrap_after=0, n='\n',
comma_first=False):
self.n = n
self.width = width
self.char = char
self.indent = 0
self.offset = 0
self.wrap_after = wrap_after
self.comma_first = comma_first
self._curr_stmt = None
self._last_stmt = None
def _flatten_up_to_token(self, token):
"""Yields all tokens up to token but excluding current."""
if token.is_group:
token = next(token.flatten())
for t in self._curr_stmt.flatten():
if t == token:
break
yield t
@property
def leading_ws(self):
return self.offset + self.indent * self.width
def _get_offset(self, token):
raw = u''.join(map(text_type, self._flatten_up_to_token(token)))
line = (raw or '\n').splitlines()[-1]
# Now take current offset into account and return relative offset.
return len(line) - len(self.char * self.leading_ws)
def nl(self, offset=0):
return sql.Token(
T.Whitespace,
self.n + self.char * max(0, self.leading_ws + offset))
def _next_token(self, tlist, idx=-1):
split_words = ('FROM', 'STRAIGHT_JOIN$', 'JOIN$', 'AND', 'OR',
'GROUP', 'ORDER', 'UNION', 'VALUES',
'SET', 'BETWEEN', 'EXCEPT', 'HAVING', 'LIMIT')
m_split = T.Keyword, split_words, True
tidx, token = tlist.token_next_by(m=m_split, idx=idx)
if token and token.normalized == 'BETWEEN':
tidx, token = self._next_token(tlist, tidx)
if token and token.normalized == 'AND':
tidx, token = self._next_token(tlist, tidx)
return tidx, token
def _split_kwds(self, tlist):
tidx, token = self._next_token(tlist)
while token:
pidx, prev_ = tlist.token_prev(tidx, skip_ws=False)
uprev = text_type(prev_)
if prev_ and prev_.is_whitespace:
del tlist.tokens[pidx]
tidx -= 1
if not (uprev.endswith('\n') or uprev.endswith('\r')):
tlist.insert_before(tidx, self.nl())
tidx += 1
tidx, token = self._next_token(tlist, tidx)
def _split_statements(self, tlist):
ttypes = T.Keyword.DML, T.Keyword.DDL
tidx, token = tlist.token_next_by(t=ttypes)
while token:
pidx, prev_ = tlist.token_prev(tidx, skip_ws=False)
if prev_ and prev_.is_whitespace:
del tlist.tokens[pidx]
tidx -= 1
# only break if it's not the first token
if prev_:
tlist.insert_before(tidx, self.nl())
tidx += 1
tidx, token = tlist.token_next_by(t=ttypes, idx=tidx)
def _process(self, tlist):
func_name = '_process_{cls}'.format(cls=type(tlist).__name__)
func = getattr(self, func_name.lower(), self._process_default)
func(tlist)
def _process_where(self, tlist):
tidx, token = tlist.token_next_by(m=(T.Keyword, 'WHERE'))
# issue121, errors in statement fixed??
tlist.insert_before(tidx, self.nl())
with indent(self):
self._process_default(tlist)
def _process_parenthesis(self, tlist):
ttypes = T.Keyword.DML, T.Keyword.DDL
_, is_dml_dll = tlist.token_next_by(t=ttypes)
fidx, first = tlist.token_next_by(m=sql.Parenthesis.M_OPEN)
with indent(self, 1 if is_dml_dll else 0):
tlist.tokens.insert(0, self.nl()) if is_dml_dll else None
with offset(self, self._get_offset(first) + 1):
self._process_default(tlist, not is_dml_dll)
def _process_identifierlist(self, tlist):
identifiers = list(tlist.get_identifiers())
first = next(identifiers.pop(0).flatten())
num_offset = 1 if self.char == '\t' else self._get_offset(first)
if not tlist.within(sql.Function):
with offset(self, num_offset):
position = 0
for token in identifiers:
# Add 1 for the "," separator
position += len(token.value) + 1
if position > (self.wrap_after - self.offset):
adjust = 0
if self.comma_first:
adjust = -2
_, comma = tlist.token_prev(
tlist.token_index(token))
if comma is None:
continue
token = comma
tlist.insert_before(token, self.nl(offset=adjust))
if self.comma_first:
_, ws = tlist.token_next(
tlist.token_index(token), skip_ws=False)
if (ws is not None
and ws.ttype is not T.Text.Whitespace):
tlist.insert_after(
token, sql.Token(T.Whitespace, ' '))
position = 0
self._process_default(tlist)
def _process_case(self, tlist):
iterable = iter(tlist.get_cases())
cond, _ = next(iterable)
first = next(cond[0].flatten())
with offset(self, self._get_offset(tlist[0])):
with offset(self, self._get_offset(first)):
for cond, value in iterable:
token = value[0] if cond is None else cond[0]
tlist.insert_before(token, self.nl())
# Line breaks on group level are done. let's add an offset of
# len "when ", "then ", "else "
with offset(self, len("WHEN ")):
self._process_default(tlist)
end_idx, end = tlist.token_next_by(m=sql.Case.M_CLOSE)
if end_idx is not None:
tlist.insert_before(end_idx, self.nl())
def _process_default(self, tlist, stmts=True):
self._split_statements(tlist) if stmts else None
self._split_kwds(tlist)
for sgroup in tlist.get_sublists():
self._process(sgroup)
def process(self, stmt):
self._curr_stmt = stmt
self._process(stmt)
if self._last_stmt is not None:
nl = '\n' if text_type(self._last_stmt).endswith('\n') else '\n\n'
stmt.tokens.insert(0, sql.Token(T.Whitespace, nl))
self._last_stmt = stmt
return stmt
================================================
FILE: SQLToolsAPI/lib/sqlparse/filters/right_margin.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
import re
from sqlparse import sql, tokens as T
from sqlparse.compat import text_type
# FIXME: Doesn't work
class RightMarginFilter(object):
keep_together = (
# sql.TypeCast, sql.Identifier, sql.Alias,
)
def __init__(self, width=79):
self.width = width
self.line = ''
def _process(self, group, stream):
for token in stream:
if token.is_whitespace and '\n' in token.value:
if token.value.endswith('\n'):
self.line = ''
else:
self.line = token.value.splitlines()[-1]
elif token.is_group and type(token) not in self.keep_together:
token.tokens = self._process(token, token.tokens)
else:
val = text_type(token)
if len(self.line) + len(val) > self.width:
match = re.search(r'^ +', self.line)
if match is not None:
indent = match.group()
else:
indent = ''
yield sql.Token(T.Whitespace, '\n{0}'.format(indent))
self.line = indent
self.line += val
yield token
def process(self, group):
# return
# group.tokens = self._process(group, group.tokens)
raise NotImplementedError
================================================
FILE: SQLToolsAPI/lib/sqlparse/filters/tokens.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
from sqlparse import tokens as T
from sqlparse.compat import text_type
class _CaseFilter(object):
ttype = None
def __init__(self, case=None):
case = case or 'upper'
self.convert = getattr(text_type, case)
def process(self, stream):
for ttype, value in stream:
if ttype in self.ttype:
value = self.convert(value)
yield ttype, value
class KeywordCaseFilter(_CaseFilter):
ttype = T.Keyword
class IdentifierCaseFilter(_CaseFilter):
ttype = T.Name, T.String.Symbol
def process(self, stream):
for ttype, value in stream:
if ttype in self.ttype and value.strip()[0] != '"':
value = self.convert(value)
yield ttype, value
class TruncateStringFilter(object):
def __init__(self, width, char):
self.width = width
self.char = char
def process(self, stream):
for ttype, value in stream:
if ttype != T.Literal.String.Single:
yield ttype, value
continue
if value[:2] == "''":
inner = value[2:-2]
quote = "''"
else:
inner = value[1:-1]
quote = "'"
if len(inner) > self.width:
value = ''.join((quote, inner[:self.width], self.char, quote))
yield ttype, value
================================================
FILE: SQLToolsAPI/lib/sqlparse/formatter.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""SQL formatter"""
from sqlparse import filters
from sqlparse.exceptions import SQLParseError
def validate_options(options):
"""Validates options."""
kwcase = options.get('keyword_case')
if kwcase not in [None, 'upper', 'lower', 'capitalize']:
raise SQLParseError('Invalid value for keyword_case: '
'{0!r}'.format(kwcase))
idcase = options.get('identifier_case')
if idcase not in [None, 'upper', 'lower', 'capitalize']:
raise SQLParseError('Invalid value for identifier_case: '
'{0!r}'.format(idcase))
ofrmt = options.get('output_format')
if ofrmt not in [None, 'sql', 'python', 'php']:
raise SQLParseError('Unknown output format: '
'{0!r}'.format(ofrmt))
strip_comments = options.get('strip_comments', False)
if strip_comments not in [True, False]:
raise SQLParseError('Invalid value for strip_comments: '
'{0!r}'.format(strip_comments))
space_around_operators = options.get('use_space_around_operators', False)
if space_around_operators not in [True, False]:
raise SQLParseError('Invalid value for use_space_around_operators: '
'{0!r}'.format(space_around_operators))
strip_ws = options.get('strip_whitespace', False)
if strip_ws not in [True, False]:
raise SQLParseError('Invalid value for strip_whitespace: '
'{0!r}'.format(strip_ws))
truncate_strings = options.get('truncate_strings')
if truncate_strings is not None:
try:
truncate_strings = int(truncate_strings)
except (ValueError, TypeError):
raise SQLParseError('Invalid value for truncate_strings: '
'{0!r}'.format(truncate_strings))
if truncate_strings <= 1:
raise SQLParseError('Invalid value for truncate_strings: '
'{0!r}'.format(truncate_strings))
options['truncate_strings'] = truncate_strings
options['truncate_char'] = options.get('truncate_char', '[...]')
reindent = options.get('reindent', False)
if reindent not in [True, False]:
raise SQLParseError('Invalid value for reindent: '
'{0!r}'.format(reindent))
elif reindent:
options['strip_whitespace'] = True
reindent_aligned = options.get('reindent_aligned', False)
if reindent_aligned not in [True, False]:
raise SQLParseError('Invalid value for reindent_aligned: '
'{0!r}'.format(reindent))
elif reindent_aligned:
options['strip_whitespace'] = True
indent_tabs = options.get('indent_tabs', False)
if indent_tabs not in [True, False]:
raise SQLParseError('Invalid value for indent_tabs: '
'{0!r}'.format(indent_tabs))
elif indent_tabs:
options['indent_char'] = '\t'
else:
options['indent_char'] = ' '
indent_width = options.get('indent_width', 2)
try:
indent_width = int(indent_width)
except (TypeError, ValueError):
raise SQLParseError('indent_width requires an integer')
if indent_width < 1:
raise SQLParseError('indent_width requires a positive integer')
options['indent_width'] = indent_width
wrap_after = options.get('wrap_after', 0)
try:
wrap_after = int(wrap_after)
except (TypeError, ValueError):
raise SQLParseError('wrap_after requires an integer')
if wrap_after < 0:
raise SQLParseError('wrap_after requires a positive integer')
options['wrap_after'] = wrap_after
comma_first = options.get('comma_first', False)
if comma_first not in [True, False]:
raise SQLParseError('comma_first requires a boolean value')
options['comma_first'] = comma_first
right_margin = options.get('right_margin')
if right_margin is not None:
try:
right_margin = int(right_margin)
except (TypeError, ValueError):
raise SQLParseError('right_margin requires an integer')
if right_margin < 10:
raise SQLParseError('right_margin requires an integer > 10')
options['right_margin'] = right_margin
return options
def build_filter_stack(stack, options):
"""Setup and return a filter stack.
Args:
stack: :class:`~sqlparse.filters.FilterStack` instance
options: Dictionary with options validated by validate_options.
"""
# Token filter
if options.get('keyword_case'):
stack.preprocess.append(
filters.KeywordCaseFilter(options['keyword_case']))
if options.get('identifier_case'):
stack.preprocess.append(
filters.IdentifierCaseFilter(options['identifier_case']))
if options.get('truncate_strings'):
stack.preprocess.append(filters.TruncateStringFilter(
width=options['truncate_strings'], char=options['truncate_char']))
if options.get('use_space_around_operators', False):
stack.enable_grouping()
stack.stmtprocess.append(filters.SpacesAroundOperatorsFilter())
# After grouping
if options.get('strip_comments'):
stack.enable_grouping()
stack.stmtprocess.append(filters.StripCommentsFilter())
if options.get('strip_whitespace') or options.get('reindent'):
stack.enable_grouping()
stack.stmtprocess.append(filters.StripWhitespaceFilter())
if options.get('reindent'):
stack.enable_grouping()
stack.stmtprocess.append(
filters.ReindentFilter(char=options['indent_char'],
width=options['indent_width'],
wrap_after=options['wrap_after'],
comma_first=options['comma_first']))
if options.get('reindent_aligned', False):
stack.enable_grouping()
stack.stmtprocess.append(
filters.AlignedIndentFilter(char=options['indent_char']))
if options.get('right_margin'):
stack.enable_grouping()
stack.stmtprocess.append(
filters.RightMarginFilter(width=options['right_margin']))
# Serializer
if options.get('output_format'):
frmt = options['output_format']
if frmt.lower() == 'php':
fltr = filters.OutputPHPFilter()
elif frmt.lower() == 'python':
fltr = filters.OutputPythonFilter()
else:
fltr = None
if fltr is not None:
stack.postprocess.append(fltr)
return stack
================================================
FILE: SQLToolsAPI/lib/sqlparse/keywords.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
import re
from sqlparse import tokens
def is_keyword(value):
val = value.upper()
return (KEYWORDS_COMMON.get(val) or
KEYWORDS_ORACLE.get(val) or
KEYWORDS.get(val, tokens.Name)), value
SQL_REGEX = {
'root': [
(r'(--|# )\+.*?(\r\n|\r|\n|$)', tokens.Comment.Single.Hint),
(r'/\*\+[\s\S]*?\*/', tokens.Comment.Multiline.Hint),
(r'(--|# ).*?(\r\n|\r|\n|$)', tokens.Comment.Single),
(r'/\*[\s\S]*?\*/', tokens.Comment.Multiline),
(r'(\r\n|\r|\n)', tokens.Newline),
(r'\s+', tokens.Whitespace),
(r':=', tokens.Assignment),
(r'::', tokens.Punctuation),
(r'\*', tokens.Wildcard),
(r"`(``|[^`])*`", tokens.Name),
(r"´(´´|[^´])*´", tokens.Name),
(r'(\$(?:[_A-Z]\w*)?\$)[\s\S]*?\1', tokens.Literal),
(r'\?', tokens.Name.Placeholder),
(r'%(\(\w+\))?s', tokens.Name.Placeholder),
(r'(?=~!]+', tokens.Operator.Comparison),
(r'[+/@#%^&|`?^-]+', tokens.Operator),
]}
FLAGS = re.IGNORECASE | re.UNICODE
SQL_REGEX = [(re.compile(rx, FLAGS).match, tt) for rx, tt in SQL_REGEX['root']]
KEYWORDS = {
'ABORT': tokens.Keyword,
'ABS': tokens.Keyword,
'ABSOLUTE': tokens.Keyword,
'ACCESS': tokens.Keyword,
'ADA': tokens.Keyword,
'ADD': tokens.Keyword,
'ADMIN': tokens.Keyword,
'AFTER': tokens.Keyword,
'AGGREGATE': tokens.Keyword,
'ALIAS': tokens.Keyword,
'ALL': tokens.Keyword,
'ALLOCATE': tokens.Keyword,
'ANALYSE': tokens.Keyword,
'ANALYZE': tokens.Keyword,
'ANY': tokens.Keyword,
'ARRAYLEN': tokens.Keyword,
'ARE': tokens.Keyword,
'ASC': tokens.Keyword.Order,
'ASENSITIVE': tokens.Keyword,
'ASSERTION': tokens.Keyword,
'ASSIGNMENT': tokens.Keyword,
'ASYMMETRIC': tokens.Keyword,
'AT': tokens.Keyword,
'ATOMIC': tokens.Keyword,
'AUDIT': tokens.Keyword,
'AUTHORIZATION': tokens.Keyword,
'AVG': tokens.Keyword,
'BACKWARD': tokens.Keyword,
'BEFORE': tokens.Keyword,
'BEGIN': tokens.Keyword,
'BETWEEN': tokens.Keyword,
'BITVAR': tokens.Keyword,
'BIT_LENGTH': tokens.Keyword,
'BOTH': tokens.Keyword,
'BREADTH': tokens.Keyword,
# 'C': tokens.Keyword, # most likely this is an alias
'CACHE': tokens.Keyword,
'CALL': tokens.Keyword,
'CALLED': tokens.Keyword,
'CARDINALITY': tokens.Keyword,
'CASCADE': tokens.Keyword,
'CASCADED': tokens.Keyword,
'CAST': tokens.Keyword,
'CATALOG': tokens.Keyword,
'CATALOG_NAME': tokens.Keyword,
'CHAIN': tokens.Keyword,
'CHARACTERISTICS': tokens.Keyword,
'CHARACTER_LENGTH': tokens.Keyword,
'CHARACTER_SET_CATALOG': tokens.Keyword,
'CHARACTER_SET_NAME': tokens.Keyword,
'CHARACTER_SET_SCHEMA': tokens.Keyword,
'CHAR_LENGTH': tokens.Keyword,
'CHECK': tokens.Keyword,
'CHECKED': tokens.Keyword,
'CHECKPOINT': tokens.Keyword,
'CLASS': tokens.Keyword,
'CLASS_ORIGIN': tokens.Keyword,
'CLOB': tokens.Keyword,
'CLOSE': tokens.Keyword,
'CLUSTER': tokens.Keyword,
'COALESCE': tokens.Keyword,
'COBOL': tokens.Keyword,
'COLLATE': tokens.Keyword,
'COLLATION': tokens.Keyword,
'COLLATION_CATALOG': tokens.Keyword,
'COLLATION_NAME': tokens.Keyword,
'COLLATION_SCHEMA': tokens.Keyword,
'COLLECT': tokens.Keyword,
'COLUMN': tokens.Keyword,
'COLUMN_NAME': tokens.Keyword,
'COMPRESS': tokens.Keyword,
'COMMAND_FUNCTION': tokens.Keyword,
'COMMAND_FUNCTION_CODE': tokens.Keyword,
'COMMENT': tokens.Keyword,
'COMMIT': tokens.Keyword.DML,
'COMMITTED': tokens.Keyword,
'COMPLETION': tokens.Keyword,
'CONCURRENTLY': tokens.Keyword,
'CONDITION_NUMBER': tokens.Keyword,
'CONNECT': tokens.Keyword,
'CONNECTION': tokens.Keyword,
'CONNECTION_NAME': tokens.Keyword,
'CONSTRAINT': tokens.Keyword,
'CONSTRAINTS': tokens.Keyword,
'CONSTRAINT_CATALOG': tokens.Keyword,
'CONSTRAINT_NAME': tokens.Keyword,
'CONSTRAINT_SCHEMA': tokens.Keyword,
'CONSTRUCTOR': tokens.Keyword,
'CONTAINS': tokens.Keyword,
'CONTINUE': tokens.Keyword,
'CONVERSION': tokens.Keyword,
'CONVERT': tokens.Keyword,
'COPY': tokens.Keyword,
'CORRESPONTING': tokens.Keyword,
'COUNT': tokens.Keyword,
'CREATEDB': tokens.Keyword,
'CREATEUSER': tokens.Keyword,
'CROSS': tokens.Keyword,
'CUBE': tokens.Keyword,
'CURRENT': tokens.Keyword,
'CURRENT_DATE': tokens.Keyword,
'CURRENT_PATH': tokens.Keyword,
'CURRENT_ROLE': tokens.Keyword,
'CURRENT_TIME': tokens.Keyword,
'CURRENT_TIMESTAMP': tokens.Keyword,
'CURRENT_USER': tokens.Keyword,
'CURSOR': tokens.Keyword,
'CURSOR_NAME': tokens.Keyword,
'CYCLE': tokens.Keyword,
'DATA': tokens.Keyword,
'DATABASE': tokens.Keyword,
'DATETIME_INTERVAL_CODE': tokens.Keyword,
'DATETIME_INTERVAL_PRECISION': tokens.Keyword,
'DAY': tokens.Keyword,
'DEALLOCATE': tokens.Keyword,
'DECLARE': tokens.Keyword,
'DEFAULT': tokens.Keyword,
'DEFAULTS': tokens.Keyword,
'DEFERRABLE': tokens.Keyword,
'DEFERRED': tokens.Keyword,
'DEFINED': tokens.Keyword,
'DEFINER': tokens.Keyword,
'DELIMITER': tokens.Keyword,
'DELIMITERS': tokens.Keyword,
'DEREF': tokens.Keyword,
'DESC': tokens.Keyword.Order,
'DESCRIBE': tokens.Keyword,
'DESCRIPTOR': tokens.Keyword,
'DESTROY': tokens.Keyword,
'DESTRUCTOR': tokens.Keyword,
'DETERMINISTIC': tokens.Keyword,
'DIAGNOSTICS': tokens.Keyword,
'DICTIONARY': tokens.Keyword,
'DISABLE': tokens.Keyword,
'DISCONNECT': tokens.Keyword,
'DISPATCH': tokens.Keyword,
'DO': tokens.Keyword,
'DOMAIN': tokens.Keyword,
'DYNAMIC': tokens.Keyword,
'DYNAMIC_FUNCTION': tokens.Keyword,
'DYNAMIC_FUNCTION_CODE': tokens.Keyword,
'EACH': tokens.Keyword,
'ENABLE': tokens.Keyword,
'ENCODING': tokens.Keyword,
'ENCRYPTED': tokens.Keyword,
'END-EXEC': tokens.Keyword,
'EQUALS': tokens.Keyword,
'ESCAPE': tokens.Keyword,
'EVERY': tokens.Keyword,
'EXCEPT': tokens.Keyword,
'EXCEPTION': tokens.Keyword,
'EXCLUDING': tokens.Keyword,
'EXCLUSIVE': tokens.Keyword,
'EXEC': tokens.Keyword,
'EXECUTE': tokens.Keyword,
'EXISTING': tokens.Keyword,
'EXISTS': tokens.Keyword,
'EXTERNAL': tokens.Keyword,
'EXTRACT': tokens.Keyword,
'FALSE': tokens.Keyword,
'FETCH': tokens.Keyword,
'FILE': tokens.Keyword,
'FINAL': tokens.Keyword,
'FIRST': tokens.Keyword,
'FORCE': tokens.Keyword,
'FOREACH': tokens.Keyword,
'FOREIGN': tokens.Keyword,
'FORTRAN': tokens.Keyword,
'FORWARD': tokens.Keyword,
'FOUND': tokens.Keyword,
'FREE': tokens.Keyword,
'FREEZE': tokens.Keyword,
'FULL': tokens.Keyword,
'FUNCTION': tokens.Keyword,
# 'G': tokens.Keyword,
'GENERAL': tokens.Keyword,
'GENERATED': tokens.Keyword,
'GET': tokens.Keyword,
'GLOBAL': tokens.Keyword,
'GO': tokens.Keyword,
'GOTO': tokens.Keyword,
'GRANT': tokens.Keyword,
'GRANTED': tokens.Keyword,
'GROUPING': tokens.Keyword,
'HANDLER': tokens.Keyword,
'HAVING': tokens.Keyword,
'HIERARCHY': tokens.Keyword,
'HOLD': tokens.Keyword,
'HOST': tokens.Keyword,
'IDENTIFIED': tokens.Keyword,
'IDENTITY': tokens.Keyword,
'IGNORE': tokens.Keyword,
'ILIKE': tokens.Keyword,
'IMMEDIATE': tokens.Keyword,
'IMMUTABLE': tokens.Keyword,
'IMPLEMENTATION': tokens.Keyword,
'IMPLICIT': tokens.Keyword,
'INCLUDING': tokens.Keyword,
'INCREMENT': tokens.Keyword,
'INDEX': tokens.Keyword,
'INDITCATOR': tokens.Keyword,
'INFIX': tokens.Keyword,
'INHERITS': tokens.Keyword,
'INITIAL': tokens.Keyword,
'INITIALIZE': tokens.Keyword,
'INITIALLY': tokens.Keyword,
'INOUT': tokens.Keyword,
'INPUT': tokens.Keyword,
'INSENSITIVE': tokens.Keyword,
'INSTANTIABLE': tokens.Keyword,
'INSTEAD': tokens.Keyword,
'INTERSECT': tokens.Keyword,
'INTO': tokens.Keyword,
'INVOKER': tokens.Keyword,
'IS': tokens.Keyword,
'ISNULL': tokens.Keyword,
'ISOLATION': tokens.Keyword,
'ITERATE': tokens.Keyword,
# 'K': tokens.Keyword,
'KEY': tokens.Keyword,
'KEY_MEMBER': tokens.Keyword,
'KEY_TYPE': tokens.Keyword,
'LANCOMPILER': tokens.Keyword,
'LANGUAGE': tokens.Keyword,
'LARGE': tokens.Keyword,
'LAST': tokens.Keyword,
'LATERAL': tokens.Keyword,
'LEADING': tokens.Keyword,
'LENGTH': tokens.Keyword,
'LESS': tokens.Keyword,
'LEVEL': tokens.Keyword,
'LIMIT': tokens.Keyword,
'LISTEN': tokens.Keyword,
'LOAD': tokens.Keyword,
'LOCAL': tokens.Keyword,
'LOCALTIME': tokens.Keyword,
'LOCALTIMESTAMP': tokens.Keyword,
'LOCATION': tokens.Keyword,
'LOCATOR': tokens.Keyword,
'LOCK': tokens.Keyword,
'LOWER': tokens.Keyword,
# 'M': tokens.Keyword,
'MAP': tokens.Keyword,
'MATCH': tokens.Keyword,
'MAXEXTENTS': tokens.Keyword,
'MAXVALUE': tokens.Keyword,
'MESSAGE_LENGTH': tokens.Keyword,
'MESSAGE_OCTET_LENGTH': tokens.Keyword,
'MESSAGE_TEXT': tokens.Keyword,
'METHOD': tokens.Keyword,
'MINUTE': tokens.Keyword,
'MINUS': tokens.Keyword,
'MINVALUE': tokens.Keyword,
'MOD': tokens.Keyword,
'MODE': tokens.Keyword,
'MODIFIES': tokens.Keyword,
'MODIFY': tokens.Keyword,
'MONTH': tokens.Keyword,
'MORE': tokens.Keyword,
'MOVE': tokens.Keyword,
'MUMPS': tokens.Keyword,
'NAMES': tokens.Keyword,
'NATIONAL': tokens.Keyword,
'NATURAL': tokens.Keyword,
'NCHAR': tokens.Keyword,
'NCLOB': tokens.Keyword,
'NEW': tokens.Keyword,
'NEXT': tokens.Keyword,
'NO': tokens.Keyword,
'NOAUDIT': tokens.Keyword,
'NOCOMPRESS': tokens.Keyword,
'NOCREATEDB': tokens.Keyword,
'NOCREATEUSER': tokens.Keyword,
'NONE': tokens.Keyword,
'NOT': tokens.Keyword,
'NOTFOUND': tokens.Keyword,
'NOTHING': tokens.Keyword,
'NOTIFY': tokens.Keyword,
'NOTNULL': tokens.Keyword,
'NOWAIT': tokens.Keyword,
'NULL': tokens.Keyword,
'NULLABLE': tokens.Keyword,
'NULLIF': tokens.Keyword,
'OBJECT': tokens.Keyword,
'OCTET_LENGTH': tokens.Keyword,
'OF': tokens.Keyword,
'OFF': tokens.Keyword,
'OFFLINE': tokens.Keyword,
'OFFSET': tokens.Keyword,
'OIDS': tokens.Keyword,
'OLD': tokens.Keyword,
'ONLINE': tokens.Keyword,
'ONLY': tokens.Keyword,
'OPEN': tokens.Keyword,
'OPERATION': tokens.Keyword,
'OPERATOR': tokens.Keyword,
'OPTION': tokens.Keyword,
'OPTIONS': tokens.Keyword,
'ORDINALITY': tokens.Keyword,
'OUT': tokens.Keyword,
'OUTPUT': tokens.Keyword,
'OVERLAPS': tokens.Keyword,
'OVERLAY': tokens.Keyword,
'OVERRIDING': tokens.Keyword,
'OWNER': tokens.Keyword,
'PAD': tokens.Keyword,
'PARAMETER': tokens.Keyword,
'PARAMETERS': tokens.Keyword,
'PARAMETER_MODE': tokens.Keyword,
'PARAMATER_NAME': tokens.Keyword,
'PARAMATER_ORDINAL_POSITION': tokens.Keyword,
'PARAMETER_SPECIFIC_CATALOG': tokens.Keyword,
'PARAMETER_SPECIFIC_NAME': tokens.Keyword,
'PARAMATER_SPECIFIC_SCHEMA': tokens.Keyword,
'PARTIAL': tokens.Keyword,
'PASCAL': tokens.Keyword,
'PCTFREE': tokens.Keyword,
'PENDANT': tokens.Keyword,
'PLACING': tokens.Keyword,
'PLI': tokens.Keyword,
'POSITION': tokens.Keyword,
'POSTFIX': tokens.Keyword,
'PRECISION': tokens.Keyword,
'PREFIX': tokens.Keyword,
'PREORDER': tokens.Keyword,
'PREPARE': tokens.Keyword,
'PRESERVE': tokens.Keyword,
'PRIMARY': tokens.Keyword,
'PRIOR': tokens.Keyword,
'PRIVILEGES': tokens.Keyword,
'PROCEDURAL': tokens.Keyword,
'PROCEDURE': tokens.Keyword,
'PUBLIC': tokens.Keyword,
'RAISE': tokens.Keyword,
'RAW': tokens.Keyword,
'READ': tokens.Keyword,
'READS': tokens.Keyword,
'RECHECK': tokens.Keyword,
'RECURSIVE': tokens.Keyword,
'REF': tokens.Keyword,
'REFERENCES': tokens.Keyword,
'REFERENCING': tokens.Keyword,
'REINDEX': tokens.Keyword,
'RELATIVE': tokens.Keyword,
'RENAME': tokens.Keyword,
'REPEATABLE': tokens.Keyword,
'RESET': tokens.Keyword,
'RESOURCE': tokens.Keyword,
'RESTART': tokens.Keyword,
'RESTRICT': tokens.Keyword,
'RESULT': tokens.Keyword,
'RETURN': tokens.Keyword,
'RETURNED_LENGTH': tokens.Keyword,
'RETURNED_OCTET_LENGTH': tokens.Keyword,
'RETURNED_SQLSTATE': tokens.Keyword,
'RETURNING': tokens.Keyword,
'RETURNS': tokens.Keyword,
'REVOKE': tokens.Keyword,
'RIGHT': tokens.Keyword,
'ROLE': tokens.Keyword,
'ROLLBACK': tokens.Keyword.DML,
'ROLLUP': tokens.Keyword,
'ROUTINE': tokens.Keyword,
'ROUTINE_CATALOG': tokens.Keyword,
'ROUTINE_NAME': tokens.Keyword,
'ROUTINE_SCHEMA': tokens.Keyword,
'ROW': tokens.Keyword,
'ROWS': tokens.Keyword,
'ROW_COUNT': tokens.Keyword,
'RULE': tokens.Keyword,
'SAVE_POINT': tokens.Keyword,
'SCALE': tokens.Keyword,
'SCHEMA': tokens.Keyword,
'SCHEMA_NAME': tokens.Keyword,
'SCOPE': tokens.Keyword,
'SCROLL': tokens.Keyword,
'SEARCH': tokens.Keyword,
'SECOND': tokens.Keyword,
'SECURITY': tokens.Keyword,
'SELF': tokens.Keyword,
'SENSITIVE': tokens.Keyword,
'SEQUENCE': tokens.Keyword,
'SERIALIZABLE': tokens.Keyword,
'SERVER_NAME': tokens.Keyword,
'SESSION': tokens.Keyword,
'SESSION_USER': tokens.Keyword,
'SETOF': tokens.Keyword,
'SETS': tokens.Keyword,
'SHARE': tokens.Keyword,
'SHOW': tokens.Keyword,
'SIMILAR': tokens.Keyword,
'SIMPLE': tokens.Keyword,
'SIZE': tokens.Keyword,
'SOME': tokens.Keyword,
'SOURCE': tokens.Keyword,
'SPACE': tokens.Keyword,
'SPECIFIC': tokens.Keyword,
'SPECIFICTYPE': tokens.Keyword,
'SPECIFIC_NAME': tokens.Keyword,
'SQL': tokens.Keyword,
'SQLBUF': tokens.Keyword,
'SQLCODE': tokens.Keyword,
'SQLERROR': tokens.Keyword,
'SQLEXCEPTION': tokens.Keyword,
'SQLSTATE': tokens.Keyword,
'SQLWARNING': tokens.Keyword,
'STABLE': tokens.Keyword,
'START': tokens.Keyword.DML,
# 'STATE': tokens.Keyword,
'STATEMENT': tokens.Keyword,
'STATIC': tokens.Keyword,
'STATISTICS': tokens.Keyword,
'STDIN': tokens.Keyword,
'STDOUT': tokens.Keyword,
'STORAGE': tokens.Keyword,
'STRICT': tokens.Keyword,
'STRUCTURE': tokens.Keyword,
'STYPE': tokens.Keyword,
'SUBCLASS_ORIGIN': tokens.Keyword,
'SUBLIST': tokens.Keyword,
'SUBSTRING': tokens.Keyword,
'SUCCESSFUL': tokens.Keyword,
'SUM': tokens.Keyword,
'SYMMETRIC': tokens.Keyword,
'SYNONYM': tokens.Keyword,
'SYSID': tokens.Keyword,
'SYSTEM': tokens.Keyword,
'SYSTEM_USER': tokens.Keyword,
'TABLE': tokens.Keyword,
'TABLE_NAME': tokens.Keyword,
'TEMP': tokens.Keyword,
'TEMPLATE': tokens.Keyword,
'TEMPORARY': tokens.Keyword,
'TERMINATE': tokens.Keyword,
'THAN': tokens.Keyword,
'TIMESTAMP': tokens.Keyword,
'TIMEZONE_HOUR': tokens.Keyword,
'TIMEZONE_MINUTE': tokens.Keyword,
'TO': tokens.Keyword,
'TOAST': tokens.Keyword,
'TRAILING': tokens.Keyword,
'TRANSATION': tokens.Keyword,
'TRANSACTIONS_COMMITTED': tokens.Keyword,
'TRANSACTIONS_ROLLED_BACK': tokens.Keyword,
'TRANSATION_ACTIVE': tokens.Keyword,
'TRANSFORM': tokens.Keyword,
'TRANSFORMS': tokens.Keyword,
'TRANSLATE': tokens.Keyword,
'TRANSLATION': tokens.Keyword,
'TREAT': tokens.Keyword,
'TRIGGER': tokens.Keyword,
'TRIGGER_CATALOG': tokens.Keyword,
'TRIGGER_NAME': tokens.Keyword,
'TRIGGER_SCHEMA': tokens.Keyword,
'TRIM': tokens.Keyword,
'TRUE': tokens.Keyword,
'TRUNCATE': tokens.Keyword,
'TRUSTED': tokens.Keyword,
'TYPE': tokens.Keyword,
'UID': tokens.Keyword,
'UNCOMMITTED': tokens.Keyword,
'UNDER': tokens.Keyword,
'UNENCRYPTED': tokens.Keyword,
'UNION': tokens.Keyword,
'UNIQUE': tokens.Keyword,
'UNKNOWN': tokens.Keyword,
'UNLISTEN': tokens.Keyword,
'UNNAMED': tokens.Keyword,
'UNNEST': tokens.Keyword,
'UNTIL': tokens.Keyword,
'UPPER': tokens.Keyword,
'USAGE': tokens.Keyword,
'USE': tokens.Keyword,
'USER': tokens.Keyword,
'USER_DEFINED_TYPE_CATALOG': tokens.Keyword,
'USER_DEFINED_TYPE_NAME': tokens.Keyword,
'USER_DEFINED_TYPE_SCHEMA': tokens.Keyword,
'USING': tokens.Keyword,
'VACUUM': tokens.Keyword,
'VALID': tokens.Keyword,
'VALIDATE': tokens.Keyword,
'VALIDATOR': tokens.Keyword,
'VALUES': tokens.Keyword,
'VARIABLE': tokens.Keyword,
'VERBOSE': tokens.Keyword,
'VERSION': tokens.Keyword,
'VIEW': tokens.Keyword,
'VOLATILE': tokens.Keyword,
'WHENEVER': tokens.Keyword,
'WITH': tokens.Keyword.CTE,
'WITHOUT': tokens.Keyword,
'WORK': tokens.Keyword,
'WRITE': tokens.Keyword,
'YEAR': tokens.Keyword,
'ZONE': tokens.Keyword,
# Name.Builtin
'ARRAY': tokens.Name.Builtin,
'BIGINT': tokens.Name.Builtin,
'BINARY': tokens.Name.Builtin,
'BIT': tokens.Name.Builtin,
'BLOB': tokens.Name.Builtin,
'BOOLEAN': tokens.Name.Builtin,
'CHAR': tokens.Name.Builtin,
'CHARACTER': tokens.Name.Builtin,
'DATE': tokens.Name.Builtin,
'DEC': tokens.Name.Builtin,
'DECIMAL': tokens.Name.Builtin,
'FLOAT': tokens.Name.Builtin,
'INT': tokens.Name.Builtin,
'INT8': tokens.Name.Builtin,
'INTEGER': tokens.Name.Builtin,
'INTERVAL': tokens.Name.Builtin,
'LONG': tokens.Name.Builtin,
'NUMBER': tokens.Name.Builtin,
'NUMERIC': tokens.Name.Builtin,
'REAL': tokens.Name.Builtin,
'ROWID': tokens.Name.Builtin,
'ROWLABEL': tokens.Name.Builtin,
'ROWNUM': tokens.Name.Builtin,
'SERIAL': tokens.Name.Builtin,
'SERIAL8': tokens.Name.Builtin,
'SIGNED': tokens.Name.Builtin,
'SMALLINT': tokens.Name.Builtin,
'SYSDATE': tokens.Name.Builtin,
'TEXT': tokens.Name.Builtin,
'TINYINT': tokens.Name.Builtin,
'UNSIGNED': tokens.Name.Builtin,
'VARCHAR': tokens.Name.Builtin,
'VARCHAR2': tokens.Name.Builtin,
'VARYING': tokens.Name.Builtin,
}
KEYWORDS_COMMON = {
'SELECT': tokens.Keyword.DML,
'INSERT': tokens.Keyword.DML,
'DELETE': tokens.Keyword.DML,
'UPDATE': tokens.Keyword.DML,
'REPLACE': tokens.Keyword.DML,
'MERGE': tokens.Keyword.DML,
'DROP': tokens.Keyword.DDL,
'CREATE': tokens.Keyword.DDL,
'ALTER': tokens.Keyword.DDL,
'WHERE': tokens.Keyword,
'FROM': tokens.Keyword,
'INNER': tokens.Keyword,
'JOIN': tokens.Keyword,
'STRAIGHT_JOIN': tokens.Keyword,
'AND': tokens.Keyword,
'OR': tokens.Keyword,
'LIKE': tokens.Keyword,
'ON': tokens.Keyword,
'IN': tokens.Keyword,
'SET': tokens.Keyword,
'BY': tokens.Keyword,
'GROUP': tokens.Keyword,
'ORDER': tokens.Keyword,
'LEFT': tokens.Keyword,
'OUTER': tokens.Keyword,
'FULL': tokens.Keyword,
'IF': tokens.Keyword,
'END': tokens.Keyword,
'THEN': tokens.Keyword,
'LOOP': tokens.Keyword,
'AS': tokens.Keyword,
'ELSE': tokens.Keyword,
'FOR': tokens.Keyword,
'WHILE': tokens.Keyword,
'CASE': tokens.Keyword,
'WHEN': tokens.Keyword,
'MIN': tokens.Keyword,
'MAX': tokens.Keyword,
'DISTINCT': tokens.Keyword,
}
KEYWORDS_ORACLE = {
'ARCHIVE': tokens.Keyword,
'ARCHIVELOG': tokens.Keyword,
'BACKUP': tokens.Keyword,
'BECOME': tokens.Keyword,
'BLOCK': tokens.Keyword,
'BODY': tokens.Keyword,
'CANCEL': tokens.Keyword,
'CHANGE': tokens.Keyword,
'COMPILE': tokens.Keyword,
'CONTENTS': tokens.Keyword,
'CONTROLFILE': tokens.Keyword,
'DATAFILE': tokens.Keyword,
'DBA': tokens.Keyword,
'DISMOUNT': tokens.Keyword,
'DOUBLE': tokens.Keyword,
'DUMP': tokens.Keyword,
'EVENTS': tokens.Keyword,
'EXCEPTIONS': tokens.Keyword,
'EXPLAIN': tokens.Keyword,
'EXTENT': tokens.Keyword,
'EXTERNALLY': tokens.Keyword,
'FLUSH': tokens.Keyword,
'FREELIST': tokens.Keyword,
'FREELISTS': tokens.Keyword,
# groups seems too common as table name
# 'GROUPS': tokens.Keyword,
'INDICATOR': tokens.Keyword,
'INITRANS': tokens.Keyword,
'INSTANCE': tokens.Keyword,
'LAYER': tokens.Keyword,
'LINK': tokens.Keyword,
'LISTS': tokens.Keyword,
'LOGFILE': tokens.Keyword,
'MANAGE': tokens.Keyword,
'MANUAL': tokens.Keyword,
'MAXDATAFILES': tokens.Keyword,
'MAXINSTANCES': tokens.Keyword,
'MAXLOGFILES': tokens.Keyword,
'MAXLOGHISTORY': tokens.Keyword,
'MAXLOGMEMBERS': tokens.Keyword,
'MAXTRANS': tokens.Keyword,
'MINEXTENTS': tokens.Keyword,
'MODULE': tokens.Keyword,
'MOUNT': tokens.Keyword,
'NOARCHIVELOG': tokens.Keyword,
'NOCACHE': tokens.Keyword,
'NOCYCLE': tokens.Keyword,
'NOMAXVALUE': tokens.Keyword,
'NOMINVALUE': tokens.Keyword,
'NOORDER': tokens.Keyword,
'NORESETLOGS': tokens.Keyword,
'NORMAL': tokens.Keyword,
'NOSORT': tokens.Keyword,
'OPTIMAL': tokens.Keyword,
'OWN': tokens.Keyword,
'PACKAGE': tokens.Keyword,
'PARALLEL': tokens.Keyword,
'PCTINCREASE': tokens.Keyword,
'PCTUSED': tokens.Keyword,
'PLAN': tokens.Keyword,
'PRIVATE': tokens.Keyword,
'PROFILE': tokens.Keyword,
'QUOTA': tokens.Keyword,
'RECOVER': tokens.Keyword,
'RESETLOGS': tokens.Keyword,
'RESTRICTED': tokens.Keyword,
'REUSE': tokens.Keyword,
'ROLES': tokens.Keyword,
'SAVEPOINT': tokens.Keyword,
'SCN': tokens.Keyword,
'SECTION': tokens.Keyword,
'SEGMENT': tokens.Keyword,
'SHARED': tokens.Keyword,
'SNAPSHOT': tokens.Keyword,
'SORT': tokens.Keyword,
'STATEMENT_ID': tokens.Keyword,
'STOP': tokens.Keyword,
'SWITCH': tokens.Keyword,
'TABLES': tokens.Keyword,
'TABLESPACE': tokens.Keyword,
'THREAD': tokens.Keyword,
'TIME': tokens.Keyword,
'TRACING': tokens.Keyword,
'TRANSACTION': tokens.Keyword,
'TRIGGERS': tokens.Keyword,
'UNLIMITED': tokens.Keyword,
}
================================================
FILE: SQLToolsAPI/lib/sqlparse/lexer.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""SQL Lexer"""
# This code is based on the SqlLexer in pygments.
# http://pygments.org/
# It's separated from the rest of pygments to increase performance
# and to allow some customizations.
from sqlparse import tokens
from sqlparse.keywords import SQL_REGEX
from sqlparse.compat import bytes_type, text_type, file_types
from sqlparse.utils import consume
class Lexer(object):
"""Lexer
Empty class. Leaving for backwards-compatibility
"""
@staticmethod
def get_tokens(text, encoding=None):
"""
Return an iterable of (tokentype, value) pairs generated from
`text`. If `unfiltered` is set to `True`, the filtering mechanism
is bypassed even if filters are defined.
Also preprocess the text, i.e. expand tabs and strip it if
wanted and applies registered filters.
Split ``text`` into (tokentype, text) pairs.
``stack`` is the inital stack (default: ``['root']``)
"""
if isinstance(text, file_types):
text = text.read()
if isinstance(text, text_type):
pass
elif isinstance(text, bytes_type):
if encoding:
text = text.decode(encoding)
else:
try:
text = text.decode('utf-8')
except UnicodeDecodeError:
text = text.decode('unicode-escape')
else:
raise TypeError(u"Expected text or file-like object, got {!r}".
format(type(text)))
iterable = enumerate(text)
for pos, char in iterable:
for rexmatch, action in SQL_REGEX:
m = rexmatch(text, pos)
if not m:
continue
elif isinstance(action, tokens._TokenType):
yield action, m.group()
elif callable(action):
yield action(m.group())
consume(iterable, m.end() - pos - 1)
break
else:
yield tokens.Error, char
def tokenize(sql, encoding=None):
"""Tokenize sql.
Tokenize *sql* using the :class:`Lexer` and return a 2-tuple stream
of ``(token type, value)`` items.
"""
return Lexer().get_tokens(sql, encoding)
================================================
FILE: SQLToolsAPI/lib/sqlparse/sql.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
"""This module contains classes representing syntactical elements of SQL."""
from __future__ import print_function
import re
from sqlparse import tokens as T
from sqlparse.compat import string_types, text_type, unicode_compatible
from sqlparse.utils import imt, remove_quotes
@unicode_compatible
class Token(object):
"""Base class for all other classes in this module.
It represents a single token and has two instance attributes:
``value`` is the unchange value of the token and ``ttype`` is
the type of the token.
"""
__slots__ = ('value', 'ttype', 'parent', 'normalized', 'is_keyword',
'is_group', 'is_whitespace')
def __init__(self, ttype, value):
value = text_type(value)
self.value = value
self.ttype = ttype
self.parent = None
self.is_group = False
self.is_keyword = ttype in T.Keyword
self.is_whitespace = self.ttype in T.Whitespace
self.normalized = value.upper() if self.is_keyword else value
def __str__(self):
return self.value
# Pending tokenlist __len__ bug fix
# def __len__(self):
# return len(self.value)
def __repr__(self):
cls = self._get_repr_name()
value = self._get_repr_value()
q = u'"' if value.startswith("'") and value.endswith("'") else u"'"
return u"<{cls} {q}{value}{q} at 0x{id:2X}>".format(
id=id(self), **locals())
def _get_repr_name(self):
return str(self.ttype).split('.')[-1]
def _get_repr_value(self):
raw = text_type(self)
if len(raw) > 7:
raw = raw[:6] + '...'
return re.sub(r'\s+', ' ', raw)
def flatten(self):
"""Resolve subgroups."""
yield self
def match(self, ttype, values, regex=False):
"""Checks whether the token matches the given arguments.
*ttype* is a token type. If this token doesn't match the given token
type.
*values* is a list of possible values for this token. The values
are OR'ed together so if only one of the values matches ``True``
is returned. Except for keyword tokens the comparison is
case-sensitive. For convenience it's ok to pass in a single string.
If *regex* is ``True`` (default is ``False``) the given values are
treated as regular expressions.
"""
type_matched = self.ttype is ttype
if not type_matched or values is None:
return type_matched
if isinstance(values, string_types):
values = (values,)
if regex:
# TODO: Add test for regex with is_keyboard = false
flag = re.IGNORECASE if self.is_keyword else 0
values = (re.compile(v, flag) for v in values)
for pattern in values:
if pattern.search(self.normalized):
return True
return False
if self.is_keyword:
values = (v.upper() for v in values)
return self.normalized in values
def within(self, group_cls):
"""Returns ``True`` if this token is within *group_cls*.
Use this method for example to check if an identifier is within
a function: ``t.within(sql.Function)``.
"""
parent = self.parent
while parent:
if isinstance(parent, group_cls):
return True
parent = parent.parent
return False
def is_child_of(self, other):
"""Returns ``True`` if this token is a direct child of *other*."""
return self.parent == other
def has_ancestor(self, other):
"""Returns ``True`` if *other* is in this tokens ancestry."""
parent = self.parent
while parent:
if parent == other:
return True
parent = parent.parent
return False
@unicode_compatible
class TokenList(Token):
"""A group of tokens.
It has an additional instance attribute ``tokens`` which holds a
list of child-tokens.
"""
__slots__ = 'tokens'
def __init__(self, tokens=None):
self.tokens = tokens or []
[setattr(token, 'parent', self) for token in tokens]
super(TokenList, self).__init__(None, text_type(self))
self.is_group = True
def __str__(self):
return u''.join(token.value for token in self.flatten())
# weird bug
# def __len__(self):
# return len(self.tokens)
def __iter__(self):
return iter(self.tokens)
def __getitem__(self, item):
return self.tokens[item]
def _get_repr_name(self):
return type(self).__name__
def _pprint_tree(self, max_depth=None, depth=0, f=None):
"""Pretty-print the object tree."""
indent = u' | ' * depth
for idx, token in enumerate(self.tokens):
cls = token._get_repr_name()
value = token._get_repr_value()
q = u'"' if value.startswith("'") and value.endswith("'") else u"'"
print(u"{indent}{idx:2d} {cls} {q}{value}{q}"
.format(**locals()), file=f)
if token.is_group and (max_depth is None or depth < max_depth):
token._pprint_tree(max_depth, depth + 1, f)
def get_token_at_offset(self, offset):
"""Returns the token that is on position offset."""
idx = 0
for token in self.flatten():
end = idx + len(token.value)
if idx <= offset < end:
return token
idx = end
def flatten(self):
"""Generator yielding ungrouped tokens.
This method is recursively called for all child tokens.
"""
for token in self.tokens:
if token.is_group:
for item in token.flatten():
yield item
else:
yield token
def get_sublists(self):
for token in self.tokens:
if token.is_group:
yield token
@property
def _groupable_tokens(self):
return self.tokens
def _token_matching(self, funcs, start=0, end=None, reverse=False):
"""next token that match functions"""
if start is None:
return None
if not isinstance(funcs, (list, tuple)):
funcs = (funcs,)
if reverse:
assert end is None
for idx in range(start - 2, -1, -1):
token = self.tokens[idx]
for func in funcs:
if func(token):
return idx, token
else:
for idx, token in enumerate(self.tokens[start:end], start=start):
for func in funcs:
if func(token):
return idx, token
return None, None
def token_first(self, skip_ws=True, skip_cm=False):
"""Returns the first child token.
If *skip_ws* is ``True`` (the default), whitespace
tokens are ignored.
if *skip_cm* is ``True`` (default: ``False``), comments are
ignored too.
"""
# this on is inconsistent, using Comment instead of T.Comment...
funcs = lambda tk: not ((skip_ws and tk.is_whitespace) or
(skip_cm and imt(tk, t=T.Comment, i=Comment)))
return self._token_matching(funcs)[1]
def token_next_by(self, i=None, m=None, t=None, idx=-1, end=None):
funcs = lambda tk: imt(tk, i, m, t)
idx += 1
return self._token_matching(funcs, idx, end)
def token_not_matching(self, funcs, idx):
funcs = (funcs,) if not isinstance(funcs, (list, tuple)) else funcs
funcs = [lambda tk: not func(tk) for func in funcs]
return self._token_matching(funcs, idx)
def token_matching(self, funcs, idx):
return self._token_matching(funcs, idx)[1]
def token_prev(self, idx, skip_ws=True, skip_cm=False):
"""Returns the previous token relative to *idx*.
If *skip_ws* is ``True`` (the default) whitespace tokens are ignored.
If *skip_cm* is ``True`` comments are ignored.
``None`` is returned if there's no previous token.
"""
return self.token_next(idx, skip_ws, skip_cm, _reverse=True)
# TODO: May need to re-add default value to idx
def token_next(self, idx, skip_ws=True, skip_cm=False, _reverse=False):
"""Returns the next token relative to *idx*.
If *skip_ws* is ``True`` (the default) whitespace tokens are ignored.
If *skip_cm* is ``True`` comments are ignored.
``None`` is returned if there's no next token.
"""
if idx is None:
return None, None
idx += 1 # alot of code usage current pre-compensates for this
funcs = lambda tk: not ((skip_ws and tk.is_whitespace) or
(skip_cm and imt(tk, t=T.Comment, i=Comment)))
return self._token_matching(funcs, idx, reverse=_reverse)
def token_index(self, token, start=0):
"""Return list index of token."""
start = start if isinstance(start, int) else self.token_index(start)
return start + self.tokens[start:].index(token)
def group_tokens(self, grp_cls, start, end, include_end=True,
extend=False):
"""Replace tokens by an instance of *grp_cls*."""
start_idx = start
start = self.tokens[start_idx]
end_idx = end + include_end
# will be needed later for new group_clauses
# while skip_ws and tokens and tokens[-1].is_whitespace:
# tokens = tokens[:-1]
if extend and isinstance(start, grp_cls):
subtokens = self.tokens[start_idx + 1:end_idx]
grp = start
grp.tokens.extend(subtokens)
del self.tokens[start_idx + 1:end_idx]
grp.value = text_type(start)
else:
subtokens = self.tokens[start_idx:end_idx]
grp = grp_cls(subtokens)
self.tokens[start_idx:end_idx] = [grp]
grp.parent = self
for token in subtokens:
token.parent = grp
return grp
def insert_before(self, where, token):
"""Inserts *token* before *where*."""
if not isinstance(where, int):
where = self.token_index(where)
token.parent = self
self.tokens.insert(where, token)
def insert_after(self, where, token, skip_ws=True):
"""Inserts *token* after *where*."""
if not isinstance(where, int):
where = self.token_index(where)
nidx, next_ = self.token_next(where, skip_ws=skip_ws)
token.parent = self
if next_ is None:
self.tokens.append(token)
else:
self.tokens.insert(nidx, token)
def has_alias(self):
"""Returns ``True`` if an alias is present."""
return self.get_alias() is not None
def get_alias(self):
"""Returns the alias for this identifier or ``None``."""
# "name AS alias"
kw_idx, kw = self.token_next_by(m=(T.Keyword, 'AS'))
if kw is not None:
return self._get_first_name(kw_idx + 1, keywords=True)
# "name alias" or "complicated column expression alias"
_, ws = self.token_next_by(t=T.Whitespace)
if len(self.tokens) > 2 and ws is not None:
return self._get_first_name(reverse=True)
def get_name(self):
"""Returns the name of this identifier.
This is either it's alias or it's real name. The returned valued can
be considered as the name under which the object corresponding to
this identifier is known within the current statement.
"""
return self.get_alias() or self.get_real_name()
def get_real_name(self):
"""Returns the real name (object name) of this identifier."""
# a.b
dot_idx, _ = self.token_next_by(m=(T.Punctuation, '.'))
return self._get_first_name(dot_idx)
def get_parent_name(self):
"""Return name of the parent object if any.
A parent object is identified by the first occuring dot.
"""
dot_idx, _ = self.token_next_by(m=(T.Punctuation, '.'))
_, prev_ = self.token_prev(dot_idx)
return remove_quotes(prev_.value) if prev_ is not None else None
def _get_first_name(self, idx=None, reverse=False, keywords=False):
"""Returns the name of the first token with a name"""
tokens = self.tokens[idx:] if idx else self.tokens
tokens = reversed(tokens) if reverse else tokens
types = [T.Name, T.Wildcard, T.String.Symbol]
if keywords:
types.append(T.Keyword)
for token in tokens:
if token.ttype in types:
return remove_quotes(token.value)
elif isinstance(token, (Identifier, Function)):
return token.get_name()
class Statement(TokenList):
"""Represents a SQL statement."""
def get_type(self):
"""Returns the type of a statement.
The returned value is a string holding an upper-cased reprint of
the first DML or DDL keyword. If the first token in this group
isn't a DML or DDL keyword "UNKNOWN" is returned.
Whitespaces and comments at the beginning of the statement
are ignored.
"""
first_token = self.token_first(skip_cm=True)
if first_token is None:
# An "empty" statement that either has not tokens at all
# or only whitespace tokens.
return 'UNKNOWN'
elif first_token.ttype in (T.Keyword.DML, T.Keyword.DDL):
return first_token.normalized
elif first_token.ttype == T.Keyword.CTE:
# The WITH keyword should be followed by either an Identifier or
# an IdentifierList containing the CTE definitions; the actual
# DML keyword (e.g. SELECT, INSERT) will follow next.
fidx = self.token_index(first_token)
tidx, token = self.token_next(fidx, skip_ws=True)
if isinstance(token, (Identifier, IdentifierList)):
_, dml_keyword = self.token_next(tidx, skip_ws=True)
if dml_keyword.ttype == T.Keyword.DML:
return dml_keyword.normalized
# Hmm, probably invalid syntax, so return unknown.
return 'UNKNOWN'
class Identifier(TokenList):
"""Represents an identifier.
Identifiers may have aliases or typecasts.
"""
def is_wildcard(self):
"""Return ``True`` if this identifier contains a wildcard."""
_, token = self.token_next_by(t=T.Wildcard)
return token is not None
def get_typecast(self):
"""Returns the typecast or ``None`` of this object as a string."""
midx, marker = self.token_next_by(m=(T.Punctuation, '::'))
nidx, next_ = self.token_next(midx, skip_ws=False)
return next_.value if next_ else None
def get_ordering(self):
"""Returns the ordering or ``None`` as uppercase string."""
_, ordering = self.token_next_by(t=T.Keyword.Order)
return ordering.normalized if ordering else None
def get_array_indices(self):
"""Returns an iterator of index token lists"""
for token in self.tokens:
if isinstance(token, SquareBrackets):
# Use [1:-1] index to discard the square brackets
yield token.tokens[1:-1]
class IdentifierList(TokenList):
"""A list of :class:`~sqlparse.sql.Identifier`\'s."""
def get_identifiers(self):
"""Returns the identifiers.
Whitespaces and punctuations are not included in this generator.
"""
for token in self.tokens:
if not (token.is_whitespace or token.match(T.Punctuation, ',')):
yield token
class Parenthesis(TokenList):
"""Tokens between parenthesis."""
M_OPEN = T.Punctuation, '('
M_CLOSE = T.Punctuation, ')'
@property
def _groupable_tokens(self):
return self.tokens[1:-1]
class SquareBrackets(TokenList):
"""Tokens between square brackets"""
M_OPEN = T.Punctuation, '['
M_CLOSE = T.Punctuation, ']'
@property
def _groupable_tokens(self):
return self.tokens[1:-1]
class Assignment(TokenList):
"""An assignment like 'var := val;'"""
class If(TokenList):
"""An 'if' clause with possible 'else if' or 'else' parts."""
M_OPEN = T.Keyword, 'IF'
M_CLOSE = T.Keyword, 'END IF'
class For(TokenList):
"""A 'FOR' loop."""
M_OPEN = T.Keyword, ('FOR', 'FOREACH')
M_CLOSE = T.Keyword, 'END LOOP'
class Comparison(TokenList):
"""A comparison used for example in WHERE clauses."""
@property
def left(self):
return self.tokens[0]
@property
def right(self):
return self.tokens[-1]
class Comment(TokenList):
"""A comment."""
def is_multiline(self):
return self.tokens and self.tokens[0].ttype == T.Comment.Multiline
class Where(TokenList):
"""A WHERE clause."""
M_OPEN = T.Keyword, 'WHERE'
M_CLOSE = T.Keyword, ('ORDER', 'GROUP', 'LIMIT', 'UNION', 'EXCEPT',
'HAVING', 'RETURNING', 'INTO')
class Case(TokenList):
"""A CASE statement with one or more WHEN and possibly an ELSE part."""
M_OPEN = T.Keyword, 'CASE'
M_CLOSE = T.Keyword, 'END'
def get_cases(self, skip_ws=False):
"""Returns a list of 2-tuples (condition, value).
If an ELSE exists condition is None.
"""
CONDITION = 1
VALUE = 2
ret = []
mode = CONDITION
for token in self.tokens:
# Set mode from the current statement
if token.match(T.Keyword, 'CASE'):
continue
elif skip_ws and token.ttype in T.Whitespace:
continue
elif token.match(T.Keyword, 'WHEN'):
ret.append(([], []))
mode = CONDITION
elif token.match(T.Keyword, 'THEN'):
mode = VALUE
elif token.match(T.Keyword, 'ELSE'):
ret.append((None, []))
mode = VALUE
elif token.match(T.Keyword, 'END'):
mode = None
# First condition without preceding WHEN
if mode and not ret:
ret.append(([], []))
# Append token depending of the current mode
if mode == CONDITION:
ret[-1][0].append(token)
elif mode == VALUE:
ret[-1][1].append(token)
# Return cases list
return ret
class Function(TokenList):
"""A function or procedure call."""
def get_parameters(self):
"""Return a list of parameters."""
parenthesis = self.tokens[-1]
for token in parenthesis.tokens:
if isinstance(token, IdentifierList):
return token.get_identifiers()
elif imt(token, i=(Function, Identifier), t=T.Literal):
return [token, ]
return []
class Begin(TokenList):
"""A BEGIN/END block."""
M_OPEN = T.Keyword, 'BEGIN'
M_CLOSE = T.Keyword, 'END'
class Operation(TokenList):
"""Grouping of operations"""
================================================
FILE: SQLToolsAPI/lib/sqlparse/tokens.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
#
# The Token implementation is based on pygment's token system written
# by Georg Brandl.
# http://pygments.org/
"""Tokens"""
class _TokenType(tuple):
parent = None
def __contains__(self, item):
return item is not None and (self is item or item[:len(self)] == self)
def __getattr__(self, name):
new = _TokenType(self + (name,))
setattr(self, name, new)
new.parent = self
return new
def __repr__(self):
# self can be False only if its the `root` ie. Token itself
return 'Token' + ('.' if self else '') + '.'.join(self)
Token = _TokenType()
# Special token types
Text = Token.Text
Whitespace = Text.Whitespace
Newline = Whitespace.Newline
Error = Token.Error
# Text that doesn't belong to this lexer (e.g. HTML in PHP)
Other = Token.Other
# Common token types for source code
Keyword = Token.Keyword
Name = Token.Name
Literal = Token.Literal
String = Literal.String
Number = Literal.Number
Punctuation = Token.Punctuation
Operator = Token.Operator
Comparison = Operator.Comparison
Wildcard = Token.Wildcard
Comment = Token.Comment
Assignment = Token.Assignment
# Generic types for non-source code
Generic = Token.Generic
# String and some others are not direct childs of Token.
# alias them:
Token.Token = Token
Token.String = String
Token.Number = Number
# SQL specific tokens
DML = Keyword.DML
DDL = Keyword.DDL
CTE = Keyword.CTE
Command = Keyword.Command
================================================
FILE: SQLToolsAPI/lib/sqlparse/utils.py
================================================
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com
#
# This module is part of python-sqlparse and is released under
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
import itertools
import re
from collections import deque
from contextlib import contextmanager
from sqlparse.compat import text_type
# This regular expression replaces the home-cooked parser that was here before.
# It is much faster, but requires an extra post-processing step to get the
# desired results (that are compatible with what you would expect from the
# str.splitlines() method).
#
# It matches groups of characters: newlines, quoted strings, or unquoted text,
# and splits on that basis. The post-processing step puts those back together
# into the actual lines of SQL.
SPLIT_REGEX = re.compile(r"""
(
(?: # Start of non-capturing group
(?:\r\n|\r|\n) | # Match any single newline, or
[^\r\n'"]+ | # Match any character series without quotes or
# newlines, or
"(?:[^"\\]|\\.)*" | # Match double-quoted strings, or
'(?:[^'\\]|\\.)*' # Match single quoted strings
)
)
""", re.VERBOSE)
LINE_MATCH = re.compile(r'(\r\n|\r|\n)')
def split_unquoted_newlines(stmt):
"""Split a string on all unquoted newlines.
Unlike str.splitlines(), this will ignore CR/LF/CR+LF if the requisite
character is inside of a string."""
text = text_type(stmt)
lines = SPLIT_REGEX.split(text)
outputlines = ['']
for line in lines:
if not line:
continue
elif LINE_MATCH.match(line):
outputlines.append('')
else:
outputlines[-1] += line
return outputlines
def remove_quotes(val):
"""Helper that removes surrounding quotes from strings."""
if val is None:
return
if val[0] in ('"', "'") and val[0] == val[-1]:
val = val[1:-1]
return val
def recurse(*cls):
"""Function decorator to help with recursion
:param cls: Classes to not recurse over
:return: function
"""
def wrap(f):
def wrapped_f(tlist):
for sgroup in tlist.get_sublists():
if not isinstance(sgroup, cls):
wrapped_f(sgroup)
f(tlist)
return wrapped_f
return wrap
def imt(token, i=None, m=None, t=None):
"""Helper function to simplify comparisons Instance, Match and TokenType
:param token:
:param i: Class or Tuple/List of Classes
:param m: Tuple of TokenType & Value. Can be list of Tuple for multiple
:param t: TokenType or Tuple/List of TokenTypes
:return: bool
"""
clss = i
types = [t, ] if t and not isinstance(t, list) else t
mpatterns = [m, ] if m and not isinstance(m, list) else m
if token is None:
return False
elif clss and isinstance(token, clss):
return True
elif mpatterns and any((token.match(*pattern) for pattern in mpatterns)):
return True
elif types and any([token.ttype in ttype for ttype in types]):
return True
else:
return False
def consume(iterator, n):
"""Advance the iterator n-steps ahead. If n is none, consume entirely."""
deque(itertools.islice(iterator, n), maxlen=0)
@contextmanager
def offset(filter_, n=0):
filter_.offset += n
yield
filter_.offset -= n
@contextmanager
def indent(filter_, n=1):
filter_.indent += n
yield
filter_.indent -= n
================================================
FILE: SQLToolsConnections.sublime-settings
================================================
{
"connections": {
/*
"Generic Template": { // Connection name, used in menu (Display name)
// connection properties set to "null" will prompt for value when connecting
"type" : "pgsql", // DB type: (mysql, pgsql, oracle, vertica, sqlite, firebird, sqsh)
"host" : "HOSTNAME", // DB host to connect to
"port" : PORT, // DB port
"database" : "DATABASE", // DB name (for SQLite this is the path to DB file)
"username" : "USERNAME", // DB username
"password" : "PASSWORD", // DB password (see RDMBS specific comments below)
"encoding" : "utf-8" // DB encoding. Default: utf-8
},
"Connection MySQL": {
"type" : "mysql",
"host" : "127.0.0.1",
"port" : 3306,
"database": "dbname",
"username": "user",
// use of password for MySQL is not recommended (use "defaults-extra-file" or "login-path")
"password": "password", // you will get a security warning in the output
// "defaults-extra-file": "/path/to/defaults_file_with_password", // use [client] or [mysql] section
// "login-path": "your_login_path", // login path in your ".mylogin.cnf"
"default-character-set": "utf8",
"encoding": "utf-8"
},
"Connection PostgreSQL": {
"type" : "pgsql",
"host" : "127.0.0.1",
"port" : 5432,
"database": "dbname",
"username": "anotheruser",
// password is optional (setup "pgpass.conf" file instead)
"password": "password",
"encoding": "utf-8"
},
"Connection Oracle": {
"type" : "oracle",
"host" : "127.0.0.1",
"port" : 1521,
"database": "dbname",
"username": "anotheruser",
"password": "password",
"service" : "servicename",
// nls_lang is optional
"nls_lang": "american_america.al32utf8",
"encoding": "utf-8"
},
"Connection MSSQL": {
"type" : "mssql",
// use either ("host", "port") and remove "instance"
// or ("host", "instance") and remove "port"
"host" : "localhost",
"instance": "SQLEXPRESS",
// "port" : 1433,
// "username" and "password" are optional (remove if not needed)
"username": "sa",
"password": "password",
"database": "sample",
"encoding": "utf-8"
},
"Connection SQLite": {
"type" : "sqlite",
// note the forward slashes in path
"database": "c:/sqlite/sample_db/chinook.db",
"encoding": "utf-8"
},
"Connection Vertica": {
"type" : "vertica",
"host" : "localhost",
"port" : 5433,
"username": "anotheruser",
"password": "password",
"database": "dbname",
"encoding": "utf-8"
},
"Connection Firebird": {
"type" : "firebird",
"host" : "localhost",
"port" : 3050,
"username": "sysdba",
"password": "password",
// note the forward slashes (if path is used)
"database": "c:/firebird/examples/empbuild/employee.fdb",
"encoding": "utf-8"
},
"Connection Snowflake": {
"type" : "snowsql",
"database": "database",
// for possible authentication configurations see
// https://docs.snowflake.net/manuals/user-guide/snowsql-start.html#authenticator
"user" : "user@example.com",
"account" : "account_name",
"auth": : "snowflake | externalbrowser | ",
// if using "auth": "snowflake", provide a password
// you can alternatively set SNOWSQL_PWD in you environment instead
// if using "auth": "externalbrowser" or "", no password needed
"password": "pwd"
}
*/
},
"default": null
}
================================================
FILE: SQLToolsSavedQueries.sublime-settings
================================================
{
}
================================================
FILE: messages/install.md
================================================
# SQLTools
===============
Your swiss knife SQL for Sublime Text.
Write your SQL with smart completions and handy table and function definitions, execute SQL and explain queries, format your queries and save them in history.
Project website: https://code.mteixeira.dev/SublimeText-SQLTools/
## Features
* Works with PostgreSQL, MySQL, Oracle, MSSQL, SQLite, Vertica, Firebird and Snowflake
* Smart completions (except SQLite)
* Run SQL Queries `CTRL+e, CTRL+e`
* View table description `CTRL+e, CTRL+d`
* Show table records `CTRL+e, CTRL+s`
* Show explain plan for queries `CTRL+e, CTRL+x`
* Formatting SQL Queries `CTRL+e, CTRL+b`
* View Queries history `CTRL+e, CTRL+h`
* Save queries `CTRL+e, CTRL+q`
* List and Run saved queries `CTRL+e, CTRL+l`
* Remove saved queries `CTRL+e, CTRL+r`
* Threading support to prevent lockups
* Query timeout (kill thread if query takes too long)
## Configuration
Documentation: https://code.mteixeira.dev/SublimeText-SQLTools/
================================================
FILE: messages/v0.1.6.md
================================================
# SQLTools
===============
Your swiss knife SQL for Sublime Text.
## v0.1.6 Changelog
### Improvements
* History sorted reversed (newer queries first)
* Some package improvements (auto reload)
### Fixes
* Issue #5 - Command window is now hidden
* Issue #9 - Fixed buildArgs
================================================
FILE: messages/v0.2.0.md
================================================
## v0.2.0 Changelog
Now you can open your saved queries using `CTRL+E CTRL+O`!
### Improvements
* Added opening support for saved queries (Issue #12)
```javascript
// new hotkeys setting
{
"keys": ["ctrl+e", "ctrl+o"],
"command": "st_list_queries",
"args": {
"mode" : "open"
}
}
```
### Fixes
No fixes for this version
================================================
FILE: messages/v0.3.0.md
================================================
## v0.3.0 Changelog
### Features
* Describe user functions `CTRL+E CTRL+F` for PostgreSQL!
* PostgreSQL user functions added to auto complete.
### Improvements
* Window result shown always in the same tab
================================================
FILE: messages/v0.8.2.md
================================================
## v0.8.2 Notes
### Features
* New smarter completions that will suggest tables, columns,
aliases, columns for table aliases and join conditions.
Demo of new functionality:
https://github.com/mtxr/SQLTools/issues/67#issuecomment-297849135
**NOTE**: It is highly recommended that you review your SQLTools
settings file (Users/SQLTools.sublime-settings) and leave only
those settings that you altered specifically to your needs and
remove all other settings. This way the updated queries listed
in default settings file would be used, for new smarter
completions to work correctly.
### Improvements
* Plain Text syntax is used in the output panel when executing
queries (for performance reasons and to prevent weird highlighting)
================================================
FILE: messages/v0.9.0.md
================================================
## v0.9.0 Notes
### Features
* Added support for query result streaming [#19](https://github.com/mtxr/SQLTools/issues/19)
================================================
FILE: messages/v0.9.1.md
================================================
## v0.9.1 Notes
### Improvements
* Display errors inline instead of appending them at the bottom [#92](https://github.com/mtxr/SQLTools/issues/92)
### Fixes
* Thread timeout is always triggered when streaming results output (MacOS) [#90](https://github.com/mtxr/SQLTools/issues/90)
* stderr output is ignored when streaming results output [#91](https://github.com/mtxr/SQLTools/issues/91)
================================================
FILE: messages/v0.9.10.md
================================================
## v0.9.10 Notes
### Fixes
* Added `focus_result` setting closing issue [#183]
================================================
FILE: messages/v0.9.11.md
================================================
## v0.9.11 Notes
### Fixes
* New command `ST: Format SQL All File` (internal name `st_format_all`) to format the entire file [#182]
================================================
FILE: messages/v0.9.12.md
================================================
## v0.9.12 Notes
### Improvements
* Snowflake (`snowsql`) support
* Added support for prompting of the connection parameters
* Table/view/function selection quick panel is pre-populated with the text of the current selection
* New command `ST: Refresh Connection Data` (internal name `st_refresh_connection_data`) to reload the list of database objects
* Improve performance (completions and connection listing)
* Add a configurable setting to enable/disable completion for certain selectors (`autocomplete_selectors_ignore`, `autocomplete_selectors_active`)
### Fixes
* Fix PostgreSQL v11 compatibility issue (functions were not listed)
* Do not create an empty settings file [#144]
* Bump timeout to 60 seconds for internal commands
* Use python core logging for logs
================================================
FILE: messages/v0.9.2.md
================================================
## v0.9.2 Notes
### Improvements
* Support PostgreSQL "password" setting in Connections file [#106](https://github.com/mtxr/SQLTools/issues/106)
* Improved performance of smart completions
* If configured, use stream output for saved and history queries
* [MySQL] Add support for `Describe Function`
### Fixes
* Fix Query History [#96](https://github.com/mtxr/SQLTools/issues/96)
* Fix functionality of "clear_output" for pannel output [#102](https://github.com/mtxr/SQLTools/issues/102)
================================================
FILE: messages/v0.9.3.md
================================================
## v0.9.3 Notes
### Improvements
* [Oracle] Get identifiers (tables, columns) in all schemas [#112](https://github.com/mtxr/SQLTools/issues/112)
Please restart Sublime Text after installing this update.
================================================
FILE: messages/v0.9.4.md
================================================
## v0.9.4 Notes
### Improvements
* Execute All File [#114]
https://github.com/mtxr/SQLTools/issues/114
* Configurable top/bottom `show_query` placement
https://github.com/mtxr/SQLTools/pull/116
* [Oracle] In addition to tables views are listed as well (Describe Table works with views) [#115]
https://github.com/mtxr/SQLTools/pull/115
================================================
FILE: messages/v0.9.5.md
================================================
## v0.9.5 Notes
### Improvements
* New Feature: List and Insert Saved Queries (ctrl+e ctrl+i) [#126]
* Better error messages if setting json file could not be parsed
### Fixes
* Display completions for upper case aliases [#142]
* Fix the display of status bar message when query is executed [#130]
* Open Saved Queries executed the query if not connected to a database prior [#125]
================================================
FILE: messages/v0.9.6.md
================================================
## v0.9.6 Notes
### Fixes
* [MySQL] Added basic backtick escaping of identifiers [#147]
================================================
FILE: messages/v0.9.7.md
================================================
## v0.9.7 Notes
### Fixes
* Completions not working with identifiers containing $ symbol [#152]
================================================
FILE: messages/v0.9.8.md
================================================
## v0.9.8 Notes
### Improvements
* Add MSSQL support via native `sqlcmd` CLI
* Add more options for expanding the empty selection. Instead of config option `expand_to_paragraph`, introduce new option called `expand_to`, which can be configured to expand empty selection to: `line`, `paragraph` or `file`
* General review/improvement of each DB config
* Use the `encoding` option supplied in connection settings when writing to standard input and reading from standard output of CLI command
* Changes how top level and per-query `options` apply to CLI invocations
* Changes how `before` and `after` applied (top level and per-query)
* Introduction of new `execute` named query section which is used when executing statements with `ST: Execute` and friends
* Now all named query formatting is done via `str.format()`, therefore all instances of `%s` in template strings got replaced with `{0}`, with back-patch support (i.e. `%s` should still work for those users who have it on their own user config)
* Improve the way the output is shown - the output panel is not shown until the first output from DB CLI arrives
* Add sample connections for `Vertica` and `Firebird`
* [PostgreSQL] Connection options `host`, `port`, `username` are now optional (can be set via environment vars and other means)
* [MySQL] Add configurable connection option `default-character-set`
* [MySQL] Add `--no-auto-rehash` and `--compress` to improve MySQL startup time and improve latency on slow networks
* [MySQL] Connection options `host`, `port`, `username` are now optional (can be set via `--defaults-extra-file` and `--login-path`)
* [MySQL] Supply password via environment variable `MYSQL_PWD` to avoid security warning
* [Oracle] Add ability to configure `NSL_LANG` to match the server encoding
* [Oracle] Add support for quoted table and column names
* [Oracle] Add support for functions & procedures completions as well as getting and functions & procedures definitions (both top level and those in packages)
* [Vertica] Add support for quoted identifiers
* Other minor improvements
### Fixes
* Remove unused settings option `unescape_quotes` from config
================================================
FILE: messages/v0.9.9.md
================================================
## v0.9.9 Notes
### Improvements
* Use `utf-8` encoding if Connection `encoding` is not a valid python encoding
### Fixes
* Exception when Connection `encoding` is set to null [#177]
================================================
FILE: messages.json
================================================
{
"install": "messages/install.md",
"0.1.6": "messages/v0.1.6.md",
"0.2.0": "messages/v0.2.0.md",
"0.3.0": "messages/v0.3.0.md",
"0.3.1": "messages/v0.3.0.md",
"0.8.2": "messages/v0.8.2.md",
"0.9.0": "messages/v0.9.0.md",
"0.9.1": "messages/v0.9.1.md",
"0.9.2": "messages/v0.9.2.md",
"0.9.3": "messages/v0.9.3.md",
"0.9.4": "messages/v0.9.4.md",
"0.9.5": "messages/v0.9.5.md",
"0.9.6": "messages/v0.9.6.md",
"0.9.7": "messages/v0.9.7.md",
"0.9.8": "messages/v0.9.8.md",
"0.9.9": "messages/v0.9.9.md",
"0.9.10": "messages/v0.9.10.md",
"0.9.11": "messages/v0.9.11.md",
"0.9.12": "messages/v0.9.12.md"
}