public inbox for [email protected]  
help / color / mirror / Atom feed
From: Surinder Kumar <[email protected]>
To: pgadmin-hackers <[email protected]>
Subject: [pgAdmin4][Patch][RM2257]: Query tool - Insert row doesn't use default values
Date: Sat, 1 Apr 2017 17:15:16 +0530
Message-ID: <CAM5-9D_SSL81uT4AqsRr8WPABWA6S-iE34OxLb8YqfVN3myeJg@mail.gmail.com> (raw)
List-Unsubscribe:  <mailto:[email protected]?body=unsub%20pgadmin-hackers>

Hi

Issues fixed:

1. If a column is defined with a default modifier, there is now way to
insert the row with those defaults.
The column will be left blank and it will take default value automatically.

2. If a column has a not-null constraint then an error is returned and the
row is not inserted.
The column will be left blank
​ ​

​The default values for new added rows will be displayed on refresh/execute.

Please find attached patch and review.


Thanks
Surinder


-- 
Sent via pgadmin-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers


Attachments:

  [application/octet-stream] RM_2257.patch (7.6K, 3-RM_2257.patch)
  download | inline diff:
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 "<span class='pull-right'>[null]</span>";
+    // 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 "<span class='pull-right'>[null]</span>";
     }
     else {
       return "<span style='float:right'>" + value + "</span>";
@@ -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 "<span class='pull-left'>[null]</span>";
     }
     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);
                 });


view thread (25+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: [pgAdmin4][Patch][RM2257]: Query tool - Insert row doesn't use default values
  In-Reply-To: <CAM5-9D_SSL81uT4AqsRr8WPABWA6S-iE34OxLb8YqfVN3myeJg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox