comparison Postarc/Postarc/PostGISConnection.cs @ 0:1aca3d413885 tip

Initial import of Postarc
author Christian Lins <christian.lins@intevation.de>
date Fri, 05 Oct 2012 23:55:06 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1aca3d413885
1 /*
2 * Postarc
3 *
4 * Author:
5 * Christian Lins <christian.lins@intevation.de>
6 *
7 * Copyright:
8 * Copyright (C) 2012 Intevation GmbH <http://www.intevation.de/>
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 using System;
25 using System.Collections.Generic;
26 using System.Linq;
27 using System.Text;
28 using System.Runtime.Serialization;
29 using Npgsql;
30
31 namespace Postarc
32 {
33 [Serializable]
34 public class PostGISConnection
35 {
36 private string name = "<new profile>";
37 private string host;
38 private int port = 5432;
39 private string user;
40 private string password;
41 private string database;
42
43 public string Name
44 {
45 get
46 {
47 return this.name;
48 }
49 set
50 {
51 this.name = value;
52 }
53 }
54
55 public string Host
56 {
57 get
58 {
59 return this.host;
60 }
61 set
62 {
63 this.host = value;
64 }
65 }
66
67 public int Port
68 {
69 get
70 {
71 return this.port;
72 }
73 set
74 {
75 this.port = value;
76 }
77 }
78
79 public string User
80 {
81 get
82 {
83 return this.user;
84 }
85 set
86 {
87 this.user = value;
88 }
89 }
90
91 public string Password
92 {
93 get
94 {
95 return this.password;
96 }
97 set
98 {
99 this.password = value;
100 }
101 }
102
103 public string Database
104 {
105 get
106 {
107 return "postgis"; // this.database;
108 }
109 set
110 {
111 this.database = value;
112 }
113 }
114
115 public PostGISConnection()
116 {
117 }
118
119 public string CreateConnectionString()
120 {
121 StringBuilder cs = new StringBuilder();
122 cs.Append("Server="); cs.Append(host);
123 cs.Append(";Port="); cs.Append(port);
124 cs.Append(";User Id="); cs.Append(user);
125 cs.Append(";Password="); cs.Append(password);
126 cs.Append(";Database="); cs.Append(database);
127 cs.Append(";");
128 return cs.ToString();
129 }
130
131 /// <summary>
132 /// Creates and opens an NpgsqlConnection to the PostGIS database
133 /// described by this PostGISConnection instance.
134 /// </summary>
135 /// <returns></returns>
136 public NpgsqlConnection Open()
137 {
138 NpgsqlConnection conn = new NpgsqlConnection(CreateConnectionString());
139 conn.ChangeDatabase(this.Database);
140 return conn;
141 }
142
143 /// <summary>
144 /// Queries the table names from the information_schema.tables table
145 /// of the PostgreSQL system.
146 /// </summary>
147 /// <returns></returns>
148 public List<string> QueryTables()
149 {
150 List<string> tables = new List<string>();
151
152 NpgsqlConnection conn = Open();
153 NpgsqlCommand cmd = new NpgsqlCommand(
154 "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';",
155 conn);
156 NpgsqlDataReader reader = cmd.ExecuteReader();
157 while (reader.Read())
158 {
159 string str = reader.GetString(0);
160 if (!str.Equals("spatial_ref_sys") &&
161 !str.Equals("geography_columns") &&
162 !str.Equals("geometry_columns") &&
163 !str.Equals("raster_columns") &&
164 !str.Equals("raster_overviews"))
165 {
166 tables.Add(str);
167 }
168 }
169
170 conn.Close();
171 return tables;
172 }
173
174 public override string ToString()
175 {
176 return Name;
177 }
178 }
179 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)