PDA

View Full Version : Get list of oracle tnsnames



seneca
25th February 2006, 21:02
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 ;)


#include <QFile>
#include <QFileInfo>
#include <QHostInfo>
#include <QSettings>
#include <QStringList>

QStringList tnsnames()
{
QStringList names;
QSettings settings("HKEY_LOCAL_MACHINE\\Software\\ORACLE", QSettings::NativeFormat);
QString home(settings.value("ORACLE_HOME").toString().replace("\\", "/"));
if (!home.endsWith("/")) home += "/";
QFileInfo info(home+"network/admin/tnsnames.ora");
if (!info.exists()) {
info.setFile(home+"net80/admin/tnsnames.ora");
if (!info.exists()) return names;
} // if
QFile file(info.absoluteFilePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return names;
QString str;
while (!file.atEnd()) {
QString line(file.readLine());
if (line.trimmed().startsWith("#")) continue;
str += line;
} // while
file.close();
QRegExp rx("(\\w+|=|\\(|\\))");
QString word;
int pos = 0;
int paren = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
QString token(rx.capturedTexts()[0]);
if (token == "=") {
if (paren==0 && !word.isEmpty()) names << word.toLower();
word = QString::null;
} else
if (token == "(") {
paren++;
word = QString::null;
} else
if (token == ")") {
if (paren > 0) paren--;
word = QString::null;
} else {
if (paren == 0) word = token;
} // if
pos += rx.matchedLength();
} // while
names.sort();
return names;
} // tnsnames

seneca
14th March 2006, 12:11
Updated version recognizing 10g:


#include <QFile>
#include <QFileInfo>
#include <QHostInfo>
#include <QSettings>
#include <QStringList>

QStringList tnsnames()
{
QStringList names;
QSettings settings("HKEY_LOCAL_MACHINE\\Software\\ORACLE", QSettings::NativeFormat);
QString home(settings.value("ORACLE_HOME").toString()); // before 10g
if (home.isEmpty()) {
// try 10g
QStringList keys(settings.childGroups());
foreach (QString key, keys)
if (key.startsWith("KEY_")) {
home = settings.value(key+"/ORACLE_HOME").toString();
if (!home.isEmpty()) break;
} // if
} // if
if (home.isEmpty()) return names; // hopeless
if (!home.replace("\\", "/").endsWith("/")) home += "/";
QFileInfo info(home+"network/admin/tnsnames.ora");
if (!info.exists()) {
info.setFile(home+"net80/admin/tnsnames.ora");
if (!info.exists()) return names;
} // if
QFile file(info.absoluteFilePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return names;
QString str;
while (!file.atEnd()) {
QString line(file.readLine());
if (line.trimmed().startsWith("#")) continue;
str += line;
} // while
file.close();
QRegExp rx("(\\w+|=|\\(|\\))");
QString word;
int pos = 0;
int paren = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
QString token(rx.capturedTexts()[0]);
if (token == "=") {
if (paren==0 && !word.isEmpty()) names << word.toLower();
word = QString::null;
} else
if (token == "(") {
paren++;
word = QString::null;
} else
if (token == ")") {
if (paren > 0) paren--;
word = QString::null;
} else {
if (paren == 0) word = token;
} // if
pos += rx.matchedLength();
} // while
names.sort();
return names;
} // tnsnames