mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Improve handling of nulls and default values in the data editor. Fixes #2257
This commit is contained in:
committed by
Dave Page
parent
05787fdba9
commit
4f9628ed43
@@ -441,8 +441,23 @@ def get_columns(trans_id):
|
||||
columns = dict()
|
||||
columns_info = None
|
||||
primary_keys = None
|
||||
rset = None
|
||||
status, error_msg, conn, trans_obj, session_obj = check_transaction_status(trans_id)
|
||||
if status and conn is not None and session_obj is not None:
|
||||
|
||||
ver = conn.manager.version
|
||||
# Get the template path for the column
|
||||
template_path = 'column/sql/#{0}#'.format(ver)
|
||||
command_obj = pickle.loads(session_obj['command_obj'])
|
||||
if hasattr(command_obj, 'obj_id'):
|
||||
SQL = render_template("/".join([template_path,
|
||||
'nodes.sql']),
|
||||
tid=command_obj.obj_id)
|
||||
# rows with attribute not_null
|
||||
status, rset = conn.execute_2darray(SQL)
|
||||
if not status:
|
||||
return internal_server_error(errormsg=rset)
|
||||
|
||||
# Check PK column info is available or not
|
||||
if 'primary_keys' in session_obj:
|
||||
primary_keys = session_obj['primary_keys']
|
||||
@@ -450,10 +465,17 @@ def get_columns(trans_id):
|
||||
# Fetch column information
|
||||
columns_info = conn.get_column_info()
|
||||
if columns_info is not None:
|
||||
for col in columns_info:
|
||||
for key, col in enumerate(columns_info):
|
||||
col_type = dict()
|
||||
col_type['type_code'] = col['type_code']
|
||||
col_type['type_name'] = None
|
||||
if rset:
|
||||
col_type['not_null'] = col['not_null'] = \
|
||||
rset['rows'][key]['not_null']
|
||||
|
||||
col_type['has_default_val'] = col['has_default_val'] = \
|
||||
rset['rows'][key]['has_default_val']
|
||||
|
||||
columns[col['name']] = col_type
|
||||
|
||||
# As we changed the transaction object we need to
|
||||
@@ -603,6 +625,7 @@ def save(trans_id):
|
||||
status, error_msg, conn, trans_obj, session_obj = check_transaction_status(trans_id)
|
||||
if status and conn is not None \
|
||||
and trans_obj is not None and session_obj is not None:
|
||||
setattr(trans_obj, 'columns_info', session_obj['columns_info'])
|
||||
|
||||
# If there is no primary key found then return from the function.
|
||||
if len(session_obj['primary_keys']) <= 0 or len(changed_data) <= 0:
|
||||
|
||||
@@ -442,6 +442,25 @@ class TableCommand(GridCommand):
|
||||
|
||||
# For newly added rows
|
||||
if of_type == 'added':
|
||||
|
||||
# When new rows are added, only changed columns data is
|
||||
# sent from client side. But if column is not_null and has
|
||||
# no_default_value, set column to blank, instead
|
||||
# of not null which is set by default.
|
||||
column_data = {}
|
||||
column_type = {}
|
||||
pk_names, primary_keys = self.get_primary_keys()
|
||||
|
||||
for each_col in self.columns_info:
|
||||
if (
|
||||
self.columns_info[each_col]['not_null'] and
|
||||
not self.columns_info[each_col][
|
||||
'has_default_val']
|
||||
):
|
||||
column_data[each_col] = None
|
||||
column_type[each_col] =\
|
||||
self.columns_info[each_col]['type_name']
|
||||
|
||||
for each_row in changed_data[of_type]:
|
||||
data = changed_data[of_type][each_row]['data']
|
||||
# Remove our unique tracking key
|
||||
@@ -450,12 +469,19 @@ class TableCommand(GridCommand):
|
||||
data_type = set_column_names(changed_data[of_type][each_row]['data_type'])
|
||||
list_of_rowid.append(data.get('__temp_PK'))
|
||||
|
||||
# Update columns value and data type
|
||||
# with columns having not_null=False and has
|
||||
# no default value
|
||||
column_data.update(data)
|
||||
column_type.update(data_type)
|
||||
|
||||
sql = render_template("/".join([self.sql_path, 'insert.sql']),
|
||||
data_to_be_saved=data,
|
||||
data_to_be_saved=column_data,
|
||||
primary_keys=None,
|
||||
object_name=self.object_name,
|
||||
nsp_name=self.nsp_name,
|
||||
data_type=data_type)
|
||||
data_type=column_type,
|
||||
pk_names=pk_names)
|
||||
list_of_sql.append(sql)
|
||||
|
||||
# For updated rows
|
||||
|
||||
@@ -420,6 +420,16 @@ input.editor-checkbox:focus {
|
||||
background: #e46b6b;
|
||||
}
|
||||
|
||||
/* Disabled row */
|
||||
.grid-canvas .disabled_row {
|
||||
background: #eeeeee;
|
||||
}
|
||||
|
||||
/* Disabled cell */
|
||||
.grid-canvas .disabled_cell {
|
||||
color: #999999;
|
||||
}
|
||||
|
||||
/* color the first column */
|
||||
.sr .sc:first-child {
|
||||
background-color: #2c76b4;
|
||||
@@ -445,4 +455,4 @@ input.editor-checkbox:focus {
|
||||
|
||||
.sr.ui-widget-content {
|
||||
border-top: 1px solid silver;
|
||||
}
|
||||
}
|
||||
@@ -557,7 +557,9 @@ define(
|
||||
id: c.name,
|
||||
pos: c.pos,
|
||||
field: c.name,
|
||||
name: c.label
|
||||
name: c.label,
|
||||
not_null: c.not_null,
|
||||
has_default_val: c.has_default_val
|
||||
};
|
||||
|
||||
// Get the columns width based on data type
|
||||
@@ -625,6 +627,12 @@ define(
|
||||
}
|
||||
}
|
||||
}
|
||||
// Disable rows having default values
|
||||
if (!_.isUndefined(self.handler.rows_to_disable) &&
|
||||
_.indexOf(self.handler.rows_to_disable, i) !== -1
|
||||
) {
|
||||
cssClass += ' disabled_row';
|
||||
}
|
||||
return {'cssClasses': cssClass};
|
||||
}
|
||||
|
||||
@@ -702,6 +710,14 @@ define(
|
||||
// This will be used to collect primary key for that row
|
||||
grid.onBeforeEditCell.subscribe(function (e, args) {
|
||||
var before_data = args.item;
|
||||
|
||||
// If newly added row is saved but grid is not refreshed,
|
||||
// then disable cell editing for that row
|
||||
if(self.handler.rows_to_disable &&
|
||||
_.contains(self.handler.rows_to_disable, args.row)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(self.handler.can_edit && before_data && '__temp_PK' in before_data) {
|
||||
var _pk = before_data.__temp_PK,
|
||||
_keys = self.handler.primary_keys,
|
||||
@@ -1629,6 +1645,8 @@ define(
|
||||
self.query_start_time = new Date();
|
||||
self.rows_affected = 0;
|
||||
self._init_polling_flags();
|
||||
// keep track of newly added rows
|
||||
self.rows_to_disable = new Array();
|
||||
|
||||
self.trigger(
|
||||
'pgadmin-sqleditor:loading-icon:show',
|
||||
@@ -2077,7 +2095,9 @@ define(
|
||||
'label': column_label,
|
||||
'cell': col_cell,
|
||||
'can_edit': self.can_edit,
|
||||
'type': type
|
||||
'type': type,
|
||||
'not_null': c.not_null,
|
||||
'has_default_val': c.has_default_val
|
||||
};
|
||||
columns.push(col);
|
||||
});
|
||||
@@ -2320,6 +2340,10 @@ define(
|
||||
grid.setSelectedRows([]);
|
||||
}
|
||||
|
||||
// Add last row(new row) to keep track of it
|
||||
if (is_added) {
|
||||
self.rows_to_disable.push(grid.getDataLength()-1);
|
||||
}
|
||||
// Reset data store
|
||||
self.data_store = {
|
||||
'added': {},
|
||||
|
||||
Reference in New Issue
Block a user