Beispiel #1
0
        private IList <HasMany> DetermineHasManyRelationships(Table table)
        {
            var hasManyRelationships = new List <HasMany>();
            var conn = new CUBRIDConnection(connectionStr);

            conn.Open();

            try
            {
                using (conn)
                {
                    var       schema = new CUBRIDSchemaProvider(conn);
                    DataTable dt     = schema.GetForeignKeys(new[] { "%" });
                    for (var i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["PKTABLE_NAME"].ToString() == table.Name)
                        {
                            var newHasManyItem = new HasMany
                            {
                                Reference       = dt.Rows[i]["FKTABLE_NAME"].ToString(),
                                ConstraintName  = dt.Rows[i]["FK_NAME"].ToString(),
                                ReferenceColumn = dt.Rows[i]["FKCOLUMN_NAME"].ToString()
                            };
                            hasManyRelationships.Add(newHasManyItem);
                        }
                    }
                }
            }
            finally
            {
                conn.Close();
            }

            return(hasManyRelationships);
        }
Beispiel #2
0
        /// <summary>
        /// Test CUBRIDSchemaProvider GetForeignKeys() method
        /// </summary>
        private static void Test_GetForeignKeys()
        {
            using (CUBRIDConnection conn = new CUBRIDConnection())
            {
                conn.ConnectionString = TestCases.connString;
                conn.Open();

                CUBRIDSchemaProvider schema = new CUBRIDSchemaProvider(conn);
                DataTable            dt     = schema.GetForeignKeys(new string[] { "game" });

                Debug.Assert(dt.Columns.Count == 9);
                Debug.Assert(dt.Rows.Count == 2);

                Debug.Assert(dt.Rows[1][0].ToString() == "event");
                Debug.Assert(dt.Rows[1][1].ToString() == "code");
                Debug.Assert(dt.Rows[1][2].ToString() == "game");
                Debug.Assert(dt.Rows[1][3].ToString() == "event_code");
                Debug.Assert(dt.Rows[1][4].ToString() == "1");
                Debug.Assert(dt.Rows[1][5].ToString() == "1");
                Debug.Assert(dt.Rows[1][6].ToString() == "1");
                Debug.Assert(dt.Rows[1][7].ToString() == "fk_game_event_code");
                Debug.Assert(dt.Rows[1][8].ToString() == "pk_event_code");

                Debug.Assert(dt.Rows[0][0].ToString() == "athlete");
                Debug.Assert(dt.Rows[0][1].ToString() == "code");
                Debug.Assert(dt.Rows[0][2].ToString() == "game");
                Debug.Assert(dt.Rows[0][3].ToString() == "athlete_code");
                Debug.Assert(dt.Rows[0][4].ToString() == "1");
                Debug.Assert(dt.Rows[0][5].ToString() == "1");
                Debug.Assert(dt.Rows[0][6].ToString() == "1");
                Debug.Assert(dt.Rows[0][7].ToString() == "fk_game_athlete_code");
                Debug.Assert(dt.Rows[0][8].ToString() == "pk_athlete_code");
            }
        }
Beispiel #3
0
        public Task <IList <Column> > GetTableDetails(Table table, string owner)
        {
            IList <Column> columns = new List <Column>();
            var            conn    = new CUBRIDConnection(connectionStr);

            conn.Open();
            try
            {
                using (conn)
                {
                    var       schema = new CUBRIDSchemaProvider(conn);
                    DataTable dt_fk  = schema.GetForeignKeys(new[] { table.Name.ToLower() });

                    string sqlInfo   = String.Format("select * from [{0}] limit 1", table.Name.ToLower());
                    var    adapter   = new CUBRIDDataAdapter(sqlInfo, conn);
                    var    tableInfo = new DataTable();
                    adapter.FillSchema(tableInfo, SchemaType.Source);

                    using (var reader = new DataTableReader(tableInfo))
                    {
                        DataTable schemaTable = reader.GetSchemaTable();
                        for (var k = 0; k < schemaTable.Rows.Count; k++)
                        {
                            string columnName    = schemaTable.Rows[k]["ColumnName"].ToString().ToLower();
                            var    isUnique      = (bool)schemaTable.Rows[k]["IsUnique"];
                            var    isNullable    = (bool)schemaTable.Rows[k]["AllowDBNull"];
                            var    isPrimaryKey  = (bool)schemaTable.Rows[k]["IsKey"];
                            var    isIdentity    = (bool)schemaTable.Rows[k]["IsAutoIncrement"];
                            var    dataLength    = (int)schemaTable.Rows[k]["ColumnSize"];
                            int    dataPrecision = 0;
                            if (schemaTable.Rows[k]["NumericPrecision"].ToString() != String.Empty)
                            {
                                dataPrecision = (int)schemaTable.Rows[k]["NumericPrecision"];
                            }
                            int dataScale = 0;
                            if (schemaTable.Rows[k]["NumericScale"].ToString() != String.Empty)
                            {
                                dataScale = (int)schemaTable.Rows[k]["NumericScale"];
                            }
                            bool   isForeignKey   = false;
                            string fkTableName    = "";
                            string constraintName = "";
                            for (var i_fk = 0; i_fk < dt_fk.Rows.Count; i_fk++)
                            {
                                if (dt_fk.Rows[i_fk]["FKCOLUMN_NAME"].ToString().ToLower() == columnName)
                                {
                                    isForeignKey   = true;
                                    fkTableName    = dt_fk.Rows[i_fk]["PKTABLE_NAME"].ToString();
                                    constraintName = dt_fk.Rows[i_fk]["FK_NAME"].ToString();
                                    break;
                                }
                            }
                            string dataType;
                            using (var cmd = new CUBRIDCommand(sqlInfo, conn))
                            {
                                using (var CUBRIDReader = (CUBRIDDataReader)cmd.ExecuteReader())
                                {
                                    CUBRIDReader.Read();
                                    dataType = CUBRIDReader.GetColumnTypeName(k);
                                }
                            }
                            var m = new DataTypeMapper();
                            columns.Add(new Column
                            {
                                Name                = columnName,
                                DataType            = dataType,
                                IsNullable          = isNullable,
                                IsUnique            = isUnique,
                                IsPrimaryKey        = isPrimaryKey,
                                IsForeignKey        = isForeignKey,
                                IsIdentity          = isIdentity,
                                DataLength          = dataLength,
                                DataPrecision       = dataPrecision,
                                DataScale           = dataScale,
                                ForeignKeyTableName = fkTableName,
                                ConstraintName      = constraintName,
                                MappedDataType      = m.MapFromDBType(ServerType.CUBRID, dataType, null, null, null),
                            });
                        }
                    }
                }

                table.Columns = columns;

                table.Owner      = owner;
                table.PrimaryKey = DeterminePrimaryKeys(table);

                table.HasManyRelationships = DetermineHasManyRelationships(table);
            }
            finally
            {
                conn.Close();
            }

            return(Task.FromResult(columns));
        }