Click here to Skip to main content
15,892,072 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.7K   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 large object wrapper
	/// </summary>
	public class LargeObject {
		PostgreSQL psql;
		int fd;
		
		/// <summary>
		/// Constructor
		/// </summary>
		public LargeObject(PostgreSQL _psql) {
			psql = _psql;
			fd = -1;
		}
		
		/// <summary>
		/// Deletes a large object
		/// </summary>
		public void Delete(int oid) {
			if (PG.lo_unlink(psql.conn, oid) < 0)
				throw new PostgreSQLException(PG.PQerrorMessage(psql.conn));
		}
		
		/// <summary>
		/// Exports a large object to a local file
		/// </summary>
		public void Export(int oid, string filename) {
			if (PG.lo_export(psql.conn, oid, filename) < 0)
				throw new PostgreSQLException(PG.PQerrorMessage(psql.conn));
		}

		/// <summary>
		/// Imports a local file into a large object and returns its id
		/// </summary>
		public int  Import(int oid, string filename) {
			int id = PG.lo_import(psql.conn, filename);
			if (id < 0)
				throw new PostgreSQLException(PG.PQerrorMessage(psql.conn));
			
			return id;
		}

		/// <summary>
		/// Create a new large object and returns the object id
		/// </summary>
		public int Create() {
			psql.StartTransaction();
			int oid = PG.lo_creat(psql.conn, PG.INV_READ|PG.INV_WRITE);	
			psql.ComitTransaction();
			return oid;
		}
		
		/// <summary>
		/// Opens a large object for reading/writing
		/// </summary>
		public void Open(int id) {
			psql.StartTransaction();
			fd = PG.lo_open(psql.conn, id, PG.INV_READ|PG.INV_WRITE);
			if (fd < 0) {
				psql.RollbackTransaction();
				throw new PostgreSQLException(PG.PQerrorMessage(psql.conn));
			}
		}
		
		/// <summary>
		/// Closes a large object
		/// </summary>
		public void Close() {
			if (fd < 0)
				return;
			PG.lo_close(psql.conn, fd);
			psql.ComitTransaction();
			fd = -1;
		}
		
		/// <summary>
		/// Write data to the large object
		/// </summary>
		public int Write(byte[] data, int length) {
			return PG.lo_write(psql.conn, fd, data, length);
		}
		
		/// <summary>
		/// Reads data from the large object
		/// </summary>
		public int Read(byte[] data, int length) {
			return PG.lo_read(psql.conn, fd, data, length);
		}
		
		/// <summary>
		/// The size of the large object
		/// </summary>
		public int Size() {
			return PG.lo_length(psql.conn, fd);
		}
	}
}

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