Next Previous Contents

11. Perl Database Interface (DBI) Driver gia PostgreSQL

11.1 Diasundesh Perl 5 me PostgreSQL

PERL einai akrwnumio toy 'Practical Extraction kai Report Language'. H Perl einai dia8esimh sxedon se ka8e leitoyrgiko kai platforma hardware. Mporeite na xrhsimopoihste Perl se Windows95/NT, ola ta Unix (Solaris, HPUX, AIX, Linux, Irix, SCO klp.), se mainframe MVS, desktop OS/2, OS/400, Amdahl UTS kai polla alla. H Perl trexei AKOMH kai se polla mh dhmofilh/genika agnwsta leitoyrgika systhmata kai ylika!!

Etsi, mhn ekplageite an deite thn perl na trexei se polla oxi syxna xrhsimopoioumena leitoyrgika.

Mporeite na fantasteite thn terastia bash xrhstwn kai programmatistwn se Perl.

H diasundesh Perl me PostgreSQL perilambanetai sth dianomh ths PostgreSQL. Koitajte ston katalogo src/pgsql_perl5.

11.2 Perl Database Interface DBI

TI EINAI TO DBI ;

To parakatw einai apospasma apo thn kentrikh selida toy Perl DBI.

To Perl Database Interface (DBI) einai ena programmatistiko interface (API) se Perl gia diasundesh me baseis dedomenwn. Oi prosdiorismoi toy Perl DBI API orizoyn ena sunolo apo roytines, metablhtes kai symbaseis poy parexoyn ena synektiko interface bashs dedomenwn anejarthto apo thn pragmatikh bash poy xrhsimopoieitai. Oi plhrofories gia ayto to tmhma DBI prohl8e apo to keimeno 'DBI FAQ' to opoio exei gracei o Alligator Descartes to anaparagagame me thn adeia toy.

Odhgos DBI gia thn PostgreSQL - DBD-Pg-0.89

Katebaste to DBD-Pg-0.89.tar.gz apo ta

APAITHSEIS:

Texnikh yposthrijh gia to DBI

Parakalw steilte ta sxolia sas kai anafores la8wn sto

Symperilabete parakalw thn ejodo ths entolh perl -v, kai perl -V, thn ekdosh ths PostgreSQL, thn ekdosh toy DBD-Pg, kai thn ekdosh toy DBI sthn anafora la8wn.

Ti einai ta DBI, DBperl, Oraperl kai *perl;

Opws eipe kai o Tim Bunce, o sxediasths kai syggrafeas toy DBI:

``To DBI einai ena programmatistiko interface (API) se Perl gia diasundesh me diasundesh me baseis dedomenwn. Oi prosdiorismoi toy DBI API orizoyn ena sunolo apo roytines, metablhtes kai symbaseis poy parexoyn ena synektiko interface bashs dedomenwn anejarthto apo thn pragmatikh bash poy xrhsimopoieitai.''

Me apla logia, to DBI interface epitrepei stoys xrhstes na exoyn diafanh prosbash se pollaplous tupoys basewn. Etsi, an syndeeste thn Oracle, Informix, mSQL, Sybase h opoiadhpote allh bash, de xreiazetai na gnwrizete toys ypokeimenoys mhxanismous toy epipedoy 3GL. To API poy orizetai apo to DBI 8a doylecei se oloys toys tupoys basewn.

Ena paromoio pleonekthma einai h ikanothta na synde8eite se duo baseis diaforetikou tupoy mesw toy idioy perl script, px, 8elete na diabasete dedomena apo mia bash Oracle kai na ta eisagete se mia bash Informix mesa sto idio programma. To epipedo DBI sas epitrepei na to kanete ayto apla kai isxyra.

DBperl einai to palio onoma gia tis prodiagrafes toy interface. Xrhsimopoieitai synh8ws gia na ypodhlwsei ta perl4 modules gia diasundesh me baseis, opws, oraperl, isqlperl, ingperl kai loipa. Ayta ta interfaces den eixan stantar API kai genika den yposthrizontai.

