private void AddColumn(string columnName, bool throwIfAlreadyPresent) { // Check parameters if (columnName == null) { throw new ArgumentNullException("columnName"); } // Find the column in the view LearningStoreViewColumn column = null; if (!m_view.TryGetColumnByName(columnName, out column)) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, LearningStoreStrings.ColumnNotFound, columnName)); } // Verify that the column name doesn't already exist if (m_columns.Contains(column)) { if (throwIfAlreadyPresent) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, LearningStoreStrings.ColumnAlreadyExistsInQuery, columnName)); } } else { m_columns.Add(column); } }
/// <summary> /// Initializes a new instance of the <Typ>LearningStoreQuerySort</Typ> class. /// </summary> /// <param name="column"></param> /// <param name="direction"></param> public LearningStoreQuerySort(LearningStoreViewColumn column, LearningStoreSortDirection direction) { // Check the parameters if (column == null) { throw new LearningComponentsInternalException("LSTR2220"); } m_column = column; m_direction = direction; }
/// <summary> /// Add a sort order for the returned data /// </summary> /// <param name="columnName">Name of the column to be sorted on. This must /// refer to a non-XML, non-Guid column.</param> /// <param name="direction">Direction for the sort.</param> /// <exception cref="ArgumentNullException"><paramref name="columnName"/> is a null reference.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="direction"/> is invalid.</exception> /// <exception cref="InvalidOperationException"> /// The column was not found or is not sortable</exception> /// <example> /// The following code creates a new query that returns the names of /// all the users in the system sorted by name: /// <code language="C#"> /// LearningStoreQuery query = store.CreateQuery("UserItemView"); /// query.AddColumn("Name"); /// query.AddSort("Name", LearningStoreSortDirection.Ascending); /// </code> /// </example> public void AddSort(string columnName, LearningStoreSortDirection direction) { // Check input parameters if (columnName == null) { throw new ArgumentNullException("columnName"); } if ((direction != LearningStoreSortDirection.Ascending) && (direction != LearningStoreSortDirection.Descending)) { throw new ArgumentOutOfRangeException("direction"); } // Find the column in the view LearningStoreViewColumn column = null; if (!m_view.TryGetColumnByName(columnName, out column)) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, LearningStoreStrings.ColumnNotFound, columnName)); } // Can't sort on some columns if (!column.Sortable) { throw new InvalidOperationException( LearningStoreStrings.ColumnCannotBeSorted); } // Verify that the same sort hasn't already been added if (m_sorts.Exists(delegate(LearningStoreQuerySort otherSort) { return(Object.ReferenceEquals(column, otherSort.Column)); })) { throw new InvalidOperationException( LearningStoreStrings.ColumnAlreadySortedOn); } // Add the sort LearningStoreQuerySort sort = new LearningStoreQuerySort( column, direction); m_sorts.Add(sort); }
/// <summary> /// Initializes a new instance of the <Typ>LearningStoreCondition</Typ> class. /// </summary> /// <param name="column"></param> /// <param name="conditionOperator"></param> /// <param name="conditionValue"></param> public LearningStoreCondition(LearningStoreViewColumn column, LearningStoreConditionOperator conditionOperator, object conditionValue) { // Check the parameters if (column == null) { throw new LearningComponentsInternalException("LSTR2200"); } if ((conditionOperator != LearningStoreConditionOperator.Equal) && (conditionOperator != LearningStoreConditionOperator.GreaterThan) && (conditionOperator != LearningStoreConditionOperator.GreaterThanEqual) && (conditionOperator != LearningStoreConditionOperator.LessThan) && (conditionOperator != LearningStoreConditionOperator.LessThanEqual) && (conditionOperator != LearningStoreConditionOperator.NotEqual)) { throw new LearningComponentsInternalException("LSTR2210"); } m_column = column; m_conditionOperator = conditionOperator; m_conditionValue = conditionValue; }
[SuppressMessage("Microsoft.Maintainability", "CA1502")] // Much of the complexity is due to simple switch statements public static DataTable ReadDataTableResult(LogableSqlCommand command, IList <LearningStoreViewColumn> columns, CultureInfo locale) { // Check input parameters if (command == null) { throw new LearningComponentsInternalException("LSTR1050"); } if (columns == null) { throw new LearningComponentsInternalException("LSTR1060"); } // Create the DataTable DataTable table = new DataTable(); table.Locale = locale; // Add the columns foreach (LearningStoreViewColumn column in columns) { Type t = null; switch (column.ValueType.TypeCode) { case LearningStoreValueTypeCode.Boolean: t = typeof(Boolean); break; case LearningStoreValueTypeCode.DateTime: t = typeof(DateTime); break; case LearningStoreValueTypeCode.Double: t = typeof(Double); break; case LearningStoreValueTypeCode.Enumeration: t = typeof(Int32); break; case LearningStoreValueTypeCode.Int32: t = typeof(Int32); break; case LearningStoreValueTypeCode.ItemIdentifier: t = typeof(LearningStoreItemIdentifier); break; case LearningStoreValueTypeCode.Single: t = typeof(Single); break; case LearningStoreValueTypeCode.String: t = typeof(String); break; case LearningStoreValueTypeCode.Xml: t = typeof(LearningStoreXml); break; case LearningStoreValueTypeCode.ByteArray: t = typeof(System.Byte[]); break; case LearningStoreValueTypeCode.Guid: t = typeof(Guid); break; default: throw new LearningComponentsInternalException("LSTR1070"); } table.Columns.Add(column.Name, t); } // Begin loading the data table.BeginLoadData(); // Enumerate through each row while (command.Read()) { // Remember the current input column index int inputColumnIndex = 0; // Create a new array that will hold the items object[] data = new object[columns.Count]; // Enumerate through each column for (int outputColumnIndex = 0; outputColumnIndex < columns.Count; outputColumnIndex++) { // Get the column LearningStoreViewColumn column = columns[outputColumnIndex]; // Read the value switch (column.ValueType.TypeCode) { case LearningStoreValueTypeCode.Boolean: case LearningStoreValueTypeCode.DateTime: case LearningStoreValueTypeCode.Double: case LearningStoreValueTypeCode.Int32: case LearningStoreValueTypeCode.Single: case LearningStoreValueTypeCode.String: case LearningStoreValueTypeCode.Guid: case LearningStoreValueTypeCode.Enumeration: case LearningStoreValueTypeCode.ByteArray: if (command.IsDBNull(inputColumnIndex)) { data[outputColumnIndex] = null; } else { data[outputColumnIndex] = command.GetValue(inputColumnIndex); } inputColumnIndex++; break; case LearningStoreValueTypeCode.ItemIdentifier: data[outputColumnIndex] = ReadItemIdentifierColumns(command, ref inputColumnIndex, column.ValueType.ReferencedItemType); break; case LearningStoreValueTypeCode.Xml: data[outputColumnIndex] = ReadXmlColumns(command, ref inputColumnIndex); break; default: throw new LearningComponentsInternalException("LSTR1080"); } } table.Rows.Add(data); } // Finish loading the data table.EndLoadData(); table.AcceptChanges(); return(table); }
/// <summary> /// Add a condition that restricts the data returned in a query. /// </summary> /// <param name="columnName">Name of the column to be compared</param> /// <param name="conditionOperator">Condition operator</param> /// <param name="conditionValue">Value to be compared against.</param> /// <exception cref="ArgumentNullException"><paramref name="columnName"/> is a null reference.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="conditionOperator"/> is invalid.</exception> /// <exception cref="InvalidOperationException"> /// Invalid comparison, the column name wasn't found in the view, or an invalid /// <paramref name="conditionValue"/> value.</exception> /// <remarks> /// See the <Typ>LearningStoreQuery</Typ> documentation for more /// information about creating and executing queries. /// <p/> /// <paramref name="conditionValue"/> must contain a value /// that is valid based on the type of the column specified by /// <paramref name="columnName"/>: /// <ul> /// <li><b>Boolean:</b> A System.Boolean, or an object that can be converted into /// a System.Boolean using <Typ>/System.IConvertible</Typ>.</li> /// <li><b>ByteArray</b> A System.Byte array.</li> /// <li><b>DateTime:</b> A System.DateTime, or an object that can be converted into /// a System.DateTime using <Typ>/System.IConvertible</Typ>.</li> /// <li><b>Double:</b> A System.Double, or an object that can be converted into /// a System.Double using <Typ>/System.IConvertible</Typ>.</li> /// <li><b>Enumeration:</b> An System.Int32, or an object that can be converted into /// a System.Int32 using <Typ>/System.IConvertible</Typ>.</li> /// <li><b>Guid</b> A System.Guid or a string representing a Guid.</li> /// <li><b>Item identifier:</b> A <Typ>LearningStoreItemIdentifier</Typ> /// of the associated item type.</li> /// <li><b>Int32:</b> A System.Int32, or an object that can be converted into /// a System.Int32 using <Typ>/System.IConvertible</Typ>.</li> /// <li><b>Single:</b> A System.Single, or an object that can be converted into /// a System.Single using <Typ>/System.IConvertible</Typ>.</li> /// <li><b>String:</b> A System.String, or an object that can be converted into /// a System.String using <Typ>/System.IConvertible</Typ>.</li> /// <li><b>Xml:</b> A <Typ>LearningStoreXml</Typ>.</li> /// </ul> /// <p/> /// <paramref name="conditionValue"/> must not contain a <Typ>LearningStoreItemIdentifier</Typ> returned /// from <Mth>../LearningStoreJob.AddItem</Mth>. /// <p/> /// If <paramref name="columnName"/> refers to an XML column, then the following restrictions apply: /// <ul> /// <li><paramref name="conditionOperator"/> must be <Fld>../LearningStoreConditionOperator.Equal</Fld> or /// <Fld>../LearningStoreConditionOperator.NotEqual</Fld></li> /// <li><paramref name="conditionValue"/> must be null.</li> /// </ul> /// <p/> /// If <paramref name="columnName"/> refers to an Guid column, then the following restrictions apply: /// <ul> /// <li><paramref name="conditionOperator"/> must be <Fld>../LearningStoreConditionOperator.Equal</Fld> or /// <Fld>../LearningStoreConditionOperator.NotEqual</Fld></li> /// </ul> /// <p/> /// If <paramref name="conditionOperator"/> is <Fld>../LearningStoreConditionOperator.GreaterThan</Fld>, /// <Fld>../LearningStoreConditionOperator.GreaterThanEqual</Fld>, /// <Fld>../LearningStoreConditionOperator.LessThan</Fld>, or /// <Fld>../LearningStoreConditionOperator.LessThanEqual</Fld>, then /// <paramref name="conditionValue"/> must not contain null. /// <p/> /// Care must be taken when null is involved in a comparison. In /// particular:<ul> /// <li>null == null: True</li> /// <li>null != null: False</li> /// <li>null == any other value: False</li> /// <li>null != any other value: True</li> /// <li>null < any other value: False</li> /// <li>null > any other value: False</li> /// </ul> /// </remarks> /// <example> /// The following code creates a new query that returns the id and name /// of the users with a name less than "Joe": /// <code language="C#"> /// LearningStoreQuery query = store.CreateQuery("UserItem"); /// query.AddColumn("Id"); /// query.AddColumn("Name"); /// query.AddCondition("Name", LearningStoreConditionOperator.LessThan, "Joe"); /// </code> /// </example> public void AddCondition(string columnName, LearningStoreConditionOperator conditionOperator, object conditionValue) { // Check input parameters if (columnName == null) { throw new ArgumentNullException("columnName"); } else if ((conditionOperator != LearningStoreConditionOperator.Equal) && (conditionOperator != LearningStoreConditionOperator.GreaterThan) && (conditionOperator != LearningStoreConditionOperator.GreaterThanEqual) && (conditionOperator != LearningStoreConditionOperator.LessThan) && (conditionOperator != LearningStoreConditionOperator.LessThanEqual) && (conditionOperator != LearningStoreConditionOperator.NotEqual)) { throw new ArgumentOutOfRangeException("conditionOperator"); } // Find the column in the view LearningStoreViewColumn column = null; if (!m_view.TryGetColumnByName(columnName, out column)) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, LearningStoreStrings.ColumnNotFound, columnName)); } // Cast the comparison value to the associated property type conditionValue = column.CastValue(conditionValue, m_locale, delegate(string reason, Exception innerException) { return(new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, LearningStoreStrings.InvalidConditionValueWithDescription, reason), innerException)); } ); // Fail if operator isn't compatible with null if ((conditionValue == null) && (conditionOperator != LearningStoreConditionOperator.Equal) && (conditionOperator != LearningStoreConditionOperator.NotEqual)) { throw new InvalidOperationException(LearningStoreStrings.InvalidOperatorForNullConditionValue); } // Verify special rules for Xml properties if ((column.IsXml) && (conditionValue != null)) { throw new InvalidOperationException(LearningStoreStrings.InvalidConditionValueForXmlColumn); } // Verify special rules for Guid properties if ((column.IsGuid) && (conditionOperator != LearningStoreConditionOperator.Equal) && (conditionOperator != LearningStoreConditionOperator.NotEqual)) { throw new InvalidOperationException(LearningStoreStrings.InvalidConditionOperatorForGuidColumn); } // Verify special rules for LearningStoreItemIdentifier properties LearningStoreItemIdentifier idConditionValue = conditionValue as LearningStoreItemIdentifier; if ((idConditionValue != null) && (!idConditionValue.HasKey)) { throw new InvalidOperationException(LearningStoreStrings.InvalidIdConditionValue); } // Add the condition LearningStoreCondition condition = new LearningStoreCondition(column, conditionOperator, conditionValue); m_conditions.Add(condition); }