private void Get_Fields() { string _sqltext = @"SELECT a.oid ,c.attnum as num ,c.attname as field , (case when f.character_maximum_length is null then c.attlen else f.character_maximum_length end) as length ,c.attnotnull as notnull ,d.description as comment ,(case when e.typcategory ='G' then e.typname when e.typelem = 0 then e.typname else e2.typname end) as type ,(case when e.typelem = 0 then e.typtype else e2.typtype end) as data_type ,e.typcategory ,f.is_identity from pg_class a inner join pg_namespace b on a.relnamespace=b.oid inner join pg_attribute c on attrelid = a.oid LEFT OUTER JOIN pg_description d ON c.attrelid = d.objoid AND c.attnum = d.objsubid and c.attnum > 0 inner join pg_type e on e.oid=c.atttypid left join pg_type e2 on e2.oid=e.typelem inner join information_schema.columns f on f.table_schema = b.nspname and f.table_name=a.relname and column_name = c.attname WHERE b.nspname='{0}' and a.relname='{1}';"; _sqltext = string.Format(_sqltext, this.schemaName, this.table.name); PgSqlHelper.ExecuteDataReader(dr => { FieldInfo fi = new FieldInfo(); fi.Oid = Convert.ToInt32(dr["oid"]); fi.Field = dr["field"].ToString(); fi.Length = Convert.ToInt32(dr["length"].ToString()); fi.Is_not_null = Convert.ToBoolean(dr["notnull"]); fi.Comment = dr["comment"].ToString(); fi.Data_Type = dr["data_type"].ToString(); fi.Db_type = dr["type"].ToString(); fi.Db_type = fi.Db_type.StartsWith("_") ? fi.Db_type.Remove(0, 1) : fi.Db_type; fi.PgDbType = PgsqlType.SwitchToSql(fi.Data_Type, fi.Db_type); fi.Is_identity = dr["is_identity"].ToString() == "YES"; fi.Is_array = dr["typcategory"].ToString() == "A"; fi.Is_enum = fi.Data_Type == "e"; fi.CsType = PgsqlType.SwitchToCSharp(fi.Db_type); if (fi.Is_enum) { fi.CsType = fi.CsType.ToUpperPascal(); } string _notnull = ""; if ( fi.CsType != "string" && fi.CsType != "byte[]" && fi.CsType != "JToken" && !fi.Is_array && fi.CsType != "System.Net.IPAddress" && fi.CsType != "System.Net.NetworkInformation.PhysicalAddress" && fi.CsType != "System.Xml.Linq.XDocument" && fi.CsType != "System.Collections.BitArray" && fi.CsType != "object" ) { _notnull = fi.Is_not_null ? "" : "?"; } string _array = fi.Is_array ? "[]" : ""; fi.RelType = $"{fi.CsType}{_notnull}{_array}"; // dal this.fieldList.Add(fi); }, CommandType.Text, _sqltext); }
protected void Update_Generator(StreamWriter writer, string class_model, string dal_name) { List <string> d_key = new List <string>(); List <string> d_key_fields = new List <string>(); foreach (var item in pkList) { FieldInfo fs = fieldList.FirstOrDefault(f => f.Field == item.Field); d_key.Add(fs.RelType + " " + fs.Field); d_key_fields.Add(fs.Field); } string updateName = CreateName() + "UpdateBuilder"; if (d_key.Count > 0) { writer.WriteLine($"\t\tpublic static {updateName} Update({string.Join(",", d_key)})"); writer.WriteLine("\t\t{"); writer.WriteLine($"\t\t\treturn new {updateName}(null, {string.Join(",", d_key_fields)});"); writer.WriteLine("\t\t}"); writer.WriteLine(); } writer.WriteLine($"\t\tpublic static {updateName} UpdateBuilder {{ get {{ return new {updateName}(); }} }}"); writer.WriteLine(); string dkString = d_key.Count > 0 ? $"{ string.Join(",", d_key)}" : ""; var modelUpper = class_model.ToUpperPascal(); writer.WriteLine($"\t\tpublic class {updateName} : UpdateBuilder<{modelUpper}>"); writer.WriteLine("\t\t{"); void CreateConstructor(string paramString, string onChange = null) { var baseT = onChange == null ? "" : " : base(onChanged)"; writer.WriteLine($"\t\t\tpublic {updateName}({onChange}{dkString}){baseT}"); writer.WriteLine("\t\t\t{"); if (pkList.Count > 0) { writer.Write($"\t\t\t\tbase.Where(f => "); for (int i = 0; i < pkList.Count; i++) { var item = pkList[i]; writer.Write($"f.{item.Field.ToUpperPascal()} == {item.Field}"); if (i + 1 < pkList.Count) { writer.Write(" && "); } } writer.Write(");\n"); } writer.WriteLine("\t\t\t}"); } // 默认构造函数 CreateConstructor(dkString); writer.WriteLine(); // 重载构造函数 var separator = d_key.Count > 0 ? ", " : ""; CreateConstructor(dkString, $"Action<{modelUpper}> onChanged" + separator); if (d_key.Count > 0) { writer.WriteLine(); writer.WriteLine($"\t\t\tpublic {updateName}() {{ }}"); writer.WriteLine(); } writer.WriteLine($"\t\t\tpublic new {updateName} Where(Expression<Func<{class_model.ToUpperPascal()}, bool>> predicate)"); writer.WriteLine("\t\t\t{"); writer.WriteLine($"\t\t\t\t base.Where(predicate);"); writer.WriteLine($"\t\t\t\t return this;"); writer.WriteLine("\t\t\t}"); writer.WriteLine(); writer.WriteLine($"\t\t\tpublic new {updateName} Where(string formatCommad, params object[] pValue)"); writer.WriteLine("\t\t\t{"); writer.WriteLine($"\t\t\t\t base.Where(formatCommad,pValue);"); writer.WriteLine($"\t\t\t\t return this;"); writer.WriteLine("\t\t\t}"); writer.WriteLine(); foreach (var item in fieldList) { if (item.Is_identity) { continue; } NpgsqlDbType _dbtype = PgsqlType.SwitchToSql(item.Data_Type, item.Db_type); writer.WriteLine($"\t\t\tpublic {updateName} Set{item.Field.ToUpperPascal()}({item.RelType} {item.Field})"); writer.WriteLine("\t\t\t{"); string specificType = GetspecificType(item); string ap = item.Is_array ? " | NpgsqlDbType.Array" : ""; writer.WriteLine($"\t\t\t\treturn base.SetField(\"{ item.Field}\", NpgsqlDbType.{_dbtype}{ap}, {item.Field}, {item.Length}, {specificType}) as {updateName};"); writer.WriteLine("\t\t\t}"); if (item.Is_array) { writer.WriteLine($"\t\t\tpublic {updateName} Set{item.Field.ToUpperPascal()}Append({item.CsType} {item.Field})"); writer.WriteLine("\t\t\t{"); writer.WriteLine($"\t\t\t\treturn base.SetArrayAppend(\"{ item.Field}\", NpgsqlDbType.{_dbtype}, {item.Field}, {item.Length}, {specificType}) as {updateName};"); writer.WriteLine("\t\t\t}"); writer.WriteLine($"\t\t\tpublic {updateName} Set{item.Field.ToUpperPascal()}Remove({item.CsType} {item.Field})"); writer.WriteLine("\t\t\t{"); writer.WriteLine($"\t\t\t\treturn base.SetArrayRemove(\"{ item.Field}\", NpgsqlDbType.{_dbtype}, {item.Field}, {item.Length}, {specificType}) as {updateName};"); writer.WriteLine("\t\t\t}"); } writer.WriteLine(); } writer.WriteLine("\t\t}"); }