Na mia lista apo DBperl modules, to antistoixo DBI tairi toys kai plhrofories yposthrijhs. Erwthseis sxetika me ton odhgo DBI 8a prepei na apey8unontai sth lista allhlografias dbi-users .

    Onoma Module Apaitoumenh Bash     Syggrafeas          DBI
    ------------ -----------------   ----------          ---
    Sybperl     Sybase              Michael Peppler     DBD::Sybase
                                    <mpeppler@datamig.com>
                                    http://www.mbay.net/~mpeppler
    Oraperl     Oracle 6 & 7        Kevin Stock             DBD::Oracle
                                    <dbi-users@fugue.com>
    Ingperl     Ingres              Tim Bunce &             DBD::Ingres
                                    Ted Lemon
                                    <dbi-users@fugue.com>
    Interperl   Interbase           Buzz Moschetti      DBD::Interbase
                                    <buzz@bear.com>
    Uniperl     Unify 5.0           Rick Wargo          -
                                    <rickers@coe.drexel.edu>
    Pgperl      Postgres            Igor Metz           DBD::Pg
                                    <metz@iam.unibe.ch>
    Btreeperl   NDBM                John Conover        SDBM;
                                    <john@johncon.com>
    Ctreeperl   C-Tree              John Conover        -
                                    <john@johncon.com>
    Cisamperl   Informix C-ISAM     Mathias Koerber     -
                                    <mathias@unicorn.swi.com.sg>
    Duaperl     X.500 Directory     Eric Douglas        -
                User Agent
Parola ayta, merika DBI modules exoyn epipeda prosomoiwshs DBperl. Etsi, h DBD::Oracle erxetai me ena epipedo prosomoiwshs Oraperl, poy sas epitrepei na trejete klhronomika oraperl scripts xwris allages. To epipedo prosomoiwshs metafrazei tis klhseis toy oraperl API se klhseis DBI kai tis ektelei mesw toy DBI switch.

Na enas pinakas me plhrofories gia ta epipeda prosomoiwshs :

    Module          Epipedo prosomoiwshs      Katastash
    ------          ---------------------     ------
    DBD::Oracle     Oraperl                   Oloklhrwmeno
    DBD::Informix   Isqlperl                  Ypo anaptyjh
    DBD::Sybase     Sybperl                   Doyleuei; (xreiazetai epalh8eysh )
    DBD::mSQL       Msqlperl                  Ekdo8hke peiramatika me thn
                                              DBD::mSQL-0.61
H prosomoiwsh Msqlperl einai mia eidikh periptwsh. To Msqlperl einai enas odhgos perl5 gia mSQL baseis, alla de symmorfwnetai me tis prodiagrafes toy DBI. H xrhsh apodokimazetai rhta gia xarh toy DBD::mSQL. Mporeite na katebasete thn Msqlperl apo th CPAN apo to:

Prodiagrafes DBI

Yparxoyn liges phges plhroforias gia DBI.

Yparxoyn duo prodiagrafes dia8esimes se ayto to link, oi nees Proxeires (Draft) DBI prodiagrafes, ayto to keimeno allazei ka8hmerina ka8ws h omada anaptyjhs badizei oros ena sta8ero interface, kai oi palies istorikes DBperl prodiagrafes apo tis opoies anaptux8hke to trexon DBI interface.

To teleytaio keimeno prepei na diabastei mono kai mono apo istoriko endiaferon kai na mh xrhsimopoih8ei ws egxeiridio programmatismou, h ws egkyro me opoiadhpote ennoia. Parola ayta, paramenei mia akomh xrhsimh phgh anaforas.

Tekmhriwsh POD Ta PODs einai kommatia tekmhriwshs poy enswmatwnontai synh8ws se programmata perl poy tekmhriwnoyn ton kwdika ``in place'', parexontas ena xrhsimo boh8hma gia programmatistes kai xrhstes modules. To POD gia DBI kai drivers ginetai olo kai perissotero koinotopo, kai h tekmhriwsh gia ayta ta modules mporei na diabastei me tis parakatw entoles.

Prodiagrafes DBI Ta POD gia prodiagrafes DBI mporei na diabastei me thn entolh:

