# First import two useful modules from win32com.client import Dispatch from adoconstants import * import dia ADODataType = { '0' : 'adEmpty', '2' : 'adSmallInt', '3' : 'adInteger', '4' : 'adSingle', '5' : 'adDouble', '6' : 'adCurrency', '7' : 'adDate', '8' : 'adBSTR', '9' : 'adIDispatch', '10' : 'adError', '11' : 'adBoolean', '12' : 'adVariant', '13' : 'adIUnknown', '14' : 'adDecimal', '16' : 'adTinyInt', '17' : 'adUnsignedTinyInt', '18' : 'adUnsignedSmallInt', '19' : 'adUnsignedInt', '20' : 'adBigInt', '21' : 'adUnsignedBigInt', '64' : 'adFileTime', '72' : 'adGUID', '128' : 'adBinary', '129' : 'adChar', '130' : 'adWChar', '131' : 'adNumeric', '132' : 'adUserDefined', '133' : 'adDBDate', '134' : 'adDBTime', '135' : 'adDBTimeStamp', '136' : 'adChapter', '137' : 'adDBFileTime', '138' : 'adPropVariant', '139' : 'adVarNumeric', '200' : 'adVarChar', '201' : 'adLongVarChar', '202' : 'adVarWChar', '203' : 'adLongVarWChar', '204' : 'adVarBinary', '205' : 'adLongVarBinary'} def test(): # Create the ADO Connection object via COM. oConn = Dispatch('ADODB.Connection') Catalog = Dispatch('ADOX.Catalog') Table = Dispatch('ADOX.Table') Column = Dispatch('ADOX.Column') Key = Dispatch('ADOX.Key') # Now set the connection properties via the ConnectionString # We're connecting to a SQL Server on 192.168.1.100 using OLEDB. oConn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="+sFile+";" print oConn.ConnectionString # Now open the connection oConn.Open() # Instead of setting the ConnectionString and then calling Open, it is also # possible to call the Open method directly and pass the connection string # as an argument to the method. {i.e.) # oConn.Open("Provider=SQLOLEDB.1; Data Source=.....") if oConn.State == adStateOpen: # Do something here print "We've connected to the database." Catalog.ActiveConnection = oConn print Catalog.Tables for Table in Catalog.Tables: for Key in Table.Keys: print Key if oConn.State == adStateOpen: oConn.Close() oConn = None def access_doc(sFile, diagramData) : # Create the ADO Connection object via COM. oConn = Dispatch('ADODB.Connection') Catalog = Dispatch('ADOX.Catalog') Table = Dispatch('ADOX.Table') Column = Dispatch('ADOX.Column') # Now set the connection properties via the ConnectionString # We're connecting to a SQL Server on 192.168.1.100 using OLEDB. oConn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="+sFile+";" print oConn.ConnectionString # Now open the connection oConn.Open() # Instead of setting the ConnectionString and then calling Open, it is also # possible to call the Open method directly and pass the connection string # as an argument to the method. {i.e.) # oConn.Open("Provider=SQLOLEDB.1; Data Source=.....") diagram = dia.new("PyDiaObjects.dia") data = diagram.data display = diagram.display() layer = data.active_layer oType = dia.get_object_type ("UML - Class") if oConn.State == adStateOpen: # Do something here print "We've connected to the database." Catalog.ActiveConnection = oConn print Catalog.Tables for Table in Catalog.Tables: print Table.name o, h1, h2 = oType.create (0,0) layer.add_object (o) o.properties["name"] = Table.name attr=[] for Column in Table.Columns: # (name,type,value,comment,visibility,abstract,class_scope) attr.append((Column.Name.encode('ascii', 'ignore'),ADODataType[str(Column.Type)],'','',0,0,0)) #print attr o.properties["attributes"]=attr # Execute a stored procedure. #oConn.Execute("select * from MSysobjects") # Execute an INSERT statement #oConn.Execute("INSERT INTO table(col1, col2) VALUES (2, 'Test String')") else: print "We failed to connect to the database." # Close up the connection and unload the COM object if oConn.State == adStateOpen: oConn.Close() oConn = None #test() dia.register_import("Access Mdb", "mdb", access_doc) #dia.register_action ("Access2","Access1", "/DisplayMenu/Test/access",access_doc)