// From ------------------------------------- private string GetSqlText_FromClause( IDbQueryFromClause clause, IDbSingleQuery query = null, IDataElementSet parameterSet = null, IScriptVariableSet scriptVariableSet = null, IBdoLog log = null) { string queryString = ""; if (clause == null) { // we add the query's default table queryString += GetSqlText_Table( query.DataModule, query.Schema, query.DataTable, query.DataTableAlias, query, parameterSet, DbQueryFieldMode.CompleteName, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } else { if (clause?.Expression != null) { string expression = Scope?.Interpreter.Evaluate(clause.Expression, scriptVariableSet, log)?.ToString() ?? ""; queryString += expression; } else if (!(clause?.Statements?.Count > 0)) { queryString += GetSqlText_Table( query.DataModule, query.Schema, query.DataTable, query.DataTableAlias, query, parameterSet, DbQueryFieldMode.CompleteNameOrValueAsAlias, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } else { foreach (var statement in clause.Statements) { // if the first table is not a joined one then we add first the query's default table if (statement.Tables == null || statement.Tables.Count == 0 || statement.Tables[0] is DbJoinedTable) { queryString += GetSqlText_Table( query.DataModule, query.Schema, query.DataTable, query.DataTableAlias, query, parameterSet, DbQueryFieldMode.CompleteNameOrValueAsAlias, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } else if (statement.Tables?.Count > 0) { if (query?.Kind == DbQueryKind.Delete) { foreach (var table in statement.Tables) { queryString += GetSqlText_Table( table, query, parameterSet, DbQueryFieldMode.CompleteNameOrValueAsAlias, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } } else { foreach (var table in statement.Tables) { queryString += GetSqlText_Table( table, query, parameterSet, DbQueryFieldMode.CompleteNameOrValueAsAlias, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } } } } } } queryString = queryString.If(!string.IsNullOrEmpty(queryString), " from " + queryString); return(queryString); }
// From ------------------------------------- private string GetSqlText_FromClause( IDbQueryFromClause clause, IDbSingleQuery query = null, DbQueryFromClauseKind kind = DbQueryFromClauseKind.FromPreffix, IDataElementSet parameterSet = null, IScriptVariableSet scriptVariableSet = null, IBdoLog log = null) { string queryString = ""; if (query != null) { if (query.Kind == DbQueryKind.Insert) { if ((clause?.Statements?.Count == 1 && clause?.Statements[0]?.Tables.Any(p => p is DbDerivedTable) != true) || (query.WhereClause != null)) { var subQuery = DbFluent.SelectQuery(null) .WithFields(query.Fields?.ToArray()); subQuery.FromClause = query.FromClause; subQuery.WhereClause = query.WhereClause; clause = new DbQueryFromClause { Statements = new List <DbQueryFromStatement>() }; clause.Statements.Add(new DbQueryFromStatement() { Tables = new List <DbTable>() { DbFluent.TableAsQuery(subQuery) } }); } } if (clause == null) { if (query.Kind == DbQueryKind.Insert) { var table = DbFluent.TableAsTuples( DbFluent.Tuple(query.Fields?.ToArray())); queryString += GetSqlText_Table( table, query, parameterSet, DbQueryTableMode.CompleteName, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } else if (query.Kind != DbQueryKind.Update) { queryString += GetSqlText_Table( query.DataModule, query.Schema, query.DataTable, query.DataTableAlias, DbQueryTableMode.CompleteName, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } } else { if (clause?.Expression != null) { string expression = Scope?.Interpreter.Evaluate(clause.Expression, scriptVariableSet, log)?.ToString() ?? ""; queryString += expression; } else if (!(clause?.Statements?.Count > 0)) { queryString += GetSqlText_Table( query.DataModule, query.Schema, query.DataTable, query.DataTableAlias, DbQueryTableMode.CompleteNameAsAlias, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } else { foreach (var statement in clause.Statements) { // if the first table is not a joined one then we add first the query's default table if (statement.Tables == null || statement.Tables.Count == 0 || statement.Tables[0] is DbJoinedTable) { queryString += GetSqlText_Table( query.DataModule, query.Schema, query.DataTable, query.DataTableAlias, DbQueryTableMode.CompleteNameAsAlias, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } if (statement.Tables?.Count > 0) { var existingTbale = false; foreach (var table in statement.Tables) { if (query?.Kind == DbQueryKind.Delete && table is DbJoinedTable joinedTable) { queryString += !existingTbale ? " using " : ", "; queryString += GetSqlText_Table( joinedTable.Table, query, parameterSet, DbQueryTableMode.CompleteNameAsAlias, scriptVariableSet: scriptVariableSet, log: log); existingTbale = true; // we update the where clause to add relationships if (query?.WhereClause == null) { query.WhereClause = new DbQueryWhereClause(); } if (query.WhereClause.Expression == null) { query.WhereClause.Expression = joinedTable.Condition; } else { query.WhereClause.Expression = DbFluent.And(query.WhereClause.Expression, joinedTable.Condition); } } else { queryString += GetSqlText_Table( table, query, parameterSet, DbQueryTableMode.CompleteNameAsAlias, query.DataModule, query.Schema, scriptVariableSet: scriptVariableSet, log: log); } } } } } } } queryString = queryString.If(!string.IsNullOrEmpty(queryString), (kind == DbQueryFromClauseKind.FromPreffix ? "from " : "") + queryString); return(queryString); }