Command line emulator written in Python
_____ __ __ __ _ ___ __ __ _ __ __ _ ___
| _,\ `v' /' _/ /__\| | __ / _//__\| \| |/' _/ /__\| | | __|
| v_/`. .'`._`.| \/ | ||__| \_| \/ | | ' |`._`.| \/ | |_| _|
|_| !_! |___/ \_V_\___| \__/\__/|_|\__||___/ \__/|___|___|
v.0.3.0.2
Command line emulator written in Python 3.x
Introduction
Current features
Planned features
File structure and dependant methods listing
Requirements
List of available commands
List of commands’ aliases
CLI’s interactive mode
To do
Known issues
Release notes
PySql-Console is a simple program emulating the command line interface, designed to interact with MS SQL environment. Due to Python’s limitations related to internal command execution, some of the features introduced in this program are a sort of workarounds (for example user authentication system or multiple input()
parameters treated as a separate strings kept in list()
object). Thus the code might look a bit groggy and unsophisticated in some places, but during the development process I’m going to polish some things up.
PySql-Console allows user to interact with MS SQL database and its content. In future I’m going to implement other features like SQL-XML converter, text editor or even system-wide operations’ support.
pysqlconsole.py
- top layer py script handling user input and calling sub-routines from commands.py
drawInitBoard()
- renders the program’s logo screenUserAuthentication()
- method responsible for performing user authentication. It keeps the main user identity connection opened during whole runtimeInputHandler()
- method processesing user input received from MainActivity()
and executing calls to commands.py
stored methodsMainActivity()
- method responsible for acquiring user input and calling InputHandler()
methodStartup()
- core pysql-console
method invoking all dependant methods in a strict, predetermined ordercommands.py
- contains implementations of all internal commands used by the programExit()
Connect()
Close()
Logout()
List()
Delete()
Drop()
Import()
Metrics()
Databases()
ConvertToXml()
Show()
Query()
Export()
Clear()
Status()
Switch()
Help()
settings.py
- global configuration file feeding requested data to both pysqlconsole.py
and commands.py
pyodbc
and tabulate
librariesSQL Server and Windows Authentication mode
enabledUserAuthentication()
method to your personal needs. Although using master
as a default database might be sufficient, I highly recommend testing some of these settings beforehand. If you wish to connect with a certain server instance or you are using a non-standard port, you can use Server=server_name\instance_name
Server=server_name,port_number
, respectively. Use below template as a general reference point:
dbConnection = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=;'
'Database=;'
'uid='+username+';'
'pwd='+password+';'
'Trusted_Connection=no;')
add <table>: Add new record to the selected table
clear: This command clears the console window
close: Close active connection to the database
connect <server> <database>: Open new connection to the target database
da <file_name> <param>: Execute a Data Analysis over the given CSV file - optional parameter determines the exact method - if it is not provided, the default method describe() weill be executed.
databases: Display list of all databases within the selected MS SQL Server instance
delete <table> <rowId>: Remove the existing record from the selected table
drop <table>: Drop the selected table
edit <table> <rowId>: Modify the existing record in the selected table
exit - Exit the program
export <table>: Exports currently selected table to .csv file
help: Displays the list of available commands
import <destination_table> <file_name>: Import existing CSV file into the selected database
list: Display list of tables in the selected database
logout: Return to splash screen
metrics: Display CPU/Memory usage of MS SQL Server
query: Run a specific query in the database
show: List all rows in the selected table
status: Displays current session's data
switch <table>: If no new table name is provided, remove focus from the current table, otherwise switch to the another table
xml <table>: Export selected table into the pre-formatted XML file
exp: export command alias
quit: exit command alias
del: delete command alias
Commands with implemented interactive mode allows user to provide the additional parameters “on the run” - in case of not providing any of the specified parameters or just a part of them. For example:
js $ export users
There is no connection established. Redirecting to connect action...
Server name: localhost
Database: test_db
Successfully connected to the localhost->test_db
js $ export users
users.csv export task finished successfully.
js $
Result:
close
add
- partially implementeddelete
edit
- partially implementeddrop
status
list
query
read
help
xml
export
commands.py
method calls coming from pysqlconsole.py
to the nested dictionaries binded with command’s keys.sql connect
, sql show
, sql export
, sql edit
, etc.) - postponedExit()
methodExport()
methodpyodbc
library’s limitations prevents some of the tables from being processed (throws DataError
exception) - most likely caused by datetime
type fields. The problem lies mostly in Microsoft’s way of data types’ implementation, so unfortunately there is no easy fix for it.23000
upon inserting a foreign key ID into the selected table. Implementation of Add()
method postponed until further noticeUserAuthentication()
database connection sub-routine in try…except clauseExit()
method - script is now finishing properlyUserAuthentication()
and InputLoop()
methods - first execution of any command leads to login screen. After inserting credentials for the second time, all commands seem to work correctly. Currently under investigationMainActivity()
- removed redundant nested execution loop and wrapped internal code in try…except clauseshow
command’s call. Still needs a bit of tinkering.UserAuthentication()
/InputLoop()
bug causing user to land on login page after first command’s execution.UserAuthentication()
method - switched pyodbc
driver to ODBC Driver 17 for SQL Server
which allows for more reliable server-side user validation. The same driver change applies to all other methods establishing active connection with the databasesettings.py
global configuration file storing the globally-accessible DB connection data, user’s active credentials, etc.pyodbc.Error
returning codes 28000
and 42S02
for incorrect table/database name.status
displaying content of global_config_array
, including non-set key-value pairs.status
outputswitch
allows users to lift their focus off the currently selected table or move it to another table.Export()
method: fix of issue related with file validation sub-routine.08001
in case of connection failure to non-existing or not DNS-mapped SQL server.commands
dictionary - migrated method calls from pysqlconsole.py
directly into nested command’s dictionaries with exec
key. Added new query
command which allows for writing and executing a personalized SQL select statement. Command’s fallback is similar to export
or show
commands - in case of no active DB connection, user is redirected to Connect()
action.tabulate
library. Now tables’ output is much more readable and organized.connect
action, user is redirected back to the initial command while all previously obtained optional parameters were stored for later use and are fully accessible.KeyboardInterrupt
exception handle for Delete()
method.Logout()
method related to active_sql_session
field not being set to None
upon method’s callback.list
command added.InputHandler()
method’s logic by applying mutable *args
parameter, allowing for handling multiple scenarios with optional arguments being passed into the function or not.test_branch
as a staging environment for testing unverified changes.table_schema
column in List()
for better tables’ identification.import
command, allowing user to import data from CSV file directly into the selected table/database.drop
command, allowing user to drop selected table from the database.xml
command allowing user to export a selected table into pre-formatted XML fileConvertToXml()
method handling the above operationInputHandler()
method for passing optional parameter to Query()
methodquery
parameter inside Query()
method, allowing user to pass a query statement directly after command.metrics
command implemented, allowing for checking CPU and memory usage by MS SQL Server.databases
command implemented, which lists all databases within a specific instance of MS SQL Server.DataAnalysis()
method and command da
related to it, for analysing data from CSV files (WIP).user $
it is displayed as user@servername $
, as well as some display tweaks of monits.settings.global_config_array
“sourceCsvFile” added in order to store file path previously given by the user in da
commanddb_drivers
dictionary in settings.py
for future app’s development and enabling support for MySQL database engine