Exemplo n.º 1
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.º 2
0
        /// <summary>
        ///     Creates a single Insert Statement with the propertys of
        ///     <paramref name="entry" />
        /// </summary>
        /// <returns></returns>
        public static IDbCommand CreateInsert(IDatabase database, DbClassInfoCache classInfo, object entry)
        {
            var identityInsert = DbIdentityInsertScope.Current != null;
            var ignore         =
                classInfo
                .Propertys
                .Select(s => s.Value)
                .Where(f => f.InsertIgnore || (!identityInsert && f.PrimaryKeyAttribute != null))
                .Select(s => s.DbName)
                .ToArray();

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

            var    propertyInfos = classInfo.FilterDbSchemaMapping(ignore).ToArray();
            var    csvprops      = classInfo.CreatePropertyCsv(ignore);
            string query;

            if (string.IsNullOrEmpty(csvprops))
            {
                query = $"INSERT INTO [{classInfo.TableName}] DEFAULT VALUES";
            }
            else
            {
                var values = "";
                for (var index = 0; index < propertyInfos.Length; index++)
                {
                    values = values + ("@" + index) + ",";
                }
                values = values.Remove(values.Length - 1);
                query  = "INSERT INTO [" + classInfo.TableName + "] ( " + csvprops + " ) VALUES ( " + values + " )";
            }

            var orignialProps = classInfo.GetPropertysViaRefection(ignore).ToArray();

            return(database.CreateCommandWithParameterValues(classInfo, query, orignialProps, entry));
        }
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()));
        }