Beispiel #1
0
        public Table AddTable(string schema, string catalog, string name, string subselect, bool isAbstract, string schemaAction)
        {
            string key = subselect ?? dialect.Qualify(catalog, schema, name);
            Table  table;

            if (!tables.TryGetValue(key, out table))
            {
                table               = new Table();
                table.IsAbstract    = isAbstract;
                table.Name          = name;
                table.Schema        = schema;
                table.Catalog       = catalog;
                table.Subselect     = subselect;
                table.SchemaActions = GetSchemaActions(schemaAction);
                tables[key]         = table;
            }
            else
            {
                if (!isAbstract)
                {
                    table.IsAbstract = false;
                }
            }

            return(table);
        }
Beispiel #2
0
        public void ValidateColumns(Dialect.Dialect dialect, IMapping mapping, ITableMetadata tableInfo)
        {
            IEnumerable <Column> iter = ColumnIterator;

            foreach (Column column in iter)
            {
                IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name);

                if (columnInfo == null)
                {
                    throw new HibernateException(string.Format("Missing column: {0} in {1}", column.Name,
                                                               dialect.Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name)));
                }

                //TODO: Add new method to ColumnMetadata :getTypeCode
                bool typesMatch = column.GetSqlType(dialect, mapping).StartsWith(columnInfo.TypeName, StringComparison.OrdinalIgnoreCase);
                //|| columnInfo.get() == column.GetSqlTypeCode(mapping);
                if (!typesMatch)
                {
                    throw new HibernateException(string.Format("Wrong column type in {0} for column {1}. Found: {2}, Expected {3}",
                                                               dialect.Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name),
                                                               column.Name, columnInfo.TypeName.ToLowerInvariant(),
                                                               column.GetSqlType(dialect, mapping)));
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Configures the SequenceGenerator by reading the value of <c>sequence</c> and
        /// <c>schema</c> from the <c>parms</c> parameter.
        /// </summary>
        /// <param name="type">The <see cref="IType"/> the identifier should be.</param>
        /// <param name="parms">An <see cref="IDictionary"/> of Param values that are keyed by parameter name.</param>
        /// <param name="dialect">The <see cref="Dialect.Dialect"/> to help with Configuration.</param>
        public virtual void Configure(IType type, IDictionary <string, string> parms, Dialect.Dialect dialect)
        {
            var  nativeSequenceName = PropertiesHelper.GetString(Sequence, parms, "hibernate_sequence");
            bool needQuote          = StringHelper.IsBackticksEnclosed(nativeSequenceName);
            bool isQuelified        = nativeSequenceName.IndexOf('.') > 0;

            if (isQuelified)
            {
                string qualifier = StringHelper.Qualifier(nativeSequenceName);
                nativeSequenceName = StringHelper.Unqualify(nativeSequenceName);
                nativeSequenceName = StringHelper.PurgeBackticksEnclosing(nativeSequenceName);
                sequenceName       = qualifier + '.' + (needQuote ? dialect.QuoteForTableName(nativeSequenceName) : nativeSequenceName);
            }
            else
            {
                nativeSequenceName = StringHelper.PurgeBackticksEnclosing(nativeSequenceName);
                sequenceName       = needQuote ? dialect.QuoteForTableName(nativeSequenceName) : nativeSequenceName;
            }
            string schemaName;
            string catalogName;

            parms.TryGetValue(Parameters, out parameters);
            parms.TryGetValue(PersistentIdGeneratorParmsNames.Schema, out schemaName);
            parms.TryGetValue(PersistentIdGeneratorParmsNames.Catalog, out catalogName);

            if (!isQuelified)
            {
                sequenceName = dialect.Qualify(catalogName, schemaName, sequenceName);
            }

            identifierType = type;
            sql            = new SqlString(dialect.GetSequenceNextValString(sequenceName));
        }
