Exemplo n.º 1
0
        public FK[] TableFKs()
        {
            System.Collections.Generic.List <FK> _output
                = new System.Collections.Generic.List <FK>();

            XS_objectType _table_a;
            string        _tableName_a;
            string        _tableFieldName_a;

            XS_objectType _table_b;
            string        _tableName_b;
            string        _tableFieldName_b;

            string _direction;

            XS_objectTypeCollection _objecttypecollection = (XS_objectTypeCollection)this.parent_ref;
            XS_layerType            _layertype            = (XS_layerType)_objecttypecollection.parent_ref;
            XS_layerTypeCollection  _layertypecollection  = (XS_layerTypeCollection)_layertype.parent_ref;
            XS__diagram             _root_ref             = (XS__diagram)_layertypecollection.parent_ref;

            for (int l = 0; l < _root_ref.LayerCollection.Count; l++)
            {
                for (int o = 0; o < _root_ref.LayerCollection[l].ObjectCollection.Count; o++)
                {
                    switch (_root_ref.LayerCollection[l].ObjectCollection[o].Type)
                    {
                    case "UML - Association":
                        if (_root_ref.LayerCollection[l].ObjectCollection[o].Connections.ConnectionCollection.Count != 2)
                        {
                            break;
                        }

                        _table_a          = null;
                        _tableName_a      = "";
                        _tableFieldName_a = "";

                        _table_b          = null;
                        _tableName_b      = "";
                        _tableFieldName_b = "";

                        _direction = "";

                        for (int a = 0; a < _root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection.Count; a++)
                        {
                            switch (_root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection[a].Name)
                            {
                            case "direction":
                                _direction = _root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection[a].Enum.Val;
                                break;

                            case "role_a":
                                _table_a = _root_ref.Table_search(
                                    _root_ref.LayerCollection[l].ObjectCollection[o].Connections.ConnectionCollection[0].To
                                    );
                                _tableName_a      = _table_a.TableName;
                                _tableFieldName_a = _root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection[a].String.Replace("#", "");
                                break;

                            case "role_b":
                                _table_b = _root_ref.Table_search(
                                    _root_ref.LayerCollection[l].ObjectCollection[o].Connections.ConnectionCollection[1].To
                                    );
                                _tableName_b      = _table_b.TableName;
                                _tableFieldName_b = _root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection[a].String.Replace("#", "");
                                break;
                            }
                        }

                        if (
                            (_table_a == this)
                            &&
                            (_direction == "2")
                            )
                        {
                            _output.Add(new FK(
                                            _tableFieldName_a,
                                            _tableName_b,
                                            _tableFieldName_b
                                            ));
                        }
                        if (
                            (_table_b == this)
                            &&
                            (_direction == "1")
                            )
                        {
                            _output.Add(new FK(
                                            _tableFieldName_b,
                                            _tableName_a,
                                            _tableFieldName_a
                                            ));
                        }
                        break;
                    }
                }
            }

            return(_output.ToArray());
        }
