/// <summary> /// Gets rows from the datasource based on the PK_Classes index. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="_id"></param> /// <param name="start">Row number at which to start reading.</param> /// <param name="pageLength">Number of rows to return.</param> /// <param name="count">out parameter to get total records for query.</param> /// <returns>Returns an instance of the <see cref="School.Entities.Classes"/> class.</returns> /// <remarks></remarks> /// <exception cref="System.Exception">The command could not be executed.</exception> /// <exception cref="System.Data.DataException">The <paramref name="transactionManager"/> is not open.</exception> /// <exception cref="System.Data.Common.DbException">The command could not be executed.</exception> public override School.Entities.Classes GetById(TransactionManager transactionManager, System.Int32 _id, int start, int pageLength, out int count) { SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Classes_GetById", _useStoredProcedure); database.AddInParameter(commandWrapper, "@Id", DbType.Int32, _id); IDataReader reader = null; TList <Classes> tmp = new TList <Classes>(); try { //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "GetById", tmp)); if (transactionManager != null) { reader = Utility.ExecuteReader(transactionManager, commandWrapper); } else { reader = Utility.ExecuteReader(database, commandWrapper); } //Create collection and fill Fill(reader, tmp, start, pageLength); count = -1; if (reader.NextResult()) { if (reader.Read()) { count = reader.GetInt32(0); } } //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "GetById", tmp)); } finally { if (reader != null) { reader.Close(); } commandWrapper = null; } if (tmp.Count == 1) { return(tmp[0]); } else if (tmp.Count == 0) { return(null); } else { throw new DataException("Cannot find the unique instance of the class."); } //return rows; }
/// <summary> /// Returns rows from the DataSource that meet the parameter conditions. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="parameters">A collection of <see cref="SqlFilterParameter"/> objects.</param> /// <param name="orderBy">Specifies the sort criteria for the rows in the DataSource (Name ASC; BirthDay DESC, Name ASC);</param> /// <param name="start">Row number at which to start reading.</param> /// <param name="pageLength">Number of rows to return.</param> /// <param name="count">out. The number of rows that match this query.</param> /// <returns>Returns a typed collection of School.Entities.Classes objects.</returns> public override TList <Classes> Find(TransactionManager transactionManager, IFilterParameterCollection parameters, string orderBy, int start, int pageLength, out int count) { SqlFilterParameterCollection filter = null; if (parameters == null) { filter = new SqlFilterParameterCollection(); } else { filter = parameters.GetParameters(); } SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Classes_Find_Dynamic", typeof(ClassesColumn), filter, orderBy, start, pageLength); SqlFilterParameter param; for (int i = 0; i < filter.Count; i++) { param = filter[i]; database.AddInParameter(commandWrapper, param.Name, param.DbType, param.GetValue()); } TList <Classes> rows = new TList <Classes>(); IDataReader reader = null; try { //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "Find", rows)); if (transactionManager != null) { reader = Utility.ExecuteReader(transactionManager, commandWrapper); } else { reader = Utility.ExecuteReader(database, commandWrapper); } Fill(reader, rows, 0, int.MaxValue); count = rows.Count; if (reader.NextResult()) { if (reader.Read()) { count = reader.GetInt32(0); } } //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "Find", rows)); } finally { if (reader != null) { reader.Close(); } commandWrapper = null; } return(rows); }
} //end getall #endregion #region GetPaged Methods /// <summary> /// Gets a page of rows from the DataSource. /// </summary> /// <param name="start">Row number at which to start reading.</param> /// <param name="pageLength">Number of rows to return.</param> /// <param name="count">Number of rows in the DataSource.</param> /// <param name="whereClause">Specifies the condition for the rows returned by a query (Name='John Doe', Name='John Doe' AND Id='1', Name='John Doe' OR Id='1').</param> /// <param name="orderBy">Specifies the sort criteria for the rows in the DataSource (Name ASC; BirthDay DESC, Name ASC);</param> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <remarks></remarks> /// <returns>Returns a typed collection of School.Entities.Classes objects.</returns> public override TList <Classes> GetPaged(TransactionManager transactionManager, string whereClause, string orderBy, int start, int pageLength, out int count) { SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Classes_GetPaged", _useStoredProcedure); if (commandWrapper.CommandType == CommandType.Text && commandWrapper.CommandText != null) { commandWrapper.CommandText = commandWrapper.CommandText.Replace(SqlUtil.PAGE_INDEX, string.Concat(SqlUtil.PAGE_INDEX, Guid.NewGuid().ToString("N").Substring(0, 8))); } database.AddInParameter(commandWrapper, "@WhereClause", DbType.String, whereClause); database.AddInParameter(commandWrapper, "@OrderBy", DbType.String, orderBy); database.AddInParameter(commandWrapper, "@PageIndex", DbType.Int32, start); database.AddInParameter(commandWrapper, "@PageSize", DbType.Int32, pageLength); IDataReader reader = null; //Create Collection TList <Classes> rows = new TList <Classes>(); try { //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "GetPaged", rows)); if (transactionManager != null) { reader = Utility.ExecuteReader(transactionManager, commandWrapper); } else { reader = Utility.ExecuteReader(database, commandWrapper); } Fill(reader, rows, 0, int.MaxValue); count = rows.Count; if (reader.NextResult()) { if (reader.Read()) { count = reader.GetInt32(0); } } //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "GetPaged", rows)); } catch (Exception) { throw; } finally { if (reader != null) { reader.Close(); } commandWrapper = null; } return(rows); }
} //end Delete #endregion #region Find Functions #region Parsed Find Methods /// <summary> /// Returns rows meeting the whereClause condition from the DataSource. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="whereClause">Specifies the condition for the rows returned by a query (Name='John Doe', Name='John Doe' AND Id='1', Name='John Doe' OR Id='1').</param> /// <param name="start">Row number at which to start reading.</param> /// <param name="pageLength">Number of rows to return.</param> /// <param name="count">out. The number of rows that match this query.</param> /// <remarks>Operators must be capitalized (OR, AND).</remarks> /// <returns>Returns a typed collection of School.Entities.Classes objects.</returns> public override TList <Classes> Find(TransactionManager transactionManager, string whereClause, int start, int pageLength, out int count) { count = -1; if (whereClause.IndexOf(";") > -1) { return(new TList <Classes>()); } SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Classes_Find", _useStoredProcedure); bool searchUsingOR = false; if (whereClause.IndexOf(" OR ") > 0) // did they want to do "a=b OR c=d OR..."? { searchUsingOR = true; } database.AddInParameter(commandWrapper, "@SearchUsingOR", DbType.Boolean, searchUsingOR); database.AddInParameter(commandWrapper, "@Id", DbType.Int32, DBNull.Value); database.AddInParameter(commandWrapper, "@ClassNumber", DbType.String, DBNull.Value); // replace all instances of 'AND' and 'OR' because we already set searchUsingOR whereClause = whereClause.Replace(" AND ", "|").Replace(" OR ", "|"); string[] clauses = whereClause.ToLower().Split('|'); // Here's what's going on below: Find a field, then to get the value we // drop the field name from the front, trim spaces, drop the '=' sign, // trim more spaces, and drop any outer single quotes. // Now handles the case when two fields start off the same way - like "Friendly='Yes' AND Friend='john'" char[] equalSign = { '=' }; char[] singleQuote = { '\'' }; foreach (string clause in clauses) { if (clause.Trim().StartsWith("id ") || clause.Trim().StartsWith("id=")) { database.SetParameterValue(commandWrapper, "@Id", clause.Trim().Remove(0, 2).Trim().TrimStart(equalSign).Trim().Trim(singleQuote)); continue; } if (clause.Trim().StartsWith("classnumber ") || clause.Trim().StartsWith("classnumber=")) { database.SetParameterValue(commandWrapper, "@ClassNumber", clause.Trim().Remove(0, 11).Trim().TrimStart(equalSign).Trim().Trim(singleQuote)); continue; } throw new ArgumentException("Unable to use this part of the where clause in this version of Find: " + clause); } IDataReader reader = null; //Create Collection TList <Classes> rows = new TList <Classes>(); try { //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "Find", rows)); if (transactionManager != null) { reader = Utility.ExecuteReader(transactionManager, commandWrapper); } else { reader = Utility.ExecuteReader(database, commandWrapper); } Fill(reader, rows, start, pageLength); if (reader.NextResult()) { if (reader.Read()) { count = reader.GetInt32(0); } } //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "Find", rows)); } finally { if (reader != null) { reader.Close(); } commandWrapper = null; } return(rows); }