Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: MathiasK on April 22, 2014, 06:57:43 pm
-
Hi
hope everybody had some nice free days ,
may someone can give me a hint if i can do the following:
search a Model for special class-names, attribute-names and values and some other stuff and collect them in a collection and show that collection in a special window or highlight the elements with colors.
With EA-Scripting?
With EA-Addins?
With from outside with the API?
With Something other?
Edit:
About scripting i found (german):
"Auf komfortable Wizards und GUIs muß bei der Verwendung von Skripten allerdings verzichtet werden." [german]
in english: "
"You have to resign compfortable wizards and guis in scripting"
http://www.sparxsystems.de/fileadmin/user_upload/Ressources/Scripting_Model_Documents/SPX_CE_-_HK_-Scripting_Model_Documents.pdf
(Page 7)
-
You can create own windows that will display inside EA. But it's a bit more advanced stuff. To find elements by name you should use Repository.SQLQuery and supply the right query in t_object or whatever is applicable. To invoke such a script you can either write an add-in, use the internal script editor or call an external program that accesses the EA instance.
q.
-
Mathias,
Check out my EA Navigator (http://bellekens.com/ea-navigator/). The quicksearch function probably is more or less what you are looking for.
The add-in is open source (https://www.ohloh.net/p/EANavigator) so you start from that and customize as you like.
Geert
-
To find Elements by name you could also use Repository.GetElementSet (<SQL>, 2);
i.e. Repository.GetElementSet ("SELECT * FROM t_object WHERE name LIKE '%" + name + "%'", 2);
It returns a collection of EA.Element.
-
To find Elements by name you could also use Repository.GetElementSet (<SQL>, 2);
i.e. Repository.GetElementSet ("SELECT * FROM t_object WHERE name LIKE '%" + name + "%'", 2);
It returns a collection of EA.Element.
... If you use a RDBMS. EAP (Mickeysoft) uses a '*' instead of '%'.
q.
-
If you are writing code for both, I've written a little helper function to make your modify the syntax of your xml according to the database type you are working on.
From Model.cs (https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EAWrappers/Model.cs)
Part1
/// generic query operation on the model.
/// Returns results in an xml format
public XmlDocument SQLQuery(string sqlQuery)
{
sqlQuery = this.formatSQL(sqlQuery);
XmlDocument results = new XmlDocument();
results.LoadXml(this.wrappedModel.SQLQuery(sqlQuery));
return results;
}
/// <summary>
/// sets the correct wildcards depending on the database type.
/// changes '%' into '*' if on ms access
/// and _ into ? on msAccess
/// </summary>
/// <param name="sqlQuery">the original query</param>
/// <returns>the fixed query</returns>
private string formatSQL(string sqlQuery)
{
sqlQuery = replaceSQLWildCards(sqlQuery);
sqlQuery = formatSQLTop(sqlQuery);
sqlQuery = formatSQLFunctions(sqlQuery);
return sqlQuery;
}
/// <summary>
/// Operation to translate SQL functions in there equivalents in different sql syntaxes
/// supported functions:
///
/// - lcase -> lower in T-SQL (SQLSVR and ASA)
/// </summary>
/// <param name="sqlQuery">the query to format</param>
/// <returns>a query with traslated functions</returns>
private string formatSQLFunctions(string sqlQuery)
{
string formattedSQL = sqlQuery;
//lcase -> lower in T-SQL (SQLSVR and ASA and Oracle and FireBird)
if (this.repositoryType == RepositoryType.SQLSVR ||
this.repositoryType == RepositoryType.ASA ||
this.repositoryType == RepositoryType.ORACLE ||
this.repositoryType == RepositoryType.FIREBIRD)
{
formattedSQL = formattedSQL.Replace("lcase(","lower(");
}
return formattedSQL;
}
-
Part2
/// <summary>
/// limiting the number of results in an sql query is different on different platforms.
///
/// "SELECT TOP N" is used on
/// SQLSVR
/// ADOJET
/// ASA
/// OPENEDGE
/// ACCESS2007
///
/// "WHERE rowcount <= N" is used on
/// ORACLE
///
/// "LIMIT N" is used on
/// MYSQL
/// POSTGRES
///
/// This operation will replace the SELECT TOP N by the appropriate sql syntax depending on the repositorytype
/// </summary>
/// <param name="sqlQuery">the sql query to format</param>
/// <returns>the formatted sql query </returns>
private string formatSQLTop(string sqlQuery)
{
string formattedQuery = sqlQuery;
string selectTop = "select top ";
int begintop = sqlQuery.ToLower().IndexOf(selectTop);
if (begintop >= 0)
{
int beginN = begintop + selectTop.Length;
int endN = sqlQuery.ToLower().IndexOf(" ",beginN) +1;
if (endN > beginN)
{
string N = sqlQuery.ToLower().Substring(beginN, endN - beginN);
string selectTopN = sqlQuery.Substring(begintop, endN);
switch ( this.repositoryType)
{
case RepositoryType.ORACLE :
// remove "top N" clause
formattedQuery = formattedQuery.Replace(selectTopN, "select ");
// find where clause
string whereString = "where ";
int beginWhere = formattedQuery.ToLower().IndexOf(whereString);
string rowcountCondition = "rownum <= " + N + " and ";
// add the rowcount condition
formattedQuery = formattedQuery.Insert(beginWhere + whereString.Length,rowcountCondition);
break;
case RepositoryType.MYSQL :
case RepositoryType.POSTGRES :
// remove "top N" clause
formattedQuery = formattedQuery.Replace(selectTopN, "select ");
string limitString = " limit " + N ;
// add limit clause
formattedQuery = formattedQuery + limitString;
break;
case RepositoryType.FIREBIRD:
// in firebird top becomes first
formattedQuery = formattedQuery.Replace(selectTopN,selectTopN.Replace("top","first"));
break;
}
}
}
return formattedQuery;
}
/// <summary>
/// replace the wildcards in the given sql query string to match either MSAccess or ANSI syntax
/// </summary>
/// <param name="sqlQuery">the sql string to edit</param>
/// <returns>the same sql query, but with its wildcards replaced according to the required syntax</returns>
private string replaceSQLWildCards(string sqlQuery)
{
bool msAccess = this.repositoryType == RepositoryType.ADOJET;
int beginLike = sqlQuery.IndexOf("like",StringComparison.InvariantCultureIgnoreCase);
if (beginLike > 1 )
{
int beginString = sqlQuery.IndexOf("'",beginLike + "like".Length);
if (beginString > 0)
{
int endString = sqlQuery.IndexOf("'",beginString +1);
if (endString > beginString)
{
string originalLikeString = sqlQuery.Substring(beginString +1,endString - beginString );
string likeString = originalLikeString;
if (msAccess)
{
likeString = likeString.Replace('%','*');
likeString = likeString.Replace('_','?');
likeString = likeString.Replace('^','!');
}
else
{
likeString = likeString.Replace('*','%');
likeString = likeString.Replace('?','_');
likeString = likeString.Replace('#','_');
likeString = likeString.Replace('^','!');
}
string next = string.Empty;
if (endString < sqlQuery.Length)
{
next = replaceSQLWildCards(sqlQuery.Substring(endString +1));
}
sqlQuery = sqlQuery.Substring(0,beginString+1) + likeString + next;
}
}
}
return sqlQuery;
}
Geert
-
Thank you, i appreciate your help . I havend figured out the context of sqlQuery here (havn't learned sql yet :-/ , but iam on it :) ).
thx so far ^^