예제 #1
0
        public Dict ConvertParamsToDict(dynamic statementParams, bool allowKeyValueAsSourceParams = true)
        {
            // If statementParams is null, return empty dictionary
            if (statementParams == null)
            {
                return(new Dict());
            }

            // If statementParams is already a dictionary, return the dictionary
            else if (statementParams is Dict d)
            {
                return(new Dict(d));
            }

            // If statementParams is a single string, assume it is a primary key value
            else if (statementParams is string keyValue)
            {
                if (!allowKeyValueAsSourceParams)
                {
                    throw new Exception("Statement doesn't allow passing single key value as source params");
                }
                if (this.StatementFromRefs.Length != 1)
                {
                    throw new Exception("Statement must have exactly one table to pass single string value as where condition");
                }
                return(DictionaryX.ParseKeyValue(keyValue, this.StatementFromRefs[0].table.Indexes[0].FieldNames));
            }

            // Otherwise, convert statementParams to a dictionary
            else
            {
                return(DynamicX.ToDictionary(statementParams));
            }
        }
예제 #2
0
 private static bool Contains(
     Dictionary <string, string> properties, string virtualProperty)
 {
     return(DictionaryX.FindAll <string, string>(
                properties,
                delegate(KeyValuePair <string, string> pair) {
         bool value;
         return pair.Key == virtualProperty &&
         Boolean.TryParse(pair.Value, out value) &&
         value;
     }).Count > 0);
 }
예제 #3
0
        /*
         * Given this SQL...
         *
         *  SELECT e.name, d.name
         *  FROM employee e
         *      LEFT JOIN department d ON e.department_id=d.id
         *  WHERE e.name LIKE 'm%'
         *
         * An event on employee would be checked by running...
         *
         *  SELECT e.name, d.name
         *  FROM employee e
         *      LEFT JOIN department d ON e.department_id=d.id
         *  WHERE e.name LIKE 'm%' AND e.id='123'
         *
         * An event on department would be checked by running...
         *
         *  SELECT e.name, d.name
         *  FROM employee e
         *      LEFT JOIN department d ON e.department_id=d.id AND d.id='456'
         *  WHERE e.name LIKE 'm%'
         *
         */
        internal async Task <Dict[]> GetImpactedRecordsAsync(KeyValueDataEvent keyValueDataEvent)
        {
            if (!this.dynamicStatementFromRefByTableName.TryGetValue(keyValueDataEvent.name, out StatementFromRef selectedStatementFromRef))
            {
                return(null);
            }
            logger.Trace($"GetImpactedRecordsAsync():name={name}");

            StringBuilder newFromClause  = new StringBuilder();
            string        newWhereClause = this.selectStatement.whereClause;
            Dict          newVarsDict    = new Dict(this.varsDict);

            foreach (StatementFromRef statementFromRef in this.selectStatement.StatementFromRefs)
            {
                StringBuilder sb = new StringBuilder();
                if (statementFromRef == selectedStatementFromRef)
                {
                    Dict primaryKeyValues = DictionaryX.ParseKeyValue(keyValueDataEvent.keyValue, this.dynamicViewSet.Database.TableByName[keyValueDataEvent.name].Indexes[0].FieldNames);

                    foreach (var fieldName in this.dynamicViewSet.Database.TableByName[keyValueDataEvent.name].Indexes[0].FieldNames)
                    {
                        logger.Trace($"GetImpactedRecordsAsync():tableName={statementFromRef.table.Name},fieldName={fieldName}");

                        string prefix;
                        if (this.selectStatement.StatementFromRefs.Length == 1)
                        {
                            prefix = "";
                        }
                        else if (!string.IsNullOrEmpty(statementFromRef.tableAlias))
                        {
                            prefix = $"{statementFromRef.tableAlias}.";
                        }
                        else
                        {
                            prefix = $"{statementFromRef.table.Name}.";
                        }
                        logger.Trace($"GetImpactedRecordsAsync():prefix={prefix}");

                        var paramName = $"__{fieldName}";
                        var condition = $"{prefix}{fieldName}=@{paramName}";
                        logger.Trace($"GetImpactedRecordsAsync():condition={condition}");

                        if (sb.Length > 0)
                        {
                            sb.Append(" AND ");
                        }
                        sb.Append(condition);

                        newVarsDict[paramName] = primaryKeyValues[fieldName];
                    }
                    logger.Trace($"GetImpactedRecordsAsync():newAndCondition={newWhereClause}");
                }

                // JoinType = None only happens on first table reference
                if (statementFromRef.joinType == JoinType.None)
                {
                    newFromClause.Append(statementFromRef.table.Name);
                    if (!string.IsNullOrEmpty(statementFromRef.tableAlias))
                    {
                        newFromClause.Append($" {statementFromRef.tableAlias}");
                    }

                    if (sb.Length > 0)
                    {
                        if (string.IsNullOrEmpty(newWhereClause))
                        {
                            newWhereClause = sb.ToString();
                        }
                        else
                        {
                            newWhereClause = $"({newWhereClause}) AND ({sb.ToString()})";
                        }
                    }
                }
                else
                {
                    newFromClause.Append($" {statementFromRef.joinType.ToString().ToUpper()} JOIN");
                    newFromClause.Append($" {statementFromRef.table.Name}");
                    if (!string.IsNullOrEmpty(statementFromRef.tableAlias))
                    {
                        newFromClause.Append($" {statementFromRef.tableAlias}");
                    }
                    newFromClause.Append($" ON");

                    if (sb.Length > 0)
                    {
                        newFromClause.Append($" ({statementFromRef.joinCondition}) AND ({sb.ToString()})");
                    }
                    else
                    {
                        newFromClause.Append($" {statementFromRef.joinCondition}");
                    }
                }
            }

            SelectStatement newStatement = new SelectStatement(this.dynamicViewSet.Database, this.selectStatement.selectClause, newFromClause.ToString(), newWhereClause?.ToString(), null);

            return(await this.dynamicViewSet.Database.SelectRowsAsync(newStatement, newVarsDict));
        }