Clover.NET coverage report - Coverage for s2dao.net

Coverage timestamp: 2006年5月18日 15:09:15

File Stats: LOC: 534   Methods: 35
NCLOC: 458 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Seasar.Dao.Impl\BeanMetaDataImpl.cs 58.5% 62.8% 77.1% 63.3%
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 44 public BeanMetaDataImpl(Type beanType, IDatabaseMetaData dbMetaData,
49   IDbms dbms) : this(beanType, dbMetaData, dbms, false)
50   {
51   }
52  
53 44 public BeanMetaDataImpl(Type beanType, IDatabaseMetaData dbMetaData,
54   IDbms dbms, bool relation)
55   {
56 44 BeanType = beanType;
57 44 this.relation = relation;
58 44 SetupTableName(beanType);
59 44 SetupVersionNoPropertyName(beanType);
60 44 SetupTimestampPropertyName(beanType);
61 44 SetupProperty(beanType, dbMetaData, dbms);
62 44 SetupDatabaseMetaData(beanType, dbMetaData, dbms);
63 44 SetupPropertiesByColumnName();
64   }
65  
66   #region IBeanMetaData メンバ
67  
68   public string TableName
69   {
70 90 get
71   {
72 90 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 33 get
87   {
88 33 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 64 get
103   {
104 64 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 33 get
119   {
120 33 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 64 get
135   {
136 64 return HasPropertyType(timestampPropertyName);
137   }
138   }
139  
140 0 public string ConvertFullColumnName(string alias)
141   {
142   if(HasPropertyTypeByColumnName(alias))
143   return tableName + "." + alias;
144   int index = alias.LastIndexOf('_');
145   if(index < 0)
146   throw new ColumnNotFoundRuntimeException(tableName, alias);
147   string columnName = alias.Substring(0, index);
148   string relnoStr = alias.Substring(index + 1);
149   int relno = -1;
150   try
151   {
152   relno = int.Parse(relnoStr);
153   }
154   catch(Exception)
155   {
156   throw new ColumnNotFoundRuntimeException(tableName, alias);
157   }
158   IRelationPropertyType rpt = GetRelationPropertyType(relno);
159   if(!rpt.BeanMetaData.HasPropertyTypeByColumnName(columnName))
160   throw new ColumnNotFoundRuntimeException(tableName, alias);
161   return rpt.PropertyName + "." + columnName;
162   }
163  
164 0 public Seasar.Extension.ADO.IPropertyType GetPropertyTypeByAliasName(string aliasName)
165   {
166   if(HasPropertyTypeByColumnName(aliasName))
167   return GetPropertyTypeByColumnName(aliasName);
168   int index = aliasName.LastIndexOf('_');
169   if(index < 0)
170   throw new ColumnNotFoundRuntimeException(tableName, aliasName);
171   string columnName = aliasName.Substring(0, index);
172   string relnoStr = aliasName.Substring(index + 1);
173   int relno = -1;
174   try
175   {
176   relno = int.Parse(relnoStr);
177   }
178   catch(Exception)
179   {
180   throw new ColumnNotFoundRuntimeException(tableName, columnName);
181   }
182   IRelationPropertyType rpt = GetRelationPropertyType(relno);
183   if(!rpt.BeanMetaData.HasPropertyTypeByColumnName(columnName))
184   throw new ColumnNotFoundRuntimeException(tableName, aliasName);
185   return rpt.BeanMetaData.GetPropertyTypeByAliasName(columnName);
186   }
187  
188 69 public Seasar.Extension.ADO.IPropertyType GetPropertyTypeByColumnName(string columnName)
189   {
190 69 IPropertyType propertyType = (IPropertyType) propertyTypesByColumnName[columnName];
191 69 if(propertyType == null)
192 0 throw new ColumnNotFoundRuntimeException(tableName, columnName);
193  
194 69 return propertyType;
195   }
196  
197 0 public bool HasPropertyTypeByColumnName(string columnName)
198   {
199   return propertyTypesByColumnName[columnName] != null;
200   }
201  
202 0 public bool HasPropertyTypeByAliasName(string aliasName)
203   {
204   if(HasPropertyTypeByColumnName(aliasName)) return true;
205   int index = aliasName.LastIndexOf('_');
206   if(index < 0) return false;
207   string columnName = aliasName.Substring(0, index);
208   string relnoStr = aliasName.Substring(index + 1);
209   int relno = -1;
210   try
211   {
212   relno = int.Parse(relnoStr);
213   }
214   catch(Exception)
215   {
216   return false;
217   }
218   if(relno >= RelationPropertyTypeSize) return false;
219   IRelationPropertyType rpt = GetRelationPropertyType(relno);
220   return rpt.BeanMetaData.HasPropertyTypeByColumnName(columnName);
221   }
222  
223   public int RelationPropertyTypeSize
224   {
225 45 get
226   {
227 45 return relationProeprtyTypes.Count;
228   }
229   }
230  
231 53 public IRelationPropertyType GetRelationPropertyType(int index)
232   {
233 53 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 291 get
251   {
252 291 return primaryKeys.Length;
253   }
254   }
255  
256 181 public string GetPrimaryKey(int index)
257   {
258 181 return primaryKeys[index];
259   }
260  
261   public IIdentifierGenerator IdentifierGenerator
262   {
263 336 get
264   {
265 336 return identifierGenerator;
266   }
267   }
268  
269   public string AutoSelectList
270   {
271 41 get
272   {
273 41 lock(this)
274   {
275 41 if(autoSelectList != null)
276 26 return autoSelectList;
277 15 SetupAutoSelectList();
278 15 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 44 protected void SetupTableName(Type beanType)
294   {
295 44 TableAttribute attr = AttributeUtil.GetTableAttribute(beanType);
296 44 if(attr != null)
297 42 tableName = attr.TableName;
298   else
299 2 tableName = beanType.Name;
300   }
301  
302 44 protected void SetupVersionNoPropertyName(Type beanType)
303   {
304 44 VersionNoPropertyAttribute attr = AttributeUtil.GetVersionNoPropertyAttribute(beanType);
305 0 if(attr != null) versionNoPropertyName = attr.PropertyName;
306  
307 44 int i = 0;
308  
309 44 do
310   {
311 44 versionNoBindingName = versionNoPropertyName + i++;
312 44 } while (HasPropertyType(versionNoBindingName));
313  
314   }
315  
316 44 protected void SetupTimestampPropertyName(Type beanType)
317   {
318 44 TimestampPropertyAttribute attr = AttributeUtil.GetTimestampPropertyAttribute(beanType);
319 0 if(attr != null) timestampPropertyName = attr.PropertyName;
320  
321 44 int i = 0;
322  
323 44 do
324   {
325 44 timestampBindingName = timestampPropertyName + i++;
326 44 } while (HasPropertyType(timestampBindingName));
327  
328 44 if(timestampBindingName.Equals(versionNoBindingName))
329 0 timestampBindingName = timestampPropertyName + i++;
330   }
331  
332 44 protected void SetupProperty(Type beanType, IDatabaseMetaData dbMetaData,IDbms dbms)
333   {
334 44 foreach(PropertyInfo pi in beanType.GetProperties())
335   {
336 259 IPropertyType pt = null;
337 259 RelnoAttribute relnoAttr = AttributeUtil.GetRelnoAttribute(pi);
338 259 if(relnoAttr != null)
339   {
340 15 if(!relation)
341   {
342 15 IRelationPropertyType rpt = CreateRelationPropertyType(
343   beanType, pi, relnoAttr, dbMetaData, dbms);
344 15 AddRelationPropertyType(rpt);
345   }
346   }
347   else
348   {
349 244 pt = CreatePropertyType(pi);
350 244 AddPropertyType(pt);
351   }
352 259 if(IdentifierGenerator == null)
353   {
354 257 IDAttribute idAttr = AttributeUtil.GetIDAttribute(pi);
355 257 if(idAttr != null)
356   {
357 2 identifierGenerator = IdentifierGeneratorFactory.CreateIdentifierGenerator(
358   pi.Name, dbms, idAttr);
359 2 primaryKeys = new string[] { pt.ColumnName };
360 2 pt.IsPrimaryKey = true;
361   }
362   }
363   }
364   }
365  
366 44 protected void SetupDatabaseMetaData(Type beanType,
367   IDatabaseMetaData dbMetaData, IDbms dbms)
368   {
369 44 SetupPropertyPersistentAndColumnName(beanType, dbMetaData);
370 44 SetupPrimaryKey(dbMetaData, dbms);
371   }
372  
373 44 protected void SetupPrimaryKey(IDatabaseMetaData dbMetaData, IDbms dbms)
374   {
375 44 if(IdentifierGenerator == null)
376   {
377 42 ArrayList pkeyList = new ArrayList();
378 42 IList primaryKeySet = dbMetaData.GetPrimaryKeySet(tableName);
379 282 for(int i = 0; i < PropertyTypeSize; ++i)
380   {
381 240 IPropertyType pt = GetPropertyType(i);
382 240 if(primaryKeySet.Contains(pt.ColumnName))
383   {
384 42 pt.IsPrimaryKey = true;
385 42 pkeyList.Add(pt.ColumnName);
386   }
387   else
388   {
389 198 pt.IsPrimaryKey = false;
390   }
391   }
392 42 primaryKeys = (string[]) pkeyList.ToArray(typeof(string));
393 42 identifierGenerator = IdentifierGeneratorFactory
394   .CreateIdentifierGenerator(null, dbms);
395   }
396   }
397  
398 44 protected void SetupPropertyPersistentAndColumnName(Type beanType,
399   IDatabaseMetaData dbMetaData)
400   {
401 44 IList columnSet = dbMetaData.GetColumnSet(tableName);
402 44 if(columnSet == null || columnSet.Count == 0)
403   {
404 0 logger.Log("WDAO0002", new object[] { tableName });
405   } else {
406 334 for(IEnumerator enu = columnSet.GetEnumerator(); enu.MoveNext();)
407   {
408 290 string columnName = (string) enu.Current;
409 290 string columnName2 = columnName.Replace("_", "");
410 1312 for(int i = 0; i < PropertyTypeSize; ++i)
411   {
412 1217 IPropertyType pt = GetPropertyType(i);
413 1217 if(string.Compare(pt.ColumnName, columnName2, true) == 0)
414   {
415 195 pt.ColumnName = columnName;
416 195 break;
417   }
418   }
419   }
420   }
421 44 NoPersistentPropsAttribute noPersistentPropsAttr =
422   AttributeUtil.GetNoPersistentPropsAttribute(beanType);
423 44 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 288 for(int i = 0; i < PropertyTypeSize; ++i)
434   {
435 244 IPropertyType pt = GetPropertyType(i);
436 244 if(!columnSet.Contains(pt.ColumnName))
437 49 pt.IsPersistent = false;
438   }
439   }
440   }
441  
442 44 protected void SetupPropertiesByColumnName()
443   {
444 288 for(int i = 0; i < PropertyTypeSize; ++i)
445   {
446 244 IPropertyType pt = GetPropertyType(i);
447 244 propertyTypesByColumnName[pt.ColumnName] = pt;
448   }
449   }
450  
451 15 protected IRelationPropertyType CreateRelationPropertyType(Type beanType,
452   PropertyInfo propertyInfo, RelnoAttribute relnoAttr,
453   IDatabaseMetaData dbMetaData, IDbms dbms)
454   {
455 15 string[] myKeys = new string[0];
456 15 string[] yourKeys = new string[0];
457 15 RelkeysAttribute relkeysAttr =
458   AttributeUtil.GetRelkeysAttribute(propertyInfo);
459 15 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 15 IRelationPropertyType rpt = new RelationPropertyTypeImpl(propertyInfo,
482   relnoAttr.Relno, myKeys, yourKeys, dbMetaData, dbms);
483 15 return rpt;
484   }
485  
486 15 protected void AddRelationPropertyType(IRelationPropertyType rpt)
487   {
488 30 for(int i = relationProeprtyTypes.Count; i <= rpt.RelationNo; ++i)
489   {
490 15 relationProeprtyTypes.Add(null);
491   }
492 15 relationProeprtyTypes[rpt.RelationNo] = rpt;
493   }
494  
495 15 protected void SetupAutoSelectList()
496   {
497 15 StringBuilder buf = new StringBuilder(100);
498 15 buf.Append("SELECT ");
499 96 for(int i = 0; i < PropertyTypeSize; ++i)
500   {
501 81 IPropertyType pt = GetPropertyType(i);
502 81 if(pt.IsPersistent)
503   {
504 67 buf.Append(tableName);
505 67 buf.Append(".");
506 67 buf.Append(pt.ColumnName);
507 67 buf.Append(", ");
508   }
509   }
510 15 foreach(IRelationPropertyType rpt in relationProeprtyTypes)
511   {
512 7 IBeanMetaData bmd = rpt.BeanMetaData;
513 42 for(int i = 0; i < bmd.PropertyTypeSize; ++i)
514   {
515 35 IPropertyType pt = bmd.GetPropertyType(i);
516 35 if(pt.IsPersistent)
517   {
518 28 string columnName = pt.ColumnName;
519 28 buf.Append(rpt.PropertyName);
520 28 buf.Append(".");
521 28 buf.Append(columnName);
522 28 buf.Append(" AS ");
523 28 buf.Append(pt.ColumnName).Append("_");
524 28 buf.Append(rpt.RelationNo);
525 28 buf.Append(", ");
526   }
527   }
528   }
529 15 buf.Length = buf.Length - 2;
530 15 autoSelectList = buf.ToString();
531   }
532   }
533   }
534