Click here to Skip to main content
15,886,742 members
Articles / Programming Languages / C#

PostgreSQL Synchronization Tool

Rate me:
Please Sign up or sign in to vote.
4.11/5 (4 votes)
19 Aug 2009CDDL3 min read 39.4K   688   19  
Idea and implementation of a simple and easy database synchronization tool.
/* npq - A C# libpq wrapper
 * 
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * http://www.opensource.org/licenses/cddl1.php.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Copyright 2009 by martin.faust@e56.de
 */
using System;

namespace npq {
	/// <summary>
	/// A PostgresSQL connection
	/// </summary>
	public class PostgreSQL : IDisposable {
		internal IntPtr conn = IntPtr.Zero;
		int protocol;
		string version;
		
		/// <value>
		/// Returns the version string of the server
		/// </value>
		public string Version { get { return version; }}
		/// <value>
		/// Returns the protocol version
		/// </value>
		public int Protocol { get { return protocol; }}
		
		/// <summary>
		/// Connect to the database
		/// </summary>
		/// <param name="info">Connection string as described in the libpq docs</param>
		public PostgreSQL(string info) {
			conn = PG.PQconnectdb(info);
			if (PG.PQstatus(conn) != ConnStatusType.CONNECTION_OK)
				throw new PostgreSQLException(PG.PQerrorMessage(conn));
			
			int v = PG.PQserverVersion(conn);
			int major = v / 10000;
			v -= major * 10000;
			int minor = v / 100;
			v -= minor * 100;
			
			version = string.Format("{0}.{1}.{2}", major, minor, v);
			protocol = PG.PQprotocolVersion(conn);
		}

		/// <value>
		/// Returns the last error message
		/// </value>
		public string Error {
			get { return PG.PQerrorMessage(conn); }
		}
		
		/// <summary>
		/// Begin a transaction
		/// </summary>
		public void StartTransaction() { Exec("START TRANSACTION").Dispose(); }

		/// <summary>
		/// Comit transaction
		/// </summary>
		public void ComitTransaction() { Exec("COMMIT").Dispose(); }
		/// <summary>
		/// Rollback a transaction
		/// </summary>
		public void RollbackTransaction() { Exec("ROLLBACK").Dispose(); }
		
		/// <summary>
		/// Excecute a sql query
		/// </summary>
		public Result Exec(string query) { return new Result(this, PG.PQexec(conn, query)); }

		/// <summary>
		/// Free ressources
		/// </summary>
		public void Dispose() {
			if (conn != IntPtr.Zero) {
				PG.PQfinish(conn);
				conn = IntPtr.Zero;
			}		
		}		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Architect
Germany Germany
Utilizing my experience of industry-scale real-time graphics programming, design, development of software, as well as European research projects I want to bring in new ideas for creative projects.
Companies:
- 2009-now BTC AG: software for the renewable energy sector.
- 2007-2009 Digital Media: Audio, Graphics and GIS Web Services(http://maps.bremen.de)
- 2001-2007 artecLab://art/work/technology: Mixed Reality, Computer Games and eLearning
- 1998-2001 STN ATLAS Elektronik: real-time graphics for ground warfare simulation

For a complete resume see http://e56.de/download/resume.pdf

Comments and Discussions