Ejemplo n.º 1
0
        private void InitStatementString(SqlString projection, SqlString condition,
                                         SqlString orderBy, string groupBy, SqlString having, LockMode lockMode)
        {
            int joins = CountEntityPersisters(associations);

            Suffixes = BasicLoader.GenerateSuffixes(joins + 1);
            JoinFragment ojf = MergeOuterJoins(associations);

            SqlString selectClause = projection
                                     ??
                                     new SqlString(persister.SelectFragment(alias, Suffixes[joins]) + SelectString(associations));

            SqlSelectBuilder select = new SqlSelectBuilder(Factory)
                                      .SetLockMode(lockMode)
                                      .SetSelectClause(selectClause)
                                      .SetFromClause(Dialect.AppendLockHint(lockMode, persister.FromTableFragment(alias)) + persister.FromJoinFragment(alias, true, true))
                                      .SetWhereClause(condition)
                                      .SetOuterJoins(ojf.ToFromFragmentString, ojf.ToWhereFragmentString + WhereFragment)
                                      .SetOrderByClause(OrderBy(associations, orderBy))
                                      .SetGroupByClause(groupBy)
                                      .SetHavingClause(having);

            if (Factory.Settings.IsCommentsEnabled)
            {
                select.SetComment(Comment);
            }

            SqlString = select.ToSqlString();
        }
Ejemplo n.º 2
0
        /// <summary></summary>
        public SqlString ToSqlString()
        {
            //TODO: add default capacity
            SqlStringBuilder sqlBuilder = new SqlStringBuilder();

            if (comment != null)
            {
                sqlBuilder.Add("/* " + comment + " */ ");
            }

            bool commaNeeded = false;

            sqlBuilder.Add("SELECT ");

            for (int i = 0; i < columnNames.Count; i++)
            {
                string column = columnNames[i];
                string alias  = GetAlias(column);

                if (commaNeeded)
                {
                    sqlBuilder.Add(StringHelper.CommaSpace);
                }

                sqlBuilder.Add(column);
                if (alias != null && !alias.Equals(column))
                {
                    sqlBuilder.Add(" AS ")
                    .Add(alias);
                }

                commaNeeded = true;
            }

            sqlBuilder.Add(" FROM ")
            .Add(Dialect.AppendLockHint(lockMode, tableName));

            sqlBuilder.Add(" WHERE ");

            if (whereStrings.Count > 1)
            {
                sqlBuilder.Add(whereStrings.ToArray(), null, "AND", null, false);
            }
            else
            {
                sqlBuilder.Add(whereStrings[0]);
            }

            if (orderBy != null)
            {
                sqlBuilder.Add(orderBy);
            }

            if (lockMode != null)
            {
                sqlBuilder.Add(Dialect.GetForUpdateString(lockMode));
            }

            return(sqlBuilder.ToSqlString());
        }
Ejemplo n.º 3
0
		public TableStructure(Dialect.Dialect dialect, string tableName, string valueColumnName, int initialValue,
		                      int incrementSize)
		{
			this.tableName = tableName;
			this.valueColumnName = valueColumnName;
			this.initialValue = initialValue;
			this.incrementSize = incrementSize;

			SqlStringBuilder b = new SqlStringBuilder();
			b.Add("select ").Add(valueColumnName).Add(" id_val").Add(" from ").Add(dialect.AppendLockHint(LockMode.Upgrade,
			                                                                                              tableName)).Add(
				dialect.ForUpdateString);
			select = b.ToSqlString();

			b = new SqlStringBuilder();
			b.Add("update ").Add(tableName).Add(" set ").Add(valueColumnName).Add(" = ").Add(Parameter.Placeholder).Add(" where ")
				.Add(valueColumnName).Add(" = ").Add(Parameter.Placeholder);
			update = b.ToSqlString();
		}
