Example #1
0
        /// <summary>
        /// Render the where condition for a (batch) load by identifier / collection key
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="alias"></param>
        /// <param name="columnNames"></param>
        /// <param name="type"></param>
        /// <param name="batchSize"></param>
        /// <returns></returns>
        protected static SqlStringBuilder WhereString(ISessionFactoryImplementor factory, string alias, string[] columnNames, IType type, int batchSize)
        {
            Parameter[]         columnParameters = Parameter.GenerateParameters(factory, columnNames, type);
            ConditionalFragment byId             = new ConditionalFragment()
                                                   .SetTableAlias(alias)
                                                   .SetCondition(columnNames, columnParameters);

            SqlStringBuilder whereString = new SqlStringBuilder();

            if (batchSize == 1)
            {
                whereString.Add(byId.ToSqlStringFragment());
            }
            else
            {
                whereString.Add(StringHelper.OpenParen);                   // TODO: unnecessary for databases with ANSI-style joins
                DisjunctionFragment df = new DisjunctionFragment();
                for (int i = 0; i < batchSize; i++)
                {
                    df.AddCondition(byId);
                }
                whereString.Add(df.ToFragmentString());
                whereString.Add(StringHelper.ClosedParen);                   // TODO: unnecessary for databases with ANSI-style joins
            }

            return(whereString);
        }
Example #2
0
        /// <summary>
        /// Render the where condition for a (batch) load by identifier / collection key
        /// </summary>
        protected SqlStringBuilder WhereString(string alias, string[] columnNames, int batchSize)
        {
            if (columnNames.Length == 1)
            {
                // if not a composite key, use "foo in (?, ?, ?)" for batching
                // if no batch, and not a composite key, use "foo = ?"
                string     tableAlias = GenerateAliasForColumn(alias, columnNames[0]);
                InFragment inf        = new InFragment().SetColumn(tableAlias, columnNames[0]);

                for (int i = 0; i < batchSize; i++)
                {
                    inf.AddValue(Parameter.Placeholder);
                }

                return(new SqlStringBuilder(inf.ToFragmentString()));
            }
            else
            {
                var fragments = new ConditionalFragment[batchSize];
                for (int i = 0; i < batchSize; i++)
                {
                    fragments[i] = new ConditionalFragment()
                                   .SetTableAlias(alias)
                                   .SetCondition(columnNames, Parameter.GenerateParameters(columnNames.Length));
                }

                var whereString = new SqlStringBuilder();

                if (fragments.Length == 1)
                {
                    // if no batch, use "foo = ? and bar = ?"
                    whereString.Add(fragments[0].ToSqlStringFragment());
                }
                else
                {
                    // if batching, use "( (foo = ? and bar = ?) or (foo = ? and bar = ?) )"
                    var df = new DisjunctionFragment(fragments);

                    whereString.Add(StringHelper.OpenParen);
                    whereString.Add(df.ToFragmentString());
                    whereString.Add(StringHelper.ClosedParen);
                }

                return(whereString);
            }
        }
Example #3
0
        /// <summary>
        /// Render the where condition for a (batch) load by identifier / collection key
        /// </summary>
        protected SqlStringBuilder WhereString(string alias, string[] columnNames, int batchSize)
        {
            if (columnNames.Length == 1)
            {
                // if not a composite key, use "foo in (?, ?, ?)" for batching
                // if no batch, and not a composite key, use "foo = ?"
                string     tableAlias = GenerateAliasForColumn(alias, columnNames[0]);
                InFragment inf        = new InFragment().SetColumn(tableAlias, columnNames[0]);

                for (int i = 0; i < batchSize; i++)
                {
                    inf.AddValue(Parameter.Placeholder);
                }

                return(new SqlStringBuilder(inf.ToFragmentString()));
            }
            else
            {
                Parameter[]         columnParameters = Parameter.GenerateParameters(columnNames.Length);
                ConditionalFragment byId             = new ConditionalFragment()
                                                       .SetTableAlias(alias)
                                                       .SetCondition(columnNames, columnParameters);

                SqlStringBuilder whereString = new SqlStringBuilder();

                if (batchSize == 1)
                {
                    // if no batch, use "foo = ? and bar = ?"
                    whereString.Add(byId.ToSqlStringFragment());
                }
                else
                {
                    // if a composite key, use "( (foo = ? and bar = ?) or (foo = ? and bar = ?) )" for batching
                    whereString.Add(StringHelper.OpenParen);                     // TODO: unnecessary for databases with ANSI-style joins
                    DisjunctionFragment df = new DisjunctionFragment();
                    for (int i = 0; i < batchSize; i++)
                    {
                        df.AddCondition(byId);
                    }
                    whereString.Add(df.ToFragmentString());
                    whereString.Add(StringHelper.ClosedParen);                     // TODO: unnecessary for databases with ANSI-style joins
                }

                return(whereString);
            }
        }