TimeScape QL+ .NET Demo in C#

Query


IntroductionExample QueriesResultsView Code

Welcome

This demonstration provides web access to the new query language, TimeScape QL+, and the TimeScape Analytics Library.

The Example Queries tab contains QL+ queries for you to try. Click a query and hit the Query TimeScape button to request sample data from. You can also create your own custom queries... enjoy!

Try these example queries

The following C# code is used to power this demo. It was developed with the .NET Framework 2.0 BETA, using Visual Studio 2005. It has been tested on Windows 2000, IIS5, IE6.


public partial class _default  
{
    // Member variables
    private static Type XENOMORPH_LIST = typeof(Xenomorph.List);
    private static Type XENOMORPH_TIMESERIES = typeof(Xenomorph.TimeSeries);

    private void Page_Load(object sender, System.EventArgs e)
    {
        if (Page.IsPostBack)
        {
            string strError = "";
            string strTmp = "";
            string strQuery = tbxQuery.Text;
        
            // Instantiate a Xenomorph.TimeScape.DataServices.Data object using the DataServicesFactory and reference it using the
            // Xenomorph.TimeScape.DataServices.IData interface.
            
            Xenomorph.TimeScape.DataServices.IData oData = DataServicesFactory.CreateData(DataServicesFactory.TransportType.Local, null);
            
            try
            {
                // Run the query using the Query function,
                // object Query(string strQuery, string strServerOpt, string strDataRuleOpt, Xenomorph.QueryParameter[ ] oArgsOpt)
                
                object oRet = oData.Query(strQuery, null, null, null);

                // Prepare to display the result

                // Populate a data table with values
                System.Data.DataTable oResultTable = new DataTable("ResultsTable");
                
                // Running a query can return simple and complex types and need 
                // to act accordingly
                
                // Handle the Xenomorph.List type
                if (XENOMORPH_LIST.IsInstanceOfType(oRet))
                {
                    Xenomorph.List oList = (Xenomorph.List)oRet;
                    oResultTable.Columns.Clear();
                    foreach (List.Column oCol in oList.Columns)
                    {
                        oResultTable.Columns.Add();
                    }

                    for (int nRow = 0; nRow < oList.RowCount; nRow++)
                    {
                        System.Data.DataRow drRow = oResultTable.NewRow();

                        for (int nCol = 0; nCol < oList.ColCount; nCol++)
                        {
                            // If data type is a List or TimeSeries only display the dimensions 
                            if (XENOMORPH_LIST.IsInstanceOfType(oList.GetElement(nRow, nCol)) )
                            {
                                drRow[nCol] = oList.GetElement(nRow, nCol).ToString() 
                                + "( " + oList.RowCount + " by " + oList.ColCount + " )";
                            }
                            else if ( XENOMORPH_TIMESERIES.IsInstanceOfType( oList.GetElement(nRow, nCol) ))
                            {
                                drRow[nCol] = XENOMORPH_TIMESERIES.ToString() 
                                + "( " + ((Xenomorph.TimeSeries)oList.GetElement(nRow, nCol)).Dates.Length
                                + " by 2 )";
                            }
                            else
                            {
                                drRow[nCol] = oList.GetElement(nRow, nCol);
                            }
                        }
                        oResultTable.Rows.Add(drRow);
                    }
                }
                // ... and TimeSeries
                else if( XENOMORPH_TIMESERIES.IsInstanceOfType(oRet) )
                {
                    Xenomorph.TimeSeries oTimeSeries = (Xenomorph.TimeSeries)oRet;

                    oResultTable.Columns.Add( "Date", typeof( String ) );
                    oResultTable.Columns.Add( "Value", typeof( String ) );
                    
                    for (int nRow = 0; nRow < oTimeSeries.Dates.Length; nRow++)
                    {
                        System.Data.DataRow drRow = oResultTable.NewRow();

                        drRow[ 0 ] = oTimeSeries.Dates[ nRow ].DateTime.ToShortDateString();
                        drRow[ 1 ] = ((System.Array)oTimeSeries.Values).GetValue( nRow );

                        oResultTable.Rows.Add(drRow);
                    }
                }
                // ... finally treat the rest as simple types
                else 
                {
                    oResultTable.Columns.Add();
                    System.Data.DataRow drRow = oResultTable.NewRow();

                    drRow[0] = oRet.ToString();
                    oResultTable.Rows.Add(drRow);
                }

                // Populate the web grid with the data using the table
                gvQueryResult.DataSource = oResultTable;
                gvQueryResult.DataBind();
            }
            catch (Exception ee)
            {
                strError = "<br /><b>The query returned the following error:</b> <span style='color: #900;'>";
                strError += ee.Message + "</span>";
            }
            lblResultsHead.Text = "<p><b>Query:</b> " + tbxQuery.Text + strError + "</p>";
        }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        InitializeComponent();
        base.OnInit(e);
    }
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    }
    #endregion
}