Results 1 to 2 of 2

Thread: Get list of oracle tnsnames

  1. #1
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Get list of oracle tnsnames

    For oracle login dialogs a list of tnsnames would be handy, however I couldn't find anything in the Qt SQL driver class. I therefore created the function below and post it here in public domain. Please note that this is designed for windows only, linux and mac zealots are of cause welcome to help creating a multiplatform version

    Qt Code:
    1. #include <QFile>
    2. #include <QFileInfo>
    3. #include <QHostInfo>
    4. #include <QSettings>
    5. #include <QStringList>
    6.  
    7. QStringList tnsnames()
    8. {
    9. QStringList names;
    10. QSettings settings("HKEY_LOCAL_MACHINE\\Software\\ORACLE", QSettings::NativeFormat);
    11. QString home(settings.value("ORACLE_HOME").toString().replace("\\", "/"));
    12. if (!home.endsWith("/")) home += "/";
    13. QFileInfo info(home+"network/admin/tnsnames.ora");
    14. if (!info.exists()) {
    15. info.setFile(home+"net80/admin/tnsnames.ora");
    16. if (!info.exists()) return names;
    17. } // if
    18. QFile file(info.absoluteFilePath());
    19. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return names;
    20. QString str;
    21. while (!file.atEnd()) {
    22. QString line(file.readLine());
    23. if (line.trimmed().startsWith("#")) continue;
    24. str += line;
    25. } // while
    26. file.close();
    27. QRegExp rx("(\\w+|=|\\(|\\))");
    28. QString word;
    29. int pos = 0;
    30. int paren = 0;
    31. while ((pos = rx.indexIn(str, pos)) != -1) {
    32. QString token(rx.capturedTexts()[0]);
    33. if (token == "=") {
    34. if (paren==0 && !word.isEmpty()) names << word.toLower();
    35. word = QString::null;
    36. } else
    37. if (token == "(") {
    38. paren++;
    39. word = QString::null;
    40. } else
    41. if (token == ")") {
    42. if (paren > 0) paren--;
    43. word = QString::null;
    44. } else {
    45. if (paren == 0) word = token;
    46. } // if
    47. pos += rx.matchedLength();
    48. } // while
    49. names.sort();
    50. return names;
    51. } // tnsnames
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get list of oracle tnsnames

    Updated version recognizing 10g:

    Qt Code:
    1. #include <QFile>
    2. #include <QFileInfo>
    3. #include <QHostInfo>
    4. #include <QSettings>
    5. #include <QStringList>
    6.  
    7. QStringList tnsnames()
    8. {
    9. QStringList names;
    10. QSettings settings("HKEY_LOCAL_MACHINE\\Software\\ORACLE", QSettings::NativeFormat);
    11. QString home(settings.value("ORACLE_HOME").toString()); // before 10g
    12. if (home.isEmpty()) {
    13. // try 10g
    14. QStringList keys(settings.childGroups());
    15. foreach (QString key, keys)
    16. if (key.startsWith("KEY_")) {
    17. home = settings.value(key+"/ORACLE_HOME").toString();
    18. if (!home.isEmpty()) break;
    19. } // if
    20. } // if
    21. if (home.isEmpty()) return names; // hopeless
    22. if (!home.replace("\\", "/").endsWith("/")) home += "/";
    23. QFileInfo info(home+"network/admin/tnsnames.ora");
    24. if (!info.exists()) {
    25. info.setFile(home+"net80/admin/tnsnames.ora");
    26. if (!info.exists()) return names;
    27. } // if
    28. QFile file(info.absoluteFilePath());
    29. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return names;
    30. QString str;
    31. while (!file.atEnd()) {
    32. QString line(file.readLine());
    33. if (line.trimmed().startsWith("#")) continue;
    34. str += line;
    35. } // while
    36. file.close();
    37. QRegExp rx("(\\w+|=|\\(|\\))");
    38. QString word;
    39. int pos = 0;
    40. int paren = 0;
    41. while ((pos = rx.indexIn(str, pos)) != -1) {
    42. QString token(rx.capturedTexts()[0]);
    43. if (token == "=") {
    44. if (paren==0 && !word.isEmpty()) names << word.toLower();
    45. word = QString::null;
    46. } else
    47. if (token == "(") {
    48. paren++;
    49. word = QString::null;
    50. } else
    51. if (token == ")") {
    52. if (paren > 0) paren--;
    53. word = QString::null;
    54. } else {
    55. if (paren == 0) word = token;
    56. } // if
    57. pos += rx.matchedLength();
    58. } // while
    59. names.sort();
    60. return names;
    61. } // tnsnames
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QFtp hidden files/folder's list
    By jay in forum Qt Programming
    Replies: 1
    Last Post: 26th December 2008, 12:12
  2. Replies: 2
    Last Post: 19th September 2008, 05:21
  3. Single slot for multiple list boxes
    By Sheetal in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 06:53
  4. Replies: 1
    Last Post: 22nd October 2007, 02:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.