Example #1
0
SQLRETURN ODBCArgExecute(CHAR *Mess,...)
{
	va_list Ah;

	va_start(Ah,Mess);
	vsprintf(EhOdbc.lpSQLCommand,Mess,Ah); // Messaggio finale

//	win_infoarg("[%s]",EhOdbc.lpSQLCommand);

	// Preparo
	EhOdbc.sqlLastError=SQLPrepare(EhOdbc.hStmt, EhOdbc.lpSQLCommand, SQL_NTS);
	if ((EhOdbc.sqlLastError!=SQL_SUCCESS)&&(EhOdbc.sqlLastError!=SQL_SUCCESS_WITH_INFO))
	{if (!EhOdbc.fNoErrorView) ODBCError("AE:Prepare"); 
	 goto FINE;
	}

	// Creo il dizionario
	ODBCDoDictionary(); // Creo il dizionario del risultato

	// Eseguo il comando
	EhOdbc.sqlLastError=SQLExecute(EhOdbc.hStmt);
	if (EhOdbc.sqlLastError!=SQL_SUCCESS) {if (!EhOdbc.fNoErrorView) ODBCError("AE:Execute");}

/*
	EhOdbc.sqlLastError=SQLSetPos(EhOdbc.hStmt,1,SQL_POSITION,SQL_LOCK_NO_CHANGE);
	if (EhOdbc.sqlLastError!=SQL_SUCCESS) 
	{
		if (!EhOdbc.fNoErrorView) ODBCError("AE:SetPos");
	}
*/

FINE:
	va_end(Ah);
	return EhOdbc.sqlLastError;
}
Example #2
0
int WriteDataToDB(DBHandles *d, DataFeedData *df)
{
    SQLINTEGER stringLength = SQL_NTS;
    SQLRETURN r;

    //only daily data need this, because the times are jacked up so
    //I just manually set it to midnight
    if (o.useDaily == true)
    {
        char newTransactionTime[50];
        char dummy[50];
        sscanf(df->timestamp, "%s %s", newTransactionTime, dummy);
        sprintf(df->timestamp, "%s 00:00:00", newTransactionTime);
    }

    r = SQLExecute(d->hstmt);

    if (!SQL_SUCCEEDED(r))
    {
        std::string errorString;
        int errorNum = ODBCError(d, errorString);
        //error code denoting that the row already exists
        if (errorNum == 2601 || errorNum == 2627)
            return 0;
        else
            throw DataException(__FILE__, __LINE__);
    }

    return 1;
}
Example #3
0
// -------------------------------------------------
// ODBCConnect()
//
BOOL ODBCConnect(CHAR *lpServerName,CHAR *lpUserName,CHAR *lpPassword)
{
	win_open(EHWP_SCREENCENTER,50,300,59,-1,3,ON,"Connessione a ...");
	dispfm(10,27,1,-1,STYLE_BOLD,"#Arial",25,lpServerName);
	
	// --------------------------------------------------------------------
	// Mi connetto al server
	//
	EhOdbc.sqlLastError=SQLConnect(EhOdbc.hConn,    // Handle della connessione
								   lpServerName,    // Nome del server
								   SQL_NTS, // Nome del file / Driver da usare
								   lpUserName,SQL_NTS, // UserName
								   lpPassword,SQL_NTS); // Password
	if (EhOdbc.sqlLastError!=SQL_SUCCESS&&EhOdbc.sqlLastError!=SQL_SUCCESS_WITH_INFO) 
		{if (!EhOdbc.fNoErrorView) ODBCError("Connect"); 
		 win_close();
	     return TRUE;
		}

	// --------------------------------------------------------------------
	// Alloca memoria per comandi (ritorna puntatore in hStmt)
	//
	// sqlReturn=SQLAllocStmt(EhOdbc.hConn, &EhOdbc.hStmt);
	//
	EhOdbc.sqlLastError=SQLAllocHandle(SQL_HANDLE_STMT, EhOdbc.hConn, &EhOdbc.hStmt);
	if (EhOdbc.sqlLastError!=SQL_SUCCESS) {ODBCError("SQLAllocHandle/Env");  PRG_end("Ambiente stantment:?");}

	SQLSetStmtAttr(EhOdbc.hStmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_READ_ONLY, 0);
	SQLSetStmtAttr(EhOdbc.hStmt, SQL_ATTR_CURSOR_SCROLLABLE, (SQLPOINTER) SQL_SCROLLABLE , 0);
	SQLSetCursorName(EhOdbc.hStmt, "SQL_CURSOR", SQL_NTS);

	SQLTRY(SQL_HANDLE_STMT,"SET1",EhOdbc.hStmt,SQLSetStmtAttr(EhOdbc.hStmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0));
	SQLTRY(SQL_HANDLE_STMT,"SET2",EhOdbc.hStmt,SQLSetStmtAttr(EhOdbc.hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) 1, 0));
	SQLTRY(SQL_HANDLE_STMT,"SET3",EhOdbc.hStmt,SQLSetStmtAttr(EhOdbc.hStmt, SQL_ATTR_ROW_STATUS_PTR, NULL, 0));
	SQLTRY(SQL_HANDLE_STMT,"SET4",EhOdbc.hStmt,SQLSetStmtAttr(EhOdbc.hStmt, SQL_ATTR_ROWS_FETCHED_PTR, NULL, 0));

	win_close();
	return FALSE;
}
Example #4
0
// --------------------------------------------------------------------------------
// ODBCTableList
// Crea una lista delle tabelle di un dbase
// --------------------------------------------------------------------------------
SINT ODBCTableList(CHAR *lpLibrary,CHAR *lpName,CHAR *lpType)
{
	ARMaker(WS_OPEN,NULL);
	if (!EhOdbc.hStmt) goto FINE;
    // Carica il driver 
	//win_open(EHWP_SCREENCENTER,50,232,59,-1,3,ON,"Lettura delle Tabelle");
	mouse_graph(0,0,"CLEX");

	ODBCCloseCursor();
	if (lpLibrary) 
	{
		if (!*lpLibrary) lpLibrary=NULL;
	}
	// Chiedo l'elenco delle tabelle
//	EhOdbc.sqlLastError=SQLTables(EhOdbc.hStmt, lpLibrary, SQL_NTS, NULL, 0, "%", SQL_NTS, NULL, 0);
	EhOdbc.sqlLastError=SQLTables(EhOdbc.hStmt, 
								  NULL, 0, // Catalogo
								  //"",SQL_NTS,
								  lpLibrary, lpLibrary?SQL_NTS:0, //Schema
								  lpName, (lpName?SQL_NTS:0), // Nome della tabella
								  lpType, lpType?SQL_NTS:0); // Tipo della tabella

	if (EhOdbc.sqlLastError!=SQL_SUCCESS) 
	{if (!EhOdbc.fNoErrorView) ODBCError("ODBCTableList()::SQLTables()"); 
	 goto FINE;
	}

	ODBCDoDictionary(); // Creo il dizionario del risultato
	while (!ODBCNext())
	{  
		//dispx("[%s] %s     <        ",ODBCFldPtr("TABLE_TYPE"),ODBCFldPtr("TABLE_NAME")); mouse_input();
		if (!strcmp(ODBCFldPtr("TABLE_TYPE"),lpType))
			//!strcmp(ODBCFldPtr("TABLE_TYPE"),"SYSTEM TABLE"))
			{	
				ARMaker(WS_ADD,ODBCFldPtr("TABLE_NAME"));
			}
	} 

	ODBCCloseCursor();
FINE:
	//win_close();
	MouseCursorDefault();
	return ARMaker(WS_CLOSE,"ODBCSource");
}
Example #5
0
SQLRETURN ODBCArgPrepare(CHAR *Mess,...)
{
	va_list Ah;

	va_start(Ah,Mess);
	vsprintf(EhOdbc.lpSQLCommand,Mess,Ah); // Messaggio finale

	// Preparo
	EhOdbc.sqlLastError=SQLPrepare(EhOdbc.hStmt, EhOdbc.lpSQLCommand, SQL_NTS);
	if (EhOdbc.sqlLastError!=SQL_SUCCESS) 
	{if (!EhOdbc.fNoErrorView) ODBCError("ArgSqlPrepare:"); 
	 goto FINE;
	}

	// Creo il dizionario
	ODBCDoDictionary(); // Creo il dizionario del risultato

FINE:
//	free(lpBuf);
	va_end(Ah);
	return EhOdbc.sqlLastError;
}
Example #6
0
// -------------------------------------------------
// ODBCFileOpen()
// Apre un file ODBC
// Ricerca in base all'estensione il driver da utilizzare
// 
BOOL ODBCFileOpen(CHAR *lpFileName)
{
	CHAR *lpBuf;
	SQLCHAR szConnStr[255];
    SWORD cbConnStr;
	SINT a,Hdl;
	CHAR **Driver;
	CHAR *lpExt;
	SINT nDriver=-1;
	
	win_open(EHWP_SCREENCENTER,50,300,59,-1,3,ON,"Connessione a ...");
	dispfm(10,27,1,-1,STYLE_BOLD,"#Arial",25,file_name(lpFileName));
	
	lpExt=rstrstr(lpFileName,"."); 
	if (lpExt==NULL) 
	{
		win_infoarg("Il file non contiene estensione [%s]",lpFileName);
		win_close(); return TRUE;
	}

	Hdl=ODBCDriverList("");
	Driver=memo_heap(Hdl);

	nDriver=-1;
	for (a=0;;a++)
	{
		if (Driver[a]==NULL) break;
		if (strstr(Driver[a],lpExt)) {nDriver=a; break;}
	}

	if (nDriver==-1)
	{
		memo_libera(Hdl,"?");
		win_infoarg("Driver sconosciuto per [%s]",lpExt);
		win_close();
	    return TRUE;
	}
/*
	sqlReturn=SQLConnect(EhOdbc.hConn,    // Handle della connessione
						 lpFileName,    // Handle della Windows
						 SQL_NTS, // Nome del file / Driver da usare
						 "",SQL_NTS, // UserName
						 "",SQL_NTS); // Password

	if (sqlReturn!=SQL_SUCCESS) 
		{ODBCError("Connect",sqlReturn); 
		 win_close();
	     return TRUE;
		}
iCheck=ODBCConnect("DBQ=C:\\Convert\\pa.xls;DRIVER={Microsoft Excel Driver (*.xls)}");
 */
	lpBuf=malloc(1024);
	sprintf(lpBuf,"DBQ=%s;DRIVER={%s}",lpFileName,Driver[nDriver]);
	EhOdbc.sqlLastError=SQLDriverConnect(EhOdbc.hConn,    // Handle della connessione
			                  NULL,    // Handle della Windows
							  lpBuf, // Nome del file / Driver da usare
							  (SQLSMALLINT) strlen(lpBuf), // Lunghezza
							  szConnStr, // Connection string di ritorno
							  (SQLSMALLINT) sizeof(szConnStr), 
							  &cbConnStr, 
							  SQL_DRIVER_NOPROMPT);
	free(lpBuf);

	if (EhOdbc.sqlLastError!=SQL_SUCCESS) 
		{
		 memo_libera(Hdl,"?");
		 if (!EhOdbc.fNoErrorView) ODBCError("ODBCFileOpen"); 
		 win_close();
	     return TRUE;
		}

	// --------------------------------------------------------------------
	// Alloca memoria per comandi (ritorna puntatore in hStmt)
	//
	//sqlReturn=SQLAllocStmt(EhOdbc.hConn, &EhOdbc.hStmt);
	EhOdbc.sqlLastError=SQLAllocHandle(SQL_HANDLE_STMT, EhOdbc.hConn, &EhOdbc.hStmt);
	if (EhOdbc.sqlLastError!=SQL_SUCCESS) {ODBCError("SQLAllocHandle/Env");  PRG_end("Ambiente stantment:?");}
	memo_libera(Hdl,"?");

	win_close();
	return FALSE;

/*
 iCheck=ODBCConnect("DBQ=C:\\Convert\\pa.xls;DRIVER={Microsoft Excel Driver (*.xls)}");
   // Alloca Ambiente ODBC
	SQLTRY("SQLAllocEnv", SQLAllocEnv(&EhOdbc->hEnv),EhOdbc);

    // Alloca Memoria per la connessione
	SQLTRY("SQLAllocConnect", SQLAllocConnect(EhOdbc->hEnv, &EhOdbc->hDBC),EhOdbc);

    // Carica il driver 
	SQLTRY("SQLDriverConnect", 
	        SQDriverConnect(EhOdbc->hDBC,    // Handle della connessione
			                 NULL,    // Handle della Windows
							 File, // Nome del file / Driver da usare
							 strlen(File), // Lunghezza
							 szConnStr, // Connection string di ritorno
							 sizeof(szConnStr), 
							 &cbConnStr, 
							 SQL_DRIVER_NOPROMPT),
							 EhOdbc);

	// Alloca memoria per i comandi (ritorna puntatore in hStmt)
	SQLTRY("SQLAllocStmt", SQLAllocStmt(EhOdbc->hDBC, &EhOdbc->hStmt),EhOdbc);
	return NULL;
}
*/
}
Example #7
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);
}