Exemplo n.º 2
0
//		public void New(
//			string applicationPath_in, 
//			string documentationName_in, 
//			dNotifyBack notifyBack_in
//		) {
//			if (notifyBack_in != null) notifyBack_in("creating...", true);
//			#region DocMetadata _metadata_temp = new DocMetadata(); ...;
//			XS__documentation _metadata_temp = new XS__documentation();
//			_metadata_temp.DocumentationName = documentationName_in;
//			#endregion
//
//			if (notifyBack_in != null) notifyBack_in("- generating xml file", true);
//			#region string _xmlfile = ...;
//			string _xmlfile = string.Format(
//				"{0}{1}OGenDoc-metadatas{1}MD_{2}.OGenDoc-metadata.xml", 
//				/*0*/applicationPath_in, 
//				/*1*/System.IO.Path.DirectorySeparatorChar, 
//				/*2*/documentationName_in
//			);
//			#endregion
//			_metadata_temp.SaveState_toFile(_xmlfile);
//
//			if (notifyBack_in != null) notifyBack_in("... finished!", true);
//			if (notifyBack_in != null) notifyBack_in("", true);
//
//			Open(
//				_xmlfile, 
//				true, 
//				notifyBack_in
//			);
//		}
		#endregion
		#region public void Open(...);
		public void Open(
			string filename_in, 
			bool force_doNOTsave_in, 
			dNotifyBack notifyBack_in
		) {
			#region Checking...
			if (this.hasChanges) {
				if (!force_doNOTsave_in) {
					throw new Exception(string.Format(
						"{0}.{1}.Open(): - must save before open", 
						this.GetType().Namespace, 
						this.GetType().Name
					));
				}
			}
			#endregion
			filename_ = filename_in;

			if (notifyBack_in != null) notifyBack_in("opening...", true);
			if (notifyBack_in != null) notifyBack_in("- reading metadata from xml file", true);

			diagram_ = XS__diagram.Load_fromFile(
				filename_
			)[0];
			diagram_.FilePath = filename_;

			if (notifyBack_in != null) notifyBack_in("... finished", true);


			#region more Checking...
			if (notifyBack_in != null) notifyBack_in("checking...", true);

			OGen.lib.datalayer.PostgreSQL.DBUtils_convert_Postgresql _utils_pgsql = new OGen.lib.datalayer.PostgreSQL.DBUtils_convert_Postgresql();
			OGen.lib.datalayer.SQLServer.DBUtils_convert_SQLServer _utils_sqls = new OGen.lib.datalayer.SQLServer.DBUtils_convert_SQLServer();
			Nullable<System.Data.DbType> _dbtype_psql;
			Nullable<System.Data.DbType> _dbtype_sqls;
			bool _isUsingPostgreSQL = false;
			bool _isUsingSQLServer = false;
			DBTableField[] _dbtablefields;
			DBTableField[] _dbtablefields2;
			XS_objectType.FK[] __fks;
			System.Collections.Generic.Dictionary<string, XS_objectType.FK> _fks;
			bool _foundFKTable;
			bool _foundFKField;
			for (int l = 0; l < diagram_.LayerCollection.Count; l++) {
				for (int o = 0; o < diagram_.LayerCollection[l].ObjectCollection.Count; o++) {
					_dbtablefields = diagram_.Table_search(l, o).TableFields();
					diagram_.Table_search(l, o).TableFKs(
						out __fks,
						out _fks
					);

					#region checking for invalid foreign keys
					if (
						_fks.ContainsKey("")
					) {
						throw new Exception(string.Format(
							"invalid foreign key at table: {0}.? -> {1}.?",

							diagram_.Table_search(l, o).TableName,
							_fks[""].FK_TableName
						));
					}
					#endregion

					#region _isUsingPostgreSQL = ...; _isUsingSQLServer = ...;
					for (int f = 0; f < _dbtablefields.Length; f++) {
						if (
							(_dbtablefields[f].PostgreSQLTypeName != null)
							&&
							(_dbtablefields[f].PostgreSQLTypeName.Trim() != "")
						) {
							_isUsingPostgreSQL = true;
						}
						if (
							(_dbtablefields[f].SQLServerTypeName != null)
							&&
							(_dbtablefields[f].SQLServerTypeName.Trim() != "")
						) {
							_isUsingSQLServer = true;
						}
						if (
							_isUsingPostgreSQL
							&&
							_isUsingSQLServer
						) {
							break;
						}
					}
					#endregion

					for (int f = 0; f < _dbtablefields.Length; f++) {
						#region checking if db server type supported
						if (
							!_isUsingPostgreSQL
							&&
							!_isUsingSQLServer
						) {
							throw new Exception(string.Format(
								"no db type defined (should use at least one of the supported db servers: PostgreSQL or SQLServer)",
								_dbtablefields[f].TableName,
								_dbtablefields[f].Name
							));
						}
						#endregion

						#region checking postgresql field type . . .
						_dbtype_psql = null;

						if (
							_isUsingPostgreSQL
						) {
							if (
								(_dbtablefields[f].PostgreSQLTypeName == null)
								||
								(_dbtablefields[f].PostgreSQLTypeName.Trim() == "")
							) {
								throw new Exception(string.Format(
									"invalid table field type - empty postgresql type: {0}.{1}",
									_dbtablefields[f].TableName,
									_dbtablefields[f].Name
								));
							}

							_dbtype_psql = _utils_pgsql.XDbType2DbType(
								_utils_pgsql.XDbType_Parse(
									_dbtablefields[f].PostgreSQLTypeName,
									false
								)
							);
						} 
						#endregion
						#region checking sql server field type . . .
						_dbtype_sqls = null;

						if (
							_isUsingSQLServer
						) {
							if (
								(_dbtablefields[f].SQLServerTypeName == null)
								||
								(_dbtablefields[f].SQLServerTypeName.Trim() == "")
							) {
								throw new Exception(string.Format(
									"invalid table field type - empty sql server type: {0}.{1}",
									_dbtablefields[f].TableName,
									_dbtablefields[f].Name
								));
							}

							_dbtype_sqls = _utils_sqls.XDbType2DbType(
								_utils_sqls.XDbType_Parse(
									_dbtablefields[f].SQLServerTypeName,
									false
								)
							);
						} 
						#endregion

						#region checking if field types match . . .
						if (
							_isUsingPostgreSQL
							&&
							_isUsingSQLServer
						) {
							if (
								(_dbtype_psql == null)
								||
								(_dbtype_sqls == null)
								||
								(
									_dbtype_psql.Value
									!=
									_dbtype_sqls.Value
								)
							) {
								throw new Exception(string.Format(
									"table field types don't match: {0}.{1}",
									_dbtablefields[f].TableName,
									_dbtablefields[f].Name
								));
							}
						} 
						#endregion

						#region //checking FKs . . .
						//if (
						//    (_dbtablefields[f].FK_TableName != null)
						//    &&
						//    (_dbtablefields[f].FK_TableName.Trim() != "")
						//) {
						//    _foundFKTable = false;
						//    _foundFKField = false;
						//    for (int l2 = 0; l2 < diagram_.LayerCollection.Count; l2++) {
						//        for (int o2 = 0; o2 < diagram_.LayerCollection[l2].ObjectCollection.Count; o2++) {
						//            if (
						//                diagram_.Table_search(l2, o2).TableName
						//                ==
						//                _dbtablefields[f].FK_TableName
						//            ) {
						//                _dbtablefields2 = diagram_.Table_search(l2, o2).TableFields();

						//                for (int f2 = 0; f2 < _dbtablefields2.Length; f2++) {
						//                    if (
						//                        _dbtablefields2[f2].Name
						//                        ==
						//                        _dbtablefields[f].FK_FieldName
						//                    ) {

						//                        if (
						//                            _dbtablefields2[f2].PostgreSQLTypeName
						//                            !=
						//                            _dbtablefields[f].PostgreSQLTypeName
						//                        ) {
						//                            throw new Exception(string.Format(
						//                                "foreign key postgresql db type mismatch: {0}.{1}",
						//                                _dbtablefields[f].FK_TableName,
						//                                _dbtablefields[f].FK_FieldName
						//                            ));
						//                        }
						//                        if (
						//                            _dbtablefields2[f2].SQLServerTypeName
						//                            !=
						//                            _dbtablefields[f].SQLServerTypeName
						//                        ) {
						//                            throw new Exception(string.Format(
						//                                "foreign key sql server db type mismatch: {0}.{1}",
						//                                _dbtablefields[f].FK_TableName,
						//                                _dbtablefields[f].FK_FieldName
						//                            ));
						//                        }

						//                        _foundFKField = true;
						//                        break;
						//                    }
						//                }

						//                _foundFKTable = true;
						//                break;
						//            }
						//        }
						//    }

						//    if (!_foundFKTable) {
						//        throw new Exception(string.Format(
						//            "can't find foreign key TABLE: {0}.{1}",
						//            _dbtablefields[f].FK_TableName,
						//            _dbtablefields[f].FK_FieldName
						//        ));
						//    }
						//    if (!_foundFKField) {
						//        throw new Exception(string.Format(
						//            "can't find foreign key FIELD: {0}.{1}",
						//            _dbtablefields[f].FK_TableName,
						//            _dbtablefields[f].FK_FieldName
						//        ));
						//    }
						//}
						#endregion
						#region checking FKs . . .
						if (
							_fks.ContainsKey(
								_dbtablefields[f].Name
							)
						) {
							_foundFKTable = false;
							_foundFKField = false;
							for (int l2 = 0; l2 < diagram_.LayerCollection.Count; l2++) {
							    for (int o2 = 0; o2 < diagram_.LayerCollection[l2].ObjectCollection.Count; o2++) {
									if (
										diagram_.Table_search(l2, o2).TableName
										==
										_fks[_dbtablefields[f].Name].FK_TableName
									) {
										_dbtablefields2 = diagram_.Table_search(l2, o2).TableFields();

										for (int f2 = 0; f2 < _dbtablefields2.Length; f2++) {
											if (
												_dbtablefields2[f2].Name
												==
												_fks[_dbtablefields[f].Name].FK_TableFieldName
											) {

												if (
													(
														(_dbtablefields2[f2].PostgreSQLTypeName == null)
														!=
														(_dbtablefields[f].PostgreSQLTypeName == null)
													)
													||
													(
														//_dbtablefields2[f2].PostgreSQLTypeName
														//!=
														//_dbtablefields[f].PostgreSQLTypeName

														!(
															(_dbtablefields2[f2].PostgreSQLTypeName == _dbtablefields[f].PostgreSQLTypeName)
															||
															(
																(_dbtablefields2[f2].PostgreSQLTypeName == "serial")
																&&
																(_dbtablefields[f].PostgreSQLTypeName == "integer")
															)
															||
															(
																(_dbtablefields[f].PostgreSQLTypeName == "serial")
																&&
																(_dbtablefields2[f2].PostgreSQLTypeName == "integer")
															)
															||
															(
																(_dbtablefields2[f2].PostgreSQLTypeName == "bigserial")
																&&
																(_dbtablefields[f].PostgreSQLTypeName == "bigint")
															)
															||
															(
																(_dbtablefields[f].PostgreSQLTypeName == "bigserial")
																&&
																(_dbtablefields2[f2].PostgreSQLTypeName == "bigint")
															)
														)
													)
												) {
													throw new Exception(string.Format(
														"foreign key postgresql db type mismatch: {0}.{1} -> {2}.{3}",

														diagram_.Table_search(l, o).TableName,
														_dbtablefields[f].Name,

														_fks[_dbtablefields[f].Name].FK_TableName,
														_fks[_dbtablefields[f].Name].FK_TableFieldName
													));
												}
												if (
													(
														(_dbtablefields2[f2].SQLServerTypeName == null)
														!=
														(_dbtablefields[f].SQLServerTypeName == null)
													)
													||
													(
														_dbtablefields2[f2].SQLServerTypeName
														!=
														_dbtablefields[f].SQLServerTypeName
													)
												) {
													throw new Exception(string.Format(
														"foreign key sql server db type mismatch: {0}.{1} -> {2}.{3}",

														diagram_.Table_search(l, o).TableName,
														_dbtablefields[f].Name,

														_fks[_dbtablefields[f].Name].FK_TableName,
														_fks[_dbtablefields[f].Name].FK_TableFieldName
													));
												}

												_foundFKField = true;
												break;
											}
										}

										_foundFKTable = true;
										break;
							        }
							    }
							}

							if (!_foundFKTable) {
								throw new Exception(string.Format(
									"can't find foreign key TABLE: {0}.{1} -> {2}.{3}",

									diagram_.Table_search(l, o).TableName,
									_dbtablefields[f].Name,

									_fks[_dbtablefields[f].Name].FK_TableName,
									_fks[_dbtablefields[f].Name].FK_TableFieldName
								));
							}
							if (!_foundFKField) {
								throw new Exception(string.Format(
									"can't find foreign key FIELD: {0}.{1} -> {2}.{3}",

									diagram_.Table_search(l, o).TableName,
									_dbtablefields[f].Name,

									_fks[_dbtablefields[f].Name].FK_TableName,
									_fks[_dbtablefields[f].Name].FK_TableFieldName
								));
							}
						}
						#endregion
					}
				}
			}

			if (notifyBack_in != null) notifyBack_in("... finished", true);
			#endregion
		}
