Пример #1
0
/// connect postgresql 
bool CDBManager::ConnectDB(const char* connectInfo)
{
	m_pdbConnection = PQconnectdb(connectInfo);
	string errorInfo = "Connection to database failed:";
	if (PQstatus(m_pdbConnection) != CONNECTION_OK)
	{
		errorInfo += PQerrorMessage(m_pdbConnection);
		//cout<<errorInfo<<endl;
		theLog.WriteLog(LOG_LEVEL_ERROR,errorInfo.c_str());
		DisconnectDB();
		return false;
	}
	else
	return true;
	
}
Пример #2
0
CDBManager::~CDBManager()
{
	DisconnectDB();
}
Пример #3
0
int main(int argc, char* argv[])
{
#ifdef CHECK_MEMLEAK
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

    //Initialize the ICU calendar
    UErrorCode status = U_ZERO_ERROR;
    dateGC = new GregorianCalendar(status);

    //get all the command line options
    GetOptionFlags(argc, argv, o);
    s.username = o.username;
    s.password = o.password;
    s.serverName = o.serverName;

    //set up the log file
    std::string dateString = GetDateString(dateGC->getNow(), dateGC);
    std::string fileName = dateString + o.gSymbol;
    fileName = GetSymbolTableName(fileName, o);
    fileName += ".log";
    gLogFile = fopen(fileName.c_str(), "w+");
    if (gLogFile == NULL)
    {
        printf("Could not open file %s, exiting.  Sucks that I can't log this output\n", fileName.c_str());
        exit(-1);
    }

    //Initialize IQ Feed
    InitDataFeed();

    //set the SQL pointers based on the data being downloaded
    SetSQLPointers(o);

    /////////////////////////////
    // Get the symbols
    /////////////////////////////
    if (o.gSymbol.compare("dow30") == 0)
        o.gSymbol = dow30;

    boost::char_separator<char> sep(",");
    boost::tokenizer< boost::char_separator<char> > tokens(o.gSymbol, sep);

    // Create a list from the tokens
    std::vector<std::string> symbolList(tokens.begin(), tokens.end());
    std::vector<std::string> contractList;
    std::map<std::string, std::string> contractMap;

    //get all the symbols
    if (o.isOption == true || o.isFutures == true)
    {
        contractList = convertSymbolsToContracts(symbolList, contractMap);
        symbolList = contractList;
    }

    int numSymbols = symbolList.size();
    WriteLog("There are %d symbols\n", numSymbols);

    //determine if we should use the hashTable, currently only
    //for ticks and non-contract-related data
    if (o.useTicks == true)// && (o.isOption == false && o.isFutures == false))
        o.useHashTable = true;

    DBHandles s;
    s.username = o.username;
    s.serverName = o.serverName;
    s.password = o.password;

    ConnectDB(&s);

    for (int i = 0; i < numSymbols; i++)
    {
        std::string currentSymbol = symbolList[i];

        // Get the data from the data feed and stuff into link list
        link *head = NULL;
        try
        {
            head = GetData(currentSymbol);
        }
        catch (const DataException &d)
        {
            std::string errorString;
            ODBCError(&s, errorString);
            WriteLog("GetData error: %s\n", errorString.c_str());
            WriteLog("problem on file %s, line %d\n", d._function.c_str(), d._lineNum);
            exit(-1);
        }

        //if there's no data then move to the next symbol
        if (head == NULL)
            continue;

        //get the symbolTableName and create table if necessary
        std::string symbolTableName;
        if (o.useHardcodedTable == true)
            symbolTableName = o.hardcodedTable;
        else if (o.isOption == true || o.isFutures == true)
            symbolTableName = GetSymbolTableName(contractMap[currentSymbol], o);
        else
            symbolTableName = GetSymbolTableName(currentSymbol, o);

        if (CheckTableExists(&s, checkTableExists, symbolTableName.c_str()) == false)
        {
            CreateTable(&s, o.gSqlCommand, o.gIndexSQL, (char *) symbolTableName.c_str());
        }

        //get the last chunk for its timestamp
        //lame could do this better but it's not too expensive for now
        link *lastChunk = head;
        while (lastChunk->next != NULL)
            lastChunk = lastChunk->next;

        //write the data
        try
        {
            boost::unordered_set<dataPoint> *hash = NULL;
            if (o.useHashTable == true)
                hash = GetDBHash(&s, symbolTableName, head->beginTimestamp, lastChunk->endTimestamp, currentSymbol);

            WriteData(&s, head, currentSymbol, symbolTableName, hash);

            delete hash;
        }
        catch (const DataException &d)
        {
            std::string errorString;
            ODBCError(&s, errorString);
            WriteLog("WriteData error: %s\n", errorString.c_str());
            WriteLog("problem on file %s, line %d\n", d._function.c_str(), d._lineNum);
            exit(-1);
        }
    }

    DisconnectDB(&s);
    RemoveClientApp(NULL);
}