Index: trunk/MagicSoft/Mars/msql/MSQLMagic.cc
===================================================================
--- trunk/MagicSoft/Mars/msql/MSQLMagic.cc	(revision 7952)
+++ trunk/MagicSoft/Mars/msql/MSQLMagic.cc	(revision 8061)
@@ -51,5 +51,5 @@
 //  On success the name of the key is returned.
 //
-TString MSQLMagic::QueryNameOfKey(TString col, const char *key)
+TString MSQLMagic::QueryValOf(TString col, const char *ext, const char *key)
 {
     if (col.EndsWith("KEY"))
@@ -58,6 +58,6 @@
         col.Remove(0, 1);
 
-    const TString query=Form("SELECT f%sName FROM %s WHERE f%sKEY=%s",
-                             col.Data(), col.Data(), col.Data(), key);
+    const TString query=Form("SELECT f%s%s FROM %s WHERE f%sKEY=%s",
+                             col.Data(), ext, col.Data(), col.Data(), key);
 
     TSQLResult *res = Query(query);
@@ -75,4 +75,72 @@
 // --------------------------------------------------------------------------
 //
+// Return the name corresponding to a key. If col starts with f or
+// end with KEY it is stripped.
+//
+//  If the query fails an empty string is returned.
+//
+//  On success the name of the key is returned.
+//
+TString MSQLMagic::QueryNameOfKey(TString col, const char *key)
+{
+    return QueryValOf(col, "Name", key);
+}
+
+// --------------------------------------------------------------------------
+//
+// Return the value corresponding to a key. If col starts with f or
+// end with KEY it is stripped.
+//
+//  If the query fails an empty string is returned.
+//
+//  On success the value of the key is returned.
+//
+TString MSQLMagic::QueryValOfKey(TString col, const char *key)
+{
+    return QueryValOf(col, "", key);
+}
+
+// --------------------------------------------------------------------------
+//
+//  return the key of f[col]KEY where f[col][ext]=[val]
+//
+//  return -1 if the query failed or the KEY was not found
+//  return  0 if the KEY could not be determined after inserting
+//  return the KEY in case of success
+//
+Int_t MSQLMagic::QueryKeyOf(const char *col, const char *ext, const char *val)
+{
+    const TString query1 = Form("SELECT f%sKEY FROM %s WHERE f%s%s='%s'",
+                                col, col, col, ext, val);
+
+    TSQLResult *res1 = Query(query1);
+    if (!res1)
+    {
+        cout << "ERROR - Query has failed: " << query1 << endl;
+        return -1;
+    }
+
+    TSQLRow *row=res1->Next();
+
+    const Int_t rc1 = row && (*row)[0] ? atoi((*row)[0]) : -1;
+    delete res1;
+    return rc1;
+}
+
+// --------------------------------------------------------------------------
+//
+//  return the key of f[col]KEY where f[col]=[val]
+//
+//  return -1 if the query failed or the KEY was not found
+//  return  0 if the KEY could not be determined after inserting
+//  return the KEY in case of success
+//
+Int_t MSQLMagic::QueryKeyOfVal(const char *col, const char *val)
+{
+    return QueryKeyOf(col, "", val);
+}
+
+// --------------------------------------------------------------------------
+//
 //  return the key of f[col]KEY where f[col]Name=[name]
 //
@@ -86,19 +154,5 @@
 Int_t MSQLMagic::QueryKeyOfName(const char *col, const char *name, Bool_t insert)
 {
-    const TString query1 = Form("SELECT f%sKEY FROM %s WHERE f%sName='%s'",
-                                col, col, col, name);
-
-    TSQLResult *res1 = Query(query1);
-    if (!res1)
-    {
-        cout << "ERROR - Query has failed: " << query1 << endl;
-        return -1;
-    }
-
-    TSQLRow *row=res1->Next();
-
-    const Int_t rc1 = row && (*row)[0] ? atoi((*row)[0]) : -1;
-
-    delete res1;
+    const Int_t rc1 = QueryKeyOf(col, "Name", name);
 
     if (rc1>=0)
Index: trunk/MagicSoft/Mars/msql/MSQLMagic.h
===================================================================
--- trunk/MagicSoft/Mars/msql/MSQLMagic.h	(revision 7952)
+++ trunk/MagicSoft/Mars/msql/MSQLMagic.h	(revision 8061)
@@ -38,6 +38,10 @@
     Bool_t IsDummy() const { return fIsDummy; }
 
+    TString QueryValOf(TString col, const char *ext, const char *key);
+    TString QueryValOfKey(TString col, const char *key);
     TString QueryNameOfKey(TString col, const char *key);
     Int_t   QueryKeyOfName(const char *col, const char *name, Bool_t insert=kTRUE);
+    Int_t   QueryKeyOfVal(const char *col, const char *val);
+    Int_t   QueryKeyOf(const char *col, const char *ext, const char *val);
     Bool_t  ExistStr(const char *column, const char *table, const char *test);
 
