Example #1
0
        internal bool GetData(Report rpt, Fields flds, Filters f)
        {
            Rows uData = this.GetMyUserData(rpt);

            if (uData != null)
            {
                this.SetMyData(rpt, uData);
                return(uData.Data == null || uData.Data.Count == 0 ? false : true);
            }

            // Treat this as a SQL statement
            DataSourceDefn ds = _DataSourceDefn;

            if (ds == null || _CommandText == null)
            {
                this.SetMyData(rpt, null);
                return(false);
            }

            IDbConnection cnSQL = ds.SqlConnect(rpt);

            if (cnSQL == null)
            {
                this.SetMyData(rpt, null);
                return(false);
            }

            Rows        _Data = new Rows(rpt, null, null, null);                // no sorting and grouping at base data
            String      sql   = _CommandText.EvaluateString(rpt, null);
            IDbCommand  cmSQL = null;
            IDataReader dr    = null;

            try
            {
                cmSQL             = cnSQL.CreateCommand();
                cmSQL.CommandText = AddParametersAsLiterals(rpt, cnSQL, sql, true);
                if (this._QueryCommandType == QueryCommandTypeEnum.StoredProcedure)
                {
                    cmSQL.CommandType = CommandType.StoredProcedure;
                }
                if (this._Timeout > 0)
                {
                    cmSQL.CommandTimeout = this._Timeout;
                }

                AddParameters(rpt, cnSQL, cmSQL, true);
                dr = cmSQL.ExecuteReader(CommandBehavior.SingleResult);

                List <Row> ar = new List <Row>();
                _Data.Data = ar;
                int rowCount   = 0;
                int maxRows    = _RowLimit > 0? _RowLimit: int.MaxValue;
                int fieldCount = flds.Items.Count;

                // Determine the query column number for each field
                int[] qcn = new int[flds.Items.Count];
                foreach (Field fld in flds)
                {
                    qcn[fld.ColumnNumber] = -1;
                    if (fld.Value != null)
                    {
                        continue;
                    }
                    try
                    {
                        qcn[fld.ColumnNumber] = dr.GetOrdinal(fld.DataField);
                    }
                    catch
                    {
                        qcn[fld.ColumnNumber] = -1;
                    }
                }

                while (dr.Read())
                {
                    Row or = new Row(_Data, fieldCount);

                    foreach (Field fld in flds)
                    {
                        if (qcn[fld.ColumnNumber] != -1)
                        {
                            or.Data[fld.ColumnNumber] = dr.GetValue(qcn[fld.ColumnNumber]);
                        }
                    }

                    // Apply the filters
                    if (f == null || f.Apply(rpt, or))
                    {
                        or.RowNumber = rowCount;                                //
                        rowCount++;
                        ar.Add(or);
                    }
                    if (--maxRows <= 0)                                                 // don't retrieve more than max
                    {
                        break;
                    }
                }
                ar.TrimExcess();                // free up any extraneous space; can be sizeable for large # rows
                if (f != null)
                {
                    f.ApplyFinalFilters(rpt, _Data, false);
                }
//#if DEBUG
//				rpt.rl.LogError(4, "Rows Read:" + ar.Count.ToString() + " SQL:" + sql );
//#endif
            }
            catch (Exception e)
            {
                rpt.rl.LogError(8, "SQL Exception" + e.Message + "\r\n" + e.StackTrace);
            }
            finally
            {
                if (cmSQL != null)
                {
                    cmSQL.Dispose();
                    if (dr != null)
                    {
                        dr.Close();
                    }
                }
            }
            this.SetMyData(rpt, _Data);
            return(_Data == null || _Data.Data == null || _Data.Data.Count == 0 ? false : true);
        }
Example #2
0
        // Handle parsing of function in final pass
        override internal void FinalPass()
        {
            if (_CommandText != null)
            {
                _CommandText.FinalPass();
            }
            if (_QueryParameters != null)
            {
                _QueryParameters.FinalPass();
            }

            // verify the data source
            DataSourceDefn ds = null;

            if (OwnerReport.DataSourcesDefn != null &&
                OwnerReport.DataSourcesDefn.Items != null)
            {
                ds = OwnerReport.DataSourcesDefn[_DataSourceName];
            }
            if (ds == null)
            {
                OwnerReport.rl.LogError(8, "Query references unknown data source '" + _DataSourceName + "'");
                return;
            }
            _DataSourceDefn = ds;

            IDbConnection cnSQL = ds.SqlConnect(null);

            if (cnSQL == null || _CommandText == null)
            {
                return;
            }

            // Treat this as a SQL statement
            String      sql   = _CommandText.EvaluateString(null, null);
            IDbCommand  cmSQL = null;
            IDataReader dr    = null;

            try
            {
                cmSQL             = cnSQL.CreateCommand();
                cmSQL.CommandText = AddParametersAsLiterals(null, cnSQL, sql, false);
                if (this._QueryCommandType == QueryCommandTypeEnum.StoredProcedure)
                {
                    cmSQL.CommandType = CommandType.StoredProcedure;
                }

                AddParameters(null, cnSQL, cmSQL, false);
                dr = cmSQL.ExecuteReader(CommandBehavior.SchemaOnly);
                if (dr.FieldCount < 10)
                {
                    _Columns = new ListDictionary();                            // Hashtable is overkill for small lists
                }
                else
                {
                    _Columns = new Hashtable(dr.FieldCount);
                }

                for (int i = 0; i < dr.FieldCount; i++)
                {
                    QueryColumn qc = new QueryColumn(i, dr.GetName(i), Type.GetTypeCode(dr.GetFieldType(i)));

                    try { _Columns.Add(qc.colName, qc); }
                    catch                       // name has already been added to list:
                    {                           // According to the RDL spec SQL names are matched by Name not by relative
                                                //   position: this seems wrong to me and causes this problem; but
                                                //   user can fix by using "as" keyword to name columns in Select
                                                //    e.g.  Select col as "col1", col as "col2" from tableA
                        OwnerReport.rl.LogError(8, String.Format("Column '{0}' is not uniquely defined within the SQL Select columns.", qc.colName));
                    }
                }
            }
            catch (Exception e)
            {
                OwnerReport.rl.LogError(4, "SQL Exception during report compilation: " + e.Message + "\r\nSQL: " + sql);
            }
            finally
            {
                if (cmSQL != null)
                {
                    cmSQL.Dispose();
                    if (dr != null)
                    {
                        dr.Close();
                    }
                }
            }

            return;
        }