-
Notifications
You must be signed in to change notification settings - Fork 1
/
mi_RDB.py
executable file
·141 lines (116 loc) · 4.48 KB
/
mi_RDB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /usr/bin/env python
"""
Database Session
"""
# --
# Copyright 2012, Model Integration, LLC
# Developer: Leon Starr / leon_starr@modelint.com
# This file is part of the miUML metamodel library.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. The license text should be viewable at
# http://www.gnu.org/licenses/
# --
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
# System
import os
import re
import sys
import psycopg2
# Local
_MODULE_DIR = os.path.abspath("../Modules")
if _MODULE_DIR not in sys.path:
sys.path.append(_MODULE_DIR)
from mi_Error import *
from mi_Structured_File import Structured_File
# Diagnostic
import pdb # debug
# The variable 'x' is always a cursor in this module.
# Command used when deferring constraints
DEFER_CMD = 'set constraints %s deferred'
class db_Session:
""" The miUML Editor Database Session"""
def __init__( self ):
self.load_deferrals()
try:
self.conn = psycopg2.connect( "dbname=miUML" )
except:
raise mi_Error( "Cannot connect to miUML database." )
self.conn.set_session(
isolation_level='serializable', readonly=False, autocommit=False
)
self.x = self.conn.cursor()
try: # Set the search path
self.x.execute( "set search_path to mi, mitrack, miuml, mitype, midom, miclass, "
"mirel, miform, mirrid, mistate, mipoly" )
self.conn.commit()
except:
raise mi_Error( "Cannot set the db search_path." )
self.x.close()
def load_deferrals( self ):
""" Loads a dictionary of api_calls with required constraint deferrals """
self.deferrals = {}
# Read the file lines into a single 'deferrals' section
self.dfdata = Structured_File( os.path.join( "Resources", "rdb.mi" ) )
current_api = ""
for record in self.dfdata.sections['deferrals']:
# Each record in the deferrals section (the only section)
# is either indented or it isn't.
if record.startswith( ' ' ):
# Indented, add the constraint deferral to the current api_call
self.deferrals[current_api].append( record.strip() )
else:
# Not indented, add a new api_call
current_api = record
self.deferrals[current_api] = []
def exec_command( self, cmd, pvals, ovals, diagnostic_on, verbose_on ):
"""
Execute a command and return the result
"""
self.x = self.conn.cursor()
# Set any deferrals required by this api
api_name = cmd.split('(')[0] # Left side of api, minus (params)
if api_name in self.deferrals: # Any constraints to defer?
# make a csv list of constraints and defer them for this transaction
defer_cmd = "set constraints " + ", ".join( self.deferrals[api_name] ) + " deferred"
if verbose_on:
defer_string = str( self.x.mogrify( defer_cmd ) ).lstrip( "b" )
print( "====> [{}]".format( defer_string[1:-1] ) ) # strip single or double quotes
self.x.execute( defer_cmd )
scmd = "select * from " + cmd
if verbose_on:
cmd_string = str( self.x.mogrify( scmd, pvals ) ).lstrip( "b" ) # convert from b string
print( "----> [{}]".format( cmd_string[1:-1] ) ) # strip single or double quotes
if diagnostic_on:
return None, None
try:
self.x.execute( scmd, pvals )
self.conn.commit()
except Exception as e:
self.x.close()
raise mi_DB_Error( e.pgcode, e.pgerror )
relations = self.x.fetchall()
self.x.close()
return relations, ovals
def close( self ):
"""Closes the session"""
self.conn.close()
if __name__ == '__main__':
db = db_Session()
#app_command = "select UI_new_modeled_domain( p_name:=%s, p_alias:=%s )"
#pvals = [ "Air Traffic Control", "ATC" ]
app_command = "select * from UI_getall_domains()"
pvals = []
rows, attrs = db.exec_command( app_command, pvals )
hlen = 0
for a in attrs:
print(a, end="\t")
hlen += len(a) + 3
else:
print("="*hlen)
for r in rows:
print(r)
else:
print("="*hlen)
db.close()