Exemplo n.º 3
0
        public void TableForeignKeys(
            out OGen.Dia.Libraries.Metadata.Diagram.ForeignKey[] foreignKeys_out,
            out System.Collections.Generic.Dictionary <string, OGen.Dia.Libraries.Metadata.Diagram.ForeignKey> foreignKeys_dic_out
            )
        {
            System.Collections.Generic.List <OGen.Dia.Libraries.Metadata.Diagram.ForeignKey> _output
                = new System.Collections.Generic.List <OGen.Dia.Libraries.Metadata.Diagram.ForeignKey>();
            System.Collections.Generic.Dictionary <string, OGen.Dia.Libraries.Metadata.Diagram.ForeignKey> _output2
                = new System.Collections.Generic.Dictionary <string, OGen.Dia.Libraries.Metadata.Diagram.ForeignKey>();

            XS_objectType _table_a;
            string        _tableName_a;
            string        _tableFieldName_a;

            XS_objectType _table_b;
            string        _tableName_b;
            string        _tableFieldName_b;

            string _direction;

            XS_objectTypeCollection _objecttypecollection = (XS_objectTypeCollection)this.parent_ref;
            XS_layerType            _layertype            = (XS_layerType)_objecttypecollection.parent_ref;
            XS_layerTypeCollection  _layertypecollection  = (XS_layerTypeCollection)_layertype.parent_ref;
            XS__diagram             _root_ref             = (XS__diagram)_layertypecollection.parent_ref;

            OGen.Dia.Libraries.Metadata.Diagram.ForeignKey _aux;

            for (int l = 0; l < _root_ref.LayerCollection.Count; l++)
            {
                for (int o = 0; o < _root_ref.LayerCollection[l].ObjectCollection.Count; o++)
                {
                    switch (_root_ref.LayerCollection[l].ObjectCollection[o].Type)
                    {
                    case "UML - Association":
                        if (_root_ref.LayerCollection[l].ObjectCollection[o].Connections.ConnectionCollection.Count != 2)
                        {
                            break;
                        }

                        _table_a          = null;
                        _tableName_a      = "";
                        _tableFieldName_a = "";

                        _table_b          = null;
                        _tableName_b      = "";
                        _tableFieldName_b = "";

                        _direction = "";

                        for (int a = 0; a < _root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection.Count; a++)
                        {
                            switch (_root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection[a].Name)
                            {
                            case "direction":
                                _direction = _root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection[a].Enum.Value;
                                break;

                            case "role_a":
                                _table_a = _root_ref.Table_search(
                                    _root_ref.LayerCollection[l].ObjectCollection[o].Connections.ConnectionCollection[0].To
                                    );
                                _tableName_a      = _table_a.TableName;
                                _tableFieldName_a = _root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection[a].String.Replace("#", "");
                                break;

                            case "role_b":
                                _table_b = _root_ref.Table_search(
                                    _root_ref.LayerCollection[l].ObjectCollection[o].Connections.ConnectionCollection[1].To
                                    );
                                _tableName_b      = _table_b.TableName;
                                _tableFieldName_b = _root_ref.LayerCollection[l].ObjectCollection[o].AttributeCollection[a].String.Replace("#", "");
                                break;
                            }
                        }

                        if (
                            (_table_a == this)
                            &&
                            (_direction == "1")
                            )
                        {
                            _output2.Add(
                                _tableFieldName_a,
                                _aux = new OGen.Dia.Libraries.Metadata.Diagram.ForeignKey(
                                    _tableFieldName_a,
                                    _tableName_b,
                                    _tableFieldName_b
                                    )
                                );
                            _output.Add(_aux);
                        }
                        if (
                            (_table_b == this)
                            &&
                            (_direction == "2")
                            )
                        {
                            _output2.Add(
                                _tableFieldName_b,
                                _aux = new OGen.Dia.Libraries.Metadata.Diagram.ForeignKey(
                                    _tableFieldName_b,
                                    _tableName_a,
                                    _tableFieldName_a
                                    )
                                );
                            _output.Add(_aux);
                        }
                        break;
                    }
                }
            }

            foreignKeys_dic_out = _output2;
            foreignKeys_out     = _output.ToArray();

            //return _output.ToArray();
        }