C#发现之旅:基于反射和动态编译的快速ORM框架(下)
2010-09-30 21:08:22 来源:WEB开发网对于字符串类型的属性,其默认值就是“DBNull”。而对于其他的整数或者日期类型的属性,并没有默认值,因此是无条件的插入到数据库中。
我们使用以下的代码来生成上述代码文本
myWriter.WriteLine("public override int FillInsertCommand( System.Data.IDbCommand cmd , object objRecord )");
myWriter.BeginGroup("{");
myWriter.WriteLine("if( cmd == null ) throw new ArgumentNullException(\"cmd\");");
myWriter.WriteLine("if( objRecord == null ) throw new ArgumentNullException(\"objRecord\");");
myWriter.WriteLine(RecordType.FullName + " myRecord = objRecord as " + RecordType.FullName + " ;");
myWriter.WriteLine("if( myRecord == null ) throw new ArgumentException(\"must type '" + RecordType.FullName + "' \");");
myWriter.WriteLine("System.Collections.ArrayList myFieldNames = new System.Collections.ArrayList();");
myWriter.WriteLine("System.Collections.ArrayList myValues = new System.Collections.ArrayList();");
for (int iCount = 0; iCount < ps.Length; iCount++)
{
System.Reflection.PropertyInfo p = ps[iCount];
if (p.CanRead == false)
{
continue;
}
BindFieldAttribute fa = (BindFieldAttribute)Attribute.GetCustomAttribute(
p, typeof(BindFieldAttribute));
string FieldName = GetBindFieldName(p);
myWriter.WriteLine("");
Type pt = p.PropertyType;
object DefaultValue = this.GetDefaultValue(p);
if (pt.Equals(typeof(string)))
{
myWriter.WriteLine("if( myRecord." + p.Name + " != null && myRecord." + p.Name + ".Length != 0 )");
myWriter.BeginGroup("{");
myWriter.WriteLine("myFieldNames.Add( \"" + FieldName + "\" );");
myWriter.WriteLine("myValues.Add( myRecord." + p.Name + " );");
myWriter.EndGroup("}");
}
else if (pt.Equals(typeof(DateTime)))
{
myWriter.WriteLine("myFieldNames.Add( \"" + FieldName + "\" );");
if (fa.WriteFormat != null && fa.WriteFormat.Length > 0)
{
myWriter.WriteLine("myValues.Add( myRecord." + p.Name + ".ToString(\"" + fa.WriteFormat + "\") );");
}
else
{
myWriter.WriteLine("myValues.Add( myRecord." + p.Name + ".ToString(\"yyyy-MM-dd HH:mm:ss\") );");
}
}
else
{
myWriter.WriteLine("myFieldNames.Add( \"" + FieldName + "\" );");
myWriter.WriteLine("myValues.Add( myRecord." + p.Name + " );");
}
}//for
myWriter.WriteLine("");
myWriter.WriteLine("if( myFieldNames.Count == 0 ) return 0 ;");
myWriter.WriteLine("cmd.Parameters.Clear() ;");
myWriter.WriteLine("System.Text.StringBuilder mySQL = new System.Text.StringBuilder();");
myWriter.WriteLine("mySQL.Append( \"Insert Into " + TableName + " ( \" );");
myWriter.WriteLine("mySQL.Append( ConcatStrings( myFieldNames ));");
myWriter.WriteLine("mySQL.Append( \" ) Values ( \" );");
myWriter.WriteLine("for( int iCount = 0 ; iCount < myValues.Count ; iCount ++ )");
myWriter.BeginGroup("{");
myWriter.WriteLine("if( iCount > 0 ) mySQL.Append(\" , \" );");
if (bolNamedParameter)
{
myWriter.WriteLine("mySQL.Append(\" @Value\" + iCount ) ;");
myWriter.WriteLine("System.Data.IDbDataParameter parameter = cmd.CreateParameter();");
myWriter.WriteLine("parameter.Value = myValues[ iCount ] ;");
myWriter.WriteLine("parameter.ParameterName = \"Value\" + iCount ;");
myWriter.WriteLine("cmd.Parameters.Add( parameter );");
}
else
{
myWriter.WriteLine("mySQL.Append(\" ? \") ;");
myWriter.WriteLine("System.Data.IDbDataParameter parameter = cmd.CreateParameter();");
myWriter.WriteLine("parameter.Value = myValues[ iCount ] ;");
myWriter.WriteLine("cmd.Parameters.Add( parameter );");
}
myWriter.EndGroup("}//for");
myWriter.WriteLine("mySQL.Append( \" ) \" );");
myWriter.WriteLine("cmd.CommandText = mySQL.ToString();");
myWriter.WriteLine("return myValues.Count ;");
myWriter.EndGroup(")//public override int FillInsertCommand( System.Data.IDbCommand cmd , object objRecord )");
更多精彩
赞助商链接