Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations &...

39
©2005 Wiseknot. All rights reserved. 1 PGCON2008 – Ottawa – Copyright© 2002-2008 Npgsql Development Group.  All Rights Reserved. Introducing Npgsql A .Net Data Provider for Postgresql PGCON 2008 – Ottawa – Canada Francisco Figueiredo Jr. [email protected] Hiroshi Saito [email protected] Josh Cooley [email protected]

Transcript of Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations &...

Page 1: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

©2005 Wiseknot. All rights reserved. 1PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Introducing NpgsqlA .Net Data Provider for Postgresql

PGCON 2008 – Ottawa – Canada

Francisco Figueiredo [email protected]

Hiroshi [email protected]

Josh [email protected]

Page 2: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Topics

HistoryFeaturesInternalsAbout Npgsql in JapanDeployment of NpgsqlGnu/Linux <-> Windows

Page 3: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Topics

SamplesConsoleImagemake Large ObjectSystem.Transactions DemoEntity Framework

CommunityFuture work

Page 4: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

History

Work began on Npgsql in May 5, 2002

This was when I fist started coding

At that time, I was already a big fan of OpenSource philosophyI was only an spectator.

● I wanted to contribute

I wanted a project to work on a network protocol

Page 5: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

History

I had a look at Postgresql and MySqlI decided by Postgresql because of the excellent protocol documentation!Where Npgsql name came from?

● Miguel de Icaza once asked me that

Page 6: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

History

By the time Npgsql started, it was common to prefix projects with N (for .Net)

● Nant● Nhibernate● NUnit

So I thought about: N(.Net) pgsql (Postgresql)

Page 7: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

History

Beginning of 2003, Daniel Morgan from Mono project told me they choose Npgsql as official Postgresql Data Provider That made us be sure we were in the right track! :)

Page 8: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Features

Npgsql is done with 100% C#There is no dependency on external libraries

● This was by design, so it would be easier to install and deploy

Works on Mono and MS .Net Runtimes. This means it works on Gnu/Linux, Windows, MacOS and any other supported platform

Page 9: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Features

Supports Postgresql server from 7.3 and aboveImplements protocol versions 2 and 3You can use Npgsql to send usual queries: select, insert, update and delete

Page 10: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Features

Has support for Large Object API

● Hiroshi Saito will show an example which uses it

Has support for clear and md5 passwords

● Starting with Npgsql2 beta3, we added support for SSPI authentication on windows. Thanks Brar Piening patch

Page 11: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Internals

Today we have two versions of Npgsql:We have Npgsql 1.0 which is now only maintained with critical bug fixesAnd there is Npgsql 2.0 which is being actively developed and targets .Net 2.0+ runtime

Page 12: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Internals

We have project files for Nant, Monodevelop, VS.Net 2005 and VS.Net 2008 The easiest way to compile Npgsql is grab cvs or source code release, enter Npgsql/src folder and type nantStarting with Beta3, thanks to Jon Hanna's work, Npgsql now returns data on demand to client

Page 13: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Internals

Previous versions got all data from server before making it available to clientThis behavior has a big impact when dealing with large resultsets

Page 14: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Examples

This is a simple console example:

