示例#1
0
        private bool CheckPresenceValue(RelationInfo info, IRow row)
        {
            if (info.PresenceField is object)
            {
                if (!(info.PresenceField is BooleanField) &&
                    info.PresenceValue is bool b)
                {
                    if (info.PresenceField.IsNull(row) == b)
                    {
                        return(false);
                    }
                }
                else
                {
                    var newRow = row.CreateNew();
                    info.PresenceField.AsObject(newRow, info.PresenceField.ConvertValue(
                                                    info.PresenceValue, CultureInfo.InvariantCulture));
                    if (info.PresenceField.IndexCompare(row, newRow) != 0)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
示例#2
0
        public static bool IsUniqueIndexException(IDbConnection connection,
                                                  Exception exception, string indexName,
                                                  IRow oldRow, IRow newRow, params Field[] indexFields)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            if (indexFields == null ||
                indexFields.Length == 0)
            {
                throw new ArgumentNullException("indexField");
            }

            if (indexName != null &&
                !exception.Message.Contains(indexName))
            {
                return(false);
            }

            if (oldRow != null)
            {
                bool anyDifferent = false;
                foreach (var field in indexFields)
                {
                    if (field.IndexCompare(oldRow, newRow) != 0)
                    {
                        anyDifferent = true;
                        break;
                    }
                }

                if (!anyDifferent)
                {
                    return(false);
                }
            }

            var row     = newRow.CreateNew();
            var idField = newRow.IdField;

            var query = new SqlQuery()
                        .Dialect(connection.GetDialect())
                        .From(row).Select(idField);

            foreach (var field in indexFields)
            {
                query.WhereEqual(field, field.AsSqlValue(newRow));
            }

            if (!query.GetFirst(connection))
            {
                return(false);
            }

            return(idField.IndexCompare(row, newRow) != 0);
        }