public void HasÁndBelongsToMany()
		{
			InitKernel();
			IRelationshipBuilder relService = ObtainService();

			DatabaseDefinition dbdef = new DatabaseDefinition("alias");

			TableDefinition compTable = new TableDefinition("companies", dbdef );
			compTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
			compTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );

			TableDefinition peopleTable = new TableDefinition("people", dbdef );
			peopleTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
			peopleTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );

			TableDefinition assocTable = new TableDefinition("companiespeople", dbdef );
			assocTable.AddColumn( new ColumnDefinition("comp_id", true, true, true, false, OleDbType.Integer) );
			assocTable.AddColumn( new ColumnDefinition("person_id", true, true, false, false, OleDbType.Integer) );

			ActiveRecordDescriptor desc = new ActiveRecordDescriptor("Company");
			ActiveRecordDescriptor targetDesc = new ActiveRecordDescriptor("Person");

			RelationshipInfo info = new RelationshipInfo( AssociationEnum.HasAndBelongsToMany, desc, targetDesc );
			info.AssociationTable = assocTable;
			info.ParentCol = new ColumnDefinition("comp_id", false, true, false, true, OleDbType.Integer);
			info.ChildCol = new ColumnDefinition("person_id", false, true, false, true, OleDbType.Integer);

			ActiveRecordPropertyRelationDescriptor propDesc = relService.Build( info );
			Assert.IsNotNull(propDesc as ActiveRecordHasAndBelongsToManyDescriptor);
			Assert.IsNotNull(propDesc);
			Assert.AreEqual( "People", propDesc.PropertyName );
			Assert.AreEqual( targetDesc, propDesc.TargetType );
			Assert.AreEqual( "person_id", propDesc.ColumnName );
			Assert.AreEqual( "comp_id", (propDesc as ActiveRecordHasAndBelongsToManyDescriptor).ColumnKey);
		}
		public DatabaseDefinition Build(String alias, String connectionString)
		{
			DatabaseDefinition def = new DatabaseDefinition(alias);

			using(OleDbConnection connection = _connectionFactory.CreateConnection(connectionString))
			{
				BuildTables(connection, def);
				BuildPks(connection, def);
				BuildFks(connection, def);
			}

			return def;
		}
		private void BuildBlogPostsStructure(out TableDefinition blogTable, out TableDefinition postTable)
		{
			DatabaseDefinition dbdef = new DatabaseDefinition("alias");

			blogTable = new TableDefinition("blogs", dbdef );
			blogTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
			blogTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
	
			postTable = new TableDefinition("posts", dbdef );
			postTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
			postTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
			postTable.AddColumn( new ColumnDefinition("blog_id", false, true, false, false, OleDbType.VarChar, blogTable) );
	
			blogTable.AddManyRelation(postTable);
		}
		private void BuildPks(OleDbConnection connection, DatabaseDefinition def)
		{
			object[] objArrRestrict = new object[] {null, null, null, null};
			DataTable indexes = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Indexes, objArrRestrict);

			foreach(DataRow index in indexes.Rows)
			{
				TableDefinition tableDef = def.Tables[ (String) index["TABLE_NAME"] ];
				
				if (tableDef == null) continue;

				ColumnDefinition colDef = tableDef.Columns[ (String) index["COLUMN_NAME"] ];

				colDef.PrimaryKey = (bool) index["PRIMARY_KEY"];
				colDef.Unique = (bool) index["UNIQUE"];
			}
		}
		private void BuildFks(OleDbConnection connection, DatabaseDefinition def)
		{
			object[] objArrRestrict = new object[] {null, null, null, null};
			DataTable indexes = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, objArrRestrict);

			foreach(DataRow index in indexes.Rows)
			{
				TableDefinition fkTableDef = def.Tables[ (String) index["FK_TABLE_NAME"] ];
				TableDefinition pkTableDef = def.Tables[ (String) index["PK_TABLE_NAME"] ];
				
				if (fkTableDef == null || pkTableDef == null) continue;

				ColumnDefinition colDef = fkTableDef.Columns[ (String) index["FK_COLUMN_NAME"] ];

				colDef.ForeignKey = true;
				colDef.RelatedTable = pkTableDef;
				
				pkTableDef.AddManyRelation(fkTableDef);
			}
		}
		private void BuildTables(OleDbConnection connection, DatabaseDefinition def)
		{
			object[] objArrRestrict = new object[] {null, null, null, "TABLE"};
			DataTable tables = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, objArrRestrict);
	
			foreach(DataRow table in tables.Rows)
			{
				TableDefinition tableDef = 
					def.AddTable( new TableDefinition( (String) table["TABLE_NAME"], def ) );

				objArrRestrict = new object[] {null, null, tableDef.Name, null};

				DataTable columns = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, objArrRestrict);

				foreach(DataRow column in columns.Rows)
				{
					ColumnDefinition colDef = tableDef.AddColumn( new ColumnDefinition( (String) column["COLUMN_NAME"] ) );
					colDef.Nullable = (bool) column["IS_NULLABLE"];
					colDef.Type = (OleDbType) Enum.Parse( typeof(OleDbType), column["DATA_TYPE"].ToString() );
				}
			}
		}
