Author Topic: Get the name of the db of the current project  (Read 6509 times)

Marcos Calleja

  • EA Novice
  • *
  • Posts: 18
  • Karma: +0/-0
    • View Profile
Get the name of the db of the current project
« on: September 14, 2015, 09:09:24 pm »
Hi!

I'm trying to obtain the name of the database of the project that I'm using. In the beginning i was using the string connection to obtain it, but then I realized that actually that's the name of the ODBC connection you create to connect (it can be the name of the database or not).

Anyway I was trying to launch sql queries using repository.SQLQuery(sql) and i tried all the differents queries i saw to obtain the current name of the database in mysql. For example "SELECT Database() FROM DUAL", and work out of EA but when I launch that query inside EA it's giving me an error.

Does anyone know how to obtain it?

Greetings and thanks for your time :)

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Get the name of the db of the current project
« Reply #1 on: September 14, 2015, 09:47:39 pm »
The name of the database is its name. It is not stored as some property. The only thing you can do is to parse the ODBC connection sting and eventually using fixed tables to decompose the original database name.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13283
  • Karma: +556/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get the name of the db of the current project
« Reply #2 on: September 14, 2015, 09:52:18 pm »
I use following code in my framework
But in the more recent versions you can also use Repository.RepositoryType ()

Code: [Select]
   /// <summary>
    /// Gets the Repository type for this model
    /// </summary>
    /// <returns></returns>
    public RepositoryType getRepositoryType()
    {
          string connectionString = this.wrappedModel.ConnectionString;
          RepositoryType repoType = RepositoryType.ADOJET; //default to .eap file
    
          // if it is a .feap file then it surely is a firebird db
          if (connectionString.ToLower().EndsWith(".feap"))
          {
                repoType = RepositoryType.FIREBIRD;
          }
          else
          {
                //if it is a .eap file we check the size of it. if less then 1 MB then it is a shortcut file and we have to open it as a text file to find the actual connection string
                if (connectionString.ToLower().EndsWith(".eap"))
                {
                      System.IO.FileInfo fileInfo = new System.IO.FileInfo(connectionString);
                      if (fileInfo.Length > 1000)
                      {
                            //local .eap file, ms access syntax
                            repoType = RepositoryType.ADOJET;
                      }
                      else
                      {
                            //open the file as a text file to find the connectionstring.
                        System.IO.FileStream fileStream = new System.IO.FileStream(connectionString, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
                        System.IO.StreamReader reader = new System.IO.StreamReader(fileStream);
                        //replace connectionstring with the file contents
                        connectionString = reader.ReadToEnd();
                        reader.Close();
                      }
                }
                if (!connectionString.ToLower().EndsWith(".eap"))
                {
                      string dbTypeString = "DBType=";
                      int dbIndex = connectionString.IndexOf(dbTypeString) + dbTypeString.Length;
                      if (dbIndex > dbTypeString.Length)
                      {
                            int dbNumber;
                            string dbNumberString = connectionString.Substring(dbIndex,1);
                            if (int.TryParse(dbNumberString,out dbNumber))
                            {
                                  repoType = (RepositoryType) dbNumber;
                            }
                      }
                }
          }
          return repoType;
    }

Once you have the correct database type you can use the correct syntax for that database to figure out the name.

Why do you need to know the name of the database?

Geert
« Last Edit: September 14, 2015, 09:54:23 pm by Geert.Bellekens »

Marcos Calleja

  • EA Novice
  • *
  • Posts: 18
  • Karma: +0/-0
    • View Profile
Re: Get the name of the db of the current project
« Reply #3 on: September 14, 2015, 10:17:13 pm »
Hi! Thanks both for response.

The ODBC connection string is the thing i'm using currently, but if the user decide to create this ODBC connection with another name, it wont work.

I'm creating an integration plugin, so I need to know the name of the database (in my company is the name of the project) to connect with the other application of my integration (Testlink btw).

My first option was launch sql queries and try to obtain it with that. Depending of the database engine you have to use different queries, and for example in mysql (the one that we are using) the query is "SELECT DATABASE() FROM DUAL".

The thing is that if I launch that query in phpmyadmin or Mysql Workbench or similar it's giving me the name, but if i launch that query using the method repository.SQLQuery("SELECT DATABASE() FROM DUAL") inside EA (in my plugin) is giving me an error.

So I was thinking maybe there's another way to obtain it, but looks like not.

Greetings and thanks for your time :)

Marcos Calleja

  • EA Novice
  • *
  • Posts: 18
  • Karma: +0/-0
    • View Profile
Re: Get the name of the db of the current project
« Reply #4 on: September 16, 2015, 03:10:03 am »
Hi guys!

I was working in another thing and suddenly a good idea about this problem appear in my mind. I think that maybe the problem of the query was the name of the return field.

The query was "SELECT DATABASE() FROM DUAL". So I changed for something as simple as "SELECT DATABASE() AS id FROM DUAL" and.... WORKS!!! ^^

Something really stupid yes, but the thing is that now works.

The complete code to obtain the name of the database (in mysql) where is connected the current EA project (if it's connected of course) is:
Code: [Select]
string sql = "SELECT DATABASE() AS id FROM DUAL;";
string xml = repository.SQLQuery(sql);
XmlDocument response = new XmlDocument();
response.LoadXml(xml);
XmlNodeList node = response.GetElementsByTagName("id");
string nameOfDatabase = node.Item(0).InnerText;

Anyway thanks a lot for your help. :)
« Last Edit: September 16, 2015, 03:12:48 am by mcalleja »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13283
  • Karma: +556/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get the name of the db of the current project
« Reply #5 on: September 16, 2015, 02:07:19 pm »
Ha, EA works in mysterious way ;D

Good to see that you have a solution.

Geert

Plonka

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Get the name of the db of the current project
« Reply #6 on: January 08, 2025, 12:29:54 am »
Hello all,

I know this post is very old but would you tell me where to paste the given Code ?
I really dont know where to start here but I want to know the Database Name of my current Project, too and want to use the Code.

Thank you very much and Happy New Year 2025 :D