public static bool IsPrimaryKey(this ISqlColumn column, ISqlTable table) { var primaryKey = table.Constraints.OfType <ISqlPrimaryKeyConstraint>().FirstOrDefault(); return(primaryKey != null && primaryKey.ColumnSpecifications[0].Column.Name.Parts[2] == column.Name.Parts[2]); }
public SqlColumnBinding(SqlColumnBindingAttribute bindingAttribute, PropertyInfo propertyInfo, ISqlColumn column) { BindingAttribute = bindingAttribute; PropertyInfo = propertyInfo; Column = column; }
public static ISqlForeignKeyConstraint GetForeignKeyForColumn(this ISqlTable table, ISqlColumn column) { var foreignKeys = table.Constraints.OfType<ISqlForeignKeyConstraint>(); foreach ( var foreignKey in foreignKeys ) { var columns = foreignKey.Columns.Where(p => p.Name.Parts[2] == column.Name.Parts[2]); if (columns.Count() == 1) { return foreignKey; } } return null; }
/// <summary> /// Applies sorting in fallback manner. If no sorting is specified in the query, default /// column sorting is used. /// </summary> /// <param name="defaultColumnSort">Column used for default sorting</param> /// <param name="defaultColumnSortOrder">Sort order of default column</param> /// <param name="inputSortByColumns">Columns with sorting from the input</param> public void ApplySorting(string defaultColumnSort, SortOrder defaultColumnSortOrder, IEnumerable <ISqlSortColumn> inputSortByColumns) { foreach (ISqlSortColumn sortByColumn in inputSortByColumns) { // if the sorting column is not present in the list of columns, add it at the beginning ISqlColumn existingColumn = Columns.FirstOrDefault(x => x.Expression == sortByColumn.Expression); if (existingColumn != null) { existingColumn.SortOrder = sortByColumn.SortOrder; } } // if no sorting has been set, use sorting by the default column if (!Columns.Any(x => x.SortOrder != SortOrder.Unspecified)) { // if the column exists, just change the order ISqlColumn existingDefaultColumn = Columns.FirstOrDefault(x => x.Expression == defaultColumnSort); if (existingDefaultColumn != null) { existingDefaultColumn.SortOrder = defaultColumnSortOrder; return; } // if the column is specified in the input, keep the sort order and insert it in the list of columns ISqlSortColumn existingInputColumn = inputSortByColumns.FirstOrDefault(col => col.Expression == defaultColumnSort); if (existingInputColumn != null) { InsertColumn(existingInputColumn.Expression, true, existingInputColumn.SortOrder); return; } // if the default column does not exist in the query columns or in the input columns // create it and insert it var defaultSortingColumn = new SqlColumn(defaultColumnSort) { SortOrder = defaultColumnSortOrder, Include = true }; columns.Insert(0, defaultSortingColumn); } }
private List <SqlColumnBinding> GetColumnBindings(Type objectType) { var sqlColumnBindings = new List <SqlColumnBinding>(); var properties = objectType.GetProperties(); foreach (PropertyInfo propertyInfo in properties) { var customAttributes = propertyInfo.GetCustomAttributes(true); foreach (var customAttribute in customAttributes) { var bindingAttribute = customAttribute as SqlColumnBindingAttribute; if (bindingAttribute != null) { ISqlColumn column = FindColumn(bindingAttribute); if (column != null) { bool isNullable = propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>); if (column.IsPrimaryKey && !isNullable) { string errorMessage = $"The Property {propertyInfo.Name} is attempting to bind to column " + $"{column.Name} in table {GetTableName()}. This column is" + $"designated as the primary key, but the property is not nullable. This is required" + $"for detecting insertions and updates."; throw new InvalidSqlBindingException(errorMessage); } sqlColumnBindings.Add(new SqlColumnBinding(bindingAttribute, propertyInfo, column)); } else { var errorMessage = $"Invalid binding: The table {GetTableName()} could not find a column " + $"definition for the SqlColumnBindingAttribute of'{bindingAttribute.ColumnName}'"; throw new InvalidSqlBindingException(errorMessage); } } } } return(sqlColumnBindings); }
public static ISqlForeignKeyConstraint GetForeignKeyForColumn(this ISqlTable table, ISqlColumn column) { var foreignKeys = table.Constraints.OfType <ISqlForeignKeyConstraint>(); foreach (var foreignKey in foreignKeys) { var columns = foreignKey.Columns.Where(p => p.Name.Parts[2] == column.Name.Parts[2]); if (columns.Count() == 1) { return(foreignKey); } } return(null); }
public SqlForeignKey(ISqlColumn localColumn, string foreignTableName, ISqlColumn remoteColumn) { _localColumn = localColumn; _foreignTableName = foreignTableName; _remoteColumn = remoteColumn; }