Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » Utilities » Downloads
 
Add your own
alternative version
Go to top

PostgreSQL Synchronization Tool

, 19 Aug 2009
Idea and implementation of a simple and easy database synchronization tool.
psync.zip
npq
npq
npq.userprefs
npq.usertasks
bin
Release
npq.dll
npq.pidb
psync
bin
Release
npq.dll
psync.exe
psync.pidb
/* 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.Runtime.InteropServices;

namespace npq {

	/// <summary>
	/// Connection status
	/// </summary>
	public enum ConnStatusType {
		CONNECTION_OK,
		CONNECTION_BAD,
		CONNECTION_STARTED,
		CONNECTION_MADE,
		CONNECTION_AWAITING_RESPONSE,
		CONNECTION_AUTH_OK,
		CONNECTION_SETENV,
		CONNECTION_SSL_STARTUP,
		CONNECTION_NEEDED
	}
	
	/// <summary>
	/// Query status
	/// </summary>
	public enum ExecStatusType {
		PGRES_EMPTY_QUERY,
		PGRES_COMMAND_OK,
		PGRES_TUPLES_OK,
		PGRES_COPY_OUT,
		PGRES_COPY_IN,
		PGRES_BAD_RESPONSE,
		PGRES_NONFATAL_ERROR,
		PGRES_FATAL_ERROR
	}
	
	
	/// <summary>
	/// libpq wrapper
	/// </summary>
	public class PG {

		// Connection functions
		
		/// <summary>
		/// Makes a new connection to the database server.
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern IntPtr PQconnectdb(string conninfo);
		/// <summary>
		/// Close a connection
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern void PQfinish(IntPtr conn);
		
				
		/// <summary>
		/// Returns the connection status
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern ConnStatusType PQstatus(IntPtr conn);
		
		[DllImport("libpq.dll", EntryPoint="PQerrorMessage", CharSet=CharSet.Ansi)]
		private static extern IntPtr __PQerrorMessage(IntPtr conn);
		/// <summary>
		/// Returns the last error message
		/// </summary>
		public static string PQerrorMessage(IntPtr conn) { return Marshal.PtrToStringAnsi(__PQerrorMessage(conn)); }
		
		/// <summary>
		/// Returns the protocol version
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int PQprotocolVersion(IntPtr conn);
		/// <summary>
		/// Returns the server version
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int PQserverVersion(IntPtr conn);
		
		/// <summary>
		/// Execute a query
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern IntPtr PQexec(IntPtr conn, [MarshalAs(UnmanagedType.LPStr)] string query);	
		
		/// <summary>
		/// Returns the result status of a query
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern ExecStatusType PQresultStatus(IntPtr res);
		[DllImport("libpq.dll", EntryPoint="PQresultErrorMessage", CharSet=CharSet.Ansi)]
		private static extern IntPtr __PQresultErrorMessage(IntPtr res);
		public static string PQresultErrorMessage(IntPtr res) { return Marshal.PtrToStringAnsi(__PQresultErrorMessage(res)); }
		/// <summary>
		/// Frees the result
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern void PQclear(IntPtr res);
		
		/// <summary>
		/// The number of rows in the result
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int PQntuples(IntPtr res);
		/// <summary>
		/// The number of fields in the result
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int PQnfields(IntPtr res);
		
		[DllImport("libpq.dll", EntryPoint="PQfname",CharSet=CharSet.Ansi)]
		private static extern IntPtr __PQfname(IntPtr res, int field_num);
		/// <summary>
		/// Returns the field name
		/// </summary>
		public static string PQfname(IntPtr res, int field_num) { return Marshal.PtrToStringAnsi(__PQfname(res, field_num)); }
			
		/// <summary>
		/// Returns the field type
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int PQftype(IntPtr res, int field_num);
		/// <summary>
		/// Returns the field type as a string
		/// </summary>
		public static string PQftypename(IntPtr conn, int ftype) {
			IntPtr result = PQexec(conn, "select typname from pg_catalog.pg_type where oid = "+ftype);
			string name = PQgetvalue(result, 0, 0);
			PQclear(result);
			return name;
		}
		
		[DllImport("libpq.dll", EntryPoint="PQgetvalue",CharSet=CharSet.Ansi)]
		private static extern IntPtr __PQgetvalue(IntPtr res, int tup_num, int field_num);
		/// <summary>
		/// Gets the value of the result
		/// </summary>
		public static string PQgetvalue(IntPtr res, int tup_num, int field_num) { return Marshal.PtrToStringAnsi(__PQgetvalue(res, tup_num, field_num)); }
		
		[DllImport("libpq.dll", EntryPoint="PQcmdTuples",CharSet=CharSet.Ansi)]
		private static extern IntPtr __PQcmdTuples(IntPtr res);
		/// <summary>
		/// Gets the result for insert, update, or delete
		/// </summary>
		public static int PQcmdTuples(IntPtr res) { return int.Parse(Marshal.PtrToStringAnsi(__PQcmdTuples(res))); }
		
		// Large-object access routines
		
		/// <summary>
		/// Large Object Write Mode
		/// </summary>
		public const int INV_WRITE = 0x00020000;
		/// <summary>
		/// Large Object Read Mode
		/// </summary>
		public const int INV_READ = 0x00040000;

		/// <summary>
		/// Opens a large object and returns a file descriptor
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_open(IntPtr conn, int lobjId, int mode);
		/// <summary>
		/// Closes an opened large object
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_close(IntPtr conn, int fd);
		/// <summary>
		/// Read data from the large object
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_read(IntPtr conn, int fd, [MarshalAs(UnmanagedType.LPArray)] byte[] buf, int len);
		/// <summary>
		/// Write data into the large object
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_write(IntPtr conn, int fd, [MarshalAs(UnmanagedType.LPArray)] byte[] buf, int len);
		/// <summary>
		/// Seek inside the large object
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_lseek(IntPtr conn, int fd, int offset, int whence);
		/// <summary>
		/// Create a new large object
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_creat(IntPtr conn, int mode);
		/// <summary>
		/// Creates a new large object with a specified id
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_create(IntPtr conn, int lobjId);
		/// <summary>
		/// Tell me the position within the new large object
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_tell(IntPtr conn, int fd);
		/// <summary>
		/// Truncate a large object to a given length
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_truncate(IntPtr conn, int fd, int len);
		/// <summary>
		/// Delete a large object
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_unlink(IntPtr conn, int lobjId);
		/// <summary>
		/// Import a file and returns the large object id
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_import(IntPtr conn, [MarshalAs(UnmanagedType.LPStr)] string filename);
		/// <summary>
		/// Export a large object into a file
		/// </summary>
		[DllImport("libpq.dll")]
		public static extern int lo_export(IntPtr conn, int lobjId, [MarshalAs(UnmanagedType.LPStr)] string filename);

		/// <summary>
		/// Returns the length of a large object
		/// </summary>
		public static int lo_length(IntPtr conn, int fd) {
			lo_lseek(conn, fd, 0, 2);
			int len = lo_tell(conn, fd);
			lo_lseek(conn, fd, 0, 0);
			
			return len;
		}
	}
}

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)

Share

About the Author

mf040
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

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 19 Aug 2009
Article Copyright 2009 by mf040
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid