Exemple #1
0
void CDB_ResultProcessor::ProcessResult(CDB_Result& res)
{
    while (res.Fetch())  // fetch and forget
        continue;
}
Exemple #2
0
int main(int argc, char* argv[])
{
    string driver_name;
    string server_name;
    string user_name;
    string passwd;
    string query;
    string table_name= kEmptyStr;
    string blob_key;

    if(argc < 2) {
        cerr << argv[0]
             << " [-d<driver_name>] [-S<server_name>]"
             << " [-U<user_name>] [-P<password>] [-Q<query> | -T<table_name> -K<blob_id>] [-Z<compress_method>]"
             << endl;
        return 0;
    }

    const char* p= getParam('S', argc, argv);
    if(p == 0) {
        p= getenv("SQL_SERVER");
    }
    server_name= p? p : "MS_DEV1";

    p= getParam('d', argc, argv);
    if (p) {
        driver_name= p;
    } else {
        p= getenv("DBAPI_DRIVER");
        if(p == 0) {
            driver_name= (server_name.find("MS") != NPOS)? "ftds" : "ctlib";
        }
        else driver_name= p;
    }

    p= getParam('U', argc, argv);
    if(p == 0) {
        p= getenv("SQL_USER");
    }
    user_name= p? p : "anyone";

    p= getParam('P', argc, argv);
    if(p == 0) {
        p= getenv("SQL_PASSWD");
    }
    passwd= p? p : "allowed";

    ECompressMethod cm= eNone;
    p= getParam('Z', argc, argv);
    if(p) {
        if(*p == 'z') cm= eZLib;
        else cm= eBZLib;
    }

    bool f;
    p= getParam('Q', argc, argv, &f);
    if(p) {
        query= string("set TEXTSIZE 2147483647 ") + p;
    }
    else {
        if(f) { // query is on stdin
            query= "set TEXTSIZE 2147483647 ";
            cout << "query is on ctdin" << endl;
            char c;
            c= cin.get();
            while(!cin.eof()) {
                cout << c;
                query+= c;
                c= cin.get();
            }
            cout << endl;
        }
        if(query.empty()) {
            p= getParam('T', argc, argv);
            if(p == 0)
                p= getenv("DATA_TABLE");
            table_name= p? p : "";
            p= getParam('K', argc, argv);
            blob_key= p? p : "";
        }
    }

    try {
        C_DriverMgr drv_mgr;
        string err_msg;
        map<string, string> packet;
        packet.insert (map<string, string>::value_type (string("packet"),
                                                        string("2048")));
        I_DriverContext* my_context= drv_mgr.GetDriverContext(driver_name,
                                                              &err_msg, &packet);
        if(!my_context) {
            cerr << "blobreader: Cannot load a driver " << driver_name << " ["
                 << err_msg << "] " << endl;
            return 1;
        }
        if(!table_name.empty()) {
            CDB_Connection* con= my_context->Connect(server_name, user_name, passwd, 0, true);

            query= "select * from " + table_name + " where 0=1";
            CDB_LangCmd* lcmd = con->LangCmd(query);

            lcmd->Send();

            unsigned int n;
            int k= 0;
            string key_col_name;
            string num_col_name;

            query= "set TEXTSIZE 2147483647 select ";

            while (lcmd->HasMoreResults()) {
                CDB_Result* r = lcmd->Result();
                if (!r)
                    continue;

                if (r->ResultType() == eDB_RowResult) {
                    n= r->NofItems();
                    if(n < 2) {
                        delete r;
                        continue;
                    }


                    for(unsigned int j= 0; j < n; j++) {
                        switch (r->ItemDataType(j)) {
                        case eDB_VarChar:
                        case eDB_Char:
                        case eDB_LongChar:
                            key_col_name= r->ItemName(j);
                            break;

                        case eDB_Int:
                        case eDB_SmallInt:
                        case eDB_TinyInt:
                        case eDB_BigInt:
                            num_col_name= r->ItemName(j);
                            break;

                        case eDB_Text:
                        case eDB_Image:
                        case eDB_VarCharMax:
                        case eDB_VarBinaryMax:
                            if(k++) query+= ",";
                            query+= r->ItemName(j);
                        default:
                            break;
                        }
                    }
                    while(r->Fetch());
                }
                delete r;
            }
            delete lcmd;
            delete con;

            if(k < 1) {
                query+= "*";
            }
            query+= " from " + table_name;
            if((!blob_key.empty()) && (!key_col_name.empty())) {
                query+= " where " + key_col_name + "= '" + blob_key + "'";
                if(!num_col_name.empty()) {
                    query+= " order by " + num_col_name;
                }
            }
            else if(!key_col_name.empty()) {
                query+= " order by " + key_col_name;
                if(!num_col_name.empty()) {
                    query+= "," + num_col_name;
                }
            }
        }


        CBlobRetriever retr(my_context, server_name, user_name, passwd, query);
        while(retr.IsReady()) {
            retr.Dump(cout, cm);
        }
    } catch (CDB_Exception& e) {
        CDB_UserHandler_Stream myExHandler(&cerr);

        myExHandler.HandleIt(&e);
        return 1;
    }
    return 0;
}