Exemplo n.º 1
0
        /// <summary>
        ///     Return the value of the specified field.
        /// </summary>
        /// <param name="i">The index of the field to find.</param>
        /// <returns>
        ///     The <see cref="T:System.Object" /> which will contain the field value upon return.
        /// </returns>
        public virtual object GetValue(int i)
        {
            if (i == -1)
            {
                return(DBNull.Value);
            }
            if (i >= _baseElement.Elements().Count())
            {
                return(DBNull.Value);
            }

            var name = GetName(i);

            var mapEntiysPropToSchema = _target.GetDbToLocalSchemaMapping(name);

            var firstOrDefault = _target.GetPropertiesEx().FirstOrDefault(s => s.Name == mapEntiysPropToSchema);

            if (firstOrDefault == null)
            {
                return(DBNull.Value);
            }

            var propertyType = firstOrDefault.PropertyType;
            var xElement     = _baseElement.Elements().ElementAt(i);

            if (xElement.HasElements)
            {
                return(xElement.ToString());
            }

            var type = DataConverterExtensions.ChangeType(xElement.Value, propertyType);

            return(type);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Create a Update statement that will update the entry in the Database
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="classInfo">The class information.</param>
        /// <param name="entry">The entry.</param>
        /// <returns></returns>
        /// <exception cref="Exception">No primarykey Provied. An autogenerated Update statement could not be created</exception>
        public static IDbCommand CreateUpdate(IDatabase database, DbClassInfoCache classInfo, object entry)
        {
            var pkProperty = classInfo.PrimaryKeyProperty;

            if (pkProperty == null)
            {
                throw new Exception("No primarykey Provied. An autogenerated Update statement could not be created");
            }
            var pk = classInfo.SchemaMappingLocalToDatabase(pkProperty.PropertyName);

            var identityInsert = DbIdentityInsertScope.Current != null;
            var ignore         =
                classInfo
                .Propertys
                .Select(f => f.Value)
                .Where(s => (!identityInsert && s.PrimaryKeyAttribute != null) || s.InsertIgnore || s.UpdateIgnore || s.ForginKeyAttribute != null)
                .Select(s => s.DbName)
                .ToArray();

            if (identityInsert)
            {
                DbIdentityInsertScope.Current.EnableIdentityModfiy(classInfo.TableName, database);
            }

            var propertyInfos = classInfo.FilterDbSchemaMapping(ignore).ToArray();

            var sb = new StringBuilder();

            sb.Append("UPDATE ");
            sb.Append(classInfo.TableName);
            sb.Append(" SET ");
            var para = new List <IQueryParameter>();

            for (var index = 0; index < propertyInfos.Length; index++)
            {
                var info       = propertyInfos[index];
                var schemaName = classInfo.GetDbToLocalSchemaMapping(info);
                DbPropertyInfoCache property;
                classInfo.Propertys.TryGetValue(schemaName, out property);
                var dataValue = DataConverterExtensions.GetDataValue(property.GetConvertedValue(entry));
                para.Add(new QueryParameter(index.ToString(), dataValue, property.PropertyType));
                sb.Append(string.Format(" {0} = @{1} ", info, index));
                if (index + 1 < propertyInfos.Length)
                {
                    sb.Append(",");
                }
            }

            para.Add(new QueryParameter("pkValue", pkProperty.Getter.Invoke(entry), pkProperty.PropertyType));

            sb.Append(string.Format("WHERE {0} = @pkValue ", pk));

            return(database.CreateCommandWithParameterValues(sb.ToString(), para.ToArray()));
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Create a Update statement that will update the entry in the Database
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="classInfo">The class information.</param>
        /// <param name="entry">The entry.</param>
        /// <returns></returns>
        /// <exception cref="Exception">No primarykey Provied. An autogenerated Update statement could not be created</exception>
        public static IDbCommand CreateUpdateSimple(IDatabase database, DbClassInfoCache classInfo, object entry)
        {
            var ignore =
                classInfo
                .Propertys
                .Select(f => f.Value)
                .Where(s => s.PrimaryKeyAttribute != null || s.InsertIgnore || s.UpdateIgnore || s.ForginKeyAttribute != null)
                .Select(s => s.DbName)
                .ToArray();

            var propertyInfos = classInfo.FilterDbSchemaMapping(ignore).ToArray();

            var sb = new StringBuilder();

            sb.Append("UPDATE ");
            sb.Append(classInfo.TableName);
            sb.Append(" SET ");
            var para = new List <IQueryParameter>();

            for (var index = 0; index < propertyInfos.Length; index++)
            {
                var info       = propertyInfos[index];
                var schemaName = classInfo.GetDbToLocalSchemaMapping(info);
                DbPropertyInfoCache property;
                classInfo.Propertys.TryGetValue(schemaName, out property);
                var dataValue = DataConverterExtensions.GetDataValue(property.GetConvertedValue(entry));
                para.Add(new QueryParameter(index.ToString(), dataValue, property.PropertyType));
                sb.Append(string.Format(" {0} = @{1} ", info, index));
                if (index + 1 < propertyInfos.Length)
                {
                    sb.Append(",");
                }
            }

            return(database.CreateCommandWithParameterValues(sb.ToString(), para.ToArray()));
        }