Clover.NET coverage report - Coverage for s2dao.net

Coverage timestamp: 2006年5月30日 11:48:56

File Stats: LOC: 534   Methods: 35
NCLOC: 458 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Seasar.Dao.Impl\BeanMetaDataImpl.cs 75.6% 82.7% 88.6% 81.5%
coverage coverage
1   #region Copyright
2   /*
3   * Copyright 2005 the Seasar Foundation and the Others.
4   *
5   * Licensed under the Apache License, Version 2.0 (the "License");
6   * you may not use this file except in compliance with the License.
7   * You may obtain a copy of the License at
8   *
9   * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14   * either express or implied. See the License for the specific language
15   * governing permissions and limitations under the License.
16   */
17   #endregion
18  
19   using System;
20   using System.Collections;
21   using System.Reflection;
22   using System.Text;
23   using Seasar.Dao.Attrs;
24   using Seasar.Dao.Id;
25   using Seasar.Dao.Impl;
26   using Seasar.Extension.ADO;
27   using Seasar.Framework.Beans;
28   using Seasar.Framework.Log;
29  
30   namespace Seasar.Dao.Impl
31   {
32   public class BeanMetaDataImpl : DtoMetaDataImpl, IBeanMetaData
33   {
34   private static Logger logger = Logger.GetLogger(typeof(BeanMetaDataImpl));
35   private string tableName;
36   private Hashtable propertyTypesByColumnName = new Hashtable(
37   CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
38   private ArrayList relationProeprtyTypes = new ArrayList();
39   private string[] primaryKeys = new string[0];
40   private string autoSelectList;
41   private bool relation;
42   private IIdentifierGenerator identifierGenerator;
43   private string versionNoPropertyName = "VersionNo";
44   private string timestampPropertyName = "Timestamp";
45   private string versionNoBindingName;
46   private string timestampBindingName;
47  
48 62 public BeanMetaDataImpl(Type beanType, IDatabaseMetaData dbMetaData,
49   IDbms dbms) : this(beanType, dbMetaData, dbms, false)
50   {
51   }
52  
53 62 public BeanMetaDataImpl(Type beanType, IDatabaseMetaData dbMetaData,
54   IDbms dbms, bool relation)
55   {
56 62 BeanType = beanType;
57 62 this.relation = relation;
58 62 SetupTableName(beanType);
59 62 SetupVersionNoPropertyName(beanType);
60 62 SetupTimestampPropertyName(beanType);
61 62 SetupProperty(beanType, dbMetaData, dbms);
62 62 SetupDatabaseMetaData(beanType, dbMetaData, dbms);
63 62 SetupPropertiesByColumnName();
64   }
65  
66   #region IBeanMetaData メンバ
67  
68   public string TableName
69   {
70 102 get
71   {
72 102 return tableName;
73   }
74   }
75  
76   public Seasar.Extension.ADO.IPropertyType VersionNoPropertyType
77   {
78 14 get
79   {
80 14 return GetPropertyType(versionNoPropertyName);
81   }
82   }
83  
84   public string VersionNoPropertyName
85   {
86 39 get
87   {
88 39 return versionNoPropertyName;
89   }
90   }
91  
92   public string VersionNoBindingName
93   {
94 12 get
95   {
96 12 return versionNoBindingName;
97   }
98   }
99  
100   public bool HasVersionNoPropertyType
101   {
102 66 get
103   {
104 66 return HasPropertyType(versionNoPropertyName);
105   }
106   }
107  
108 0 public Seasar.Extension.ADO.IPropertyType TimestampPropertyType
109   {
110   get
111   {
112   return GetPropertyType(timestampPropertyName);
113   }
114   }
115  
116   public string TimestampPropertyName
117   {
118 39 get
119   {
120 39 return timestampPropertyName;
121   }
122   }
123  
124 0 public string TimestampBindingName
125   {
126   get
127   {
128   return timestampBindingName;
129   }
130   }
131  
132   public bool HasTimestampPropertyType
133   {
134 66 get
135   {
136 66 return HasPropertyType(timestampPropertyName);
137   }
138   }
139  
140 6 public string ConvertFullColumnName(string alias)
141   {
142 6 if(HasPropertyTypeByColumnName(alias))
143 5 return tableName + "." + alias;
144 1 int index = alias.LastIndexOf('_');
145 1 if(index < 0)
146 0 throw new ColumnNotFoundRuntimeException(tableName, alias);
147 1 string columnName = alias.Substring(0, index);
148 1 string relnoStr = alias.Substring(index + 1);
149 1 int relno = -1;
150 1 try
151   {
152 1 relno = int.Parse(relnoStr);
153   }
154   catch(Exception)
155   {
156 0 throw new ColumnNotFoundRuntimeException(tableName, alias);
157   }
158 1 IRelationPropertyType rpt = GetRelationPropertyType(relno);
159 1 if(!rpt.BeanMetaData.HasPropertyTypeByColumnName(columnName))
160 0 throw new ColumnNotFoundRuntimeException(tableName, alias);
161 1 return rpt.PropertyName + "." + columnName;
162   }
163  
164 3 public Seasar.Extension.ADO.IPropertyType GetPropertyTypeByAliasName(string aliasName)
165   {
166 3 if(HasPropertyTypeByColumnName(aliasName))
167 2 return GetPropertyTypeByColumnName(aliasName);
168 1 int index = aliasName.LastIndexOf('_');
169 1 if(index < 0)
170 0 throw new ColumnNotFoundRuntimeException(tableName, aliasName);
171 1 string columnName = aliasName.Substring(0, index);
172 1 string relnoStr = aliasName.Substring(index + 1);
173 1 int relno = -1;
174 1 try
175   {
176 1 relno = int.Parse(relnoStr);
177   }
178   catch(Exception)
179   {
180 0 throw new ColumnNotFoundRuntimeException(tableName, columnName);
181   }
182 1 IRelationPropertyType rpt = GetRelationPropertyType(relno);
183 1 if(!rpt.BeanMetaData.HasPropertyTypeByColumnName(columnName))
184 0 throw new ColumnNotFoundRuntimeException(tableName, aliasName);
185 1 return rpt.BeanMetaData.GetPropertyTypeByAliasName(columnName);
186   }
187  
188 71 public Seasar.Extension.ADO.IPropertyType GetPropertyTypeByColumnName(string columnName)
189   {
190 71 IPropertyType propertyType = (IPropertyType) propertyTypesByColumnName[columnName];
191 71 if(propertyType == null)
192 0 throw new ColumnNotFoundRuntimeException(tableName, columnName);
193  
194 71 return propertyType;
195   }
196  
197 18 public bool HasPropertyTypeByColumnName(string columnName)
198   {
199 18 return propertyTypesByColumnName[columnName] != null;
200   }
201  
202 5 public bool HasPropertyTypeByAliasName(string aliasName)
203   {
204 5 if(HasPropertyTypeByColumnName(aliasName)) return true;
205 4 int index = aliasName.LastIndexOf('_');
206 4 if(index < 0) return false;
207 3 string columnName = aliasName.Substring(0, index);
208 3 string relnoStr = aliasName.Substring(index + 1);
209 3 int relno = -1;
210 3 try
211   {
212 3 relno = int.Parse(relnoStr);
213   }
214   catch(Exception)
215   {
216 0 return false;
217   }
218 3 if(relno >= RelationPropertyTypeSize) return false;
219 2 IRelationPropertyType rpt = GetRelationPropertyType(relno);
220 2 return rpt.BeanMetaData.HasPropertyTypeByColumnName(columnName);
221   }
222  
223   public int RelationPropertyTypeSize
224   {
225 57 get
226   {
227 57 return relationProeprtyTypes.Count;
228   }
229   }
230  
231 57 public IRelationPropertyType GetRelationPropertyType(int index)
232   {
233 57 return (IRelationPropertyType) relationProeprtyTypes[index];
234   }
235  
236 0 IRelationPropertyType Seasar.Dao.IBeanMetaData.GetRelationPropertyType(string propertyName)
237   {
238   for(int i = 0; i < RelationPropertyTypeSize; ++i)
239   {
240   IRelationPropertyType rpt = (IRelationPropertyType) relationProeprtyTypes[i];
241   if(rpt != null
242   && string.Compare(rpt.PropertyName, propertyName, true) == 0)
243   return rpt;
244   }
245   throw new PropertyNotFoundRuntimeException(BeanType, propertyName);
246   }
247  
248   public int PrimaryKeySize
249   {
250 297 get
251   {
252 297 return primaryKeys.Length;
253   }
254   }
255  
256 186 public string GetPrimaryKey(int index)
257   {
258 186 return primaryKeys[index];
259   }
260  
261   public IIdentifierGenerator IdentifierGenerator
262   {
263 457 get
264   {
265 457 return identifierGenerator;
266   }
267   }
268  
269   public string AutoSelectList
270   {
271 55 get
272   {
273 55 lock(this)
274   {
275 55 if(autoSelectList != null)
276 32 return autoSelectList;
277 23 SetupAutoSelectList();
278 23 return autoSelectList;
279   }
280   }
281   }
282  
283 0 public bool IsRelation
284   {
285   get
286   {
287   return relation;
288   }
289   }
290  
291   #endregion
292  
293 62 protected void SetupTableName(Type beanType)
294   {
295 62 TableAttribute attr = AttributeUtil.GetTableAttribute(beanType);
296 62 if(attr != null)
297 59 tableName = attr.TableName;
298   else
299 3 tableName = beanType.Name;
300   }
301  
302 62 protected void SetupVersionNoPropertyName(Type beanType)
303   {
304 62 VersionNoPropertyAttribute attr = AttributeUtil.GetVersionNoPropertyAttribute(beanType);
305 0 if(attr != null) versionNoPropertyName = attr.PropertyName;
306  
307 62 int i = 0;
308  
309 62 do
310   {
311 62 versionNoBindingName = versionNoPropertyName + i++;
312 62 } while (HasPropertyType(versionNoBindingName));
313  
314   }
315  
316 62 protected void SetupTimestampPropertyName(Type beanType)
317   {
318 62 TimestampPropertyAttribute attr = AttributeUtil.GetTimestampPropertyAttribute(beanType);
319 0 if(attr != null) timestampPropertyName = attr.PropertyName;
320  
321 62 int i = 0;
322  
323 62 do
324   {
325 62 timestampBindingName = timestampPropertyName + i++;
326 62 } while (HasPropertyType(timestampBindingName));
327  
328 62 if(timestampBindingName.Equals(versionNoBindingName))
329 0 timestampBindingName = timestampPropertyName + i++;
330   }
331  
332 62 protected void SetupProperty(Type beanType, IDatabaseMetaData dbMetaData,IDbms dbms)
333   {
334 62 foreach(PropertyInfo pi in beanType.GetProperties())
335   {
336 354 IPropertyType pt = null;
337 354 RelnoAttribute relnoAttr = AttributeUtil.GetRelnoAttribute(pi);
338 354 if(relnoAttr != null)
339   {
340 20 if(!relation)
341   {
342 20 IRelationPropertyType rpt = CreateRelationPropertyType(
343   beanType, pi, relnoAttr, dbMetaData, dbms);
344 20 AddRelationPropertyType(rpt);
345   }
346   }
347   else
348   {
349 334 pt = CreatePropertyType(pi);
350 334 AddPropertyType(pt);
351   }
352 354 if(IdentifierGenerator == null)
353   {
354 351 IDAttribute idAttr = AttributeUtil.GetIDAttribute(pi);
355 351 if(idAttr != null)
356   {
357 3 identifierGenerator = IdentifierGeneratorFactory.CreateIdentifierGenerator(
358   pi.Name, dbms, idAttr);
359 3 primaryKeys = new string[] { pt.ColumnName };
360 3 pt.IsPrimaryKey = true;
361   }
362   }
363   }
364   }
365  
366 62 protected void SetupDatabaseMetaData(Type beanType,
367   IDatabaseMetaData dbMetaData, IDbms dbms)
368   {
369 62 SetupPropertyPersistentAndColumnName(beanType, dbMetaData);
370 62 SetupPrimaryKey(dbMetaData, dbms);
371   }
372  
373 62 protected void SetupPrimaryKey(IDatabaseMetaData dbMetaData, IDbms dbms)
374   {
375 62 if(IdentifierGenerator == null)
376   {
377 59 ArrayList pkeyList = new ArrayList();
378 59 IList primaryKeySet = dbMetaData.GetPrimaryKeySet(tableName);
379 387 for(int i = 0; i < PropertyTypeSize; ++i)
380   {
381 328 IPropertyType pt = GetPropertyType(i);
382 328 if(primaryKeySet.Contains(pt.ColumnName))
383   {
384 59 pt.IsPrimaryKey = true;
385 59 pkeyList.Add(pt.ColumnName);
386   }
387   else
388   {
389 269 pt.IsPrimaryKey = false;
390   }
391   }
392 59 primaryKeys = (string[]) pkeyList.ToArray(typeof(string));
393 59 identifierGenerator = IdentifierGeneratorFactory
394   .CreateIdentifierGenerator(null, dbms);
395   }
396   }
397  
398 62 protected void SetupPropertyPersistentAndColumnName(Type beanType,
399   IDatabaseMetaData dbMetaData)
400   {
401 62 IList columnSet = dbMetaData.GetColumnSet(tableName);
402 62 if(columnSet == null || columnSet.Count == 0)
403   {
404 0 logger.Log("WDAO0002", new object[] { tableName });
405   } else {
406 483 for(IEnumerator enu = columnSet.GetEnumerator(); enu.MoveNext();)
407   {
408 421 string columnName = (string) enu.Current;
409 421 string columnName2 = columnName.Replace("_", "");
410 1866 for(int i = 0; i < PropertyTypeSize; ++i)
411   {
412 1714 IPropertyType pt = GetPropertyType(i);
413 1714 if(string.Compare(pt.ColumnName, columnName2, true) == 0)
414   {
415 269 pt.ColumnName = columnName;
416 269 break;
417   }
418   }
419   }
420   }
421 62 NoPersistentPropsAttribute noPersistentPropsAttr =
422   AttributeUtil.GetNoPersistentPropsAttribute(beanType);
423 62 if(noPersistentPropsAttr != null)
424   {
425 0 foreach(string prop in noPersistentPropsAttr.Props)
426   {
427   IPropertyType pt = GetPropertyType(prop);
428   pt.IsPersistent = false;
429   }
430   }
431   else
432   {
433 396 for(int i = 0; i < PropertyTypeSize; ++i)
434   {
435 334 IPropertyType pt = GetPropertyType(i);
436 334 if(!columnSet.Contains(pt.ColumnName))
437 65 pt.IsPersistent = false;
438   }
439   }
440   }
441  
442 62 protected void SetupPropertiesByColumnName()
443   {
444 396 for(int i = 0; i < PropertyTypeSize; ++i)
445   {
446 334 IPropertyType pt = GetPropertyType(i);
447 334 propertyTypesByColumnName[pt.ColumnName] = pt;
448   }
449   }
450  
451 20 protected IRelationPropertyType CreateRelationPropertyType(Type beanType,
452   PropertyInfo propertyInfo, RelnoAttribute relnoAttr,
453   IDatabaseMetaData dbMetaData, IDbms dbms)
454   {
455 20 string[] myKeys = new string[0];
456 20 string[] yourKeys = new string[0];
457 20 RelkeysAttribute relkeysAttr =
458   AttributeUtil.GetRelkeysAttribute(propertyInfo);
459 20 if(relkeysAttr != null)
460   {
461 0 ArrayList myKeyList = new ArrayList();
462 0 ArrayList yourKeyList = new ArrayList();
463 0 foreach(string token in relkeysAttr.Relkeys.Split(
464   '\t', '\n', '\r', '\f', ','))
465   {
466   int index = token.IndexOf(':');
467   if(index > 0)
468   {
469   myKeyList.Add(token.Substring(0, index));
470   yourKeyList.Add(token.Substring(index + 1));
471   }
472   else
473   {
474   myKeyList.Add(token);
475   yourKeyList.Add(token);
476   }
477   }
478 0 myKeys = (string[]) myKeyList.ToArray(typeof(string));
479 0 yourKeys = (string[]) yourKeyList.ToArray(typeof(string));
480   }
481 20 IRelationPropertyType rpt = new RelationPropertyTypeImpl(propertyInfo,
482   relnoAttr.Relno, myKeys, yourKeys, dbMetaData, dbms);
483 20 return rpt;
484   }
485  
486 20 protected void AddRelationPropertyType(IRelationPropertyType rpt)
487   {
488 40 for(int i = relationProeprtyTypes.Count; i <= rpt.RelationNo; ++i)
489   {
490 20 relationProeprtyTypes.Add(null);
491   }
492 20 relationProeprtyTypes[rpt.RelationNo] = rpt;
493   }
494  
495 23 protected void SetupAutoSelectList()
496   {
497 23 StringBuilder buf = new StringBuilder(100);
498 23 buf.Append("SELECT ");
499 135 for(int i = 0; i < PropertyTypeSize; ++i)
500   {
501 112 IPropertyType pt = GetPropertyType(i);
502 112 if(pt.IsPersistent)
503   {
504 95 buf.Append(tableName);
505 95 buf.Append(".");
506 95 buf.Append(pt.ColumnName);
507 95 buf.Append(", ");
508   }
509   }
510 23 foreach(IRelationPropertyType rpt in relationProeprtyTypes)
511   {
512 8 IBeanMetaData bmd = rpt.BeanMetaData;
513 48 for(int i = 0; i < bmd.PropertyTypeSize; ++i)
514   {
515 40 IPropertyType pt = bmd.GetPropertyType(i);
516 40 if(pt.IsPersistent)
517   {
518 32 string columnName = pt.ColumnName;
519 32 buf.Append(rpt.PropertyName);
520 32 buf.Append(".");
521 32 buf.Append(columnName);
522 32 buf.Append(" AS ");
523 32 buf.Append(pt.ColumnName).Append("_");
524 32 buf.Append(rpt.RelationNo);
525 32 buf.Append(", ");
526   }
527   }
528   }
529 23 buf.Length = buf.Length - 2;
530 23 autoSelectList = buf.ToString();
531   }
532   }
533   }
534