Example #7
0
		public void AddDatabaseDefinition(DatabaseDefinition dbDef)
		{
			if (dbDef == null) throw new ArgumentNullException("dbDef");

			ActiveRecordBaseDescriptor baseDesc;
			
			if (_dbDef2ArBase.Count == 0)
			{
				baseDesc = new ActiveRecordBaseDescriptor("ActiveRecordBase");
			}
			else
			{
				String name = dbDef.Alias.Replace(" ","") + "Base";
				baseDesc = new ActiveRecordBaseDescriptor(name);
			}

			dbDef.ActiveRecordBaseDescriptor = baseDesc;
			AddActiveRecordDescriptor(baseDesc);

			_databases.Add(dbDef);
			
			// Just an optimization for later
			_dbDef2ArBase[dbDef] = baseDesc;
		}
Example #8
0
		private void PopulateTablesInComboBox(ComboBox combo, DatabaseDefinition dbDef)
		{
			combo.Items.Clear();
			combo.ValueMember = "Name";

			foreach(TableDefinition table in dbDef.Tables)
			{
				combo.Items.Add(table);
			}
		}
Example #9
0
		public TableDefinition(String name, DatabaseDefinition db)
		{
			_name = name;
			_db = db;
		}
Example #10
0
		public override void Activated(IDictionary context)
		{
			DatabaseDefinition db = context["selecteddb"] as DatabaseDefinition;

			if (db == null)
			{
				throw new ApplicationException("No database definition selected");
			}

			if (db != _oldDb)
			{
				_oldDb = db;

				listBox1.Items.Clear();
				listBox1.ValueMember = "Name";

				foreach (TableDefinition table in db.Tables)
				{
					listBox1.Items.Add(table);
				}
			}
		}
		public void SelfReference()
		{
			InitKernel();
			IRelationshipInferenceService relService = ObtainService();

			DatabaseDefinition dbdef = new DatabaseDefinition("alias");

			TableDefinition categoryTable = new TableDefinition("categories", dbdef );
			categoryTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
			categoryTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
			categoryTable.AddColumn( new ColumnDefinition("parent_id", false, true, false, false, OleDbType.Integer, categoryTable) );
	
			categoryTable.AddManyRelation(categoryTable);

			BuildContext context = new BuildContext();

			ActiveRecordDescriptor arDesc = new ActiveRecordDescriptor();

			ActiveRecordPropertyDescriptor[] descs = relService.InferRelations( arDesc, categoryTable, context );

			Assert.IsFalse(context.HasPendents);

			Assert.IsNotNull(descs);
			Assert.AreEqual( 2, descs.Length );

			ActiveRecordHasManyDescriptor desc1 = descs[0] as ActiveRecordHasManyDescriptor;
			Assert.IsNotNull(desc1);
			Assert.IsNotNull(desc1.TargetType);
			Assert.IsNotNull(desc1.PropertyType);

			Assert.AreEqual( "Categories", desc1.PropertyName );
			Assert.AreEqual( "parent_id", desc1.ColumnName );
			Assert.AreEqual( typeof(IList), desc1.PropertyType );

			ActiveRecordBelongsToDescriptor desc2 = descs[1] as ActiveRecordBelongsToDescriptor;
			Assert.IsNotNull(desc2);
			Assert.IsNotNull(desc2.TargetType);
			Assert.IsNull(desc2.PropertyType);
			Assert.AreEqual( "Category", desc2.PropertyName );
			Assert.AreEqual( "parent_id", desc2.ColumnName );
		}
		public void Add(DatabaseDefinition def)
		{
			BaseAdd(def.Alias, def);
		}
 public void Add(DatabaseDefinition def)
 {
     BaseAdd(def.Alias, def);
 }
Example #14
0
 public TableDefinition(String name, DatabaseDefinition db)
 {
     _name = name;
     _db   = db;
 }