/// <summary>
 ///     Creates a Plain Select statement by using
 ///     <paramref name="classType" />
 /// </summary>
 /// <returns></returns>
 public static string CreateSelect(DbClassInfoCache classType, string prefix = null)
 {
     return(CreateSelectByColumns(classType, classType.CreatePropertyCsv(
                                      classType
                                      .Propertys
                                      .Where(f => f.Value.ForginKeyAttribute != null ||
                                             f.Value.FromXmlAttribute != null &&
                                             f.Value.FromXmlAttribute.Attribute.LoadStrategy == LoadStrategy.NotIncludeInSelect)
                                      .Select(f => f.Key)
                                      .ToArray()), prefix));
 }
Beispiel #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));
        }