annotate Source/Templates/PostgreSQL.ttinclude @ 8:a34cfdde80d6

removed strong signing added FrameworkPathOverride for linux builds
author cin
date Wed, 29 Nov 2017 12:43:52 +0300
parents f990fcb411a9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
1 <#
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
2 ConnectionType = "Npgsql.NpgsqlConnection, Npgsql";
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
3 #><#+
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
4 private void LoadServerMetadata()
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
5 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
6 var tables = CreateList(new { ID = 0, Table = new Table() });
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
7 var columns = CreateList(new { ID = 0, Column = new Column() });
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
8
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
9 if (!string.IsNullOrEmpty(DataProviderAssembly))
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
10 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
11 System.Reflection.Assembly.LoadFile(DataProviderAssembly.Replace("Npgsql.dll", "Mono.Security.dll"));
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
12 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
13
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
14 using (var conn = GetConnection())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
15 using (var cmd = conn.CreateCommand())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
16 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
17 // Load tables & vies.
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
18 //
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
19 cmd.CommandText = @"
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
20 SELECT
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
21 pg_class.oid,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
22 pg_namespace.nspname,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
23 pg_class.relname,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
24 pg_class.relkind
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
25 FROM
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
26 pg_class
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
27 INNER JOIN pg_user ON (pg_class.relowner = pg_user.usesysid)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
28 INNER JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
29 WHERE
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
30 pg_class.relkind IN ('r','v')
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
31 AND pg_namespace.nspname NOT IN ('pg_catalog','information_schema')";
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
32
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
33 using (var rd = cmd.ExecuteReader())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
34 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
35 while (rd.Read())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
36 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
37 var t = new
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
38 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
39 ID = Convert.ToInt32(rd[0]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
40 Table = new Table
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
41 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
42 Owner = rd[1].ToString(),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
43 TableName = rd[2].ToString(),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
44 ClassName = rd[2].ToString(),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
45 IsView = rd[3].ToString() == "v",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
46 BaseClassName = BaseEntityClass,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
47 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
48 };
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
49
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
50 tables.Add(t);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
51 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
52 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
53
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
54 // Load columns.
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
55 //
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
56 cmd.CommandText = @"
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
57 SELECT
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
58 pg_attribute.attrelid as id,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
59 pg_attribute.attnum as colid,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
60 pg_attribute.attname as name,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
61 0 as status,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
62 0 as usertype,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
63 pg_attribute.atttypid as type,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
64 COALESCE(CAST(substring(format_type(pg_attribute.atttypid, pg_attribute.atttypmod) from '[0-9]{1,}') as INTEGER),0) as lenght,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
65 CASE WHEN pg_attribute.atttypid = 1700 AND format_type(pg_attribute.atttypid, pg_attribute.atttypmod) != 'numeric' THEN
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
66 COALESCE(CAST(regexp_replace(regexp_replace(format_type(pg_attribute.atttypid, pg_attribute.atttypmod),'[^,]*,',''),'[)]$','') as INTEGER),0)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
67 ELSE
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
68 0
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
69 END as prec,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
70 format_type(pg_attribute.atttypid, pg_attribute.atttypmod) as typename,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
71 CASE WHEN CAST(pg_attribute.atttypid AS TEXT) IN ('17','25','1042','1043') THEN
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
72 false
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
73 ELSE
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
74 NOT pg_attribute.attnotnull
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
75 END as isnullable,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
76 COALESCE(pg_attrdef.adsrc ~* 'nextval',FALSE) as isidentity
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
77 FROM
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
78 pg_attribute
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
79 LEFT JOIN pg_attrdef ON (pg_attribute.attrelid = pg_attrdef.adrelid AND
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
80 pg_attribute.attnum = pg_attrdef.adnum)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
81 INNER JOIN pg_class ON (pg_attribute.attrelid = pg_class.oid)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
82 INNER JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
83 WHERE
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
84 pg_class.relkind IN ('r','v') AND
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
85 pg_namespace.nspname NOT IN ('pg_catalog','information_schema') AND
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
86 pg_attribute.attnum > 0 AND
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
87 NOT pg_attribute.attisdropped";
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
88
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
89 bool addNpgsqlTypes = false;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
90
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
91 using (var rd = cmd.ExecuteReader())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
92 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
93 while (rd.Read())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
94 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
95 var col = new
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
96 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
97 ID = Convert.ToInt32(rd["id"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
98 Column = new Column
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
99 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
100 ID = Convert.ToInt16(rd["colid"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
101 ColumnName = Convert.ToString (rd["name"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
102 MemberName = Convert.ToString (rd["name"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
103 ColumnType = Convert.ToString (rd["typename"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
104 IsNullable = Convert.ToBoolean(rd["isnullable"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
105 IsIdentity = Convert.ToBoolean(rd["isidentity"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
106 Length = rd.IsDBNull(rd.GetOrdinal("lenght")) ? 0 : Convert.ToInt64(rd["lenght"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
107 Precision = rd.IsDBNull(rd.GetOrdinal("prec")) ? 0 : Convert.ToInt32(rd["prec"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
108 //Scale = rd.IsDBNull(rd.GetOrdinal("scale")) ? 0 : Convert.ToInt32(rd["scale"]),
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
109 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
110 };
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
111
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
112 var c = col.Column;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
113 var oid = Convert.ToInt32(rd["type"]);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
114
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
115 c.Type = "object";
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
116 c.DbType = DbType.Object;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
117 c.SqlDbType = SqlDbType.Variant;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
118
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
119 switch (oid)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
120 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
121 case 16 /* bool */ : c.Type = "Boolean"; c.DbType = DbType.Boolean; c.SqlDbType = SqlDbType.Bit; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
122 case 17 /* bytea */ : c.Type = "Byte[]"; c.DbType = DbType.Binary; c.SqlDbType = SqlDbType.Binary; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
123 case 20 /* int8 */ : c.Type = "Int64"; c.DbType = DbType.Int64; c.SqlDbType = SqlDbType.BigInt; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
124 case 21 /* int2 */ : c.Type = "Int16"; c.DbType = DbType.Int16; c.SqlDbType = SqlDbType.SmallInt; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
125 case 23 /* int4 */ : c.Type = "Int32"; c.DbType = DbType.Int32; c.SqlDbType = SqlDbType.Int; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
126 case 25 /* text */ : c.Type = "String"; c.DbType = DbType.String; c.SqlDbType = SqlDbType.Text; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
127 case 142 /* xml */ : c.Type = "String"; c.DbType = DbType.Xml; c.SqlDbType = SqlDbType.Xml; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
128 case 600 /* point */ : c.Type = "NpgsqlPoint"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
129 case 601 /* lseg */ : c.Type = "NpgsqlLSeg"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
130 case 602 /* path */ : c.Type = "NpgsqlPath"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
131 case 603 /* box */ : c.Type = "NpgsqlBox"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
132 case 604 /* polygon */ : c.Type = "NpgsqlPolygon"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
133 case 700 /* float4 */ : c.Type = "Single"; c.DbType = DbType.Single; c.SqlDbType = SqlDbType.Real; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
134 case 701 /* float8 */ : c.Type = "Double"; c.DbType = DbType.Double; c.SqlDbType = SqlDbType.Float; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
135 case 718 /* circle */ : c.Type = "NpgsqlCircle"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
136 case 790 /* money */ : c.Type = "Decimal"; c.DbType = DbType.Decimal; c.SqlDbType = SqlDbType.Money; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
137 case 829 /* macaddr */ : c.Type = "NpgsqlMacAddress"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
138 case 869 /* inet */ : c.Type = "NpgsqlInet"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
139 case 1042 /* char */ : c.Type = "String"; c.DbType = DbType.String; c.SqlDbType = SqlDbType.Char; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
140 case 1043 /* varchar */ : c.Type = "String"; c.DbType = DbType.String; c.SqlDbType = SqlDbType.VarChar; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
141 case 1082 /* date */ : c.Type = "DateTime"; c.DbType = DbType.Date; c.SqlDbType = SqlDbType.Date; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
142 case 1083 /* time */ : c.Type = "DateTime"; c.DbType = DbType.Time; c.SqlDbType = SqlDbType.Time; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
143 case 1114 /* datetime */ : c.Type = "DateTime"; c.DbType = DbType.DateTime; c.SqlDbType = SqlDbType.DateTime; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
144 case 1560 /* bit */ : c.Type = "BitString"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
145 case 1700 /* numeric */ : c.Type = "Decimal"; c.DbType = DbType.Decimal; c.SqlDbType = SqlDbType.Decimal; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
146 case 1186 /* interval */ : c.Type = "NpgsqlInterval"; addNpgsqlTypes = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
147 case 1184 /* timestamp with time zone */ :
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
148 case 1266 /* timestamp with time zone */ : c.Type = "DateTime"; c.DbType = DbType.DateTime2; c.SqlDbType = SqlDbType.DateTime2; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
149 case 2950 /* Guid */ : c.Type = "Guid"; c.DbType = DbType.Binary; c.SqlDbType = SqlDbType.UniqueIdentifier; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
150 default:
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
151 if (oid < 100000)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
152 throw new Exception(string.Format("Type '{0}' ({1}) is not expected in PostgreSQL.tt", rd["typename"], rd["type"]));
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
153 break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
154 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
155
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
156 switch (c.Type)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
157 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
158 case "object" :
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
159 case "String" :
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
160 case "byte[]" : c.IsClass = true; break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
161 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
162
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
163 if (c.IsNullable && !c.IsClass)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
164 c.Type += "?";
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
165
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
166 columns.Add(col);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
167 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
168 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
169
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
170 if (addNpgsqlTypes)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
171 Usings.Add("NpgsqlTypes");
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
172
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
173 // Load PKs.
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
174 //
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
175 cmd.CommandText = @"
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
176 SELECT
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
177 pg_constraint.conrelid as id,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
178 pg_constraint.conname as ""name"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
179 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[1]) as colname,
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
180 pg_constraint.conkey[1] as ""order"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
181 pg_constraint.conkey[1] as colid
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
182 FROM
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
183 pg_constraint
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
184 WHERE
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
185 pg_constraint.contype = 'p'";
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
186
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
187 using (var rd = cmd.ExecuteReader())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
188 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
189 while (rd.Read())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
190 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
191 var id = Convert.ToInt32(rd["id"]);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
192 var colid = Convert.ToInt32(rd["colid"]);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
193
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
194 columns.Single(_ => _.ID == id && _.Column.ID == colid).Column.PKIndex = colid;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
195 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
196 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
197
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
198 // Load FKs.
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
199 //
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
200 cmd.CommandText = @"
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
201 SELECT
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
202 pg_constraint.conname as ""Name"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
203 pg_constraint.conrelid as ""ThisTable"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
204 pg_constraint.confrelid as ""OtherTable"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
205 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[01]) as ""ThisColumn1"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
206 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[02]) as ""ThisColumn2"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
207 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[03]) as ""ThisColumn3"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
208 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[04]) as ""ThisColumn4"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
209 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[05]) as ""ThisColumn5"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
210 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[06]) as ""ThisColumn6"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
211 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[07]) as ""ThisColumn7"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
212 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[08]) as ""ThisColumn8"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
213 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[09]) as ""ThisColumn9"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
214 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[10]) as ""ThisColumn10"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
215 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[11]) as ""ThisColumn11"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
216 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[12]) as ""ThisColumn12"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
217 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[13]) as ""ThisColumn13"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
218 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[14]) as ""ThisColumn14"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
219 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[15]) as ""ThisColumn15"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
220 (select attname from pg_attribute where attrelid = pg_constraint.conrelid and attnum = pg_constraint.conkey[16]) as ""ThisColumn16"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
221 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[01]) as ""OtherColumn1"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
222 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[02]) as ""OtherColumn2"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
223 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[03]) as ""OtherColumn3"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
224 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[04]) as ""OtherColumn4"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
225 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[05]) as ""OtherColumn5"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
226 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[06]) as ""OtherColumn6"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
227 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[07]) as ""OtherColumn7"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
228 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[08]) as ""OtherColumn8"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
229 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[09]) as ""OtherColumn9"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
230 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[10]) as ""OtherColumn10"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
231 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[11]) as ""OtherColumn11"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
232 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[12]) as ""OtherColumn12"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
233 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[13]) as ""OtherColumn13"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
234 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[14]) as ""OtherColumn14"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
235 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[15]) as ""OtherColumn15"",
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
236 (select attname from pg_attribute where attrelid = pg_constraint.confrelid and attnum = pg_constraint.confkey[16]) as ""OtherColumn16""
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
237 FROM
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
238 pg_constraint
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
239 WHERE
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
240 pg_constraint.contype = 'f'";
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
241
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
242 using (var rd = cmd.ExecuteReader())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
243 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
244 while (rd.Read())
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
245 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
246 var name = Convert.ToString(rd["Name"]);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
247 var thisTableID = Convert.ToInt32 (rd["ThisTable"]);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
248 var otherTableID = Convert.ToInt32 (rd["OtherTable"]);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
249
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
250 var thisTable = (from t in tables where t.ID == thisTableID select t.Table).Single();
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
251 var otherTable = (from t in tables where t.ID == otherTableID select t.Table).Single();
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
252
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
253 thisTable.ForeignKeys.Add(name, new ForeignKey { KeyName = name, MemberName = name, OtherTable = otherTable });
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
254
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
255 for (int i = 1; i <= 16; i++)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
256 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
257 if (rd.IsDBNull(rd.GetOrdinal("ThisColumn" + i)))
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
258 break;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
259
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
260 var thisColumnName = Convert.ToString(rd["ThisColumn" + i]);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
261 var otherColumnName = Convert.ToString(rd["OtherColumn" + i]);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
262
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
263 var thisColumn = (from c in columns where c.ID == thisTableID && c.Column.ColumnName == thisColumnName select c.Column).Single();
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
264 var otherColumn = (from c in columns where c.ID == otherTableID && c.Column.ColumnName == otherColumnName select c.Column).Single();
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
265
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
266 var key = thisTable.ForeignKeys[name];
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
267
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
268 key.ThisColumns. Add(thisColumn);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
269 key.OtherColumns.Add(otherColumn);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
270 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
271 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
272 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
273 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
274
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
275 var qc =
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
276 from c in columns
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
277 group c by c.ID into gr
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
278 join t in tables on gr.Key equals t.ID
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
279 select new { t.Table, gr };
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
280
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
281 foreach (var c in qc)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
282 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
283 foreach (var col in from col in c.gr orderby col.Column.ID select col.Column)
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
284 c.Table.Columns.Add(col.ColumnName, col);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
285
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
286 if (c.Table.Owner == "postgres")
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
287 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
288 c.Table.Owner = null;
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
289 Tables.Add(c.Table.TableName, c.Table);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
290 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
291 else
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
292 {
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
293 Tables.Add(c.Table.Owner + "." + c.Table.TableName, c.Table);
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
294 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
295 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
296 }
f990fcb411a9 Копия текущей версии из github
cin
parents:
diff changeset
297 #>