diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/9.2_plus/nodes.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/9.2_plus/nodes.sql index 759e657..f3353d6 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/9.2_plus/nodes.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/9.2_plus/nodes.sql @@ -1,4 +1,5 @@ -SELECT att.attname as name, att.attnum as OID, format_type(ty.oid,NULL) AS datatype +SELECT att.attname as name, att.attnum as OID, format_type(ty.oid,NULL) AS datatype, +att.attnotnull as not_null, att.atthasdef as has_default_val FROM pg_attribute att JOIN pg_type ty ON ty.oid=atttypid JOIN pg_namespace tn ON tn.oid=ty.typnamespace diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/default/nodes.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/default/nodes.sql index 7536a9c..4f1de2a 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/default/nodes.sql +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/column/sql/default/nodes.sql @@ -1,4 +1,5 @@ -SELECT att.attname as name, att.attnum as OID, format_type(ty.oid,NULL) AS datatype +SELECT att.attname as name, att.attnum as OID, format_type(ty.oid,NULL) AS datatype, +att.attnotnull as not_null, att.atthasdef as has_default_val FROM pg_attribute att JOIN pg_type ty ON ty.oid=atttypid JOIN pg_namespace tn ON tn.oid=ty.typnamespace diff --git a/web/pgadmin/static/js/slickgrid/slick.pgadmin.editors.js b/web/pgadmin/static/js/slickgrid/slick.pgadmin.editors.js index b0ddfc5..fee6b4a 100644 --- a/web/pgadmin/static/js/slickgrid/slick.pgadmin.editors.js +++ b/web/pgadmin/static/js/slickgrid/slick.pgadmin.editors.js @@ -110,7 +110,12 @@ // When text editor opens this.loadValue = function (item) { - if (item[args.column.pos] === "") { + var col = args.column; + + if (_.isUndefined(item[args.column.pos]) && col.has_default_val) { + $input.val(""); + } + else if (item[args.column.pos] === "") { $input.val("''"); } else { @@ -120,11 +125,15 @@ }; this.serializeValue = function () { - var value = $input.val(); + var value = $input.val(), + col = args.column; // If empty return null - if (value === "") { + if (value === "" && !col.has_default_val) { return null; } + else if (value === "" && col.has_default_val) { + return ''; + } // single/double quotes represent an empty string // If found return '' else if (value === "''" || value === '""') { diff --git a/web/pgadmin/static/js/slickgrid/slick.pgadmin.formatters.js b/web/pgadmin/static/js/slickgrid/slick.pgadmin.formatters.js index b066095..c5d2057 100644 --- a/web/pgadmin/static/js/slickgrid/slick.pgadmin.formatters.js +++ b/web/pgadmin/static/js/slickgrid/slick.pgadmin.formatters.js @@ -19,7 +19,7 @@ }); function JsonFormatter(row, cell, value, columnDef, dataContext) { - if (value == null || value === "") { + if (_.isUndefined(value) || value == null || value === "") { return ""; } else { // Stringify only if it's json object @@ -42,11 +42,17 @@ } function NumbersFormatter(row, cell, value, columnDef, dataContext) { - if (_.isUndefined(value) || value === null) { - return "[null]"; + // Don't display null if column: + // 1. value is undefined (No value entered by user) + // 2. If either column is not null or has default value. + if ( + ((_.isUndefined(value) || _.isNull(value)) && + (columnDef.has_default_val || columnDef.not_null)) + ) { + return ""; } - else if (value === "") { - return ''; + else if (_.isUndefined(value) || value === null) { + return "[null]"; } else { return "" + value + ""; @@ -66,7 +72,16 @@ } function TextFormatter(row, cell, value, columnDef, dataContext) { - if (_.isUndefined(value) || value === null) { + // Don't display null if column: + // 1. value is undefined (No value entered by user) + // 2. If either column is not null or has default value. + if ( + ((_.isUndefined(value) || _.isNull(value)) && + (columnDef.has_default_val || columnDef.not_null)) + ) { + return ""; + } + else if (_.isUndefined(value) || value === null) { return "[null]"; } else { diff --git a/web/pgadmin/tools/sqleditor/__init__.py b/web/pgadmin/tools/sqleditor/__init__.py index f08b02e..883080f 100644 --- a/web/pgadmin/tools/sqleditor/__init__.py +++ b/web/pgadmin/tools/sqleditor/__init__.py @@ -442,6 +442,19 @@ def get_columns(trans_id): primary_keys = 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']) + 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'] @@ -449,10 +462,16 @@ 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 + 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 diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js index eed0e09..0bc3a1c 100644 --- a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js +++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js @@ -564,7 +564,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 @@ -2079,7 +2081,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); });