/// <summary>
        /// Get DB type of edge field - TODO: can be generic???
        /// </summary>
        public static object GetDbFieldType(EdgeTypeField field)
        {
            // edge object
            if (field.Field.FieldEdgeType != null)
            {
                return(SqlDbType.BigInt.ToString());
            }

            // TODO: Amit to remove COLLATE Hebrew_CI_AS (DB definition)
            if (field.ColumnName.StartsWith("string"))
            {
                return(String.Format("{0}(1000) COLLATE Hebrew_CI_AS", SqlDbType.NVarChar));
            }

            if (field.ColumnName.StartsWith("int"))
            {
                return(SqlDbType.Int.ToString());
            }

            if (field.ColumnName.StartsWith("float"))
            {
                return(String.Format("{0}(18,3)", SqlDbType.Float));
            }

            throw new ConfigurationErrorsException(String.Format("Cannot find DB tpe for column {0} of EdgeField {1}", field.ColumnName, field.Field.Name));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set relationships between edge fields and edge types (many-2-many) according to account id
        /// </summary>
        public static void SetEdgeTypeEdgeFieldRelation(int accountId, Dictionary <string, EdgeType> edgeTypes, List <EdgeField> edgeFields, SqlConnection connection)
        {
            try
            {
                using (var cmd = new SqlCommand("[EdgeObjects].[dbo].[MD_EdgeTypeField_Get]", connection))
                {
                    cmd.Parameters.AddWithValue("@accountID", accountId);
                    cmd.CommandType = CommandType.StoredProcedure;

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            // find parent edge type nad edge field
                            var parentTypeId = int.Parse(reader["ParentTypeID"].ToString());
                            var fieldtId     = int.Parse(reader["FieldID"].ToString());
                            // TODO: Amit to remove COLLATE Hebrew_CI_AS (DB definition)
                            var clmLength = reader["ColumnLength"] == DBNull.Value ? String.Empty : String.Format("({0}) COLLATE Hebrew_CI_AS", reader["ColumnLength"]);

                            var parentType = edgeTypes.Values.FirstOrDefault(x => x.TypeID == parentTypeId);
                            if (parentType == null)
                            {
                                throw new Exception(String.Format("Configuration error: Unknown parent edge type {0} while loading edge type fields", parentTypeId));
                            }

                            var field = edgeFields.FirstOrDefault(x => x.FieldID == fieldtId);
                            if (field == null)
                            {
                                throw new Exception(String.Format("Configuration error: Unknown edge field {0} while loading edge type fields", fieldtId));
                            }

                            var typeField = new EdgeTypeField
                            {
                                ColumnName   = reader["ColumnName"].ToString(),
                                ColumnDbType = String.Format("{0}{1}", reader["ColumnType"], clmLength),
                                IsIdentity   = bool.Parse(reader["IsIdentity"].ToString()),
                                Field        = field
                            };

                            // add edge field to parent edge type
                            if (!parentType.Fields.Contains(typeField))
                            {
                                parentType.Fields.Add(typeField);
                            }
                            else
                            {
                                throw new Exception(String.Format("Configuration error: Field {0} already exists in parent edge type {1}", field.Name, parentType.Name));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while trying to get extra fields from DB", ex);
            }
        }
        /// <summary>
        /// Set relationships between edge fields and edge types (many-2-many) according to account id
        /// </summary>
        public static void SetEdgeTypeEdgeFieldRelation(int accountid, Dictionary <string, EdgeType> edgeTypes, List <EdgeField> edgeFields, SqlConnection connection)
        {
            try
            {
                using (var cmd = SqlUtility.CreateCommand("MD_EdgeTypeField_Get", CommandType.StoredProcedure))
                {
                    cmd.Parameters.AddWithValue("@accountID", accountid);
                    cmd.Connection = connection;

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            // find parent edge type nad edge field
                            var parentTypeId = int.Parse(reader["ParentTypeID"].ToString());
                            var fieldtId     = int.Parse(reader["FieldID"].ToString());

                            var parentType = edgeTypes.Values.FirstOrDefault(x => x.TypeID == parentTypeId);
                            if (parentType == null)
                            {
                                throw new ConfigurationErrorsException(String.Format("Configuration error: Unknown parent edge type {0} while loading edge type fields", parentTypeId));
                            }

                            var field = edgeFields.FirstOrDefault(x => x.FieldID == fieldtId);
                            if (field == null)
                            {
                                throw new ConfigurationErrorsException(String.Format("Configuration error: Unknown edge field {0} while loading edge type fields", fieldtId));
                            }

                            var typeField = new EdgeTypeField
                            {
                                ColumnName = reader["ColumnName"].ToString(),
                                IsIdentity = bool.Parse(reader["IsIdentity"].ToString()),
                                Field      = field
                            };

                            // add edge field to parent edge type
                            if (!parentType.Fields.Contains(typeField))
                            {
                                parentType.Fields.Add(typeField);
                            }
                            else
                            {
                                throw new ConfigurationErrorsException(String.Format("Configuration error: Field {0} already exists in parent edge type {1}", field.Name, parentType.Name));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while trying to get extra fields from DB", ex);
            }
        }