Skip to content

Commit cc9f9ee

Browse files
committed
Version 0.6.3.
Fixed problems with postgresql query for table & view columns.
1 parent b01f678 commit cc9f9ee

13 files changed

+73
-35
lines changed

DBClient.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def database_table_schema(self, colsep='|') -> None:
157157
if skip_operation(sql_x):
158158
if sql_x == mq.not_implemented:
159159
z = mq.not_implemented
160-
print(z.format("TABLES", self.db_type.upper()))
160+
print('\n' + z.format("TABLES", self.db_type.upper()))
161161
elif sql_x == mq.not_possible_sql:
162162
z = mq.not_possible_sql
163163
print(z.format(self.db_type.upper(), self.db_lib_name.upper()))
@@ -209,12 +209,13 @@ def database_table_schema(self, colsep='|') -> None:
209209
if skip_operation(sql_x):
210210
if sql_x == mq.not_implemented:
211211
z = mq.not_implemented
212-
print(z.format("TABLE'S COLUMNS", self.db_type.upper()))
212+
print('\n' + z.format("TABLE'S COLUMNS", self.db_type.upper()))
213213
elif sql_x == mq.not_possible_sql:
214214
z = mq.not_possible_sql
215215
print(z.format(self.db_type.upper(), self.db_lib_name.upper()))
216216
else:
217217
columns_col_names, columns_rows = self._find_table_columns(my_table)
218+
print('\nHere are the columns for table {}:'.format(my_table))
218219
writer1.write_rows(columns_rows, columns_col_names)
219220
print()
220221

@@ -223,7 +224,7 @@ def database_table_schema(self, colsep='|') -> None:
223224
if skip_operation(sql_x):
224225
if sql_x == mq.not_implemented:
225226
z = mq.not_implemented
226-
print(z.format("INDEXES", self.db_type.upper()))
227+
print('\n' + z.format("INDEXES", self.db_type.upper()))
227228
elif sql_x == mq.not_possible_sql:
228229
z = mq.not_possible_sql
229230
print(z.format(self.db_type.upper(), self.db_lib_name.upper()))
@@ -238,7 +239,7 @@ def database_table_schema(self, colsep='|') -> None:
238239
if skip_operation(sql_x):
239240
if sql_x == mq.not_implemented:
240241
z = mq.not_implemented
241-
print(z.format("INDEX'S COLUMNS", self.db_type.upper()))
242+
print('\n' + z.format("INDEX'S COLUMNS", self.db_type.upper()))
242243
elif sql_x == mq.not_possible_sql:
243244
z = mq.not_possible_sql
244245
print(z.format(self.db_type.upper(), self.db_lib_name.upper()))
@@ -249,20 +250,18 @@ def database_table_schema(self, colsep='|') -> None:
249250
# Concatenate names of columns in index. In function-based indexes,
250251
# use user_ind_expressions.column_expression instead of
251252
# user_ind_columns.column_name.
252-
index_columns = ''
253+
index_columns = list()
253254
for column_pos, column_name, descend, column_expr in ind_col_rows:
254-
if index_columns != '':
255-
index_columns += ', '
256255
if column_expr is None or column_expr == '':
257-
index_columns += column_name + ' ' + descend
256+
index_columns.append(column_name + ' ' + descend)
258257
else:
259-
index_columns += column_expr + ' ' + descend
260-
index_columns = '(' + index_columns + ')'
258+
index_columns.append(column_expr + ' ' + descend)
259+
index_columns = '(' + ', '.join(index_columns) + ')'
261260
# Add index_columns to end of each index/row (index is a tuple!).
262261
indexes_rows[count] = index_row + (index_columns,)
263262

264263
# Print output.
265-
print()
264+
print('\nHere are the indexes on table {}:'.format(my_table))
266265
writer1.write_rows(indexes_rows, indexes_col_names)
267266
writer1.close_output_file()
268267
return
@@ -281,7 +280,7 @@ def database_view_schema(self, colsep='|') -> None:
281280
if skip_operation(sql_x):
282281
if sql_x == mq.not_implemented:
283282
z = mq.not_implemented
284-
print(z.format("VIEWS", self.db_type.upper()))
283+
print('\n' + z.format("VIEWS", self.db_type.upper()))
285284
elif sql_x == mq.not_possible_sql:
286285
z = mq.not_possible_sql
287286
print(z.format(self.db_type.upper(), self.db_lib_name.upper()))
@@ -334,7 +333,8 @@ def database_view_schema(self, colsep='|') -> None:
334333
my_view_sql = my_view[columns['view_sql']]
335334