perldoc DBI

Oraperl Oi xrhstes toy epipedoy prosomoiwshs Oraperl poy erxetai mazi me thn DBD::Oracle, mporoun na synexisoyn na diabasoyn gia to pws na programmatisoyn me to Oraperl plhktrologwntas:

perldoc Oraperl

Ayto 8a paragei ena enhmerwmeno antigrafo ths arxikhs selidas boh8eias oraperl, poy grafthke apo ton Kevin Stock gia perl4. To API ths oraperl emfanizetai kai perigrafetai ekei plhrws.

DBD::mSQL Oi xrhstes toy DBD::mSQL module mporoun na diabasoyn gia merikes private functions kai idiomorfies aytou toy odhgou (driver) plhktrologwntas:

perldoc DBD::mSQL

Syxna Diatypwmenes Erwthseis (FAQ)

To keimeno, Frequently Asked Questions einai epishs dia8esimo ws tekmhriwsh se POD ! Mporeite na to diabasete sto diko sas susthma, plhktrologwntas:

perldoc DBI::FAQ

Ayto mporei na einai bolikotero gia an8rwpoys poy den einai syndedemenoi monima h aneta, sto Internet.

To POD genika Plhroforhsh gia th syggrafh POD, kai gia thn filosofia toy POD genikotera, mporei na diabastei grafontas:

perldoc perlpod

Xrhstes poy exoyn egkatasthsei to Tk module isws na endiaferontai na ma8oyn pws yparxei dia8esimos enas POD anagnwsths basismenos se Tk poy legetai tkpod, kai diamorfwnei POD se mia bolikh kai anagnwsimh morfh.

``Periplanhseis'', ``Mezedakia'' kai Parathrhseis Yparxei mia seira apo peristasiakes ``periplanhseis'' apo diaforoys stis listes allhlografias gia DBI.

``DBI -- The perl5 Database Interface'' Ayto einai ena ar8ro apo ton Alligator Descartes kai ton Tim Bunce gia th domh toy DBI. Dhmosieuthke sto teuxos 5 toy ``The Perl Journal''. Einai yperbolika kalo. Phgainete kai agoraste to periodiko. Edw poy ta leme, agoraste ola ta teuxh! ``The Perl Journal''s WWW site is:

``DBperl'' Ayto to ar8ro, to opoio dhmosieuthke sthn ekdosh Noembrioy toy 1996 ``Dr. Dobbs Journal'' asxoleitai me DBperl.

``The Perl5 Database Interface'' Ayto einai ena biblio grammeno apo ton Alligator Descartes kai ekdo8hke apo toys O'Reilly kai Associates.

Listes Allhlografias yparxoyn treis listes allhlografias gia DBI poy diaxeirizetai o Ted Lemon. Se oles mporeite na grafteite kai na jegrafteite apo to World Wide Web sth dieu8ynsh :

Oi listes stis opoies mporoun na symmetexoyn oi xrhstes einai:

dbi-announce Ayth h lista allhlografias mono gia anakoinwseis. An den mporeite na xrhsimopoihsete thn forma ths parapanw WWW selidas, eggrafeite sth lista me ton akoloy8o tropo:

me keimeno sto swma (body) toy mhnumatos 'subscribe'

dbi-dev H lista ayth stoxeuei se xrhsh apo toys programmatistes poy kanoyn anaptyjh, poy syzhtane idees kai ennoies gia ta twn DBI interface, API kai driver. An den mporeite na xrhsimopoihsete thn forma ths parapanw WWW selidas, eggrafeite sth lista me ton akoloy8o tropo:

me keimeno sto swma (body) toy mhnumatos 'subscribe'

dbi-users Ayth h lista allhlografias einai mia lista genikwn syzhthsewn poy xrhsimopoieitai gia anafores sfalmatwn, syzhthsh problhmatwn kai genikes anazhthseis. An den mporeite na xrhsimopoihsete thn forma ths parapanw WWW selidas, eggrafeite sth lista me ton akoloy8o tropo:

me keimeno sto swma (body) toy mhnumatos 'subscribe'

