Changed array representation and formatting for the data types in Query Tool and View Data. Fixes #2671

This commit is contained in:
Harshal Dhumal
2017-09-18 12:07:15 +05:30
committed by Akshay Joshi
parent 8150f93c06
commit 30e7016077
9 changed files with 418 additions and 165 deletions

View File

@@ -446,7 +446,8 @@ class TableCommand(GridCommand):
query_res = dict()
count = 0
list_of_rowid = []
list_of_sql = []
operations = ('added', 'updated', 'deleted')
list_of_sql = {}
_rowid = None
if conn.connected():
@@ -457,7 +458,7 @@ class TableCommand(GridCommand):
# Iterate total number of records to be updated/inserted
for of_type in changed_data:
# No need to go further if its not add/update/delete operation
if of_type not in ('added', 'updated', 'deleted'):
if of_type not in operations:
continue
# if no data to be save then continue
if len(changed_data[of_type]) < 1:
@@ -480,6 +481,7 @@ class TableCommand(GridCommand):
# For newly added rows
if of_type == 'added':
list_of_sql[of_type] = []
# When new rows are added, only changed columns data is
# sent from client side. But if column is not_null and has
@@ -506,12 +508,13 @@ class TableCommand(GridCommand):
nsp_name=self.nsp_name,
data_type=column_type,
pk_names=pk_names)
list_of_sql.append(sql)
list_of_sql[of_type].append({'sql': sql, 'data': data})
# Reset column data
column_data = {}
# For updated rows
elif of_type == 'updated':
list_of_sql[of_type] = []
for each_row in changed_data[of_type]:
data = changed_data[of_type][each_row]['data']
pk = changed_data[of_type][each_row]['primary_keys']
@@ -521,11 +524,12 @@ class TableCommand(GridCommand):
object_name=self.object_name,
nsp_name=self.nsp_name,
data_type=column_type)
list_of_sql.append(sql)
list_of_rowid.append(data)
list_of_sql[of_type].append({'sql': sql, 'data': data})
list_of_rowid.append(data.get(client_primary_key))
# For deleted rows
elif of_type == 'deleted':
list_of_sql[of_type] = []
is_first = True
rows_to_delete = []
keys = None
@@ -559,33 +563,35 @@ class TableCommand(GridCommand):
no_of_keys=no_of_keys,
object_name=self.object_name,
nsp_name=self.nsp_name)
list_of_sql.append(sql)
list_of_sql[of_type].append({'sql': sql, 'data': {}})
for i, sql in enumerate(list_of_sql):
if sql:
status, res = conn.execute_void(sql)
rows_affected = conn.rows_affected()
for opr, sqls in list_of_sql.items():
for item in sqls:
if item['sql']:
status, res = conn.execute_void(
item['sql'], item['data'])
rows_affected = conn.rows_affected()
# store the result of each query in dictionary
query_res[count] = {'status': status, 'result': res,
'sql': sql, 'rows_affected': rows_affected}
count += 1
# store the result of each query in dictionary
query_res[count] = {'status': status, 'result': res,
'sql': sql, 'rows_affected': rows_affected}
if not status:
conn.execute_void('ROLLBACK;')
# If we roll backed every thing then update the message for
# each sql query.
for val in query_res:
if query_res[val]['status']:
query_res[val]['result'] = 'Transaction ROLLBACK'
if not status:
conn.execute_void('ROLLBACK;')
# If we roll backed every thing then update the message for
# each sql query.
for val in query_res:
if query_res[val]['status']:
query_res[val]['result'] = 'Transaction ROLLBACK'
# If list is empty set rowid to 1
try:
_rowid = list_of_rowid[i] if list_of_rowid else 1
except Exception:
_rowid = 0
# If list is empty set rowid to 1
try:
_rowid = list_of_rowid[count] if list_of_rowid else 1
except Exception:
_rowid = 0
return status, res, query_res, _rowid
return status, res, query_res, _rowid
count += 1
# Commit the transaction if there is no error found
conn.execute_void('COMMIT;')

View File

