lang_query.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:9k
源码类别:

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: lang_query.cpp,v $
  4.  * PRODUCTION Revision 1000.1  2004/06/01 19:22:08  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.3
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /* $Id: lang_query.cpp,v 1000.1 2004/06/01 19:22:08 gouriano Exp $
  10.  * ===========================================================================
  11.  *
  12.  *                            PUBLIC DOMAIN NOTICE                          
  13.  *               National Center for Biotechnology Information
  14.  *                                                                          
  15.  *  This software/database is a "United States Government Work" under the   
  16.  *  terms of the United States Copyright Act.  It was written as part of    
  17.  *  the author's official duties as a United States Government employee and 
  18.  *  thus cannot be copyrighted.  This software/database is freely available 
  19.  *  to the public for use. The National Library of Medicine and the U.S.    
  20.  *  Government have not placed any restriction on its use or reproduction.  
  21.  *                                                                          
  22.  *  Although all reasonable efforts have been taken to ensure the accuracy  
  23.  *  and reliability of the software and data, the NLM and the U.S.          
  24.  *  Government do not and cannot warrant the performance or results that    
  25.  *  may be obtained by using this software or data. The NLM and the U.S.    
  26.  *  Government disclaim all warranties, express or implied, including       
  27.  *  warranties of performance, merchantability or fitness for any particular
  28.  *  purpose.                                                                
  29.  *                                                                          
  30.  *  Please cite the author in any work or product based on this material.   
  31.  *
  32.  * ===========================================================================
  33.  *
  34.  * Author:  Vladimir Soussov
  35.  *
  36.  * This simple program illustrates how to execute SQL statements
  37.  * using one of available DBAPI drivers
  38.  *
  39.  */
  40. #include <ncbi_pch.hpp>
  41. #include <dbapi/driver/exception.hpp>
  42. #include <dbapi/driver/driver_mgr.hpp>
  43. USING_NCBI_SCOPE;
  44. static char* getParam(char tag, int argc, char* argv[], bool* flag= 0)
  45. {
  46.     for(int i= 1; i < argc; i++) {
  47.         if(((*argv[i] == '-') || (*argv[i] == '/')) && 
  48.            (*(argv[i]+1) == tag)) { // tag found
  49.             if(flag)
  50.                 *flag= true;
  51.             if(*(argv[i]+2) == '') { // tag is a separate arg
  52.                 if(i == argc - 1)
  53.                     return 0;
  54.                 if(*argv[i+1] != *argv[i])
  55.                     return argv[i+1];
  56.                 else
  57.                     return 0;
  58.             }
  59.             else {
  60.                 return argv[i]+2;
  61.             }
  62.         }
  63.     }
  64.     if(flag)
  65.         *flag= false;
  66.     return 0;
  67. }
  68. int main(int argc, char* argv[])
  69. {
  70.     string driver_name;
  71.     string server_name;
  72.     string user_name;
  73.     string passwd;
  74.     string query;
  75.     if(argc < 2) {
  76.         cout << argv[0]
  77.              << " [-d<driver_name>] [-S<server_name>]"
  78.              << " [-U<user_name>] [-P<password>] [-Q<query>]"
  79.              << endl;
  80.         return 0;
  81.     }
  82.     const char* p= getParam('S', argc, argv);
  83.     server_name= p? p : "MOZART";
  84.     
  85.     p= getParam('d', argc, argv);
  86.     if (p) {
  87.         driver_name= p;
  88.     } else {
  89.         driver_name= (server_name.find("MSSQL") != NPOS)? "ftds" : "ctlib";
  90.     }
  91.     p= getParam('U', argc, argv);
  92.     user_name= p? p : "anyone";
  93.     p= getParam('P', argc, argv);
  94.     passwd= p? p : "allowed";
  95.     bool f;
  96.     p= getParam('Q', argc, argv, &f);
  97.     if(p) {
  98.         query= p;
  99.     }
  100.     else {
  101.         if(f) { // query is on stdin
  102.             cout << "query is on ctdin" << endl;
  103.             char c;
  104.             c= cin.get();
  105.             while(!cin.eof()) {
  106.                 cout << c;
  107.                 query+= c;
  108.                 c= cin.get();
  109.             }
  110.             cout << endl;
  111.         }
  112.         if(query.empty())
  113.             query= "exec sp_who";
  114.     }
  115.     
  116.     cout << "Run query: '" << query << "' on server " << server_name 
  117.          << " using driver: " << driver_name << endl;
  118.     cout << "--------------------------------------------"
  119.          << "----------------------------"
  120.          << endl;
  121.     
  122.     try {
  123.         C_DriverMgr drv_mgr;
  124.         string err_msg;
  125.         I_DriverContext* my_context= drv_mgr.GetDriverContext(driver_name,
  126.                                                               &err_msg);
  127.         if(!my_context) {
  128.             cout << "Can not load a driver " << driver_name << " [" 
  129.                  << err_msg << "] " << endl;
  130.             return 1;
  131.         }
  132.         
  133.         CDB_Connection* con =
  134.             my_context->Connect(server_name, user_name, passwd, 0);
  135.         CDB_LangCmd* lcmd = con->LangCmd(query);
  136.         lcmd->Send();
  137.     
  138.         while (lcmd->HasMoreResults()) {
  139.             CDB_Result* r = lcmd->Result();
  140.             if (!r)
  141.                 continue;
  142.             
  143.             if (r->ResultType() == eDB_RowResult) {
  144.                 while (r->Fetch()) {
  145.                     cout << "<ROW>";
  146.                     for (unsigned int j = 0;  j < r->NofItems(); j++) {
  147.                         EDB_Type rt = r->ItemDataType(j);
  148.                         const char* iname= r->ItemName(j);
  149.                         if(iname == 0) iname= "";
  150.                         cout << '<' << iname << '>';
  151.                         if (rt == eDB_Char || rt == eDB_VarChar) {
  152.                             CDB_VarChar v;
  153.                             r->GetItem(&v);
  154.                             cout << (v.IsNULL()? "{NULL}" : v.Value());
  155.                         } 
  156.                         else if (rt == eDB_Int ||
  157.                                  rt == eDB_SmallInt ||
  158.                                  rt == eDB_TinyInt) {
  159.                             CDB_Int v;
  160.                             r->GetItem(&v);
  161.                             if(v.IsNULL()) {
  162.                                 cout << "{NULL}";
  163.                             }
  164.                             else {
  165.                                 cout << v.Value();
  166.                             }
  167.                         } 
  168.                         else if (rt == eDB_Float) {
  169.                             CDB_Float v;
  170.                             r->GetItem(&v);
  171.                             if(v.IsNULL()) {
  172.                                 cout << "{NULL}";
  173.                             }
  174.                             else {
  175.                                 cout << v.Value();
  176.                             }
  177.                         }
  178.                         else if(rt == eDB_Double) {
  179.                             CDB_Double v;
  180.                             r->GetItem(&v);
  181.                             if(v.IsNULL()) {
  182.                                 cout << "{NULL}";
  183.                             }
  184.                             else {
  185.                                 cout << v.Value();
  186.                             }
  187.                         }
  188.                         else if (rt == eDB_DateTime ||
  189.                                  rt == eDB_SmallDateTime) {
  190.                             CDB_DateTime v;
  191.                             r->GetItem(&v);
  192.                             if(v.IsNULL()) {
  193.                                 cout << "{NULL}";
  194.                             }
  195.                             else {
  196.                                 cout << v.Value().AsString();
  197.                             }
  198.                         }
  199.                         else if (rt == eDB_Numeric) {
  200.                             CDB_Numeric v;
  201.                             r->GetItem(&v);
  202.                             if(v.IsNULL()) {
  203.                                 cout << "{NULL}";
  204.                             }
  205.                             else {
  206.                                 cout << v.Value();
  207.                             }
  208.                         }
  209.                         else if (rt == eDB_Text) {
  210.                             CDB_Text v;
  211.                             r->GetItem(&v);
  212.                             cout << "{text (" << v.Size() << " bytes)}"; 
  213.                         }
  214.                         else if (rt == eDB_Image) {
  215.                             CDB_Image v;
  216.                             r->GetItem(&v);
  217.                             cout << "{image (" << v.Size() << " bytes)}"; 
  218.                         }
  219.                         else {
  220.                             r->SkipItem();
  221.                             cout << "{unprintable}";
  222.                         }
  223.                         cout << "</" << iname << '>';
  224.                     }
  225.                     cout << "</ROW>" << endl << endl;
  226.                 }
  227.                 delete r;
  228.             }
  229.         }
  230.         delete lcmd;
  231.         delete con;
  232.     } catch (CDB_Exception& e) {
  233.         CDB_UserHandler_Stream myExHandler(&cerr);
  234.         
  235.         myExHandler.HandleIt(&e);
  236.         return 1;
  237.     }
  238.     return 0;
  239. }
  240. /*
  241.  * ===========================================================================
  242.  * $Log: lang_query.cpp,v $
  243.  * Revision 1000.1  2004/06/01 19:22:08  gouriano
  244.  * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.3
  245.  *
  246.  * Revision 1.3  2004/05/17 21:16:37  gorelenk
  247.  * Added include of PCH ncbi_pch.hpp
  248.  *
  249.  * Revision 1.2  2002/06/13 21:29:22  soussov
  250.  * numeric added; double and float separated
  251.  *
  252.  * Revision 1.1  2002/06/13 19:43:07  vakatov
  253.  * Initial revision
  254.  *
  255.  * ===========================================================================
  256.  */