Xronika/Arxeia twn Listwn Allhlografias

Problhmata Metaglwttishs h alliws "Kophke stis Ejetaseis!"

An exete core dump, dokimaste to module Devel::CoreStack gia na dhmioyrghsete ena stack trace apo to core dump. To Devel::CoreStack brisketai sto CPAN sto:

Steilte Email sth lista dbi-users to stack trace, thn ekdosh twn modules, ths perl, ta test poy kanate, thn ekdosh toy leitoyrgikou systhmatos kai opoies alles sxetikes plhrofories. Oses perissoteres plhrofories steilete, toso grhgorotera 8a anixneusoyn oi programmatistes ta problhmata. An de mas steilte tipota, tote mhn perimenete tipota kai apo emas.

-->

Yposthrizetai to DBI katw apo platformes Windows 95 / NT ;

H metafora twn DBI kai DBD::Oracle Win32 einai twra ena stantar tmhma toy DBI, etsi, an katebasete ekdosh toy DBI megaluterh toy 0.81 8a prepei na doyleuei mia xara. Mporeite na exete prosbash stis baseis Microsoft Access kai SQL-Server apo to DBI mesw ODBC. Mazi me to DBI-0.79 (kai metepeita) dinetai ena peiramatiko DBI 'epipedo prosomoiwshs' gia to module Win32::ODBC. Onomazetai DBI::W32ODBC. 8a xreiasteite to module Win32::ODBC.

Ti einai to DBM; Kai giati 8a prepei na xrhsimopoiw to DBI sth 8eshtoy;

To UNIX eylogh8hke apo thn arxh me aples ``baseis'' basismenes se epipeda arxeia, gnwstes san susthma dbm. To dbm sas epitrepei na apo8hkeuete dedomena se arxeia, kai na anaktate ta dedomena grhgora Parolayta, ayto exei kai sobara meionekthmata.

Kleidwma Arxeiwn (File Locking)

Ta systhmata dbm den pareixan idiaitera isxyres dynatothtes gia kleidwma arxeiwn, oute kamia ikanothta gia th dior8wsh problhmatwn poy proekyptan apo taytoxrones eggrafes [sth bash].

Ay8airetes Domes Dedomenwn (Arbitrary Data Structures)

Ta systhmata dbm epitrepoyn mono mia sta8erh domh dedomenwn: zeygaria kleidiwn-timwn (key-value pairs). Ayth h timh mporei na einai ena sun8eto antikeimeno, opws ena [ C ] struct, alla to kleidi prepei na einai monadiko. Ayto htan megalos periorismos sth xrhsimothta twn systhmatwn dbm.

Parolayta, ta systhmata dbm akoma einai xrhsima gia xrhstes me apla sunola dedomenwn kai periorismenoys poroys, mia kai einai grhgora, dynata kai ejairetika kala elegmena. Ta modules ths Perl gia thn prosbash se systhmata dbm exoyn enswmatw8ei twra sthn dianomh ths, mesw toy module AnyDBM_File.

Perilhptika, to DBM einai mia ikanopoihtikotath lush gia (kata kanona) read-only baseis, h mikra kai apla sunola dedomenwn. Parolayta, gia perissotero isxyra kai epektasima sunola dedomenwn, xwris oute kan na anafer8oume kai se ajiopisto transactional locking, synistoume stoys xrhstes na xrhsimopoihsoyn DBI.

Yposthrizetai < to tade xarakthristiko > sto DBI;

8ewrwntas oti to xarakthristiko poy zhthsate einai ena mh-stantar, ejartwmeno apo kapoia sygkekrimenh bash, xarakthristiko, tote h apanthsh einai oxi.

To DBI antanakla ena geniko API poy 8a doylecei gia tis perissoteres baseis, kai den parexei kamia epipleon leitoyrgikothta gia kapoia sygkekrimenh bash dedomenwn (database-specific functionality).

