コード例 #1
0
        internal static string Generate <T>(T param)
        {
            if (param == null)
            {
                throw new ArgumentNullException("param can not be null.");
            }

            var type = typeof(T);
            var identityProperties = MoreDapperConfig.GetKeysFor(type);

            if (identityProperties.Count == 0)
            {
                throw new PrimaryKeyNotFoundException($"type {type.FullName} does not have a auto identity property.");
            }

            var sqlTable = SqlTableGenerator.Generate(param);
            var declares = new StringBuilder();
            var set      = new List <string>();

            var where = new List <string>();

            foreach (var property in sqlTable.Values)
            {
                set.Add($"{property.Name} = @{property.Name}");
            }

            foreach (var identityProperty in sqlTable.Keys)
            {
                where.Add($"{identityProperty.Name} = @{identityProperty.Name}");
            }

            return(string.Concat($"UPDATE {sqlTable.Name} SET ", string.Join(", ", set), " WHERE ", string.Join(" AND ", where), ";"));
        }
コード例 #2
0
        internal static string GenerateSingle <T>(T param)
        {
            if (param == null)
            {
                throw new ArgumentNullException("param can not be null.");
            }

            var type     = typeof(T);
            var sqlTable = SqlTableGenerator.Generate(param);
            var insert   = new List <string>();
            var values   = new List <string>();

            foreach (var property in sqlTable.Values)
            {
                insert.Add($"{property.Name}");
                values.Add($"@{property.Name}");
            }

            return(string.Concat($"INSERT INTO {sqlTable.Name} (", string.Join(", ", insert), ") ", "VALUES (", string.Join(", ", values), ");"));
        }
コード例 #3
0
        internal static string Generate <T>(T param)
        {
            if (param == null)
            {
                throw new ArgumentNullException("param can not be null.");
            }

            var type     = typeof(T);
            var sqlTable = SqlTableGenerator.Generate(param);

            if (sqlTable.Keys.Count == 0)
            {
                throw new PrimaryKeyNotFoundException($"type {type.FullName} does not have a primary key.");
            }

            var where = new List <string>();

            foreach (var identityProperty in sqlTable.Keys)
            {
                where.Add($"{identityProperty.Name} = @{identityProperty.Name}");
            }

            return(string.Concat($"DELETE FROM {sqlTable.Name} WHERE ", string.Join(" AND ", where), ";"));
        }