Ejemplo n.º 1
0
        /// <summary>
        /// Converts an entity's id to the corresponding database one
        /// </summary>
        /// <param name="generator">The <seealso cref="GeneratorMapping.GeneratorType"/> used in the mapping for the entity</param>
        /// <param name="id">The entity's id to convert</param>
        /// <returns>A database generated id or the entity's one</returns>
        private string ConvertId(GeneratorMapping generator, string id)
        {
            switch (generator.Name)
            {
                // Returns the database generated Id instead of the internal one
                case GeneratorMapping.GeneratorType.native:
                case GeneratorMapping.GeneratorType.business:
                case GeneratorMapping.GeneratorType.assigned:
                    if (_NewIds.ContainsKey(id))
                        return _NewIds[id];
                    return id;

                // Returns the internal entity's id
                case GeneratorMapping.GeneratorType.guid:
                    return id;

                case GeneratorMapping.GeneratorType.inherited:
                    return id;
                    throw new NotImplementedException("GeneratorMapping.GeneratorType.inherited"); /// TODO : Implement behaviour for GeneratorType.inherited

                default:
                    return id;
            }
        }
Ejemplo n.º 2
0
        private void AddForeignKeysTo(CreateTableSQLCommand table, string columnName, string ref_column_name, string ref_table_name, GeneratorMapping map_generator, object defaultValue)
        {
            if (!table.ColumnDefinitions.Contains(columnName))
            {
                DbType type = _Dialect.GetDbTypeToPrimaryKey(map_generator);

                SqlObjectModel.LDD.ColumnConstraint constraint = null;
                if (defaultValue != null)
                    constraint = new SqlObjectModel.LDD.DefaultConstraint(new Constant(defaultValue, type), false);
                else
                    constraint = new SqlObjectModel.LDD.ColumnConstraint(true);

                int size = _Dialect.GetSizeToPrimaryKey(map_generator);
                ColumnDefinition column = new ColumnDefinition(columnName, type, size, constraint);
                table.ColumnDefinitions.Add(columnName, column);

                // Add foreign keys here

            }
        }
Ejemplo n.º 3
0
        private void AddPrimaryKeysTo(CreateTableSQLCommand table, string columnName, GeneratorMapping map_generator)
        {
            if (!table.ColumnDefinitions.Contains(columnName))
            {
                DbType type;
                try
                {
                    type = _Dialect.GetDbTypeToPrimaryKey(map_generator);
                }
                catch (NullReferenceException ex)
                {
                    throw new MappingNotFoundException(string.Format("The type is not defined for the field {0} in the table {1}", columnName, table.TableName));
                }
                int size = _Dialect.GetSizeToPrimaryKey(map_generator);
                ColumnDefinition column = new ColumnDefinition(columnName, type, size);
                if (map_generator.Name == GeneratorMapping.GeneratorType.native)
                {
                    int seed = map_generator.GetParam("seed") != null ? Convert.ToInt32(map_generator.GetParam("seed").Value) : 1;
                    int increment = map_generator.GetParam("increment") != null ? Convert.ToInt32(map_generator.GetParam("increment").Value) : 1;
                    column.EnableAutoIncrement(increment, seed);
                }
                table.ColumnDefinitions.Add(columnName, column);

                if (table.PrimaryKey == null)
                    table.PrimaryKey = new PrimaryKey(String.Concat("PK_", table.TableName), new ColumnDefinition[] { column });
                else
                    table.PrimaryKey.Columns.Add(column);
            }
            else
            {
                if (map_generator.Name == GeneratorMapping.GeneratorType.business)
                {
                    ColumnDefinition column = table.ColumnDefinitions[columnName] as ColumnDefinition;
                    if (table.PrimaryKey == null)
                        table.PrimaryKey = new PrimaryKey(String.Concat("PK_", table.TableName), new ColumnDefinition[] { column });
                    else
                    {
                        if (!table.PrimaryKey.Columns.Contains(column))
                            table.PrimaryKey.Columns.Add(column);
                    }
                }
            }
        }
Ejemplo n.º 4
0
 private ParameterDirection GetParameterDirectionToId(GeneratorMapping.GeneratorType generator)
 {
     switch (generator)
     {
         case GeneratorMapping.GeneratorType.native:
             return ParameterDirection.Output;
         case GeneratorMapping.GeneratorType.business:
         case GeneratorMapping.GeneratorType.guid:
         case GeneratorMapping.GeneratorType.assigned:
         default:
             return ParameterDirection.Input;
     }
 }
Ejemplo n.º 5
0
 public override DbType GetDbTypeToPrimaryKey(GeneratorMapping generator)
 {
     if (generator.Name == GeneratorMapping.GeneratorType.guid)
         return DbType.String;
     return base.GetDbTypeToPrimaryKey(generator);
 }
Ejemplo n.º 6
0
        public int GetSizeToPrimaryKey(GeneratorMapping generator)
        {
            switch (generator.Name)
            {
                case GeneratorMapping.GeneratorType.assigned:
                    ParamMapping param = generator.GetParam("size");
                    if (param == null)
                        throw new SqlMapperException("Property 'size' is missing in Id Generator Mapping");
                    return Int32.Parse(param.Value);
                case GeneratorMapping.GeneratorType.guid:
                    return 36;
				case GeneratorMapping.GeneratorType.native:
					param = generator.GetParam("size");
					if (param != null)
						return Int32.Parse(param.Value);
					else
						return 36;
				case GeneratorMapping.GeneratorType.business:
					param = generator.GetParam("size");
					if (param != null)
						return Int32.Parse(param.Value);
					else
						return 0;
				case GeneratorMapping.GeneratorType.inherited:
                    GeneratorMapping tmp = generator;
                    if (generator.Params == null)
                        return 36;
                    if (generator.Params.Length == 1)
                        tmp.Name = GeneratorMapping.GeneratorType.assigned;
                    if (generator.Params.Length > 1)
                        tmp.Name = GeneratorMapping.GeneratorType.native;
                    return GetSizeToPrimaryKey(tmp);
            }

            return 0;
        }
Ejemplo n.º 7
0
		public virtual DbType GetDbTypeToPrimaryKey(GeneratorMapping generator)
		{
			switch(generator.Name)
			{
				case GeneratorMapping.GeneratorType.assigned:
					ParamMapping param = generator.GetParam(DBTYPE);
					return (DbType) Enum.Parse(typeof(DbType), param.Value);

				case GeneratorMapping.GeneratorType.business:
                    ParamMapping pbiz = generator.GetParam(DBTYPE);
					return (DbType) Enum.Parse(typeof(DbType), pbiz.Value);

				case GeneratorMapping.GeneratorType.guid:
					return DbType.AnsiString;

				case GeneratorMapping.GeneratorType.native:
					StringDictionary dico = new StringDictionary();
					foreach(ParamMapping p in generator.Params)
					{
						dico.Add(p.Name, p.Value);
					}
					return GetDbTypeToNativeGenerator(dico);

				case GeneratorMapping.GeneratorType.inherited:
					GeneratorMapping tmp = generator;
					if(generator.Params == null)
						return DbType.AnsiString;
					if(generator.Params.Length == 1)
						tmp.Name = GeneratorMapping.GeneratorType.assigned;
					if(generator.Params.Length > 1)
						tmp.Name = GeneratorMapping.GeneratorType.native;
					return GetDbTypeToPrimaryKey(tmp);
			}

			return DbType.Object;
		}