Parolayta, oi syggrafeis twn odhgwn mporoun, an to epi8ymoun, na symperilaboyn hooks pros ejeidikeymenes leitoyrgies/xarakthristika kapoias bashs dedomenwn mesw ths me8odoy func poy orizetai sto DBI API. Osoi anaptussoyn scripts 8a prepei na shmeiwsoyn pws h leitoyrgikothta poy parexetai mesw twn me8odwn func einai api8ano na metafer8ei se alles baseis.

Xrhsimeuei ka8oloy to DBI gia programmatismo se CGI;

Me mia lejh, nai! To DBI einai ejairetika xrhsimo gia programmatismo se CGI! Sthn pragmatikothta, 8a tolmousa na pw oti o programmatismos se CGI einai mia apo tis kuries xrhseis toy DBI.

To DBI dinei thn ikanothta stoys programmatistes CGI na enisxusoyn tis baseis poy exoyn WWW-fronted, pragma poy parexei stoys xrhstes th dynatothta na paijoyn me terasties posothtes domhmenwn dedomenwn. Epishs, sthn periptwsh poy ena site exei perissoterh kinhsh apo oti o server ths bashs dedomenwn mporei na antimetwpisei, to DBI dinei th dynatothta anaba8mishs toy server ths bashs dedomenwn sto paraskhnio, xwris allages sta cgi scripts.

Pws petyxainw taxuteroys xronoys sundeshs me DBD Oracle kai CGI;

O Apache httpd diathrei mia dejamenh (pool) apo paidia toy (httpd children) gia na ejyphretoun tis aithseis twn clients.

Xrhsimopoiwntas to module toy Apache mod_perl toy Doug MacEachern, o metafrasths ths perl (perl interpreter) enswmatwnetai sta paidia toy httpd. Ta CGI, DBI, kai ta ypoloipa agaphmena sas modules mporoun na fortw8oun kata thn ekkinhsh ka8e paidiou (child). Ayta ta modules de 8a janafortw8oun ean den allajoyn sto disko.

Gia perissoteres plhrofories sxetika me ton Apache, deite to Apache Project's WWW site:

Pws epitygxanw monimes (persistent) syndeseis metaju DBI kai CGI

Xrhsimopoiwntas to module Apache::DBI module toy Edmund Mergl, ta logins sth bash dedomenwn apo8hkeuontai kai antistoixizontai se ena httpd child. An h efarmogh sas basizetai se enan mono xrhsth ths bashs dedomenwn, h sundesh mporei na ginetai me ka8e child. Mexri shmera, oi syndeseis stis baseis dedomenwn den mporoun na moirazontai metaju diaforetikwn paidiwn toy httpd.

Mporeite na katebasete to Apache::DBI apo th CPAN mesw:

``Otan trexw ena perl script apo thn grammh entolhs, doyleuei, alla otan to trexw apo ton httpd, apotygxanei!'' Giati;

Basika, enas logos gia ton opoio mporei na symbainei ayto einai oti o xrhsths apo ton opoio trejate to script sth grammh entolwn exei swsta ry8mismena tis metablhtes toy periballontos, sthn periptwsh toy DBD::Oracle, metablhtes opws $ORACLE_HOME, $ORACLE_SID h TWO_TASK.

H diergasia httpd trexei synh8ws katw apo to xrhsth nobody, pragma poy shmainei oti den exei ry8mismeno to periballon toy. Opoiodhpote script epixeirhsei na trejei katw apo aythn thn katastash 8a apotuxei sigoyra.

Gia na lusete ayto to problhma, 8este to periballon gia th bash sas se ena mplok BEGIN ( ) sthn arxh toy script sas. Etsi 8a dior8w8ei to problhma.

Paromoia, 8a prepei na elegjete to logfile me ta httpd la8h gia tyxon endeijeis, opws kai ta ``Idiot's Guide To Solving Perl / CGI Problems'' kai ``Perl CGI Programming FAQ'' gia perissoteres plhrofories. Einai api8ano to problhma na exei sxesh me to DBI. Diabaste kai ta duo ayta keimena prosektika!

Mporw na kanw multi-threading me to DBI;