@@ -577,7 +577,8 @@ define('tools.querytool', [
column_type: c.column_type,
column_type_internal: c.column_type_internal,
not_null: c.not_null,
has_default_val: c.has_default_val
has_default_val: c.has_default_val,
is_array: c.is_array
};
// Get the columns width based on longer string among data type or
@@ -592,26 +593,28 @@ define('tools.querytool', [
else {
options['width'] = column_size[table_name][c.name];
}
// If grid is editable then add editor else make it readonly
if (c.cell == 'Json') {
options['editor'] = is_editable ? Slick.Editors.JsonText
: Slick.Editors.ReadOnlyJsonText;
options['formatter'] = Slick.Formatters.JsonString;
options['formatter'] = c.is_array ? Slick.Formatters.JsonStringArray : Slick.Formatters.JsonString;
} else if (c.cell == 'number' ||
$.inArray(c.type, ['oid', 'xid', 'real']) !== -1
) {
options['editor'] = is_editable ? Slick.Editors.CustomNumber
: Slick.Editors.ReadOnlyText;
options['formatter'] = Slick.Formatters.Numbers;
options['formatter'] = c.is_array ? Slick.Formatters.NumbersArray : Slick.Formatters.Numbers;
} else if (c.cell == 'boolean') {
options['editor'] = is_editable ? Slick.Editors.Checkbox
: Slick.Editors.ReadOnlyCheckbox;
options['formatter'] = Slick.Formatters.Checkmark;
} else {
options['formatter'] = c.is_array ? Slick.Formatters.CheckmarkArray : Slick.Formatters.Checkmark;
} else if (c.cell == 'binary') {
// We do not support editing binary data in SQL editor and data grid.
options['formatter'] = Slick.Formatters.Binary;
}else {
options['editor'] = is_editable ? Slick.Editors.pgText
: Slick.Editors.ReadOnlypgText;
options['formatter'] = Slick.Formatters.Text;
options['formatter'] = c.is_array ? Slick.Formatters.TextArray : Slick.Formatters.Text;
}
grid_columns.push(options)
@@ -2002,7 +2005,6 @@ define('tools.querytool', [
',' + c.scale + ')' :
')';
}
// Identify cell type of column.
switch (type) {
case "json":
@@ -2012,12 +2014,19 @@ define('tools.querytool', [
col_cell = 'Json';
break;
case "smallint":
case "smallint[]":
case "integer":
case "integer[]":
case "bigint":
case "bigint[]":
case "decimal":
case "decimal[]":
case "numeric":
case "numeric[]":
case "real":
case "real[]":
case "double precision":
case "double precision[]":
col_cell = 'number';
break;
case "boolean":
@@ -2025,6 +2034,8 @@ define('tools.querytool', [
break;
case "character":
case "character[]":
case "\"char\"":
case "\"char\"[]":
case "character varying":
case "character varying[]":
if (c.internal_size && c.internal_size >= 0 && c.internal_size != 65535) {
@@ -2033,25 +2044,31 @@ define('tools.querytool', [
}
col_cell = 'string';
break;
case "bytea":
case "bytea[]":
col_cell = 'binary';
break;
default:
col_cell = 'string';
}
column_label = c.display_name + '<br>' + col_type;
var col = {
'name': c.name,
'display_name': c.display_name,
'column_type': col_type,
'column_type_internal': type,
'pos': c.pos,
'label': column_label,
'cell': col_cell,
'can_edit': self.can_edit,
'type': type,
'not_null': c.not_null,
'has_default_val': c.has_default_val
};
var array_type_bracket_index = type.lastIndexOf('[]'),
col = {
'name': c.name,
'display_name': c.display_name,
'column_type': col_type,
'column_type_internal': type,
'pos': c.pos,
'label': column_label,
'cell': col_cell,
'can_edit': self.can_edit,
'type': type,
'not_null': c.not_null,
'has_default_val': c.has_default_val,
'is_array': array_type_bracket_index > -1 && array_type_bracket_index + 2 == type.length
};
columns.push(col);
});
@@ -2981,7 +2998,15 @@ define('tools.querytool', [
),
copied_rows = rows.map(function (rowIndex) {
return data[rowIndex];
});
}),
array_types = [];
// for quick look up create list of array data types
for (var k in self.columns) {
if (self.columns[k].is_array) {
array_types.push(self.columns[k].name);
}
}
rows = rows.length == 0 ? self.last_copied_rows : rows
@@ -3000,7 +3025,8 @@ define('tools.querytool', [
_.each(arr, function (val, i) {
if (arr[i] !== undefined) {
if (_.isObject(arr[i])) {
// Do not stringify array types.
if (_.isObject(arr[i]) && array_types.indexOf(i) == -1) {
obj[String(i)] = JSON.stringify(arr[i]);
} else {
obj[String(i)] = arr[i];

View File

@@ -4,13 +4,5 @@ INSERT INTO {{ conn|qtIdent(nsp_name, object_name) }} (
{% if not loop.first %}, {% endif %}{{ conn|qtIdent(col) }}{% endfor %}
) VALUES (
{% for col in data_to_be_saved %}
{########################################################}
{# THIS IS TO CHECK IF DATA TYPE IS ARRAY? #}
{% if data_type[col].endswith('[]') %}
{% set col_value = "{%s}"|format(data_to_be_saved[col])|qtLiteral %}
{% else %}
{% set col_value = data_to_be_saved[col]|qtLiteral %}
{% endif %}
{########################################################}
{% if not loop.first %}, {% endif %}{{ col_value }}{% endfor %}
);
{% if not loop.first %}, {% endif %}%({{ col }})s::{{ data_type[col] }}{% endfor %}
);

View File

@@ -1,15 +1,7 @@
{# Update the row with primary keys (specified in primary_keys) #}
UPDATE {{ conn|qtIdent(nsp_name, object_name) }} SET
{% for col in data_to_be_saved %}
{########################################################}
{# THIS IS TO CHECK IF DATA TYPE IS ARRAY? #}
{% if data_type[col].endswith('[]') %}
{% set col_value = "{%s}"|format(data_to_be_saved[col])|qtLiteral %}
{% else %}
{% set col_value = data_to_be_saved[col]|qtLiteral %}
{% endif %}
{########################################################}
{% if not loop.first %}, {% endif %}{{ conn|qtIdent(col) }} = {{ col_value }}{% endfor %}
{% if not loop.first %}, {% endif %}{{ conn|qtIdent(col) }} = %({{ col }})s::{{ data_type[col] }}{% endfor %}
WHERE
{% for pk in primary_keys %}
{% if not loop.first %} AND {% endif %}{{ conn|qtIdent(pk) }} = {{ primary_keys[pk]|qtLiteral }}{% endfor %};
{% if not loop.first %} AND {% endif %}{{ conn|qtIdent(pk) }} = {{ primary_keys[pk]|qtLiteral }}{% endfor %};