pydia advances!.



Dear Friends!,


Yesterday I concluded with the DIA-SQL module, I finally found a way to generate their respective relations with foreign keys. Today I hope to generate the documentation.

The code generated was tested with MySQL, I hope to add other drivers and also make and installation package for some operating systems (GNU / Linux & UNIX).

No more I send the corresponding code:
CODE
_____________________________________________________________________________________
#    PyDia SQL.py : SQL dump.

import dia, sys, os, string, re

class SQLRenderer :
    def __init__ (self) :
        self.f = None
       
    def begin_render (self, data, filename) :
        self.f = open(filename, "w")
        name = os.path.split(filename)[1]
        self.f.write ("-- DiaSql-Dump\n-- version 0.01(Beta)\n-- Filename: %s\n" % (name,))
        for layer in data.layers :
            self.WriteTables (layer)
   
    def WriteTables (self, layer):
        tables = {}
        priority = {'fields' : 0 , 'foreign_keys' :100}
        for o in layer.objects :
            if o.type.name == 'Database - Table' :
                if o.properties.has_key ("name") :
                    table = o.properties["name"].value
                elif o.properties.has_key ("text") :
                    table = o.properties["text"].value.text
                else :
                    continue
                if len(table) == 0 or string.find (table, " ") >= 0 :
                    continue
                if not tables.has_key(table):
                    tables[table] = ''
                atributes = o.properties['attributes'].value
                for i in range(0,len(atributes)):
                    a = atributes[i]
                    tables[table] +=  '%0.3d\t`%s` %s' % (priority['fields']+i, a[0], a[1])
                    if a[3] == 1 :
                        tables[table] += ' PRIMARY KEY'
                    if a[4] == 0 :
                        tables[table] += ' NOT NULL'
                    if a[5] == 1 :
                        tables[table] += ' UNIQUE'
                    #add  AUTO_INCREMENT
                    if (a[0] == 'id' and re.match('.*int.*',a[1],re.I)) :
                        tables[table] += ' AUTO_INCREMENT'
                    tables[table] += '\n'
            elif o.type.name == 'Database - Reference':
                src = "">                 desc = o.properties['end_point_desc'].value.split('.')
                if len(src) != 2 and len(desc) != 2:
                    continue
                if not tables.has_key(desc[0]):
                    tables[desc[0]] = ''
                tables[desc[0]] += '%0.3d\tFOREIGN KEY (%s) REFERENCES %s(%s)\n' % (priority['foreign_keys'],desc[1],src[0],src[1])
        for k in tables.keys():
            self.f.write ('\n-- %s --\nCREATE TABLE IF NOT EXISTS `%s` (\n' % (k,k))
            sentences = sorted( tables[k].split('\n') )
            sentences = [str(s[3:]) for s in sentences if len(s)>4]
            sentences = ",\n".join( sentences)
            self.f.write ('%s\n' % sentences)
            self.f.write (') ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;\n')
    def end_render (self) :
        self.f.write ('-- End SQL-Dump\n')
        self.f.close()
# reference
dia.register_export ("SQL Dump", "sql", SQLRenderer())


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]