Skip to content

Commit 912bd2a

Browse files
committed
Updated to 0.3.0
Gives classes and methods much more descriptive names. Adds needed comments to the code. Improves documentation.
1 parent 2830a58 commit 912bd2a

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

OraclePLSQL.py renamed to OracleClient.py

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
""" OraclePLSQL.py
1+
""" OracleClient.py
22
33
SUMMARY:
4-
Class PLSQLWithBindVars executes PL/SQL with bind variables.
4+
Class OracleClient executes PL/SQL with bind variables.
55
66
REPOSITORY: https://github.com/DavidJLambert/Python-Universal-DB-Client
77
88
AUTHOR: David J. Lambert
99
10-
VERSION: 0.2.0
10+
VERSION: 0.3.0
1111
12-
DATE: Feb 16, 2020
12+
DATE: Mar 3, 2020
1313
1414
PURPOSE:
1515
A sample of my Python coding, to demonstrate that I can write decent Python,
@@ -19,10 +19,20 @@
1919
Class OracleDB contains all the information needed to log into an Oracle
2020
instance, plus it contains the connection handle to that database.
2121
22-
Class PLSQLWithBindVars executes PL/SQL with bind variables, and then prints
23-
out the results. In the method "oracle_table_schema" is code for listing all
24-
the tables owned by the current login, all the columns in those tables,
25-
and all indexes on those tables.
22+
Class OracleClient executes PL/SQL with bind variables, and then prints
23+
out the results. Its externally useful methods are:
24+
1) set_plsql_text: gets the text of PL/SQL to run, including bind variables.
25+
2) get_plsql_text_from_command_line: reads text of PL/SQL to run from the
26+
command line.
27+
3) get_bind_vars_from_command_line: reads the bind variables and their values
28+
from the command line.
29+
4) run_plsql: executes PL/SQL, regardless of whether it was read as a text
30+
variable (with set_plsql_text) or entered at the command line (by
31+
get_plsql_text_from_command_line and get_bind_vars_from_command_line).
32+
5) oracle_table_schema: lists all the tables owned by the current login, all
33+
the columns in those tables, and all indexes on those tables.
34+
35+
Stand-alone Method run_sqlplus runs sqlplus as a subprocess.
2636
2737
The code has been tested with CRUD statements (Create, Read, Update, Delete).
2838
There is nothing to prevent the end-user from entering other PL/SQL, such as
@@ -298,7 +308,7 @@ def print_all_connection_parameters(self) -> None:
298308
self.print_connection_status()
299309

300310

301-
class PLSQLWithBindVars(object):
311+
class OracleClient(object):
302312
""" Get text of a PL/SQL program with bind variables, then execute it.
303313
304314
Attributes:
@@ -348,7 +358,7 @@ def set_plsql_text(self, plsql: str, bind_var_dict: dict) -> None:
348358
self.plsql: str = plsql
349359
self.bind_var_dict: dict = bind_var_dict
350360

351-
def input_plsql_text(self) -> None:
361+
def get_plsql_text_from_command_line(self) -> None:
352362
""" Get text of PL/SQL at the command line.
353363
354364
Parameters:
@@ -377,7 +387,7 @@ def input_plsql_text(self) -> None:
377387
plsql += '\n' + response
378388
self.plsql = plsql.strip()
379389

380-
def input_bind_variables(self) -> None:
390+
def get_bind_vars_from_command_line(self) -> None:
381391
""" Get bind variables at the command line.
382392
383393
Parameters:
@@ -793,7 +803,7 @@ def oracle_view_schema(self) -> None:
793803
views = self.find_views()
794804

795805

796-
# -------- CUSTOM FUNCTIONS
806+
# -------- CUSTOM STAND-ALONE FUNCTIONS
797807

798808

799809
def print_stacktrace() -> None:
@@ -922,13 +932,15 @@ def run_sqlplus(plsql: str) -> list:
922932

923933

924934
if __name__ == '__main__':
935+
# Get Oracle instance to use.
925936
username1 = 'ds2'
926937
password1 = ask_for_password(username1)
927938
hostname1 = 'DESKTOP-C54UGSE.attlocal.net'
928939
port_num1 = 1521
929940
instance1 = 'XE'
930941

931-
plsql = """
942+
# Text of commands to run in sqlplus.
943+
plsql_for_sqlplus = """
932944
CONNECT {}/{}@{}:{}/{}
933945
-- trim trailing spaces
934946
SET TRIMOUT ON
@@ -953,46 +965,58 @@ def run_sqlplus(plsql: str) -> list:
953965
exit
954966
""".format(username1, password1, hostname1, port_num1, instance1)
955967

956-
plsql_output = run_sqlplus(plsql)
957-
for line in plsql_output:
968+
# Run above commands in sqlplus.
969+
sqlplus_output = run_sqlplus(plsql_for_sqlplus)
970+
971+
# Show the output from running above commands in sqlplus.
972+
for line in sqlplus_output:
958973
print(line)
959974

975+
# Set up to connect to Oracle instance specified above.
960976
database1 = OracleDB(username1, password1, hostname1, port_num1, instance1)
961977

978+
# Connect to that Oracle instance.
962979
database1.open_connection()
963980
database1.print_all_connection_parameters()
964981

965-
# Pass in database connection.
966-
plsql_with_bind_vars = PLSQLWithBindVars(database1)
982+
# Pass in database connection to my Oracle Client.
983+
my_oracle_client = OracleClient(database1)
967984

968-
# See the Oracle schema.
969-
plsql_with_bind_vars.oracle_table_schema()
985+
# See the Oracle schema for my login.
986+
my_oracle_client.oracle_table_schema()
970987

971988
# Pass in text of PL/SQL and a dict of bind variables and their values.
972989
plsql1 = """SELECT actor, title, price, categoryname
973990
FROM products p INNER JOIN categories c
974991
ON p.category = c.category
975992
WHERE actor = :actor"""
976993
bind_var_dict1 = {'actor': 'CHEVY FOSTER'}
977-
plsql_with_bind_vars.set_plsql_text(plsql1, bind_var_dict1)
994+
my_oracle_client.set_plsql_text(plsql1, bind_var_dict1)
995+
978996
# Execute the PL/SQL.
979-
col_names1, rows1, row_count1 = plsql_with_bind_vars.run_plsql()
997+
col_names1, rows1, row_count1 = my_oracle_client.run_plsql()
998+
980999
# Show the results.
9811000
print_rows(col_names1, rows1, align_col=True, col_sep=' ')
1001+
1002+
# Clean up.
9821003
col_names1 = None
9831004
rows1 = None
9841005
print()
9851006

9861007
# From command line, read in PL/SQL & dict of bind variables & their values.
987-
plsql_with_bind_vars.input_plsql_text()
988-
plsql_with_bind_vars.input_bind_variables()
1008+
my_oracle_client.get_plsql_text_from_command_line()
1009+
my_oracle_client.get_bind_vars_from_command_line()
1010+
9891011
# Execute the PL/SQL.
990-
col_names1, rows1, row_count1 = plsql_with_bind_vars.run_plsql()
1012+
col_names1, rows1, row_count1 = my_oracle_client.run_plsql()
1013+
9911014
# Show the results.
9921015
print_rows(col_names1, rows1, align_col=True, col_sep=' ')
1016+
1017+
# Clean up.
9931018
col_names1 = None
9941019
rows1 = None
9951020
print()
996-
9971021
database1.close_connection()
9981022
database1.print_connection_status()

0 commit comments

Comments
 (0)