public inbox for [email protected]  
help / color / mirror / Atom feed
[pgAdmin4] PATCH: Added Utility functions for triggers node and rules node
2+ messages / 2 participants
[nested] [flat]

* [pgAdmin4] PATCH: Added Utility functions for triggers node and rules node
@ 2016-03-23 15:43 Surinder Kumar <[email protected]>
  2016-04-05 15:20 ` Re: [pgAdmin4] PATCH: Added Utility functions for triggers node and rules node Dave Page <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Surinder Kumar @ 2016-03-23 15:43 UTC (permalink / raw)
  To: pgadmin-hackers

Hi,

Trigger and Rules nodes have functions which are also used by view node.
View node use them while generating reverse engineering SQL for respective
nodes.
so they are moved into schema/utils.py


Please review the patch.


Thanks,
Surinder Kumar


-- 
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] utility_functions_for_trigger_and_rule_node.patch (4.2K, 3-utility_functions_for_trigger_and_rule_node.patch)
  download | inline diff:
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/utils.py
index 9497e58..0513e6e 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/utils.py
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/utils.py
@@ -12,6 +12,7 @@
 from pgadmin.browser.collection import CollectionNodeModule
 from pgadmin.browser.utils import PGChildNodeView
 from flask import render_template
+from pgadmin.utils.ajax import internal_server_error
 
 class SchemaChildModule(CollectionNodeModule):
     """
@@ -139,3 +140,110 @@ class DataTypeReader:
             return False, str(e)
 
         return True, res
+
+
+def trigger_definition(data):
+    """
+    This functional will set the trigger definition
+
+    Args:
+        data: Properties data
+
+    Returns:
+        Updated properties data with trigger definition
+    """
+
+    # Here we are storing trigger definition
+    # We will use it to check trigger type definition
+    trigger_definition = {
+        'TRIGGER_TYPE_ROW':         (1 << 0),
+        'TRIGGER_TYPE_BEFORE':      (1 << 1),
+        'TRIGGER_TYPE_INSERT':      (1 << 2),
+        'TRIGGER_TYPE_DELETE':      (1 << 3),
+        'TRIGGER_TYPE_UPDATE':      (1 << 4),
+        'TRIGGER_TYPE_TRUNCATE':    (1 << 5),
+        'TRIGGER_TYPE_INSTEAD':     (1 << 6)
+    }
+
+    # Fires event definition
+    if data['tgtype'] & trigger_definition['TRIGGER_TYPE_BEFORE']:
+        data['fires'] = 'BEFORE'
+    elif data['tgtype'] & trigger_definition['TRIGGER_TYPE_INSTEAD']:
+        data['fires'] = 'INSTEAD OF'
+    else:
+        data['fires'] = 'AFTER'
+
+    # Trigger of type definition
+    if data['tgtype'] & trigger_definition['TRIGGER_TYPE_ROW']:
+        data['is_row_trigger'] = True
+    else:
+        data['is_row_trigger'] = False
+
+    # Event definition
+    if data['tgtype'] & trigger_definition['TRIGGER_TYPE_INSERT']:
+        data['evnt_insert'] = True
+    else:
+        data['evnt_insert'] = False
+
+    if data['tgtype'] & trigger_definition['TRIGGER_TYPE_DELETE']:
+        data['evnt_delete'] = True
+    else:
+        data['evnt_delete'] = False
+
+    if data['tgtype'] & trigger_definition['TRIGGER_TYPE_UPDATE']:
+        data['evnt_update'] = True
+    else:
+        data['evnt_update'] = False
+
+    if data['tgtype'] & trigger_definition['TRIGGER_TYPE_TRUNCATE']:
+        data['evnt_turncate'] = True
+    else:
+        data['evnt_turncate'] = False
+
+    return data
+
+
+def parse_rule_definition(res):
+    """
+    This function extracts:
+    - events
+    - do_instead
+    - statements
+    - condition
+    from the defintion row, forms an array with fields and returns it.
+    """
+    res_data = []
+    try:
+        res_data = res['rows'][0]
+        data_def = res_data['definition']
+        import re
+        # Parse data for event
+        e_match = re.search(r"ON\s+(.*)\s+TO", data_def)
+        event_data = e_match.group(1) if e_match is not None else None
+        event = event_data if event_data is not None else ''
+
+        # Parse data for do instead
+        inst_match = re.search(r"\s+(INSTEAD)\s+", data_def)
+        instead_data = inst_match.group(1) if inst_match is not None else None
+        instead = True if instead_data is not None else False
+
+        # Parse data for condition
+        condition_match = re.search(r"(?:WHERE)\s+(.*)\s+(?:DO)", data_def)
+        condition_data = condition_match.group(1) \
+            if condition_match is not None else None
+        condition = condition_data if condition_data is not None else ''
+
+        # Parse data for statements
+        statement_match = re.search(
+            r"(?:DO\s+)(?:INSTEAD\s+)?((.|\n)*)", data_def)
+        statement_data = statement_match.group(1) if statement_match else None
+        statement = statement_data if statement_data is not None else ''
+
+        # set columns parse data
+        res_data['event'] = event.lower().capitalize()
+        res_data['do_instead'] = instead
+        res_data['statements'] = statement
+        res_data['condition'] = condition
+    except Exception as e:
+        return internal_server_error(errormsg=str(e))
+    return res_data


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [pgAdmin4] PATCH: Added Utility functions for triggers node and rules node
  2016-03-23 15:43 [pgAdmin4] PATCH: Added Utility functions for triggers node and rules node Surinder Kumar <[email protected]>
@ 2016-04-05 15:20 ` Dave Page <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Dave Page @ 2016-04-05 15:20 UTC (permalink / raw)
  To: Surinder Kumar <[email protected]>; +Cc: pgadmin-hackers

Thanks - committed.

On Wed, Mar 23, 2016 at 3:43 PM, Surinder Kumar
<[email protected]> wrote:
> Hi,
>
> Trigger and Rules nodes have functions which are also used by view node.
> View node use them while generating reverse engineering SQL for respective
> nodes.
> so they are moved into schema/utils.py
>
>
> Please review the patch.
>
>
> Thanks,
> Surinder Kumar
>
>
>
> --
> Sent via pgadmin-hackers mailing list ([email protected])
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgadmin-hackers
>



-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2016-04-05 15:20 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-03-23 15:43 [pgAdmin4] PATCH: Added Utility functions for triggers node and rules node Surinder Kumar <[email protected]>
2016-04-05 15:20 ` Dave Page <[email protected]>

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