package DotNetUDFTest;
import System.*;
import System.Data.*;
import System.Data.SqlTypes.*;
import Microsoft.SqlServer.Server.*;
/*
import yahooquote.app.YahooQuote;
import yahooquote.beans.YahooQuoteBean;
*/
import gssc.*;
import System.Collections.*;
public class DotNetUDFTestLib
{
private static final DotNetUDFTestLib instance = new DotNetUDFTestLib();
private static volatile Hashtable instancePool = new Hashtable();
private static int counter = 0;
/** @attribute Microsoft.SqlServer.Server.SqlProcedure() */
public static void getTestString()
{
SqlContext.get_Pipe().Send("Hello world!!!");
}
/** @attribute Microsoft.SqlServer.Server.SqlProcedure() */
public static void getTestResultset()
{
SqlPipe lSqlPipe = SqlContext.get_Pipe();
SqlMetaData[] cols = new SqlMetaData[1];
cols[0] = new SqlMetaData("TEST_STRING", SqlDbType.NVarChar, 1024);
SqlDataRecord lSqlDataRecord = new SqlDataRecord(cols);
lSqlPipe.SendResultsStart(lSqlDataRecord);
for (int cnt = 0; cnt < 100; cnt++)
{
lSqlDataRecord.SetSqlString(0, new SqlString("test" + cnt));
lSqlPipe.SendResultsRow(lSqlDataRecord);
}
lSqlPipe.SendResultsEnd();
}
/** @attribute Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "FillRowCustomTable") */
public static IEnumerable Quotes(String exchange, String symbol, String startDate, String endDate)
{
counter++;
instancePool.Add(new Integer(counter), new Integer(0));
/*
YahooQuote yq = new YahooQuote();
yq.init("ichart.yahoo.com", "80", null);
return yq.getStockQuoteDataArr(exchange, symbol, startDate, endDate);
*/
GoogleStockQuoteServiceClient gssc = GoogleStockQuoteServiceClient.createInstance();
return gssc.getQuotes(exchange, symbol, startDate, endDate);
}
public static void FillRowCustomTable(
Object resultObj,
/** @ref */ SqlInt32 cnt,
/** @ref */ SqlDateTime date,
/** @ref */ SqlDecimal open,
/** @ref */ SqlDecimal high,
/** @ref */ SqlDecimal low,
/** @ref */ SqlDecimal close,
/** @ref */ SqlInt32 vol)
{
int i = ((Integer)instancePool.get_Item(new Integer(DotNetUDFTest.DotNetUDFTestLib.counter))).intValue();
instancePool.set_Item(new Integer(DotNetUDFTest.DotNetUDFTestLib.counter), new Integer(++i));
/*
YahooQuoteBean yqb = (YahooQuoteBean)resultObj;
cnt = new SqlInt32(i - 1);
date = new SqlDateTime(DateTime.ParseExact(yqb.getDate(), "dd.MM.yyyy", null));
open = new SqlDecimal(yqb.getOpen());
high = new SqlDecimal(yqb.getHigh());
low = new SqlDecimal(yqb.getLow());
close = new SqlDecimal(yqb.getClose());
vol = new SqlInt32(yqb.getVol());
est = new SqlDecimal(yqb.getEstim());
*/
GoogleStockQuoteBeanI gsq = (GoogleStockQuoteBeanI)resultObj;
cnt = new SqlInt32(i - 1);
date = new SqlDateTime(DateTime.ParseExact(gsq.getDate(), "yyyy-MM-dd", null));
open = new SqlDecimal(gsq.getOpen());
high = new SqlDecimal(gsq.getHigh());
low = new SqlDecimal(gsq.getLow());
close = new SqlDecimal(gsq.getClose());
vol = new SqlInt32(gsq.getVolume());
// est = new SqlDecimal(yqb.getEstim());
}
/** @attribute Microsoft.SqlServer.Server.SqlProcedure() */
public static void getQuotes(SqlString exchange, SqlString symbol, SqlString startDate, SqlString endDate)
{
SqlPipe lSqlPipe = SqlContext.get_Pipe();
SqlMetaData[] cols = new SqlMetaData[8];
int colsOrdinal = 0;
cols[colsOrdinal++] = new SqlMetaData("cnt", SqlDbType.Int); // counter
cols[colsOrdinal++] = new SqlMetaData("date", SqlDbType.DateTime); // date
cols[colsOrdinal++] = new SqlMetaData("open", SqlDbType.Decimal, (ubyte)28, (ubyte)9); // open price
cols[colsOrdinal++] = new SqlMetaData("high", SqlDbType.Decimal, (ubyte)28, (ubyte)9); // high price
cols[colsOrdinal++] = new SqlMetaData("low", SqlDbType.Decimal, (ubyte)28, (ubyte)9); // low price
cols[colsOrdinal++] = new SqlMetaData("close", SqlDbType.Decimal, (ubyte)28, (ubyte)9); // close price
cols[colsOrdinal++] = new SqlMetaData("vol", SqlDbType.Int); // volume
cols[colsOrdinal++] = new SqlMetaData("est", SqlDbType.Decimal, (ubyte)28, (ubyte)9); // estimation
SqlDataRecord lSqlDataRecord = new SqlDataRecord(cols);
lSqlPipe.SendResultsStart(lSqlDataRecord);
/*
YahooQuote yq = new YahooQuote();
yq.init("ichart.yahoo.com", "80", null);
*/
GoogleStockQuoteServiceClient gsq = GoogleStockQuoteServiceClient.createInstance();
// Object[] lst = yq.getStockQuoteDataArr(exchange.ToString(), symbol.ToString(), startDate.ToString(), endDate.ToString());
Object[] lst = gsq.getQuotes(exchange.ToString(), symbol.ToString(), startDate.ToString(), endDate.ToString());
for (int cnt = 0; cnt < lst.length; cnt++)
{
colsOrdinal = 0;
/*
YahooQuoteBean entry = (YahooQuoteBean)lst[cnt];
////////
lSqlDataRecord.SetSqlInt32(colsOrdinal++, new SqlInt32(cnt));
lSqlDataRecord.SetSqlDateTime(colsOrdinal++, new SqlDateTime(DateTime.ParseExact(entry.getDate(), "dd.MM.yyyy", null)));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getOpen()));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getHigh()));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getLow()));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getClose()));
lSqlDataRecord.SetSqlInt32(colsOrdinal++, new SqlInt32(entry.getVol()));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getEstim()));
////////
*/
GoogleStockQuoteBeanI entry = (GoogleStockQuoteBeanI)lst[cnt];
////////
lSqlDataRecord.SetSqlInt32(colsOrdinal++, new SqlInt32(cnt));
lSqlDataRecord.SetSqlDateTime(colsOrdinal++, new SqlDateTime(DateTime.ParseExact(entry.getDate(), "dd.MM.yyyy", null)));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getOpen()));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getHigh()));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getLow()));
lSqlDataRecord.SetSqlDecimal(colsOrdinal++, new SqlDecimal(entry.getClose()));
lSqlDataRecord.SetSqlInt32(colsOrdinal++, new SqlInt32(entry.getVolume()));
////////
lSqlPipe.SendResultsRow(lSqlDataRecord);
}
lSqlPipe.SendResultsEnd();
}
}