Exemple #1
0
        Query(IMappingConvention convention)
        {
            var allPropertyNames    = Properties.Select(p => convention.ToDb(p.Name)).ToArray();
            var insertPropertyNames = Properties.Except(DbGenerated).Select(p => p.Name).ToArray();
            var keyPropertyNames    = KeyProperties.Select(p => p.Name).ToArray();
            var nonKeyProperties    = Properties.Except(KeyProperties).ToArray();
            var nonKeyPropertyNames = nonKeyProperties.Select(p => p.Name).ToArray();

            Func <string, string> assign = s => $"{convention.ToDb(s)} = {convention.Parameter(s)}";
            var insertColumns            = string.Join(", ", insertPropertyNames.Select(convention.ToDb));
            var insertValues             = string.Join(", ", insertPropertyNames.Select(s => $"{convention.Parameter(s)}"));
            var whereClause   = string.Join(" AND ", keyPropertyNames.Select(assign));
            var updateColumns = string.Join(", ", nonKeyPropertyNames.Select(assign));
            var allColumns    = string.Join(", ", allPropertyNames);
            var tableName     = convention.ToDb(typeof(T).Name);

            Insert    = $"INSERT INTO {tableName} ({insertColumns}) VALUES ({insertValues})";
            Delete    = $"DELETE FROM {tableName} WHERE {whereClause}";
            Update    = $"UPDATE {tableName} SET {updateColumns} WHERE {whereClause}";
            Select    = $"SELECT {allColumns} FROM {tableName} WHERE {whereClause}";
            SelectAll = $"SELECT {allColumns} FROM {tableName}";
            Count     = $"SELECT COUNT(*) FROM {tableName}";
        }