private CodeMemberMethod BuildFillMethod(TableViewTableTypeBase table) { // Accepts a DataRow and Returns a Poco of this type. CodeMemberMethod cmmFill = new CodeMemberMethod(); cmmFill.Name = "Fill"; cmmFill.Attributes = MemberAttributes.Private; cmmFill.ReturnType = new CodeTypeReference(table.Name); CodeParameterDeclarationExpression cpdeDataRow = new CodeParameterDeclarationExpression(); cpdeDataRow.Name = "row"; cpdeDataRow.Type = new CodeTypeReference("System.Data.DataRow"); cpdeDataRow.Direction = FieldDirection.In; cmmFill.Parameters.Add(cpdeDataRow); var init_Express = new CodeSnippetExpression("new " + table.Name + "()"); var obj = new CodeVariableDeclarationStatement(new CodeTypeReference(table.Name), "obj", init_Express); cmmFill.Statements.Add(obj); foreach (Column c in table.Columns) { String DotNetTypeName = TypeConvertor.ToNetType(c.DataType.SqlDataType).ToString(); MemberGraph mGraph = new MemberGraph(c); System.CodeDom.CodeConditionStatement ccsField = new CodeConditionStatement(); ccsField.Condition = new CodeSnippetExpression("(row[\"" + c.Name + "\"] != System.DBNull.Value)"); //if (!(mGraph.IsReadOnly)) //{ // If Field is nullable Type if (mGraph.IsNullable) { if (mGraph.TypeName() == "String") { ccsField.TrueStatements.Add(new CodeSnippetExpression("obj." + mGraph.PropertyName() + " = row[\"" + c.Name + "\"].ToString()")); } else { ccsField.TrueStatements.Add(new CodeSnippetExpression("obj." + mGraph.PropertyName() + " = ((" + mGraph.TypeName() + ")(row[\"" + c.Name + "\"]))")); } } else { if (mGraph.TypeName() == "String") { ccsField.TrueStatements.Add(new CodeSnippetExpression("obj." + mGraph.PropertyName() + " = row[\"" + c.Name + "\"].ToString()")); } else { ccsField.TrueStatements.Add(new CodeSnippetExpression("obj." + mGraph.PropertyName() + " = ((" + mGraph.TypeName() + ")(row[\"" + c.Name + "\"]))")); } } cmmFill.Statements.Add(ccsField); //} } cmmFill.Statements.Add(new CodeSnippetExpression("return obj")); cmmFill.Comments.Add(new CodeCommentStatement("Returns a Hydrated POCO")); return(cmmFill); }
QueryParamInfo GetParamInfo(string name, string sqlTypeAndLength) { var qp = new QueryParamInfo(); var m = Regex.Match(sqlTypeAndLength, @"(?'type'^\w*)\(?(?'firstNum'\d*),?(?'secondNum'\d*)"); var typeOnly = m.Groups["type"].Value; int.TryParse(m.Groups["firstNum"].Value, out int firstNum); int.TryParse(m.Groups["secondNum"].Value, out int secondNum); if (secondNum != 0) { qp.Precision = firstNum; qp.Scale = secondNum; } else if (typeOnly.ToLower() == "datetime2") { qp.Precision = firstNum; } else if (firstNum > 0) { qp.Length = firstNum; } //string normalizedType; // we have no info for the nullability of query params, so we'll make them all nullable //var csType = System2Alias.Map( TypeMapDB2CS(typeOnly, out normalizedType), true ); try { // hack if (typeOnly == "sql_variant") { typeOnly = "Variant"; } var sqlDbType = (SqlDbType)Enum.Parse(typeof(SqlDbType), typeOnly, true); var csType = TypeConvertor.ToNetType(sqlDbType); var dbType = TypeConvertor.ToDbType(sqlDbType); qp.CSType = System2Alias.Map(csType.FullName, true); // will look up aliases of system types, and append the question mark. qp.FullyQualifiedCSType = csType.FullName; qp.DbType = dbType.ToString(); qp.CSNameCamel = char.ToLower(name.First()) + name.Substring(1); qp.CSNamePascal = char.ToUpper(name.First()) + name.Substring(1); qp.CSNamePrivate = "_" + qp.CSNameCamel; qp.DbName = '@' + name; return(qp); } catch (Exception ex) { throw new TypeNotMatchedException(ex.Message); } }
/// <summary> /// /// </summary> /// <param name="table"></param> /// <returns></returns> public CodeMemberMethod BuildSelectBE(TableViewTableTypeBase table) { CodeMemberMethod cmSelect = new CodeMemberMethod(); cmSelect.Attributes = MemberAttributes.Public; cmSelect.ReturnType = new CodeTypeReference("System.Data.DataSet"); String cp_name = "ssp_" + table.Name; String PocoTypeName = table.Name; String FullPocoTypeName = PocoTypeName; CodeParameterDeclarationExpression cpdePoco = new CodeParameterDeclarationExpression(); cpdePoco.Name = "query"; cpdePoco.Type = new CodeTypeReference(table.Name); cpdePoco.Direction = FieldDirection.In; cmSelect.Parameters.Add(cpdePoco); cmSelect.Attributes = MemberAttributes.Public; cmSelect.Name = "Select"; cmSelect.Statements.Add(new CodeSnippetExpression("this.Access.CreateProcedureCommand(\"" + cp_name + "\")")); foreach (Column c in table.Columns) { MemberGraph mGraph = new MemberGraph(c); String DotNetTypeName = TypeConvertor.ToNetType(c.DataType.SqlDataType).ToString(); System.CodeDom.CodeConditionStatement ccsField = new CodeConditionStatement(); if (mGraph.IsNullable) { ccsField.Condition = new CodeSnippetExpression("query." + mGraph.PropertyName() + ".HasValue"); ccsField.TrueStatements.Add(new CodeSnippetExpression("this.Access.AddParameter(\"" + mGraph.PropertyName() + "\",query." + mGraph.PropertyName() + ".Value, ParameterDirection.Input)")); ccsField.FalseStatements.Add(new CodeSnippetExpression("this.Access.AddParameter(\"" + mGraph.PropertyName() + "\", null , ParameterDirection.Input)")); } else { ccsField.Condition = new CodeSnippetExpression("query." + mGraph.PropertyName() + " == null"); ccsField.TrueStatements.Add(new CodeSnippetExpression("this.Access.AddParameter(\"" + mGraph.PropertyName() + "\", null , ParameterDirection.Input)")); ccsField.FalseStatements.Add(new CodeSnippetExpression("this.Access.AddParameter(\"" + mGraph.PropertyName() + "\",query." + mGraph.PropertyName() + ", ParameterDirection.Input)")); } cmSelect.Statements.Add(ccsField); } cmSelect.Statements.Add(new CodeSnippetExpression("return this.Access.ExecuteDataSet()")); cmSelect.Comments.Add(new CodeCommentStatement("Select by Object [Implements Query By Example], returns DataSet")); return(cmSelect); }