public static void Main(String[] args) { NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=joe;Password=secret;Database=joedata;"); conn.Open(); NpgsqlCommand command = new NpgsqlCommand("select version()", conn); String serverversion; serverversion = (String)command.ExecuteScalar(); Console.WriteLine("PostgreSQL server version: {0}", serverversion); }

Page 15: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Page 16: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

About Npgsql in Japan

Page 17: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Npgsql Document

Page 18: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Npgsql in PostgreSQL 8.2 and under is used by VC#2005. 

Deployment of Npgsql

Page 19: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Deployment of Npgsql

Npgsql in PostgreSQL 8.3 installer is used by VC#2008. 

Page 20: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Linux<->WindowsLinux(OpenSUSE 10.1) Windows­XP SP2

Use of the same binary and source is possible.

Page 21: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

OpenSUSE 10.1 (Linux)

Page 22: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Npgsql2 ­ .NET2.0

・ Npgsql.NpgsqlFactory 

- DbProviderFactories

Page 23: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Npgsql2 ­ .NET2.0

・ Npgsql.Security

­NpgsqlMembershipProvider

­NpgsqlProfileProvider

­NpgsqlRoleProvider

※ ログインソリューションを提供

Page 24: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

ImageMake­LargeObject

NpgsqlTransaction t = Polacz.BeginTransaction();

LargeObjectManager lbm = new LargeObjectManager(Polacz);

LargeObject lo = lbm.Open(takeOID(idtowaru),LargeObjectManager.READWRITE);

byte[] buf = new byte[lo.Size()];

buf = lo.Read(lo.Size());

MemoryStream ms = new

MemoryStream();

ms.Write(buf,0,lo.Size());

lo.Close();

t.Commit();

Page 25: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Page 26: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

System.Transactions in Npgsql

.Net and Mono

Page 27: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Using System.Transactions

using (TransactionScope scope = new TransactionScope()){ using (NpgsqlConnection connection = new NpgsqlConnection(connectionString)) { // auto enlist in transaction connection.Open(); // perform transacted operations } scope.Complete();

}

Page 28: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Distributed Transactions

Requires Windows and the Microsoft CLR.Requires MSDTC to support managing the distributed transaction.Does not yet support recovery from failure in the middle of the two phase commit.

Page 29: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

System.Transactions Demo

Demo distributed transaction with two connectionsDemo recovery from failure during two phase commit

Page 30: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Entity Framework

ADO.NET for .NET 3.5 SP1

*portions of presentation reused from DevDays 2007

with permission from Microsoft

Page 31: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

The Entity Framework

ADO.NET Providers

ADO.NET 2.0 

The Entity Data Model 

Language Integrated Query (LINQ)

Bottle Winery

Supplier

<book>    <title/>    <author/>    <year/>    <price/></book>

ADO.NET Providers

Entity Client

Object Services

LINQ over Entities

ADO.NET Entity Framework

Page 32: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

ADO.NET Data ProvidertADO.NET Data Provider

Entity Framework Overview

ADO.NET Data Provider

Object Services

LINQ to Entities

EDMMetaData

Store Schema

ConceptualSchema

ObjectMetadata

O­CMap

C­SMap

CQT

DataReader

DataReader

IEnumerable<T>ESQL

NativeSQL

CQT

ESQL

LINQ

EntityClient Data Provider

Client View Engine CQT

Page 33: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

EDMGEN.EXE• Generates the model files• Model.ssdl (Store Schema Definition Language)

● Describes tables and columns which map to Entities and Relationships• Model.csdl (Conceptual Schema Definition Language)

● Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets)

• Model.msl (Mapping Specification Language)● Describes how the Entity Framework maps between the Conceptual 

Model (CSDL) and the logical Storage Schema (SSDL)• Model.cs or Model.vb

● Partial class implementing the model created

Page 34: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Entity Framework Demo

Generate ModelESQL with objectsLINQ with objects

LINQ with relationship restrictions

Page 35: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Future of Npgsql

Npgsql 2.0 release (see current betas)

Includes support for .NET 2.0Includes support for .NET 3.5 SP1

Custom Type supportBetter Visual Studio integration for 2005 and 2008 (DDEX support).

Page 36: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Community

Npgsql has a very active communityProject is hosted at pgfoundry.org. Previously it was hosted at gborg.orgIn March, we registered npgsql.org domain

● Thanks Magnus for registrar tip! :)

Page 37: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Community

pgfoundry.org provided us with a great user interface which were forums. They improved very much user interactionUsers are very active at Npgsql forumsWe get great feedback thereMany users provide valuable bug reports and fixes as well!!

Page 38: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Community

We have some friendly urls redirections:

http://project.npgsql.orghttp://documentation.npgsql.orghttp://forums.npgsql.orghttp://cvs.npgsql.org

Page 39: Npgsql - PGCon · Describes the Entity Data Model (incl. EntitySets, EntityTypes, Associations & AssociationsSets) • Model.msl (Mapping Specification Language) Describes how the

 PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group.  All Rights Reserved.

Obrigado – Arigato – Thank you

References:http://www.microsoft.com/japan/msdn/http://www.mono­project.com/Main_Page

http://www.npgsql.org/

Francisco Figueiredo [email protected]

Hiroshi [email protected]

Josh [email protected]