public ISelectSqlBuilder Fields <T>(Action <ISelectFieldSelector <T> > selector, string tableName = null, string tableAlias = null)
        {
            var fieldSelector = new SelectFieldSelector <T>();

            selector(fieldSelector);
            SetTableName <T>(
                () =>
            {
                if (string.IsNullOrEmpty(TableName) ||
                    !string.Equals(TableName, SqlBuilderHelper.GetTableName <T>(SchemaNamePrefix, tableName), StringExtensions.CurrentIgnoreCaseStringComparison))
                {
                    return;
                }

                var alias = tableAlias.WithDefault(fieldSelector.GetFirstTableAlias(typeof(T)));
                if (!string.IsNullOrEmpty(alias))
                {
                    TableAlias = TableAlias.WithDefault(alias);
                }
            }, tableName, tableAlias);

            FieldsList.AddRange(fieldSelector.Build());

            return(this);
        }
        private ISelectSqlBuilder TypeJoin <TFrom, TTo>(string joinType, string fromField = null, string toField = null, string toTableAlias = null, string fromTableAlias = null,
                                                        string fromTableName = null,
                                                        string toTableName   = null)
        {
            SetTableName <TFrom>(originalTableName: fromTableName, tableAlias: fromTableAlias);
            if (string.IsNullOrEmpty(fromField))
            {
                fromField = SqlBuilderHelper.GetTableName <TTo>(null, toTableName) + "Id";
            }

            if (string.IsNullOrEmpty(toField))
            {
                toField = "Id";
            }

            var fromExpresstion = $"{GetTypeAlias<TFrom>(fromTableAlias, fromTableName)}.{fromField}";

            Joins.Add(string.Format("{0} JOIN {1} {2} ON {3} = {2}.{4}",
                                    joinType.ToUpper(),
                                    SqlBuilderHelper.GetTableName <TTo>(SchemaNamePrefix, toTableName),
                                    GetTypeAlias <TTo>(toTableAlias, toTableName),
                                    fromExpresstion,
                                    toField));
            return(this);
        }
        public string Build()
        {
            var sql = $"DELETE FROM {SqlBuilderHelper.GetTableName<TClass>(SchemaNamePrefix, TableName)}";

            if (Wheres.Count > 0)
            {
                var where = string.Join(" AND ", Wheres);
                sql       = $"{sql} WHERE {@where}";
            }

            return(sql);
        }
Exemple #4
0
        private void SetTableName <T>(Action additionAction = null, string originalTableName = null)
        {
            if (!string.IsNullOrEmpty(TableName))
            {
                additionAction?.Invoke();
                return;
            }

            TableName  = SqlBuilderHelper.GetTableName <T>(SchemaNamePrefix, originalTableName);
            TableAlias = SqlBuilderHelper.GetAliasForType <T>();

            additionAction?.Invoke();
        }