Ejemplo n.º 4
0
		public TableStructure(Dialect.Dialect dialect, string tableName, string valueColumnName, int initialValue, int incrementSize)
		{
			_tableName = tableName;
			_valueColumnName = valueColumnName;
			_initialValue = initialValue;
			_incrementSize = incrementSize;

			_selectQuery = new SqlString(
				"select ", valueColumnName, " as id_val from ",
				dialect.AppendLockHint(LockMode.Upgrade, tableName),
				dialect.ForUpdateString);

			_updateQuery = new SqlString(
				"update ", tableName,
				" set ", valueColumnName, " = ", Parameter.Placeholder,
				" where ", valueColumnName, " = ", Parameter.Placeholder);

			_updateParameterTypes = new[]
			{
				SqlTypeFactory.Int64,
				SqlTypeFactory.Int64,
			};
		}
        public async Task EntityProjectionLockModeAsync()
        {
            // For this test to succeed with SQL Anywhere, ansi_update_constraints must be off.
            // In I-SQL: set option ansi_update_constraints = 'Off'
            if (Dialect is Oracle8iDialect)
            {
                Assert.Ignore("Oracle is not supported due to #1352 bug (NH-3902)");
            }

            var upgradeHint = Dialect.ForUpdateString;

            if (string.IsNullOrEmpty(upgradeHint))
            {
                upgradeHint = Dialect.AppendLockHint(LockMode.Upgrade, string.Empty);
            }
            if (string.IsNullOrEmpty(upgradeHint))
            {
                Assert.Ignore($"Upgrade hint is not supported by dialect {Dialect.GetType().Name}");
            }

            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntitySimpleChild child1 = null;
                    child1 = await(session
                                   .QueryOver <EntityComplex>()
                                   .JoinAlias(ep => ep.Child1, () => child1)
                                   .Lock(() => child1).Upgrade
                                   .Select(Projections.Entity(() => child1))
                                   .Take(1).SingleOrDefaultAsync <EntitySimpleChild>());

                    Assert.That(child1, Is.Not.Null);
                    Assert.That(NHibernateUtil.IsInitialized(child1), Is.True, "Object must be initialized");
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                    Assert.That(sqlLog.Appender.GetEvents()[0].RenderedMessage, Does.Contain(upgradeHint));
                }
        }
		public TableStructure(Dialect.Dialect dialect, string tableName, string valueColumnName, int initialValue, int incrementSize)
		{
			_tableName = tableName;
			_valueColumnName = valueColumnName;
			_initialValue = initialValue;
			_incrementSize = incrementSize;

			var b = new SqlStringBuilder();
			b.Add("select ").Add(valueColumnName).Add(" as id_val").Add(" from ").Add(dialect.AppendLockHint(LockMode.Upgrade, tableName))
				.Add(dialect.ForUpdateString);

			_selectQuery = b.ToSqlString();

			b = new SqlStringBuilder();
			b.Add("update ").Add(tableName).Add(" set ").Add(valueColumnName).Add(" = ").Add(Parameter.Placeholder).Add(" where ")
				.Add(valueColumnName).Add(" = ").Add(Parameter.Placeholder);
			_updateQuery = b.ToSqlString();
			_updateParameterTypes = new[]
			{
				SqlTypeFactory.Int64,
				SqlTypeFactory.Int64,
			};

		}
Ejemplo n.º 7
0
        public void Configure(IType type, IDictionary <string, string> parms, Dialect dialect)
        {
            tableName   = PropertiesHelper.GetString(TableParamName, parms, DefaultTableName);
            columnName  = PropertiesHelper.GetString(ColumnParamName, parms, DefaultColumnName);
            whereColumn = PropertiesHelper.GetString(WhereColumn, parms, DefaultWhereColumnName);
            whereValue  = PropertiesHelper.GetString(WhereValue, parms, string.Empty);
            max_lo      = PropertiesHelper.GetInt64(MaxLo, parms, Int16.MaxValue);
            lo          = max_lo + 1;
            returnClass = type.ReturnedClass;

            if (string.IsNullOrEmpty(whereValue))
            {
                log.Error("wherevalue for SingleTableHiLoGenerator is empty");
                throw new InvalidOperationException("wherevalue is empty");
            }

            whereValues.Add(whereValue);

            var schemaName  = PropertiesHelper.GetString(PersistentIdGeneratorParmsNames.Schema, parms, null);
            var catalogName = PropertiesHelper.GetString(PersistentIdGeneratorParmsNames.Catalog, parms, null);

            if (tableName.IndexOf('.') < 0)
            {
                tableName = dialect.Qualify(catalogName, schemaName, tableName);
            }

            var selectBuilder = new SqlStringBuilder(100);

            selectBuilder.Add("select " + columnName)
            .Add(" from " + dialect.AppendLockHint(LockMode.Upgrade, tableName))
            .Add(" where ")
            .Add(whereColumn).Add("=").Add(whereValue);

            selectBuilder.Add(dialect.ForUpdateString);

            query = selectBuilder.ToString();

            columnType = type as PrimitiveType;
            if (columnType == null)
            {
                log.Error("Column type for TableGenerator is not a value type");
                throw new ArgumentException("type is not a ValueTypeType", "type");
            }

            // build the sql string for the Update since it uses parameters
            if (type is Int16Type)
            {
                columnSqlType = SqlTypeFactory.Int16;
            }
            else if (type is Int64Type)
            {
                columnSqlType = SqlTypeFactory.Int64;
            }
            else
            {
                columnSqlType = SqlTypeFactory.Int32;
            }
            wherecolumnSqlType = SqlTypeFactory.GetString(255);

            parameterTypes = new[] { columnSqlType, columnSqlType };

            var builder = new SqlStringBuilder(100);

            builder.Add("update " + tableName + " set ")
            .Add(columnName).Add("=").Add(Parameter.Placeholder)
            .Add(" where ")
            .Add(columnName).Add("=").Add(Parameter.Placeholder)
            .Add(" and ")
            .Add(whereColumn).Add("=").Add(whereValue);

            updateSql = builder.ToSqlString();
        }