Commit db580e10 authored by Matic Perovšek's avatar Matic Perovšek

Database Context widget: finding table connections using column names

parent a81e20e2
......@@ -24,7 +24,7 @@ class DBConnection:
return sql.connect(user=self.user, password=self.password, host=self.host, database=self.database)
class DBContext:
def __init__(self, connection):
def __init__(self, connection, find_connections=False):
'''
Initializes the fields:
tables: list of selected tables
......@@ -36,7 +36,6 @@ class DBContext:
pkeys: private key for a given table
target_table: selected table for learning
target_att: selected column for learning
target_att_val: selected target att value
'''
self.connection = connection
con = connection.connect()
......@@ -54,18 +53,27 @@ class DBContext:
for col in cols:
cursor.execute("SELECT DISTINCT `%s` FROM `%s` LIMIT 51" % (col, table))
self.col_vals[table][col] = [val for (val,) in cursor]
print self.col_vals
self.connected = {}
cursor.execute(
"SELECT table_name, column_name, referenced_table_name, referenced_column_name \
FROM information_schema.KEY_COLUMN_USAGE \
WHERE referenced_table_name IS NOT NULL AND table_schema='%s'" % connection.database)
self.fkeys = defaultdict(set)
self.pkeys = {}
if find_connections:
for table in self.tables:
for col in self.cols[table]:
if col.endswith('_id'):
ref_table = (col[:-4] + 'ies') if col[-4] == 'y' else (col[:-3] + 's')
self.connected[(table, ref_table)] = (col, 'id')
self.connected[(ref_table, table)] = ('id', col)
self.fkeys[table].add(col)
if col == 'id':
self.pkeys[table] = col
for (table, col, ref_table, ref_col) in cursor:
self.connected[(table, ref_table)] = (col, ref_col)
self.connected[(ref_table, table)] = (ref_col, col)
self.fkeys[table].add(col)
self.pkeys = {}
cursor.execute(
"SELECT table_name, column_name \
FROM information_schema.KEY_COLUMN_USAGE \
......@@ -126,5 +134,7 @@ class DBContext:
return types
def __repr__(self):
return str((self.target_table, self.target_att, self.tables, self.cols, self.connected))
#import pprint
#return pprint.pformat((self.target_table, self.target_att, self.tables, self.cols, self.connected, self.pkeys, self.fkeys))
return 'fkeys : ' + str(self.fkeys)
This diff is collapsed.
......@@ -9,11 +9,11 @@ from context import DBContext
def mysql_db_context(request, input_dict, output_dict, widget):
con = input_dict['connection']
initial_context = DBContext(con)
find_con = input_dict['find_connections'] == 'true'
initial_context = DBContext(con, find_connections=find_con)
initial_target_cols = initial_context.cols[initial_context.target_table]
initial_target_col_vals = initial_context.col_vals[initial_context.target_table][initial_target_cols[0]]
cols_dump = json.dumps(initial_context.cols)
col_vals_dump = json.dumps(initial_context.col_vals)
return render(request, 'interactions/db_context.html', {'widget':widget, 'context': initial_context, 'target_cols' : initial_target_cols, 'cols' : cols_dump, 'col_vals' : col_vals_dump, 'target_col_vals' : initial_target_col_vals})
return render(request, 'interactions/db_context.html', {'widget':widget, 'context': initial_context, 'target_cols' : initial_target_cols, 'cols' : cols_dump, 'target_col_vals' : initial_target_col_vals})
......@@ -43,8 +43,6 @@
<script type="text/javascript">
// Columns data.
var cols = {{cols|safe}};
//var col_vals = {{col_vals|safe}};
console.log(col_vals);
$('select[name="target_table{{widget.pk}}"]').change(function () {
var selected_table = $(this).first('option:selected').val();
var available_cols = cols[selected_table];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment