Posts RSS Comments RSS 14 Posts and 0 Comments till now

An update on PGORM.

It has been sometime that I have released an early version of PGORM (version 0.10.4). This is just an update about where PGORM is going with the next version.

I have completely rewritten the PostgreSQL schema reader. The new schema reader heavily relies on Npgsql to determine and translate PostgreSQL datatypes to CLR (.NET) datatypes.

The project builder also has been rewritten. It is much simpler now. The new project builder will not  use MSBuild anymore.

The new code generator now completely supports PostgreSQL’s ENUM datatype. These types will be translated to .NET enum types and can be used accordingly.

The generated API is going to be more advanced in the next release. It supports both generic and non generic methods.

I am going to release a new version when all tests are completed. So stay tuned….

This project is hosted on:

http://code.google.com/p/pgorm/

 

 

 

 

 

 

 

PGORM Release.10

After a couple of nights working late on this, I have added a little wizard to help creating ORM object faster.
Here is the results in Release.10 on http://code.google.com/p/pgorm/

PostgreSQL Object Relational Mapper for .NET Release 0.9

I am proud to announce the PGORM 0.9 release!
This release is a tested version on several databases including the
sample database Dellstore2.

Features added in this release:

- Generate “Get”,”Update”, and “Delete” methods based on column indexes
- Generate “Get”,”Update”, and “Delete” methods based on foreign keys
- Generate “Get” methods for indexed columns on views
- Generate .NET methods representing functions (stored procedures) in the database
- Generate nullable properties for the generated classes (tables)
- Auto build (compile) the generated projects.

Project Home:
http://pgorm.googlecode.com

Writing Cocoa Application in C#

You might think that this must be a joke. But you actually can develop Cocoa applications in C#. The trick to do this is to use a very cool bridge framework that is called Monobjc. Essentially this is a mono/C# wrapper around the Cocoa framework.

Monobjc can be very usefull when porting existig .NET applications to Cocoa to be rewritten in read ObjectiveC/Cocoa.

Independent software developer for the Mac

Yesterday I read a very interesting research about the culture and economy of the independent software developer for the Mac. This research is done by Michiel van Meeteren as his bachelor thesis human geography at the University Of Amsterdam.

Becoming a Jedi

After a long delay I have started learning Cocoa for the Mac. So far it looks very exciting compared to other software development experiences I have had the past 15 years. The following are the books which I am using to become a Jedi.



If you are going to develop software for a system, first you have to know and get familiar with that system. I have chosen this book to guide me. It is easy reading and contains a lot of information if you are new to the Mac.



This is probably the best book ever written to learn Mac development using Cocoa and Objective-C. It is very well written and gives you more than enough information to become a Mac developer. Many subjects are covered in this book. Fortunately this books does not go too deep into the subject matter. It stays focused on teaching you the most important thing s about Cocoa and Objective-C



At the time of writing, this books was not available yet. But judging the first edition of this book, it is the best books written to learn the Objective-C programming language.

T-DOSE 2008

This year’s t-dose meeting was fun. The very young Dutch PostgreSQL community also had a booth where we could answer various question. Personally for me it was very enlightening to see what people are considering when it comes to switching between databases. The majority of question we got was about why one should migrate from other databases (especially MySQL) to PostgreSQL. This triggered some ideas for talks in the future.

I was positively surprised to see a gentleman from the dutch opensource software initiative donating an amount of money to the PostgreSQL EU community.

Here are some photos:









Compiling Qt with native PostgreSQL drivers on Windows XP

I spent the last three hours looking and Googling to solve a very annoying problem in order to compile PostgreSQL drivers natively with Qt 4.4.0 framework. After a lot of frustration I discovered that one has to provide the path to libpq.lib and the PostgreSQL include dir in UNIX style or 8.3 short filenames to get the qmake.exe to read them correctly.

After running configure.exe, I had to modify .qmake.cache like to following to make it work:

