Book a Demo

Author Topic: Create SQL queries and invoke it on C#  (Read 5294 times)

Semedo

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Create SQL queries and invoke it on C#
« on: March 24, 2010, 05:14:55 am »
Hi everybody,

I am trying to make SQL queries to diagrams, i am using SQl editor form EA and making an add-in using C#. I've read about how to call the queries on C# but i am not having any success.

Can somebody help me with a simple walktrough or a simple code example?

Cheers!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Create SQL queries and invoke it on C#
« Reply #1 on: March 24, 2010, 05:19:11 am »
I did something like that a while ago.
If I remember correctly you need to
- export the search definition as an xml file
- load the search when you start the addin
- execute the search

I should be able to give you a code example tomorrow.

Geert

Semedo

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Create SQL queries and invoke it on C#
« Reply #2 on: March 24, 2010, 06:22:32 am »
Tanks!

 I'll try to do that!

Best Regards

Semedo

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Create SQL queries and invoke it on C#
« Reply #3 on: March 24, 2010, 08:59:33 am »
The code would be nice ^^

TY

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Create SQL queries and invoke it on C#
« Reply #4 on: March 24, 2010, 06:09:49 pm »
I've exported the search definitions into an xml file and defined a constant in my code that points to the definitions file. (should really be a config entry, or at least a relative path, but I used this on my pc only)
Code: [Select]
private const string searchDefinitions = @"C:\Visual Studio Projects\..\..\SearchDefinitions.xml";
Then you can add the search definitions so they are know to EA.
I see now that I had problems with this, and abandoned that route, but here is the code:
Code: [Select]
       /// <summary>
        /// Add a search definition. The search definition will be gone once the application is closed
        /// </summary>
        /// <param name="definitionpath">the path to the search definition export xml file</param>
        private void addSearchDefinition(string definitionpath)
        {
            // find out what the problem is with this.
            //EARepo.AddDefinedSearches(definitionpath);
        }

Then you should be able to call those searches using
Code: [Select]
//get the objects in the EA collection
                    EA.Collection eaCollection = wrappedModel.GetElementsByQuery(searchName, searchTerm);

I never found out what the problem was with loading the searches though.
I found it easier to hardcode the SQL in a dictionary and use
Code: [Select]
/// <summary>
        /// search the model using an sql query
        /// </summary>
        /// <param name="SQLQuery">the query to be performed</param>
        /// <param name="searchTerm">the searchterm to be looked for</param>
        /// <returns>a list of EA wrappers matching the required criteria</returns>
        private List<object> SearchSQL(string SQLQuery,string searchTerm)
        {
            List<object> searchResults = new List<object>();
            // add the searchTerm in the query
            SQLQuery = SQLQuery.Replace("<Search Term>", searchTerm);
            string searchResult = wrappedModel.SQLQuery(SQLQuery);
            //parse string (xml format) to find out which objects are returned
            List<string> guids = XMLParser.getTagValues(searchResult, "CLASSGUID");
            List<string> types = XMLParser.getTagValues(searchResult, "CLASSTYPE");
            for (int i = 0; i < guids.Count; i++)
            {
                object objectToBeWrapped = null;
                if (types[i] == "Attribute")
                {
                    objectToBeWrapped = wrappedModel.GetAttributeByGuid(guids[i]);
                }
                else if (types[i] == "Connector")
                {
                    objectToBeWrapped = wrappedModel.GetConnectorByGuid(guids[i]);
                }
                else if (types[i] == "Operation")
                {
                    objectToBeWrapped = wrappedModel.GetMethodByGuid(guids[i]);
                }
                else if (types[i] == "Diagram")
                {
                    objectToBeWrapped = wrappedModel.GetDiagramByGuid(guids[i]);
                }
                if (objectToBeWrapped != null)
                {
                    searchResults.Add(EAWrapperFactory.createEAWrapper(this, objectToBeWrapped));
                }
            }
            return searchResults;
        }