!INC Local Scripts.EAConstants-JScript !INC EAScriptLib.JScript-CSV !INC EAScriptLib.JScript-Dialog
/* * Script Name: Export Tables to CSV * Author: Aaron Bell * Purpose: For use as a Project Browser script type. Exports all tables * in the currently selected package to CSV format. * Date: */
function main() { var fileName = SaveCSVFileDialog(); //abort if no file was specified if ( fileName == null || fileName == "" ) return; //the currently selected package in the Project Browser will be used to contain the new elements that are imported targetPackage = Repository.GetTreeSelectedPackage(); Session.Output("Exporting to file: " + fileName); var exportColumnHeadings = true; var columns, rowCount; //columns are comma-delimited CSV_DELIMITER = ",";
columns = Array(6); columns[0] = "TableName"; columns[1] = "Column"; columns[2] = "Type"; columns[3] = "Length"; columns[4] = "PrimaryKey"; columns[5] = "ForeignKey"; CSVEExportInitialize( fileName, columns, exportColumnHeadings ) rowCount = doExport(targetPackage); CSVEExportFinalize()
Session.Output("Done. " + rowCount + " rows exported."); }
function doExport( targetPackage ) { var element as EA.Element; var attribute as EA.Attribute; var x, y; var rowCount;
rowCount = 0; for ( x = 0; x < targetPackage.Elements.Count; x++ ) { element = targetPackage.Elements.GetAt(x); if ( element.Type == "Class" && element.Stereotype == "table" && element.Attributes.Count > 0 ) { for ( y = 0; y < element.Attributes.Count; y++ ) { attribute = element.Attributes.GetAt(y);
var valueMap = new ActiveXObject( "Scripting.Dictionary" ); valueMap.add( "TableName", element.Name ); valueMap.add( "Column", attribute.Name ); valueMap.add( "Type", attribute.Type ); valueMap.add( "Length", attribute.Length ); valueMap.add( "PrimaryKey", attribute.IsOrdered ); valueMap.add( "ForeignKey", attribute.IsCollection ); CSVEExportRow( valueMap ); rowCount++; } } } return rowCount; }
function SaveCSVFileDialog() { var Project; var Filename, FilterString, Filterindex, Flags, InitialDirectory, OpenorSave, filepath;
Filename = ""; FilterString = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*||"; Filterindex = 1; Flags = 0x2; //OFN_OVERWRITEPROMPT InitialDirectory = ""; OpenorSave = 1; Project = Repository.GetProjectInterface(); filepath = Project.GetFileNameDialog(Filename, FilterString, Filterindex, Flags, InitialDirectory, OpenorSave); return filepath; }
main(); |
|