1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
.Generator
.Components
.Database
19 using System
.Data
.OleDb
;
22 public class DatabaseDefinitionBuilder
: IDatabaseDefinitionBuilder
24 private IConnectionFactory _connectionFactory
;
26 public DatabaseDefinitionBuilder(IConnectionFactory connectionFactory
)
28 _connectionFactory
= connectionFactory
;
31 public DatabaseDefinition
Build(String
alias, String connectionString
)
33 DatabaseDefinition def
= new DatabaseDefinition(alias);
35 using(OleDbConnection connection
= _connectionFactory
.CreateConnection(connectionString
))
37 BuildTables(connection
, def
);
38 BuildPks(connection
, def
);
39 BuildFks(connection
, def
);
45 private void BuildPks(OleDbConnection connection
, DatabaseDefinition def
)
47 object[] objArrRestrict
= new object[] {null, null, null, null}
;
48 DataTable indexes
= connection
.GetOleDbSchemaTable(OleDbSchemaGuid
.Indexes
, objArrRestrict
);
50 foreach(DataRow index
in indexes
.Rows
)
52 TableDefinition tableDef
= def
.Tables
[ (String
) index
["TABLE_NAME"] ];
54 if (tableDef
== null) continue;
56 ColumnDefinition colDef
= tableDef
.Columns
[ (String
) index
["COLUMN_NAME"] ];
58 colDef
.PrimaryKey
= (bool) index
["PRIMARY_KEY"];
59 colDef
.Unique
= (bool) index
["UNIQUE"];
63 private void BuildFks(OleDbConnection connection
, DatabaseDefinition def
)
65 object[] objArrRestrict
= new object[] {null, null, null, null}
;
66 DataTable indexes
= connection
.GetOleDbSchemaTable(OleDbSchemaGuid
.Foreign_Keys
, objArrRestrict
);
68 foreach(DataRow index
in indexes
.Rows
)
70 TableDefinition fkTableDef
= def
.Tables
[ (String
) index
["FK_TABLE_NAME"] ];
71 TableDefinition pkTableDef
= def
.Tables
[ (String
) index
["PK_TABLE_NAME"] ];
73 if (fkTableDef
== null || pkTableDef
== null) continue;
75 ColumnDefinition colDef
= fkTableDef
.Columns
[ (String
) index
["FK_COLUMN_NAME"] ];
77 colDef
.ForeignKey
= true;
78 colDef
.RelatedTable
= pkTableDef
;
80 pkTableDef
.AddManyRelation(fkTableDef
);
84 private void BuildTables(OleDbConnection connection
, DatabaseDefinition def
)
86 object[] objArrRestrict
= new object[] {null, null, null, "TABLE"}
;
87 DataTable tables
= connection
.GetOleDbSchemaTable(OleDbSchemaGuid
.Tables
, objArrRestrict
);
89 foreach(DataRow table
in tables
.Rows
)
91 TableDefinition tableDef
=
92 def
.AddTable( new TableDefinition( (String
) table
["TABLE_NAME"], def
) );
94 objArrRestrict
= new object[] {null, null, tableDef.Name, null}
;
96 DataTable columns
= connection
.GetOleDbSchemaTable(OleDbSchemaGuid
.Columns
, objArrRestrict
);
98 foreach(DataRow column
in columns
.Rows
)
100 ColumnDefinition colDef
= tableDef
.AddColumn( new ColumnDefinition( (String
) column
["COLUMN_NAME"] ) );
101 colDef
.Nullable
= (bool) column
["IS_NULLABLE"];
102 colDef
.Type
= (OleDbType
) Enum
.Parse( typeof(OleDbType
), column
["DATA_TYPE"].ToString() );