336335
# Print the sql for this view.
337-
print('\nHere is the SQL for this view:\n"{}"'.format(my_view_sql))
336+
z = '\nHere is the SQL for view {}:\n"{}"'
337+
print(z.format(my_view_name, my_view_sql))
338338

339339
# Set up to write output.
340340
writer1 = OutputWriter(out_file_name='', align_col=True, col_sep=colsep)
@@ -349,6 +349,7 @@ def database_view_schema(self, colsep='|') -> None:
349349
z = mq.not_possible_sql
350350
print(z.format(self.db_type.upper(), self.db_lib_name.upper()))
351351
return
352+
print('\nHere are the columns for view {}:'.format(my_view_name))
352353
columns_col_names, columns_rows = self._find_view_columns(my_view_name)
353354

354355
writer1.write_rows(columns_rows, columns_col_names)

MyFunctions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def sql_cmdline(os: str, sql: str, db_type: str, db_path: str, username: str,
157157

158158
if not file_in_path(os, db_client_exe):
159159
print('Did not find {} in PATH.'.format(db_client_exe))
160-
return
160+
return list()
161161

162162
cmd = ''
163163
if db_type == access:

MyQueries.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
" 'No' AS is_deletable,\n"
5959
" is_trigger_insertable_into, is_trigger_updatable, is_trigger_deletable\n"
6060
"FROM information_schema.views\n"
61-
"WHERE table_schema ='public'\n"
61+
"WHERE table_schema = 'public'\n"
6262
"ORDER BY table_name;")
6363
find_views_sql[sqlite] = (
6464
"SELECT name AS view_name, sql AS view_sql,\n"
@@ -68,7 +68,8 @@
6868
"WHERE type='view'\n"
6969
"ORDER BY name")
7070
find_views_sql[sqlserver] = (
71-
"SELECT name AS view_name, object_definition(object_id(name)) AS view_sql,\n"
71+
"SELECT name AS view_name, object_definition(object_id(name)) AS "
72+
" view_sql,\n"
7273
" 'No' AS check_option, 'No' AS is_updatable, 'No' AS is_insertable,\n"
7374
" 'No' AS is_deletable\n"
7475
"FROM sys.views WHERE type='V'\n"
@@ -78,7 +79,13 @@
7879

7980
find_tab_col_sql = dict()
8081
find_tab_col_sql[access] = not_possible_sql
81-
find_tab_col_sql[mysql] = not_implemented
82+
find_tab_col_sql[mysql] = (
83+
"SELECT ordinal_position AS column_id, column_name,\n"
84+
" column_type AS data_type, is_nullable as nullable,\n"
85+
" column_default AS default_value, column_comment AS comments\n"
86+
"FROM INFORMATION_SCHEMA.COLUMNS\n"
87+
"WHERE table_name = '{}'\n"
88+
"AND table_schema = database()")
8289
find_tab_col_sql[oracle] = (
8390
"SELECT column_id, c.column_name,\n"
8491
" CASE\n"
@@ -110,7 +117,37 @@
110117
"AND c.table_name = com.table_name\n"
111118
"AND c.column_name = com.column_name\n"
112119
"ORDER BY column_id")
113-
find_tab_col_sql[postgresql] = not_implemented
120+
find_tab_col_sql[postgresql] = (
121+
"SELECT ordinal_position AS column_id, column_name,\n"
122+
" CASE \n"
123+
" WHEN data_type = 'character varying'\n"
124+
" THEN 'varchar('||character_maximum_length||')'\n"
125+
" WHEN data_type = 'bit'\n"
126+
" THEN 'bit('||character_maximum_length||')'\n"
127+
" WHEN data_type = 'bit varying'\n"
128+
" THEN 'varbit('||character_maximum_length||')'\n"
129+
" WHEN data_type = 'character'\n"
130+
" THEN 'char('||character_maximum_length||')'\n"
131+
" WHEN data_type='numeric' AND numeric_precision IS NOT NULL AND "
132+
" numeric_scale IS NOT NULL\n"
133+
" THEN 'numeric('||numeric_precision||','||numeric_scale||')'\n"
134+
" WHEN data_type IN ('bigint', 'boolean', 'date', 'double precision',"
135+
" 'integer', 'money', 'numeric', 'real', 'smallint', 'text')\n"
136+
" THEN data_type\n"
137+
" WHEN data_type LIKE 'timestamp%' AND datetime_precision != 6\n"
138+
" THEN REPLACE(data_type, 'timestamp', "
139+
" 'timestamp('||datetime_precision||')')\n"
140+
" WHEN data_type LIKE 'time%' AND datetime_precision != 6\n"
141+
" THEN REGEXP_REPLACE(data_type, '^time',"
142+
" 'time('||datetime_precision||')')\n"
143+
" ELSE data_type\n"
144+
" END AS data_type,\n"
145+
" is_nullable AS nullable,\n"
146+
" column_default AS default_value,\n"
147+
" '' AS comments\n"
148+
"FROM INFORMATION_SCHEMA.COLUMNS\n"
149+
"WHERE table_name = '{}'\n"
150+
"AND table_schema = 'public'")
114151
find_tab_col_sql[sqlite] = (
115152
"SELECT cid AS column_id, name AS column_name, type AS data_type,\n"
116153
" CASE\n"
@@ -164,13 +201,16 @@
164201
"AND o.name = '{}'\n"
165202
"ORDER BY c.column_id ")
166203
find_tab_col_sql[sqlserver] = z.format('U', '{}')
204+
167205
# QUERIES FOR FINDING VIEW COLUMNS.
168206

169207
find_view_col_sql = dict()
170208
find_view_col_sql[access] = not_possible_sql
171-
find_view_col_sql[mysql] = not_implemented
209+
# TODO CHECK THIS
210+
find_view_col_sql[mysql] = find_tab_col_sql[mysql]
172211
find_view_col_sql[oracle] = find_tab_col_sql[oracle]
173-
find_view_col_sql[postgresql] = not_implemented
212+
# TODO CHECK THIS
213+
find_view_col_sql[postgresql] = find_tab_col_sql[postgresql]
174214
find_view_col_sql[sqlite] = find_tab_col_sql[sqlite]
175215
find_view_col_sql[sqlserver] = z.format('V', '{}')
176216

OutputWriter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def write_rows(self, all_rows: list, col_names: list) -> None:
183183
# Format and print the column names.
184184
formats = ['{{:^{}}}'.format(size) for size in col_sizes]
185185
col_names_fmt = self.col_sep.join(formats)
186-
self.out_file.write('\n' + col_names_fmt.format(*col_names))
186+
self.out_file.write(col_names_fmt.format(*col_names))
187187

188188
# Print line of dashes below the column names.
189189
dashes = ['-' * col_size for col_size in col_sizes]

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ REPOSITORY: https://github.com/DavidJLambert/Python-Universal-DB-Client
77

88
AUTHOR: David J. Lambert
99

10-
VERSION: 0.6.1
10+
VERSION: 0.6.3
1111

1212
DATE: Mar 22, 2020
1313

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# AUTHOR: David J. Lambert
66
#
7-
# VERSION: 0.6.1
7+
# VERSION: 0.6.3
88
#
99
# DATE: Mar 22, 2020
1010
#

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
AUTHOR: David J. Lambert
99
10-
VERSION: 0.6.1
10+
VERSION: 0.6.3
1111
1212
DATE: Mar 22, 2020
1313
"""

test/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
AUTHOR: David J. Lambert
88
9-
VERSION: 0.6.1
9+
VERSION: 0.6.3
1010
1111
DATE: Mar 22, 2020
1212

test/testUniversalClient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
AUTHOR: David J. Lambert
99
10-
VERSION: 0.6.1
10+
VERSION: 0.6.3
1111
1212
DATE: Mar 22, 2020
1313
"""

test/testUniversalClientMysql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
AUTHOR: David J. Lambert
1010
11-
VERSION: 0.6.1
11+
VERSION: 0.6.3
1212
1313
DATE: Mar 22, 2020
1414

0 commit comments

Comments
 (0)