Beispiel #4
0
        /// <summary>
        ///  Determine the table name to use for the generator values. Called during configuration.
        /// </summary>
        /// <param name="parms">The parameters supplied in the generator config (plus some standard useful extras).</param>
        /// <param name="dialect">The dialect</param>
        protected string DetermineGeneratorTableName(IDictionary <string, string> parms, Dialect.Dialect dialect)
        {
            string name = PropertiesHelper.GetString(TableParam, parms, DefaultTable);
            bool   isGivenNameUnqualified = name.IndexOf('.') < 0;

            if (isGivenNameUnqualified)
            {
                // NHibernate doesn't seem to have anything resembling this ObjectNameNormalizer. Ignore that for now.
                //ObjectNameNormalizer normalizer = ( ObjectNameNormalizer ) params.get( IDENTIFIER_NORMALIZER );
                //name = normalizer.normalizeIdentifierQuoting( name );
                //// if the given name is un-qualified we may neen to qualify it
                //string schemaName = normalizer.normalizeIdentifierQuoting( params.getProperty( SCHEMA ) );
                //string catalogName = normalizer.normalizeIdentifierQuoting( params.getProperty( CATALOG ) );

                string schemaName;
                string catalogName;
                parms.TryGetValue(PersistentIdGeneratorParmsNames.Schema, out schemaName);
                parms.TryGetValue(PersistentIdGeneratorParmsNames.Catalog, out catalogName);
                name = dialect.Qualify(catalogName, schemaName, name);
            }
            else
            {
                // if already qualified there is not much we can do in a portable manner so we pass it
                // through and assume the user has set up the name correctly.
            }
            return(name);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the schema qualified name of the Table using the specified qualifier
        /// </summary>
        /// <param name="dialect">The <see cref="Dialect"/> that knows how to Quote the Table name.</param>
        /// <param name="defaultCatalog">The catalog name.</param>
        /// <param name="defaultSchema">The schema name.</param>
        /// <returns>A String representing the Qualified name.</returns>
        /// <remarks>If this were used with MSSQL it would return a dbo.table_name.</remarks>
        public virtual string GetQualifiedName(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
        {
            if (!string.IsNullOrEmpty(subselect))
            {
                return("( " + subselect + " )");
            }
            string quotedName  = GetQuotedName(dialect);
            string usedSchema  = schema == null ? defaultSchema : GetQuotedSchema(dialect);
            string usedCatalog = catalog ?? defaultCatalog;

            return(dialect.Qualify(usedCatalog, usedSchema, quotedName));
        }
Beispiel #6
0
        /// <summary>
        /// Gets the schema qualified name of the Table using the specified qualifier
        /// </summary>
        /// <param name="dialect">The <see cref="Dialect"/> that knows how to Quote the Table name.</param>
        /// <param name="defaultCatalog">The catalog name.</param>
        /// <param name="defaultSchema">The schema name.</param>
        /// <returns>A String representing the Qualified name.</returns>
        /// <remarks>If this were used with MSSQL it would return a dbo.table_name.</remarks>
        public virtual string GetQualifiedName(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
        {
            if (!string.IsNullOrEmpty(subselect))
            {
                return("( " + subselect + " )");
            }
            var quotedName  = GetQuotedName(dialect);
            var usedSchema  = GetQuotedSchema(dialect, defaultSchema);
            var usedCatalog = GetQuotedCatalog(dialect, defaultCatalog);

            return(dialect.Qualify(usedCatalog, usedSchema, quotedName));
        }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="parms"></param>
        /// <param name="dialect"></param>
        public void Configure(IType type, IDictionary <string, string> parms, Dialect.Dialect dialect)
        {
            string tableList;
            string column;
            string schema;
            string catalog;

            if (!parms.TryGetValue("tables", out tableList))
            {
                parms.TryGetValue(PersistentIdGeneratorParmsNames.Tables, out tableList);
            }
            string[] tables = tableList.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            if (!parms.TryGetValue("column", out column))
            {
                parms.TryGetValue(PersistentIdGeneratorParmsNames.PK, out column);
            }

            _returnClass = type.ReturnedClass;

            parms.TryGetValue(PersistentIdGeneratorParmsNames.Schema, out schema);
            parms.TryGetValue(PersistentIdGeneratorParmsNames.Catalog, out catalog);

            StringBuilder buf = new StringBuilder();

            for (int i = 0; i < tables.Length; i++)
            {
                if (tables.Length > 1)
                {
                    buf.Append("select ").Append(column).Append(" from ");
                }
                buf.Append(dialect.Qualify(catalog, schema, tables[i]));
                if (i < tables.Length - 1)
                {
                    buf.Append(" union ");
                }
            }
            if (tables.Length > 1)
            {
                buf.Insert(0, "( ").Append(" ) ids_");
                column = "ids_." + column;
            }

            var sqlTxt = string.Format("select max({0}) from {1}", column, buf);

            _sql = new SqlString(sqlTxt);
        }
Beispiel #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="parms"></param>
        /// <param name="dialect"></param>
        public void Configure(IType type, IDictionary <string, string> parms, Dialect.Dialect dialect)
        {
            string tableList;
            string column;
            string schema;
            string catalog;

            if (!parms.TryGetValue("tables", out tableList))
            {
                parms.TryGetValue(PersistentIdGeneratorParmsNames.Tables, out tableList);
            }
            string[] tables = StringHelper.Split(", ", tableList);
            if (!parms.TryGetValue("column", out column))
            {
                parms.TryGetValue(PersistentIdGeneratorParmsNames.PK, out column);
            }
            returnClass = type.ReturnedClass;
            parms.TryGetValue(PersistentIdGeneratorParmsNames.Schema, out schema);
            parms.TryGetValue(PersistentIdGeneratorParmsNames.Catalog, out catalog);

            StringBuilder buf = new StringBuilder();

            for (int i = 0; i < tables.Length; i++)
            {
                if (tables.Length > 1)
                {
                    buf.Append("select ").Append(column).Append(" from ");
                }
                buf.Append(dialect.Qualify(catalog, schema, tables[i]));
                if (i < tables.Length - 1)
                {
                    buf.Append(" union ");
                }
            }
            if (tables.Length > 1)
            {
                buf.Insert(0, "( ").Append(" ) ids_");
                column = "ids_." + column;
            }

            sql = "select max(" + column + ") from " + buf;
        }
        /// <summary>
        /// Determine the name of the sequence (or table if this resolves to a physical table) to use.
        /// Called during configuration.
        /// </summary>
        /// <param name="parms"></param>
        /// <param name="dialect"></param>
        /// <returns></returns>
        protected string DetermineSequenceName(IDictionary <string, string> parms, Dialect.Dialect dialect)
        {
            string sequenceName = PropertiesHelper.GetString(SequenceParam, parms, DefaultSequenceName);

            if (sequenceName.IndexOf('.') < 0)
            {
                string schemaName;
                string catalogName;
                parms.TryGetValue(PersistentIdGeneratorParmsNames.Schema, out schemaName);
                parms.TryGetValue(PersistentIdGeneratorParmsNames.Catalog, out catalogName);
                sequenceName = dialect.Qualify(catalogName, schemaName, sequenceName);
            }
            else
            {
                // If already qualified there is not much we can do in a portable manner so we pass it
                // through and assume the user has set up the name correctly.
            }

            return(sequenceName);
        }
        /// <summary>
        /// Configures the TableGenerator by reading the value of <c>table</c>,
        /// <c>column</c>, and <c>schema</c> from the <c>parms</c> parameter.
        /// </summary>
        /// <param name="type">The <see cref="IType"/> the identifier should be.</param>
        /// <param name="parms">An <see cref="IDictionary"/> of Param values that are keyed by parameter name.</param>
        /// <param name="dialect">The <see cref="Dialect"/> to help with Configuration.</param>
        public virtual void Configure(IType type, IDictionary <string, string> parms, Dialect.Dialect dialect)
        {
            tableName   = PropertiesHelper.GetString(TableParamName, parms, DefaultTableName);
            columnName  = PropertiesHelper.GetString(ColumnParamName, parms, DefaultColumnName);
            whereClause = PropertiesHelper.GetString(Where, parms, "");
            string schemaName  = PropertiesHelper.GetString(PersistentIdGeneratorParmsNames.Schema, parms, null);
            string 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));
            if (string.IsNullOrEmpty(whereClause) == false)
            {
                selectBuilder.Add(" where ").Add(whereClause);
            }
            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;
            }

            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);
            if (string.IsNullOrEmpty(whereClause) == false)
            {
                builder.Add(" and ").Add(whereClause);
            }

            updateSql = builder.ToSqlString();
        }
Beispiel #11
0
 private string Identifier(string catalog, string schema, string name)
 {
     return(dialect.Qualify(catalog, schema, name));
 }