1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
20 /// Define the possible strategies to set the Primary Key values
23 public enum PrimaryKeyType
26 /// Use Identity column (auto number)
27 /// Note: This force an immediate call to the DB when Create() is called
35 /// Use the HiLo algorithm to get the next value
39 /// Use a sequence and a HiLo algorithm - better performance on Oracle
43 /// Use the hex representation of a unique identifier
47 /// Use the string representation of a unique identifier
51 /// Generate a Guid for the primary key
52 /// Note: You should prefer using GuidComb over this value.
56 /// Generate a Guid in sequence, so it will have better insert performance in the DB.
60 /// Use an identity or sequence if supported by the database, otherwise, use the HiLo algorithm
64 /// The primary key value is always assigned.
65 /// Note: using this you will lose the ability to call Save(), and will need to call Create() or Update()
70 /// This is a foreign key to another table
74 /// Returns a <c>Int64</c> constructed from the system
75 /// time and a counter value.
78 /// Not safe for use in a clustser
82 /// Returns a <c>Int64</c>, constructed by counting from
83 /// the maximum primary key value at startup.
86 /// Not safe for use in a cluster
90 /// A custom generator will be provided. See <see cref="PrimaryKeyAttribute.CustomGenerator"/>
96 /// Indicates the property which is the primary key.
99 /// public class Blog : ActiveRecordBase
103 /// [PrimaryKey(PrimaryKeyType.Native)]
106 /// get { return _id; }
107 /// set { _id = value; }
109 /// </code></example>
110 [AttributeUsage(AttributeTargets
.Property
, AllowMultiple
=false), Serializable
]
111 public class PrimaryKeyAttribute
: WithAccessAttribute
113 private PrimaryKeyType generator
= PrimaryKeyType
.Native
;
114 private Type customGenerator
;
115 private String column
;
116 private String unsavedValue
;
117 private String sequenceName
;
119 private String _params
;
121 private bool isOverride
;
124 /// Initializes a new instance of the <see cref="PrimaryKeyAttribute"/> class.
126 public PrimaryKeyAttribute() : this(PrimaryKeyType
.Native
)
131 /// Initializes a new instance of the <see cref="PrimaryKeyAttribute"/> class.
133 /// <param name="customGenerator">A custom identifier
134 /// generator (that implements <see cref="NHibernate.Id.IIdentifierGenerator"/>).</param>
135 public PrimaryKeyAttribute(Type customGenerator
) : this(PrimaryKeyType
.Custom
)
137 this.customGenerator
= customGenerator
;
141 /// Initializes a new instance of the <see cref="PrimaryKeyAttribute"/> class.
143 /// <param name="generator">The generator.</param>
144 public PrimaryKeyAttribute(PrimaryKeyType generator
)
146 this.generator
= generator
;
150 /// Initializes a new instance of the <see cref="PrimaryKeyAttribute"/> class.
152 /// <param name="generator">The generator.</param>
153 /// <param name="column">The PK column.</param>
154 public PrimaryKeyAttribute(PrimaryKeyType generator
, String column
) : this(generator
)
156 this.column
= column
;
160 /// Initializes a new instance of the <see cref="PrimaryKeyAttribute"/> class.
162 /// <param name="column">The PK column.</param>
163 public PrimaryKeyAttribute(string column
)
165 this.column
= column
;
169 /// Gets or sets the generator.
171 /// <value>The generator.</value>
172 public PrimaryKeyType Generator
174 get { return generator; }
175 set { generator = value; }
179 /// Gets or sets the column name
181 /// <value>The column.</value>
184 get { return column; }
185 set { column = value; }
189 /// Gets or sets the unsaved value.
191 /// <value>The unsaved value.</value>
192 public String UnsavedValue
194 get { return unsavedValue; }
195 set { unsavedValue = value; }
199 /// Gets or sets the name of the sequence.
201 /// <value>The name of the sequence.</value>
202 public String SequenceName
204 get { return sequenceName; }
205 set { sequenceName = value; }
209 /// Gets or sets the type of the column.
211 /// <value>The type of the column.</value>
212 public String ColumnType
215 set { type = value; }
219 /// Gets or sets the length of values in the column
221 /// <value>The length.</value>
224 get { return length; }
225 set { length = value; }
229 /// Gets or sets the custom generator.
230 /// The generator must implement <see cref="NHibernate.Id.IIdentifierGenerator"/>
232 /// <value>The custom generator type.</value>
233 public Type CustomGenerator
235 get { return customGenerator; }
236 set { customGenerator = value; }
240 /// Comma separated value of parameters to the generator
244 get { return _params; }
245 set { _params = value; }
249 /// Set to <c>true</c> if this primary key overrides a primary key in a base class
251 public bool IsOverride
253 get { return isOverride; }
254 set { isOverride = value; }