Example #1
0
        void OnBeforeSave(object sender, BeforeSaveEventArgs e)
        {
            DataMap  map = e.DataMap;
            Type     t   = map.ObjectType;
            ISavable obj = e.DataObject;

            var audit = new SaveAudit
            {
                Action    = e.SaveType,
                TableName = e.DataMap.DataItem.SaveToTable
            };

            if (!string.IsNullOrEmpty(e.DataMap.DataItem.SchemaName))
            {
                audit.SchemaName = e.DataMap.DataItem.SchemaName;
            }

            if (e.SaveType == SaveType.Insert)
            {
                foreach (PropertyInfo pi in t.GetProperties())
                {
                    if ((pi.Name == "DataRowState") || (pi.Name == "OriginalValues"))
                    {
                        continue;
                    }

                    audit.Properties.Add(new SaveAuditProperty(pi.Name, null, pi.GetValue(obj, null)));
                }
            }
            else if (e.SaveType == SaveType.Update)
            {
                foreach (IDataMapField field in e.DataMap.KeyFields)
                {
                    audit.Keys.Add(new SaveAuditProperty(field.FieldName, field.Property.GetValue(obj, null), null));
                }

                foreach (var kv in obj.OriginalValues)
                {
                    PropertyInfo pi = t.GetProperty(kv.Key);
                    object       v  = pi.GetValue(obj, null);

                    if (((kv.Value != null) && (!kv.Value.Equals(v))) || ((kv.Value == null) && (v != null)))
                    {
                        audit.Properties.Add(new SaveAuditProperty(kv.Key, kv.Value, v));
                    }
                }
            }

            if ((AuditHandler != null) && (audit.Properties.Count > 0))
            {
                AuditHandler(audit);
            }
        }
 public WhereExpressionParser(DataMap map, SqlDialect dialect) : base(new[] { map })
 {
     SqlDialect = dialect;
 }
 public WhereExpressionParser(DataMap map) : this(map, null)
 {
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataClassCommandBuilder"/> class.
 /// </summary>
 /// <param name="type">The type of object.</param>
 /// <param name="map">The data map</param>
 /// <param name="connection">The connection.</param>
 public DataClassCommandBuilder(Type type, DataMap map, DbConnection connection) : this(type, map, connection, null)
 {
 }
Example #5
0
        private static DataMap GenerateMapInternal(Type type, string tableName, IEnumerable <string> keyFields, int schemaVersion)
        {
            if (schemaVersion == 0)
            {
                schemaVersion = int.MaxValue;
            }

            if (keyFields == null)
            {
                keyFields = new string[0];
            }

            var map = new DataMap(type);

            // get or create data item
            IDataMapItem theDataItem = DataItemAttribute.GetFromTypeOrMember(type);

            if (theDataItem != null)
            {
                if (!string.IsNullOrEmpty(tableName))
                {
                    theDataItem.TableName = tableName;
                }
            }
            else
            {
                theDataItem = new DataMapItem(tableName, true);
            }

            // assign to data map
            map.DataItem = theDataItem;

            // create fields from properties
            foreach (var pi in type.GetProperties())
            {
                IDataMapField field = DataFieldAttribute.GetFromProperty(pi);

                if ((field == null) && theDataItem.ImplicitFieldDefinition)
                {
                    field = new DataMapField(pi, pi.Name, DataManager.GetDbType(pi.PropertyType), true);

                    if (keyFields.Contains(field.FieldName, StringComparer.OrdinalIgnoreCase))
                    {
                        field.IsKeyField = true;
                    }

                    // special handling of rowVersion field
                    if ((pi.PropertyType == typeof(byte[])) && field.FieldName.Equals("rowVersion", StringComparison.OrdinalIgnoreCase))
                    {
                        field.IsRowVersion = true;
                    }

                    // special handling of UTC fields (not in 4.0)
                    if (((pi.PropertyType == typeof(DateTime)) || (pi.PropertyType == typeof(DateTime?))) && field.FieldName.EndsWith("UTC", StringComparison.OrdinalIgnoreCase))
                    {
                        field.DateTimeKind = DateTimeKind.Utc;
                    }
                }

                if ((field != null) && (field.SchemaVersion <= schemaVersion))
                {
                    map.AppendField(field);
                }
            }

            return(map);
        }