Mexri thn hmeromhnia aytou toy FAQ, oxi. H perl den yposthrizei multi-threading. Parolayta, to multi-threading anamenetai na ginei meros toy pyrhna ths dianomhs ths perl sthn ekdosh 5.005, poy shmainei oti to DBI 8a mporei na yposthrizei multi-threading arketa suntoma meta ap' ayto.

Gia na deite paradeigmata kwdika OCI gia thn Oracle poy exei multi-threaded entoles SELECT, phgainete sto:

Pws mporw na kalesw stored procedures me to DBI;

An ypo8esoyme oti exete dhmioyrghsei mia stored procedure mesa se kapoia bash, px, sthn Oracle, mporeite na xrhsimopoihsete to $dbh->do gia na ektelesete amesa th diadikasia (procedure). Gia paradeigma,

$dbh->do( "BEGIN someProcedure END" );

Pws mporw na parw tis times poy epistrefontai apo tis stored procedures me to DBI;

8ymh8eite, omws, na kanete elegxo gia la8h!

    $sth = $dbh->prepare( "BEGIN foo(:1, :2, :3); END;" );
    $sth->bind_param(1, $a);
    $sth->bind_param_inout(2, \$path, 2000);
    $sth->bind_param_inout(3, \$success, 2000);
    $sth->execute;

Pws mporw na dhmioyrghsw h na katastrecw mia bash dedomenwn me to DBI;

H dhmioyrgia kai diagrafh mias bashs einai ennoies polu afhrhmenes gia na yposthrizontai ikanopoihtika apo to DBI. Gia paradeigma, h Oracle den yposthrizei kan thn ennoia ths diagrafhs ths bashs! Sthn Oracle epishs, o diakomisths ths bashs dedomenwn (database server) einai oysiastika h bash, enw sthn mSQL, h diergasia toy diakomisth (server process) trexei mia xara xwris na exei dhmioyrgh8ei kamia bash se ayton. To problhma einai toso anomoio gia na to 8ijoyme.

Gi' ayto to logo, merikoi odhgoi yposthrizoyn dhmioyrgia kai diagrafh basewn mesw twn private func methods. Deite thn tekmhriwsh twn odhgwn poy xrhsimopoieite gia na deite an yposthrizoyn ayto to mhxanismo.

Pws diaxeirizetai to DBI tis times NULL ;

Oi times NULL sto DBI antimetwpizontai opws h timh undef. Oi times NULL mporoun kataxwrh8oun stis baseis ws NULL, gia paradeigma:

    $rv =
        $dbh->do( "INSERT INTO table VALUES( NULL )" );
alla otan erwth8oun, oi times NULL 8a prepei na sygkri8oun me thn undef. Ayto einai stantar gia oloys toys odhgous.

Ti einai aytes oi me8odoi func (func methods);

H me8odos func orizetai sto DBI san to shmeio eisodoy gia leitoyrgikothta ejartwmenh apo th bash, px, h ikanothta na dhmioyrghsete h na diagracete baseis. To na kalesete aytes tis me8odoys poy ejartwntai apo toys odhgous einai aplo, gia paradeigma, gia na kalesete th me8odo createDatabase poy pairnei mia parametro, 8a grafame:

    $rv =
        $dbh->func( 'argument', 'createDatabase' );
Osoi anaptussoyn logismiko 8a prepei shmeiwsoyn oti oi me8odoi func den einai metafersimes metaju diaforetikwn basewn.

Emporikh Yposthrijh kai Ekpaideysh

To Interface ths Perl5 gia baseis einai ena DWREAN logismiko. ERXETAI XWRIS KAMIA EGGYHSH OPOIOYDHPOTE EIDOYS.

Parolayta, merikoi organismoi parexoyn eite texnikh yposthrijh eite programmata ekma8hshs toy DBI.

Perl Clinic : H Perl Clinic mporei na kleisei symbolaia gia emporikh yposthrijh se Perl, DBI, DBD::Oracle kai Oraperl. Yposthrijh parexetai apo thn etaireia sthn opoia doyleuei o Tim Bunce, o syggrafeas toy DBI. Gia perissoteres yphresies sxetika me tis yphresies toys, deite to :


Next Previous Contents