Click here to Skip to main content
15,884,099 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.3K   686   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;
using System.Collections.Generic;

namespace npq {
	
	
	/// <summary>
	/// Encapsulates the access to the result
	/// </summary>
	public class Result : IDisposable {
		PostgreSQL psql;
		IntPtr res;
		ExecStatusType status;
		bool valid;
		int columns, rows;
		string[] columnNames;
		
		/// <summary>
		/// Constructor
		/// </summary>
		public Result(PostgreSQL _psql, IntPtr _res) {
			psql = _psql;
			res = _res;
			if (res == IntPtr.Zero)
				throw new PostgreSQLException("Result is IntPtr.Zero");
			
			status = PG.PQresultStatus(_res);
			valid =  (status == ExecStatusType.PGRES_COMMAND_OK || status == ExecStatusType.PGRES_TUPLES_OK);	
			columns = PG.PQnfields(res);
			rows = PG.PQntuples(res);
			columnNames = new string[columns];
			for(int i=0; i<columns; i++)
				columnNames[i] = PG.PQfname(res, i);
			
			if (status != ExecStatusType.PGRES_COMMAND_OK && status != ExecStatusType.PGRES_TUPLES_OK) {
				string message = PG.PQresultErrorMessage(res);
				Dispose();
				throw new PostgreSQLException(message);
			}
		}
		
		/// <summary>
		/// Free ressources
		/// </summary>
		public void Dispose() {
			if (res != IntPtr.Zero) {
				PG.PQclear(res);
				res = IntPtr.Zero;
			}
		}

		/// <value>
		/// The number of rows affected
		/// </value>
		public int AffectedRows { get { return PG.PQcmdTuples(res); } }
		
		/// <value>
		/// Returns true, if the result is empty
		/// </value>
		public bool Empty { get { return rows == 0; }}
		
		/// <value>
		/// Returns true, if the result is valid
		/// </value>
		public bool Valid { get { return valid; }}
		/// <value>
		/// Returns the Status
		/// </value>
		public ExecStatusType Status { get { return status; }}
		
		/// <value>
		/// Returns the number of columns in the result
		/// </value>
		public int Columns { get { return columns; }}
		/// <value>
		/// Returns the number of rows in the result
		/// </value>
		public int Rows { get { return rows; }}
		
		/// <value>
		/// Access to the data
		/// </value>
		public string this[int row, int column] {
			get { 
				if (row < 0 || row >= rows || column < 0 || column >= columns)
					throw new PostgreSQLException("Row or column index out of range!");
				return PG.PQgetvalue(res, row, column);
			}
		}
	
		/// <value>
		/// Access to the data
		/// </value>
		public string this[int row, string column] {
			get { 
				if (row < 0 || row >= rows)
					throw new PostgreSQLException("Row index out of range!");
				for(int i=0; i<columns; i++) {
					if (columnNames[i] == column)
						return PG.PQgetvalue(res, row, i);
				}
				throw new PostgreSQLException(string.Format("Column {0} not found!", column));
			}
		}
		
		public int ColumnIndex(string column) {
			for(int i=0; i<columns; i++) {
				if (columnNames[i] == column)
					return i;
			}
			throw new PostgreSQLException(string.Format("Column {0} not found!", column));
		}
		
		/// <summary>
		/// Returns the column name
		/// </summary>
		public string ColumnName(int column) {
			if (column < 0 || column >= columns)
				throw new PostgreSQLException("Column index out of range!");
			return columnNames[column];
		}

		/// <summary>
		/// Returns the column name
		/// </summary>
		public string ColumnTypeName(int column) {
			if (column < 0 || column >= columns)
				throw new PostgreSQLException("Column index out of range!");
			return PG.PQftypename(psql.conn, PG.PQftype(res, column));
		}

		/// <summary>
		/// Returns the column type
		/// </summary>
		public int ColumnType(int column) {
			if (column < 0 || column >= columns)
				throw new PostgreSQLException("Column index out of range!");
			return PG.PQftype(res, column);
		}

		public bool GetBoolean(int row, int column) { return bool.Parse(this[row, column]); }
		public byte GetByte(int row, int column) { return byte.Parse(this[row, column]); }
		public short GetShort(int row, int column) { return short.Parse(this[row, column]); }
		public int GetInt(int row, int column) { return int.Parse(this[row, column]); }
		public long GetLong(int row, int column) { return long.Parse(this[row, column]); }
		public float GetFloat(int row, int column) { return float.Parse(this[row, column]); }
		public double GetDouble(int row, int column) { return double.Parse(this[row, column]); }

		public void Dump() {
			for(int col=0; col<columns; col++)
				Console.Write("{0, 15}", columnNames[col]);
			Console.WriteLine();
			for(int row=0; row<rows; row++) {
				for(int col=0; col<columns; col++)
					Console.Write("{0,15}", this[row, col]);
				Console.WriteLine();
			}
		}
	}
}

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