Mercurial > postarc > postarc
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Postarc/Postarc/PostGISConnection.cs Fri Oct 05 23:55:06 2012 +0200 @@ -0,0 +1,179 @@ +/* + * Postarc + * + * Author: + * Christian Lins <christian.lins@intevation.de> + * + * Copyright: + * Copyright (C) 2012 Intevation GmbH <http://www.intevation.de/> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.Serialization; +using Npgsql; + +namespace Postarc +{ + [Serializable] + public class PostGISConnection + { + private string name = "<new profile>"; + private string host; + private int port = 5432; + private string user; + private string password; + private string database; + + public string Name + { + get + { + return this.name; + } + set + { + this.name = value; + } + } + + public string Host + { + get + { + return this.host; + } + set + { + this.host = value; + } + } + + public int Port + { + get + { + return this.port; + } + set + { + this.port = value; + } + } + + public string User + { + get + { + return this.user; + } + set + { + this.user = value; + } + } + + public string Password + { + get + { + return this.password; + } + set + { + this.password = value; + } + } + + public string Database + { + get + { + return "postgis"; // this.database; + } + set + { + this.database = value; + } + } + + public PostGISConnection() + { + } + + public string CreateConnectionString() + { + StringBuilder cs = new StringBuilder(); + cs.Append("Server="); cs.Append(host); + cs.Append(";Port="); cs.Append(port); + cs.Append(";User Id="); cs.Append(user); + cs.Append(";Password="); cs.Append(password); + cs.Append(";Database="); cs.Append(database); + cs.Append(";"); + return cs.ToString(); + } + + /// <summary> + /// Creates and opens an NpgsqlConnection to the PostGIS database + /// described by this PostGISConnection instance. + /// </summary> + /// <returns></returns> + public NpgsqlConnection Open() + { + NpgsqlConnection conn = new NpgsqlConnection(CreateConnectionString()); + conn.ChangeDatabase(this.Database); + return conn; + } + + /// <summary> + /// Queries the table names from the information_schema.tables table + /// of the PostgreSQL system. + /// </summary> + /// <returns></returns> + public List<string> QueryTables() + { + List<string> tables = new List<string>(); + + NpgsqlConnection conn = Open(); + NpgsqlCommand cmd = new NpgsqlCommand( + "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';", + conn); + NpgsqlDataReader reader = cmd.ExecuteReader(); + while (reader.Read()) + { + string str = reader.GetString(0); + if (!str.Equals("spatial_ref_sys") && + !str.Equals("geography_columns") && + !str.Equals("geometry_columns") && + !str.Equals("raster_columns") && + !str.Equals("raster_overviews")) + { + tables.Add(str); + } + } + + conn.Close(); + return tables; + } + + public override string ToString() + { + return Name; + } + } +}