예제 #1
0
        public void InquireSchema()
        {
            Dialect.MsSql2005Dialect dialect = new MsSql2005Dialect();
            DbConnection conn = new SqlConnection(@"");
            conn.Open();

            DatabaseMetadata meta = new DatabaseMetadata(conn, dialect);

            IDataBaseSchema schema = dialect.GetDataBaseSchema(conn);
            var dt = schema.GetTables(null, null, null, new string[0]);
            var cols = schema.GetColumns(null, null, null, null);
            var keys = schema.GetForeignKeys(null, null, null);
            foreach (DataRow r in dt.Rows)
            {
                var tableMeta = schema.GetTableMetadata(r, true);
                Console.WriteLine(string.Format("Table {2}:[{0}].[{1}]",tableMeta.Schema,tableMeta.Name,tableMeta.Catalog));
                ITableMetadata tm = meta.GetTableMetadata(tableMeta.Name, tableMeta.Schema, tableMeta.Catalog, false);
                IColumnMetadata col = tm.GetColumnMetadata(cols.Rows[0].ItemArray[2] as string);
            }
        }
예제 #2
0
		///<summary>
		/// Generate DDL for altering tables
		///</summary>
		/// <seealso cref="NHibernate.Tool.hbm2ddl.SchemaUpdate"/>
		public String[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, DatabaseMetadata databaseMetadata)
		{
			SecondPassCompile();

			string defaultCatalog = PropertiesHelper.GetString(Environment.DefaultCatalog, properties, null);
			string defaultSchema = PropertiesHelper.GetString(Environment.DefaultSchema, properties, null);

			ArrayList script = new ArrayList(50);
			foreach (Table table in tables.Values)
			{
				if (table.IsPhysicalTable)
				{

					TableMetadata tableInfo = databaseMetadata.GetTableMetadata(
							table.Name,
							table.Schema ?? defaultSchema
						);
					if (tableInfo == null)
					{
						script.Add(table.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
					}
					else
					{
						string[] alterDDL = table.SqlAlterStrings(
								dialect,
								mapping,
								tableInfo,
								defaultSchema
							);
						script.AddRange(alterDDL);
					}

					string[] comments = table.SqlCommentStrings(dialect, defaultCatalog, defaultSchema);
					script.AddRange(comments);

				}
			}

			foreach (Table table in tables.Values)
			{
				if (table.IsPhysicalTable)
				{

					TableMetadata tableInfo = databaseMetadata.GetTableMetadata(
							table.Name,
							table.Schema
						);

					if (dialect.HasAlterTable)
					{
						foreach (ForeignKey fk in table.ForeignKeyIterator)
						{
							if (fk.HasPhysicalConstraint)
							{
								bool create = tableInfo == null || (
										tableInfo.GetForeignKeyMetadata(fk.Name) == null && (
									//Icky workaround for MySQL bug:
												!(dialect is MySQLDialect) ||
														tableInfo.GetIndexMetadata(fk.Name) == null
											)
									);
								if (create)
								{
									script.Add(fk.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
								}
							}
						}
					}

				}

				/*//broken, 'cos we don't generate these with names in SchemaExport
				subIter = table.getIndexIterator();
				while ( subIter.hasNext() ) {
					Index index = (Index) subIter.next();
					if ( !index.isForeignKey() || !dialect.hasImplicitIndexForForeignKey() ) {
						if ( tableInfo==null || tableInfo.GetIndexMetadata( index.getFilterName() ) == null ) {
							script.add( index.sqlCreateString(dialect, mapping) );
						}
					}
				}
				//broken, 'cos we don't generate these with names in SchemaExport
				subIter = table.getUniqueKeyIterator();
				while ( subIter.hasNext() ) {
					UniqueKey uk = (UniqueKey) subIter.next();
					if ( tableInfo==null || tableInfo.GetIndexMetadata( uk.getFilterName() ) == null ) {
						script.add( uk.sqlCreateString(dialect, mapping) );
					}
				}*/
			}

			foreach (IPersistentIdentifierGenerator generator in IterateGenerators(dialect))
			{
				Object key = generator.GeneratorKey();
				if (!databaseMetadata.IsSequence(key) && !databaseMetadata.IsTable(key))
				{
					String[] lines = generator.SqlCreateStrings(dialect);
					for (int i = 0; i < lines.Length; i++)
					{
						script.Add(lines[i]);
					}
				}
			}

			return ArrayHelper.ToStringArray(script);
		}