Introducing Npgsql

A .Net Data Provider for Postgresql

PGCON 2008 – Ottawa – Canada

Francisco Figueiredo Jr. Hiroshi Saito Josh Cooley [email protected] [email protected] [email protected]

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

History Features Internals About Npgsql in Japan Deployment of Npgsql Gnu/Linux <-> Windows

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

Samples Console Imagemake Large Object System.Transactions Demo Entity Framework Community Future work

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 philosophy I was only an spectator. ● I wanted to contribute I wanted a project to work on a network protocol

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

I had a look at Postgresql and MySql I decided by Postgresql because of the excellent protocol documentation! Where Npgsql name came from? ● Miguel de Icaza once asked me that

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)

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

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

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

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

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

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

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 fixes And there is Npgsql 2.0 which is being actively developed and targets .Net 2.0+ runtime

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 nant Starting with Beta3, thanks to Jon Hanna's work, Npgsql now returns data on demand to clientPGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Internals

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

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); }

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

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

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

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

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.

PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Linux<->Windows

Linux(OpenSUSE 10.1) Windows­XP SP2

Use of the same binary and source is possible.

PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. OpenSUSE 10.1 (Linux)

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

・ Npgsql.NpgsqlFactory - DbProviderFactories

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

・ Npgsql.Security ­NpgsqlMembershipProvider ­NpgsqlProfileProvider ­NpgsqlRoleProvider ※ ログインソリューションを提供

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.READW RITE); 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();

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

.Net and Mono

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(); }

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

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

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

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

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

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

ADO.NET Entity Framework The Entity Data Model

Bottle Winery

ADO.NET 2.0 LINQ over Entities Supplier

Object Services ADO.NET Providers

Entity Client Language Integrated Query (LINQ)

ADO.NET Providers <author/> <year/> <price/> </book></p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Entity Framework Overview LINQ EDM MetaData ESQL IEnumerable<T> LINQ to Entities</p><p>Object Object Services Metadata</p><p>ESQL CQT DataReader O­C Map</p><p>Client View Engine CQT Conceptual Schema</p><p>EntityClient Data Provider C­S Native CQT DataReader Map SQL</p><p>ADO.NET Data Provider Store Schema ADO.NET Data ProviderADO.NET Data Providert</p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. EDMGEN.EXE</p><p>• 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</p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Entity Framework Demo</p><p>Generate Model ESQL with objects LINQ with objects LINQ with relationship restrictions</p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Future of Npgsql</p><p>Npgsql 2.0 release (see current betas) Includes support for .NET 2.0 Includes support for .NET 3.5 SP1 Custom Type support Better Visual Studio integration for 2005 and 2008 (DDEX support).</p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Community</p><p>Npgsql has a very active community Project is hosted at pgfoundry.org. Previously it was hosted at gborg.org In March, we registered npgsql.org domain ● Thanks Magnus for registrar tip! :)</p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Community</p><p> pgfoundry.org provided us with a great user interface which were forums. They improved very much user interaction Users are very active at Npgsql forums We get great feedback there Many users provide valuable bug reports and fixes as well!!</p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Community</p><p>We have some friendly urls redirections: http://project.npgsql.org http://documentation.npgsql.org http://forums.npgsql.org http://cvs.npgsql.org</p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. Obrigado – Arigato – Thank you</p><p>References: http://www.microsoft.com/japan/msdn/ http://www.mono­project.com/Main_Page</p><p> http://www.npgsql.org/</p><p>Francisco Figueiredo Jr. Hiroshi Saito Josh Cooley francisco@npgsql.org hiroshi@npgsql.org jbcooley@npgsql.org</p><p>PGCON2008 – Ottawa – Copyright© 2002­2008 Npgsql Development Group. All Rights Reserved. </p> </div> </article> </div> </div> </div> <script type="text/javascript" async crossorigin="anonymous" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8519364510543070"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <script> var docId = 'edecbc002d06751d6c5967b8939bb662'; var endPage = 1; var totalPage = 39; var pfLoading = false; window.addEventListener('scroll', function () { if (pfLoading) return; var $now = $('.article-imgview .pf').eq(endPage - 1); if (document.documentElement.scrollTop + $(window).height() > $now.offset().top) { pfLoading = true; endPage++; if (endPage > totalPage) return; var imgEle = new Image(); var imgsrc = "//data.docslib.org/img/edecbc002d06751d6c5967b8939bb662-" + endPage + (endPage > 3 ? ".jpg" : ".webp"); imgEle.src = imgsrc; var $imgLoad = $('<div class="pf" id="pf' + endPage + '"><img src="/loading.gif"></div>'); $('.article-imgview').append($imgLoad); imgEle.addEventListener('load', function () { $imgLoad.find('img').attr('src', imgsrc); pfLoading = false }); if (endPage < 7) { adcall('pf' + endPage); } } }, { passive: true }); </script> <script> var sc_project = 11552861; var sc_invisible = 1; var sc_security = "b956b151"; </script> <script src="https://www.statcounter.com/counter/counter.js" async></script> </html><script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script>