/// <summary> /// Executes an insert query for the given entity using the given sql insert statement. /// This overload will automatically run an identity query if you have mapped an auto-incrementing column, /// and if an identity query has been implemented for your current database dialect. /// </summary> public object Insert <T>(T entity, string sql) { var columns = MapRepository.Instance.GetColumns(typeof(T)); var dialect = QueryFactory.CreateDialect(this); var builder = Insert <T>().Entity(entity).QueryText(sql); // If an auto-increment column exists and this dialect has an identity query... if (columns.Exists(c => c.ColumnInfo.IsAutoIncrement) && dialect.HasIdentityQuery) { builder.GetIdentity(); } return(builder.Execute()); }
public int Delete <T>(string tableName, Expression <Func <T, bool> > filter) { // Remember sql mode var previousSqlMode = SqlMode; SqlMode = SqlModes.Text; var mappingHelper = new MappingHelper(this); if (tableName == null) { tableName = MapRepository.Instance.GetTableName(typeof(T)); } var dialect = QueryFactory.CreateDialect(this); TableCollection tables = new TableCollection(); tables.Add(new Table(typeof(T))); var where = new WhereBuilder <T>(Command, dialect, filter, tables, false, false); IQuery query = QueryFactory.CreateDeleteQuery(dialect, tables[0], where.ToString()); Command.CommandText = query.Generate(); int rowsAffected = 0; try { OpenConnection(); rowsAffected = Command.ExecuteNonQuery(); } finally { CloseConnection(); } // Return to previous sql mode SqlMode = previousSqlMode; return(rowsAffected); }
/// <summary> /// Creates a QueryBuilder that allows you to build a query. /// </summary> /// <typeparam name="T">The type of object that will be queried.</typeparam> /// <returns>Returns a QueryBuilder of T.</returns> public QueryBuilder <T> Query <T>() { var dialect = QueryFactory.CreateDialect(this); return(new QueryBuilder <T>(this, dialect)); }