QMAKE_QT_VERSION_OVERRIDE = 4
LIBS           += C:\PROGRA~1\POSTGR~1\8.3\lib\libpq.lib
LIBPATH        += C:\PROGRA~1\POSTGR~1\8.3\lib
INCLUDEPATH    += C:\PROGRA~1\POSTGR~1\8.3\include
OBJECTS_DIR     = tmp\obj\debug_shared
MOC_DIR         = tmp\moc\debug_shared
RCC_DIR         = tmp\rcc\debug_shared
sql-drivers    += odbc psql
sql-plugins    += sqlite
....
....
....
QMAKE_LIBDIR_QT = $$QT_BUILD_TREE\lib

DROP_TABLE_IF_EXISTS

In this post, there is a nice explanation about executing conditional DDL. Here are my two functions which I use when I have to recreate a database schema.

create or replace function table_drop_if_exist(tbl varchar(255)) returns void as
$$
begin
	if (select count(relname) from pg_class where relname=tbl) <> 0 then
		raise notice 'dropping [%] with cascade',tbl;
		execute 'drop table ' || tbl || ' cascade';
	end if;
end;
$$
language plpgsql;

and of course

create or replace function type_drop_if_exist(typ varchar(255)) returns void as
$$
begin
	if (select count(relname) from pg_class where relname=typ and relkind='c') <> 0 then
		raise notice 'dropping [%] with cascade',typ;
		execute 'drop type ' || typ || ' cascade';
	end if;
end;
$$
language plpgsql;

Usage:

select table_drop_if_exist('tblregion');
create table tblregion
(
	regionid integer primary key not null,
	region varchar(50),
	code varchar(2)
);

Synchronizing sequence values upon manual insert.

Using sequences are common practice in order to have auto numbering in a PostgreSQL database. The easiest way to create an auto numbered identity field is to use the SERIAL data type macro.

Here is an example:

CREATE  TABLE customer
(
	customer_id SERIAL NOT NULL PRIMARY KEY,
	name varchar(50)
);

When running this script, PostgreSQL automatically will create a SEQUENCE called customer_id_seq and assign the default value of the customer_id field to the next available value from the sequence. This is done by calling nextval(’customer_id_seq’::regclass).

Upon inserting new records in the customer table, the sequence will work just fine as long as you leave the customer_id field alone. In this the sequence will be incremented twice, generating numbers 1 and 2.

INSERT INTO customer (name) values('Customer 1');
INSERT INTO customer (name) values('Customer 2');

But what will happen to the next value on our sequence when we explicitly present data to the customer_id field? For example when we are loading data from an external source.

INSERT INTO customer (customer_id,name) values(3,'Customer 3');
INSERT INTO customer (customer_id,name) values(4,'Customer 4');
INSERT INTO customer (customer_id,name) values(5,'Customer 5');

As expected the value of customer_id_seq will not increment as we insert data with the example above. This is because nextval(‘customer_id_seq’) will only be triggered when customer_id is NULL.
As expected the value of customer_id_seq will not increment as we explicitly insert data with the example above. It is because nextval(‘customer_id_seq’) will only be triggered when customer_id is NULL. This means when you try to insert another customer record without explicitly providing a unique customer_id, the nextval(‘customer_id_seq’) will incremented to 3 which is already used, resulting a nice duplicate key exception:


INSERT INTO customer (name) values('Customer 6');

----------------------------------------------------------------------
ERROR:  duplicate key value violates unique constraint "customer_pkey"

********** Error **********

ERROR: duplicate key value violates unique constraint "customer_pkey"
SQL state: 23505

This can easily break your application unexpectedly. In In order to solve this problem we have to synchronize the value of customer_id_seq with the latest value existing in the customer_id column: The following function provides just the functionality needed. Just provide the table name and the the name of the identity column which the sequence is bound to.

CREATE OR REPLACE FUNCTION sync_sequence(
		table_name VARCHAR,idfield VARCHAR)
		RETURNS VOID AS
$$
DECLARE
	max_id BIGINT;
BEGIN
	-- retriving max id from table_name
	EXECUTE 'select max(' || idfield || ') from '
		|| table_name into max_id;
	-- resetting the default sequence on table_name
	EXECUTE 'alter sequence ' || table_name || '_' ||
		idfield || '_seq restart with ' || (max_id+1);
END;
$$
LANGUAGE PLPGSQL;
SELECT sync_sequence('customer','customer_id');

Have fun with PostgreSQL :)

Next Page »