Sunteți pe pagina 1din 57

The Minimum You Need to Know

AboutQtandDatabases

ByRolandHughes

LogikalSolutions

Copyright2010byRolandHughes
Allrightsreserved

ISBN13

9780982358054

ThisbookwaspublishedbyLogikalSolutionsfortheauthor.NeitherLogikalSolutions
northeauthorshallbeheldresponsibleforanydamage,claimorexpenseincurredbya
userofthisbookasaresultofitsuseorrelianceupon.
Thesetrademarksbelongtothefollowingcompanies:
Ask.com
IAC(InterActiveCorp)
Borland
BorlandSoftwareCorporation
DataBoss
KedwellSoftware
Datatrieve
HewlettPackardDevelopmentCompany,L.P.
DEC
HewlettPackardDevelopmentCompany,L.P.
Firebird
GeneralMotorsCorporationwhentalkingaboutcars
Firebird
FirebirdFoundationIncorporatedwhenspeakingofdatabasesoftware
Pontiac
GeneralMotorsCorporation
Interbase
EmbarcaderoTechnologies.
Linux
LinusTorvalds
LotusApproach InternationalBusinessMachinesCorp.
MySQL
MySQLAB
OpenVMS
HewlettPackardDevelopmentCompany,L.P.
PostgreSQL
PostgreSQLGlobalDevelopmentGroup
Quicken
IntuitInc.
Qt
NokiaCorporation
SourceForge
Geeknet,Inc.
Ubuntu
CanonicalLtd.
Unix
TheOpenGroup
Vista
MicrosoftCorporation
Windows
MicrosoftCorporation
Allothertrademarksinadvertentlymissingfromthislistaretrademarksoftheirrespectiveowners. Abesteffortwas
madetoappropriatelycapitalizealltrademarksthatwereknownatthetimeofthiswriting.Neitherthepublishernorthe
authorcanattesttotheaccuracyofthisinformation.Useofaterminthisbookshouldnotberegardedasaffectingthe
validityofanytrademarkorservicemark.

Acknowledgments
I'dliketothankmyloyalbaseofreadersandtheincrediblecoreteamofQt
developersslavingawayonanOpenSourcelibrarythatstandsfulltorsoabovethe
commercialC++crossplatformlibrariesofold.

SourceCodeLicense
Anypersonowningacopyofthisbookmayusethesourcecodefromthisbook
freelywhendevelopingsoftwarefortheirpersonaluse,theircompany'suse,ortheir
client'suse.Suchpersonsmayincludethesourcecodeeithermodifiedorunmodified
providedthatthesourcedeliveredmakesreferencetotheoriginalauthorandispartofa
fullyfunctionalapplication. Itisexpresslyforbiddenforanyonetopostthissource
codeonanybulletinboardsystem,InternetWebsite,orotherelectronicdistribution
medium other than SourceForge.net without the express written permission of the
author.Itisalsoexpresslyforbiddentosellthissourceaspartofalibraryorshareware
distributionofsource.Allfilesforallprojectspresentedinthisbookwillbeavailable
fromhttp://www.theminimumyouneedtoknow.com
Usersofthesourcecodecontainedwithinthisbookagreetoholdharmlessboththe
author and the publisher for any errors, omissions, losses, or other financial
consequencesresultingfromtheuseofsaidsource.Thesourcecodeissuppliedasis
withnowarrantyofanykindexpressedorimplied.

TableofContents
Introduction......................................................................................................................1
1.1WhereWe'veBeenandWhereWeAreGoing.....................................................1
1.2NotaGUITutorial................................................................................................5
1.3ClientServerFullCircle.......................................................................................6
1.4Today'sCrossPlatformChallenge........................................................................8
1.5WhyUbuntu?........................................................................................................9
1.6WhyQt?..............................................................................................................12
Table1:QtDatabaseDrivers...............................................................................14
1.7ATaleofTwoMarketSegments........................................................................17
1.8TheMutantNinjaDataModel............................................................................20
1.9Exercises.............................................................................................................23
CautionaryInformation..................................................................................................25
2.1ScopeofThisChapter.........................................................................................25
2.2DevelopmentRestrictions...................................................................................25
2.3BuildingaQtProgram........................................................................................26
2.4TheDatabaseUsernameRestriction...................................................................27
2.5GarbageCollectionRestrictions..........................................................................31
2.6MoreDatabaseIssues..........................................................................................33
2.7PitfallsofMandatoryPasswords.........................................................................35
2.8DatabaseFrailty..................................................................................................37
2.9Main.cppforWhat?............................................................................................40
2.10TransactionIntegrity.........................................................................................42
2.11SQL!=SQL......................................................................................................49
2.12LassieMightNotComeHome..........................................................................52
2.13ConsistentlyInconsistent...................................................................................54
2.14JustBecauseYouFoundIt,ThatDoesn'tMeanItisThere...............................57
2.15Totr()OrNottotr()?........................................................................................59
2.16YouMightbeOpticallyIsolated.......................................................................64
2.17What?YouWanttoCreateaDatabase?...........................................................66
2.18Exercises............................................................................................................67
PostgreSQLandQt........................................................................................................69
3.1ScopeofThisChapter.........................................................................................69

3.2InstallingandConfiguringPostgreSQL..............................................................78
3.3Designer..............................................................................................................81
3.4InsertingIntoaPostgreSQLTable....................................................................109
3.5OurFlowandWhy............................................................................................114
3.6qDebugorQMessageBox.................................................................................119
3.7ErrorHandlingforRemoteSupport..................................................................124
3.8Pluginvs.CompileInDatabaseInterfaces........................................................131
3.9LoggingOn.......................................................................................................135
3.10HiddenSignalandSlotConnections...............................................................151
3.11ReadingaTextFile.........................................................................................152
3.12OurMainWindow..........................................................................................155
3.13Reporting........................................................................................................177
3.14TheBooleanIssueAgain................................................................................190
3.15BrowsingDatabaseRecords............................................................................192
3.16FollowUp.......................................................................................................199
3.17BackupViaCSV.............................................................................................200
3.18AssignmentOne..............................................................................................207
3.19AssignmentTwo.............................................................................................207
3.20AssignmentThree...........................................................................................208
3.21AssignmentFour.............................................................................................209
3.22AssignmentFive.............................................................................................209
3.23AssignmentSix...............................................................................................210
3.24AssignmentSeven...........................................................................................210
3.25AssignmentEight............................................................................................210
3.26AssignmentNine.............................................................................................210
3.27Exercises.........................................................................................................211
FirebirdandQt.............................................................................................................215
4.1WhyFirebird?...................................................................................................215
4.2Differences........................................................................................................218
4.3ObtainingandInstallingFirebird......................................................................219
4.4QuirksBetweenFlavors....................................................................................224
4.5CreatingUsers...................................................................................................225
4.6CreatingDatabases............................................................................................226
4.7Aliases...............................................................................................................230
4.8NoBoolean.......................................................................................................234

4.9QIBASENotProvidedbyDefault....................................................................235
4.10CSVImportandExportPart1........................................................................242
4.11ConsoleApplicationsandIDEs.......................................................................247
4.12#!/bin/sh..........................................................................................................258
4.13SomeSeriousSQLDifferences.......................................................................260
4.14CSVImportandExportPart2........................................................................266
4.15xpnsfb..............................................................................................................300
4.16ProgrammingAssignment1............................................................................328
4.17ProgrammingAssignment2............................................................................328
4.18ProgrammingAssignment3............................................................................328
4.19ProgrammingAssignment4............................................................................329
4.20ProgrammingAssignment5............................................................................329
4.21ProgrammingAssignment6............................................................................329
4.22FirebirdServerVersionFollowup.................................................................329
4.23DeployingThisContraption............................................................................331
4.24FirebirdSummary...........................................................................................334
4.25Exercises.........................................................................................................335
SQLiteandQt..............................................................................................................339
5.1ProsandCons...................................................................................................339
5.2WhichSQLite?..................................................................................................346
5.3MonkeyStudio..................................................................................................347
5.4ProgrammingAssignment1..............................................................................378
5.5ProgrammingAssignment2..............................................................................378
5.6ProgrammingAssignment3..............................................................................378
5.7ProgrammingAssignment4..............................................................................379
5.8ProgrammingAssignment5..............................................................................379
5.9OurApplicationwithEclipse............................................................................379
5.10ProgrammingAssignment4............................................................................415
5.11ProgrammingAssignment5............................................................................415
5.12ProgrammingAssignment6............................................................................416
5.13BuildingStaticvs.Plugin...............................................................................416
5.14SQLiteFollowup...........................................................................................417
5.15Exercises.........................................................................................................419
RuminationsandObservations.....................................................................................421
6.1CTRLY,CTRLL,andANSI...........................................................................421

6.2Pluginvs.StaticCompile..................................................................................424
6.3WhatDatabaseisRightforYourApplication?.................................................427
6.4Unstart...............................................................................................................431
6.5OutagesandSupport.........................................................................................433
6.6Ubuntu'sOriginalSin........................................................................................437
6.7Qt'sAchillesHeel..............................................................................................440
6.8PackageMaintainervs.MarketingFraud..........................................................441
6.9CodeWhatYouKnow......................................................................................443
6.10ThursdaysWhenItRains................................................................................445
6.11Bitflags............................................................................................................448

Chapter1

Introduction

1.1 WhereWe'veBeenandWhereWeAreGoing
Hopefullyyou'vebeenreadingalonginthisbookseriesandarequicklyprogressing
asadeveloperoratleastgainingasignificantappreciationforwhatdevelopersdo.
Whileyoudonotneedtohavereadtherestoftheseries,itmighthelp.Thesourcecode
for the PostgreSQL expense tracking example in this book has been uploaded to
SourceForge.netandcanbefoundinthexpnsqtproject.Allsourcefileswillbezipped
upandplacedon http://www.theminimumyouneedtoknow.com fordownloadaswell.
Unlikemostbooksinthisseries,therewillnotbeanaccompanyingCDROM.
WhileIdidnotwritethebooksinexactlythisorder,thecorrectorderofreading
wouldhavebeen:
TheMinimumYouNeedtoKnowAboutLogictoWorkinIT
ISBN13

9780977086627

TheMinimumYouNeedtoKnowtoBeanOpenVMSApplicationDeveloper
ISBN13

9780977086634

TheMinimumYouNeedtoKnowAboutJavaonOpenVMS
ISBN13

9780977086610

TheMinimumYouNeedtoKnowAboutServiceOrientedArchitecture
ISBN13

9780977086672

TheMinimumYouNeedtoKnowAboutJavaandxBaseJ
ISBN13

9780982358030

InfiniteExposure
ISBN13

9780977086696

Chapter1Introduction

OriginallyIwrotetheOpenVMSApplicationDeveloperbooktofillaneed.Lotsof
placesrunOpenVMSandnewinstallationsarehappeningallofthetime,yetthereisno
sourcefornewOpenVMSdevelopers.Itwasn'tuntilafterthatbookwaspublishedthat
Istartedgettingfeedbackaboutrecentcollegegraduatesnotunderstandinglogic. At
thatpointIdecidedtomakethisaseriesandbeganwritingthelogicbook.
Withthelogicandtraditionalapplicationdevelopmentbooksoutinthefieldthere
was a natural progression. First we covered Java on OpenVMS using the same
application we had developed in COBOL, BASIC, FORTRAN, C and C++ in the
applicationbook.ItwasnecessarytocoveraccessingofRMSfilesandRDBdatabases
viaJavaalongwithusingFMSasyourscreenmanagementtool.Seasoneddevelopers
wantedtolearnsomeofthenewtechnologyandrecentcollegegradsonlyunderstood
new technology without even the faintest clue about how to access the data which
actuallyranthecompany.
Next we stepped forward with Java on the Ubuntu platform accessing services
presented by OpenVMS on an internal company network. Some of you might be
shocked to learn that The Minimum You Need to Know About Service Oriented
Architecture won a 2008 Best Book Award in the category Business:
Computers/Technology/Internet from USA BookNews andwas a2009 Eric Hoffer
Finalist.TherearealotofbooksaboutServiceOrientedArchitectureonthemarket,
butdamnedfewactuallycoverexposingtheheritagedatasilos.Theyusuallytrytosell
youachunkofmiddlewareandanotherneverendingsupportcontractalongwithshiny
newsecurityholes.
Thenextbookintheseriesdidn'tfollowalogicalprogressionperse,itcameabout
based upon need. I was in the process of designing and writing a fuel surcharge
applicationwhichwouldeventuallybereleasedasaOpenSourceprojectandshouldbe
usable by someone who isn't any smarter than a Microsoft product. That last
requirementmeanttheapplicationhadtouseitsownindexedfilesystemratherthan
mandateauserinstall,configure,andbecomeaDBAforsomedatabaseproduct.

Chapter1Introduction

TheXBASEfamilyoffileformatsarethemostwidelyusedthroughoutthehistory
ofPCcomputing,buttheJavalanguagedoesn'tdirectlyincludeanythingtosupportit,
nordoesitincludeanyotherusableindexedfilesystem.(Yes,thereisnowamassive
developmenteffortcalledJavaDB,butnotatthetimeIneededit.)ThatmeantIhadto
learnalibrary.IdecidedthatifIhadtotakethetimetolearnanewlibrarywithscarce
documentationthatIwouldwriteatutorialandmakelifeeasierforthenextguy.
Unlikethepreviousbooksinthisseries,thexBaseJbookisavailableinelectronic
formonly.YoucanfindthePDFwiththefilesfortheprojectonSourceForge.You
canalsofinditlistedhere:
http://www.freeebooks.net/ebook/TheMinimumYouNeedtoKnowAboutJavaand
xBaseJ
Fromaneducationalperspective,Ineedtoprovideyouwithabookwhichusesa
popular cross platform C++ GUI tool to do some classic standalone development
against a database server. Many in the world of IT will call this ClientServer
development even though both the client and the server are on the same machine.
Historically,thistypeofdevelopmentoccurredpriortoanyoneeventhinkingabout
inventingtheInternet,butmoderneducationwise,itisbesttocoverthistopic after
coveringServiceOrientedArchitecture.
Why?
Kidstodayknowverylittleaboutapplicationdevelopment,buttheyknowalot
aboutsurfingtheWebandbuyingthingsonline.Itiseasierforthemtograspaprocess
whichinvolvessomekindofWebpagesendingdatatosomebackendsystem(Service
OrientedArchitecture.) OnlyafewofthemwillhavehadtoworkwithQuickenor
someotherpersonalfinance/accountingapplicationhostedontheirowncomputer. A
studentwhohasactuallyusedanapplicationonadesktopcomputerwhichstoresdata
onaremotedatabasewithoutgoingthroughtheInternetwillbeararefindindeed.
InfiniteExposureisquitesimplyanovelwhichwaswritteninresponsetoan
interviewquestionIgotfortheOpenVMSApplicationbook.Whileithasgottensome
greatreviews,itisnotpartofmytechnicalbookwriting.

Chapter1Introduction

Some of the previous books in this series used the Mega Zillionare application
developed over and over again so you could learn the differences and tradeoffs
associatedwitheachtoolchoice. IcouldborethelivingHelloutofyoudoingthat
again,butwedon'thavedifferenttools,onlydifferentdatabases.I'mgoingtohaveto
deviatefromthenormanddevelopanewapplicationformultipledatabasestoshow
youthemainissues.Yes,Qtdoesquiteagoodjobofhidingthedatabaseifyouhappen
tobeusingarelationaldatabase,butinmostcases,youcannotsimplychangethe
drivernamerecompileandgo. Therearequiteafewdifferencesbetweensupported
databases.
Don'tworry,Iknow thatmostofyouwillbeGUIonlydevelopersatbestand
cannot begin to function without the continual hand holding of an IDE (Integrated
Development Environment.) While I developed the PostgreSQL version of our
applicationwithnothingmorethanatexteditorandtheQtDesignerforformlayout,
ourlaterexampleswillincludeusingmultipleIDEsandprovidequiteafewproject
creationscreenshots.YouwillbeexposedtoQtCreator,QDevelop,MonkeyStudio,
andEclipsewiththeQtplugin.
Our application will be an expense tracking system which initially uses the
PostgreSQLdatabase.WewillthenredeveloptheapplicationusingFirebird(Borland
Interbase)andSQLite.Iwillspendonechaptercoveringtheuseofstoredprocedures
inPostgreSQLandFirebird.Ourfinaltechnicalchapterwillcoverproblemswithlong
queriesandthreading.ItistoobadOraclehasn'tcreatedaQtplugintoaccessRDBorI
could show you how to use a modern desktop with the world's highest availability
database.
Wewon'tcoverMySQL.Iknowthedatabaseispopular.Iknowmanyofyouwill
wanttouseit,butit'squitethequagmirerightnow. Bydefault,itisnotarelational
database.MyISAMisthedefaultstorageengineandisnowtheonlystorageenginefor
theembeddedversion.InnoDBwaspurchasedbyOraclesometimeagoandOraclehas
startedtosqueezethatorange.
http://digitizor.com/2010/11/05/innodbdroppedfromoraclemysqlclassicedition/

Chapter1Introduction

WhileInnoDBisstillavailableinthecommunityedition,thefactithasbeen
droppedfromtheClassiceditionandInnoDBnowhasitsownembedded/embeddable
versionseemstoindicatethecommunityversionofMySQLisgoingtobetrapped
withastagnantInnoDBrelease.
MyISAMcreatesalotofdatastorageandapplicationdesignproblems.Itdoesn't
currently have a boolean type completely implemented and there is no support for
foreignkeyconstraints.WhiletheBooleanproblemcouldbeconsideredanuisance,the
lackofforeignkeyconstraintsreallytorpedoesthisproject.Whatgoodarereference
tables containing categories and expenses if the database isn't going to enforce
referentialintegrity? IwentthroughthisexactsameproblemeonsagowhenIused
XBASEfilesforeachtable.Withoutanengine,Ihadtoenforcetheintegrityfromthe
applicationlevel.Notsuchagoodthing.
1.2 NotaGUITutorial
ThisbookwillnotbeaGUItutorialnorwillitspendmuchtimecoveringareasof
theQtlibrarywhichdonotpertaintooursampleapplication'sdatabaseportions.While
itistruethatmanydeveloperscouldusethisasaQttutorialalongwiththemanuals
Trolltechprovidesonline,thatisnotthegoalofthisbook.
Ifyouneedadditionalinformationonthegraphicalportionsofthelibraryalong
withthephilosophybehinditallIcanrecommendisapairofexcellentbooks.
FoundationsofQtDevelopmentISBN13:9781590598313
C++GUIProgrammingwithQt4SecondEditionISBN13:9780132354165
Intruth,thisbookismeanttocovertheoneareawherethosebooksreallyfell
down.It'ssadbecauseitistheoneareamostdevelopersactuallyneedtounderstand.
Very few of us get paid to write painting and drawing applications. The bulk of
businessdevelopmentrevolvesaroundgettingdataintoadatabaseandgettingreports
outofthedatabase.

Chapter1Introduction

1.3 ClientServerFullCircle.
TheWebdoesn'tliveuptoitshypeformostapplications.Inmanycasesyouwant
control,security,andlimiteddeployment.Insomecasesyousimplywantastandalone
application or an application that you control access via controlling where it gets
installed.Youdon'twantinformationaboutyournewsupersecretbilliondollarR&D
effortsomeplaceitcouldeasilybepoached.Youwantthatapplicationsecuredonits
owninternalnetworkormachinewithoutanyconnectiontotheInternetatall.
Standalone programs on standalone personal computers make sharing data abit
tough.Withoutanetworkandexposingsomeportionofyourdiskstoragetooutsiders,
theoptionsforsharingdataarelimitedtophysicalmediatransfersalongwithdialand
deliveroptions.
Most of your successful standalone development happened with midrange and
mainframecomputerswherehundredstothousandsofterminalswerescatteredabouta
company,buttheyallconnectedintoasinglecomputer.Thismadeexposingthedataas
simpleasgrantingauseraccesstothefilesordatabases.
Duringthelate1980sthroughthemid1990sClientServerwasboththerage,and
thebaneofcomputing.Nobodywaseverabletogetitright,buteverybodykepttrying.
Therealproblemwithclientserverwasapplicationdeployment. Wedidn'thaveany
automatedmethodsofpushingupdatesouttousersandmostusersdidn'thavethe
technicalabilitytoperformaninstallationontheirown.
WhencorporatedesktopsallransomeformofnonGUIDOS,fileserversseemed
to be the ultimate solution. Products like Netware provided security, the ability to
addressmuchmorediskstoragethanmostversionsofDOS,andevenprovidedan
indexedfilesystemwhichhandledmultiuseraccess.Aslongasthedesktopcomputer
trying to run the application didn't have too much of that precious 640K already
consumed,theycouldruntheexecutablesausercouldaccess.

Chapter1Introduction

Userswantedmoreandmoresophisticatedapplications.Developerswantedtouse
differenttoolswhichdidmoreandmoreoftheprogrammingworkforthem. These
toolshadvariouslicensingsystemswhichinvariablyinvolvedinstallingsomekindof
encryptedlicensekeyfilealongwitharuntimelibrary(RTL)ofsomekind.Whilethe
fileservercouldstillbeagreatplacetohosttheinstallationfilesandtheactualdata
createdbytheapplication,thedaysofhavingasingleexecutablefilewhicheverybody
couldrunwereprettymuchover.
Another drastic thing happened near the end of the 1990s: corporate desktops
startedtodiversify. Whileagreatmanypeoplewillbeignorantenoughtosaythat
Microsoftrulesthecorporatedesktop,theywouldbewrong. Applestartedmaking
someseriousinroadsduetothingstheplatformcoulddowhichMicrosoftsimplycould
not.AfterthedisastrousreleaseofWindowsVista,manycorporationsstartedloading
variousflavorsofLinuxontheircompanydesktopsaswellastheirservers.
Currently,thePCdesktopindustryisinastateofturmoil.Thisturmoilwillexist
forroughlyanotherdecade,butitwillbegoodfortheindustryingeneral.Allbutthe
mosthardenedsupportersofMicrosofthaverealizedthecompanyhasalegacyplatform
whichisinthemodeofcontinuallyshrinkingmarketshare.Applehasaknackwhenit
comestogettingpeopletopayapremiumfortheirstuff,andrecently,perthestock
market,becamebiggerthanMicrosoftonatotal_shares*share_pricebasis.Another
playergainingalotofmomentumandrespectarethevariousLinuxdistributions.After
WindowsVista,anythinglookedstable,socompaniesgaveitatry.Thentheeconomy
tankedworldwide. Suddenlycompanyheadsstartedlookingattheannuallicensing
andsupportcostsofMicrosoftproductsandrunningthenumbersagainsttheLinuxand
OpenSourcealternatives.Whenthesavingsstartedbeing$20$120peremployeeper
yearthe10,000+employeecompaniesstartedtakingnoteandissuingmarchingorders.
TheturmoiliswhatleadtotheSOA(ServiceOrientedArchitecture)stampede.
Somecompaniesneededtogetcustomapplicationsworkingontwotothreeversionsof
Windows as well as the current Apple OS and three to four different Linux
distributions.Ratherthanfightthecrossplatformbattletheylungedforwardattheidea
ofprovidingWebbasedapplicationswhichusedJavaandotherOpenSourcetoolsthat

Chapter1Introduction

ranin/onthemajorbrowsersforeachoperatingsystem.
AgreatdealofeffortwasputforthbytheW3CandmanyotherstomakeSOA
work. A method of deploying services for discovery via WSDL (Web Services
Description Language) was created so any application anywhere could find what it
neededandsimplyplugintotheservice.Theproblemwasmuchofthisdesigncentered
aroundopenness,notprivacyandsecurity.
ItneverceasestoamazemewhenyoungMBAsrefusetowriteanewapplication
onOpenVMSorevenanIBMmainframe.PCsarealltheyhaveeverseen,therefore,
thatmustbeallthereistouse.Theyaresodeadsetagainstcreatingagreenscreen
applicationwhichwouldbeabletorestrictaccesstoonlythosewhoshouldreallysee,
thattheybringusfullcircle,backtothemid1980sandcrossplatformdevelopment.
1.4 Today'sCrossPlatformChallenge.
Intoday'sworld,crossplatformhasaslightlydifferentdefinition. Itusedtojust
meandifferentoperatingsystemsonthedesktop,butnowitextendstohandhelddevices
like netbooks, PDAs (Personal Digital Assistant), and smart phones. In truth, the
distinction between cellular phone, PDA, and netbook is becoming blurred by the
continual addition of features to smart phones and the emergence of the iPad.
Companies are making wide use of VPNs (Virtual Private Networks) on handheld
devicessopersonnelinthefieldcanrunapplicationswhichaccessdatabasessecured
behindthecorporatefirewall.Theyhaveaneedtomaintainoneapplicationyethaveit
runoneverydesktop,netbook,andPDAdeployedbythecompany.Letusnotforget
younowhavetodealwithvirtualkeyboardsandmiceonmosthandhelddevices.
I'mnostrangertocrossplatformdevelopment. Iwastherewhenthefirstmajor
plungehappened.SomeofyoumightbeoldenoughtoremembertheZincIt!book
series. ItcoveredtheZincApplicationFrameworkonDOS,OS/2,Windows,andto
someextentMAC. ThereasonIknowaboutthatseriesisIwastheauthor. Zinc
Softwarewasapoorlymanagedcompany.TheproductwaspurchasedbyWindRiver
Systemsandrolledintovariousproductstheyoffer.Recently,perhapsbecauseofthis
trend,IhavefoundZincisonceagainbeingsoldasaproductbyacompany.Ihaven't
dug too deeply into it, but they claim to support desktop and embedded platforms.

Chapter1Introduction

ThereisalsotalkofanOpenSourceversionaswell.
Let's be honest. While some commercial product vendors will use the cross
platform capabilities of the library, many inhouse developers will simply use the
libraryonasingleplatformwithaneyetowardbeingabletomigratequicklyifwhims
change.Alotofcompanieswhichhadbeenstaunchly(andstupidly)intheWindows
desktopcategoryarenowstartingtouseQtforWindowsdevelopment.Theyareinthe
processofmigratingeverythingtoQtsotheycandeployUbuntu,OpenSuSE,orsome
otherLinuxdistributiononthedesktopassoonasthebeancountersrealizejusthow
much it costs to keep Microsoft around. Microsoft has been a victim of its own
marketingnow.Whenyouactuallytotalupallofthecosts,itendsupbeingthemost
expensiveplatformperuserofanycomputingplatform,includingIBMmainframesand
OpenVMSmidrangesystems.AlotofcompanieshavelearnedthattheOpenVMSbox
whichisstillhousingtheirdataandsomecoreapplicationsis/wasthecheapestperuser
solutiontheyhadeveniftheydidn'tlikegreenscreenapplications.
1.5 WhyUbuntu?
If you have already read The Minimum You Need to Know About Service
OrientedArchitecture,thenyouaren'taskingtheabovequestion. Forthoseofyou
whoskippedthatbookintheseries,UbuntuisquicklybecomingthedesktopLinux
distributionofchoiceformostITprofessionalsandevenquiteafewcorporations.Dell
hasevenstartedpreloadingUbuntuonmachinesforcorporatecustomers.Yes,Lenovo
ispreloadingOpenSuSE,butmostothervendorsareembracingUbuntu. OpenSuSE
hasbeentaintedbyabusinessdealbetweenNovellandMicrosoftwhichinfuriatedmost
ofthevariousUnix/LinuxfactionsandspawnedarewriteoftheGPL(GNUPublic
License)whichputNovellinaworldofhurt.
Wewon'tgettoodeepintoargumentsaboutwhichistechnicallybetter.Ihaveused
bothinthepast.IusedtoactuallypayfortheprofessionaleditionofSuSEbeforeitgot
thesilentOpenaddedtothefrontofit. Allcodeinthisbookwillbewrittenand
testedonversion10.10AMD64bitKUbuntu.Youshouldbeabletoadaptthingsfor
yourownenvironment.IcanwarnyouthatmanyversionsofOpenSuSEmadeitnearly
impossibletogetPostgreSQLrunning.Itwouldeventuallyrun,butyouhadtoscour

10

Chapter1Introduction

theInternetlookingforhints,snippets,andadvice.Ubuntumadeitaneasyinstalland
nearlyautomaticconfiguration. Ifyouareadeveloperthatdoesn'twanttobeaSys
Adminandyoudon'tlikehackingkernelstartupscripts,Ubuntuisthesimplerchoice.
Developerswhowantlotsofdevelopmenttoolsincludedwiththeinitialinstallation
tendtopreferusingOpenSuSE. Corporationsandtheirsupportstafftendtoprefer
Ubuntu. Ineithercase,youdon'tnoticealotofdifferenceunlessyouarewillingto
open up a terminal window and start keying in commands, or you need to install
somethingdifferent.UbuntuusestheSynapticPackageManagerforinstallationswhile
OpenSuSE uses YAST (Yet Another Setup Tool) for everything, including adding
users,changingthesystemtime,etc. YoucanloadKDEorGnomefrontendsunder
eitherdistribution.
Corporations are preferring Ubuntu desktop distributions because the default
desktopinterfaceisbasedonGnome.Thisisaclean,minimalistinterface.Userswho
don'tunderstandtheinnerworkingsofLinuxwillfindthemselvessomewhatlimitedto
surfingtheWeb,creatingdocuments,checkingemail,andwhateverapplicationlinks
were preloaded by the corporation. To get both the KDE and Gnome session
interfaces,youneedtonavigateyourwaytothepackagemanagerafterinstallationand
installtheKDEinterface(KUBUNTU).KDEisamuchmorefunctionalinterfacethan
Gnome.IwilladmittherearecertainspecificthingswhicharemucheasierinGnome
thanKDE,butmostcommonthingsarefareasierinKDEthanGnome. Ifyouhave
bothinterfacesinstalled,youcansimplylogoutandlogbackinchangingyoursession
towhicheverinterfaceiseasierforwhatyouneedtodo.
IfyouinstallbothinterfacesonUbuntuthoughbepreparedforalotofthingsto
either not work or work spastically. The Ubuntu crowd tests only one installed
interfacewitheachrelease.IhavetriedmanytimestomakebothKUbuntuandUbuntu
playnicetogether,andfinallygaveup.Everytimeyoutrytologasupportissueyou
gettoldtowipeeverythingandinstalljustUbuntu.
CorporationsaremorethanhappytosticktheiruserswiththeGnomeinterface
becausetheywanttorestrictwhatpeopledoontheirdesktop.Howmanybillionshave
beenlosttopeopleinstallingstuffontheirWindowsdesktoponlytofindoutithada
virusorsomeothernastysecurityhole?Arestrictedinterfacekeepsusersfrompoking

Chapter1Introduction

11

around and installing stuff. The user account can also be set up to not allow
installations.
TheothermajorsellingpointforUbuntuisstability.Yes,manyflavorsofLinux
andotherPCbasedoperatingsystemsclaimstability,butwhenyouaskthemhowthey
achieveit,yougetalotofbuzzwordsalongwithasonganddance.Ubuntuisgenerally
ayearbehindthebleedingedgefoundinthesourcetrunks. Whiletheywillpullin
somepatchesandfixeswhicharenewer,theyaregenerallyatleastsixmonthsbehind
thelaststablereleaseofanypackagewhichtendstobefourtosixmonthsbehindthe
bleedingedgeofthedevelopmenttrunk.ThecoredevelopersforUbuntuwilltellyou
theyreleaseeverysixmonthssotheyareonlysixmonthsbehind.Thisistrueonlyif
youaremeasuringfromthelaststablereleaseeachpackagedeveloperturnedout.
OpenSuSEreleasesonceeveryeightmonths. Theydon'tputoutanLTSrelease,
but they don't have a spastic numbering system like Ubuntu. OpenSuSE 11 has a
roadmapspanningseveralyearsandmultiple.xreleases. Ubuntureleasesduringthe
fourthandtenthmonthofeveryyear.TheirversionsarenumberYEAR.MONTH.
Inshort,weareusingUbuntubecauseitplaysthegamethebestanddoesn'thavea
taintfromtheMicrosoftNovelldealanddoesn'thaveYAST. Thatsaid,Ubuntuhas
aboutafiveyearwindowtofixwhatiswrongwithitbeforeitdisappearsfromthe
marketplacelikeCorelLinux.Formoreinformationonthatread,Ubuntu'sOriginal
SinintheRuminationschapter.

12

Chapter1Introduction

1.6 WhyQt?
Tostartwith,thereisanOpenSourceversionwhichisregularlyupdated.Itisthe
GUIlibraryusedforallKDEprojectdevelopment.Linuxisgainingalotofpopularity
andKDEisthemajordesktop.WhileGnomegainedalotofcorporatesupportbecause
itisabareinterfacewhichdoesn'tlettheexperimentalendusergetintotoomuch
trouble,formostdevelopersandseasonedcomputerusers,Gnomeismuchlikerubbing
stickstogethertomakefire.
WhileitcanbecorrectlystatedthatOpenSourceprojectsletinfartoomanyjunior
programmerswithbuggycode,thatstatementwouldbeincorrectwhenitcomestothe
Qtlibrary.Therearecommercialdeveloperssiftingthewheatfromthechaff.Thisis
nottosaythelibrarydoesn'thavebugs. WhatI'm sayingisthatthereareliterally
thousandsofdevelopersworkingonvariousportionsofKDE(andnowthenewUbuntu
GnomelikeinterfacebasedonQtinsteadofGtk+.) Thesedevelopersreportbugsas
theyfindthemandinmanycasessendalongsourcechangestobeincorporatedintothe
mainproducttrunk. Fewcommercialproductscanaffordthousandsofprofessional
developers. BygoingOpenSourcewithmuchofthelibrary,Trolltech(nowpartof
Nokia)hasaffectivelyaddedthousandsofdeveloperstotheirstaffatlittletonocost.
Thecrossplatformlibrariesfromdaysofolddidn'tgothisroute.Theytriedtogetby
withalimitednumberofinhousedevelopersfundedbyastronomicallicensefees.
LetusnotforgetthatNokiahaschosenQtasitsstandard(basicallybypurchasing
theproduct)andthereisalotofdevelopmentgoingonforhandheldapplicationsinthat
company. MoreandmoreembeddedsystemsdevelopersarechoosingtouseQton
their embedded project as a result of this. Ubuntu even announced that the main
developmentlibraryforitsGnomelikefrontendwillnowbeQtaswell.Whilethere
aremanyGtk+diehardsoutthere,thewritingisonthewall.Qtmanagedtoachievea
consensusofwhatshouldbeprovidedonallplatformsandthebenefitsofproprietary
toolkitsnolongerexceedthecostsofusingthem.

Chapter1Introduction

13

ThereisalotofonlinedocumentationonQt. TrolltechandNokiahavegoneto
greatlengthstomakeallofQt'sdocumentationavailableonline.Manyothersitesalso
hostcopiesofitincasetheprimarysitegoesdown.Inaddition,thedocumentationis
included in the free QtAssistant document browser which is installed with most
developmentpackages.Muchofthedocumentationisexpertfriendly.Itworkswell
ifyouarealreadyaseasonedveteranandjustneedtolooksomethingup. Itdoesn't
workwellfromthelearningaspect.
IneededtodevelopthexpnsqtapplicationtoreplacesomethingIhadwrittenin
LotusApproachmanyyearsago.IkeptwaitingandwaitingforLotus/IBMtohavea
divinevisitationandporttheLotusSmartSuitetoLinux.Iguessallofthemajordeities
havebeenbusyforthepastdecade. NowthatOpenOfficehasmadegreatstridesthe
wordprocessorandspreadsheetmarketsimplywouldn'tbeprofitable. Approachwas
oneofthosetoolsnobodyusedmuch.Itdidthingswellsoyousimplydidn'tremember
whattoolyouusedfordevelopment. Thexpnsqtapplicationtookme45minutesto
develop with Lotus Approach well over a decade ago without ever having used
Approachbefore. Thereplacementweighedinaroundfourdays. Twoandahalfof
thosedayswerespenttryingtouseanewQtdatabaseclassoneofthebookswasgaga
about.Thatclasssimplywasn'treadytorunwiththebigdogs.
YouhavequiteafewdatabasechoiceswhendevelopingwithQt. Thedatabase
classesareactuallywrapperclassesforvariousdatabaseplugins.Manyoftheplugins
canbecompiledintothelibrarysotheyarestaticallylinkedwithyourapplication.
Staticlinkingprovidesanimmenseamountofdataandapplicationsecurityoverplugin
use.Formoreonthattopicseepluginvs.StaticCompileintheRuminationschapter.
AtthetimeofthiswritingQtsupportsthefollowingdatabaseplugins:

14

Chapter1Introduction

Driver

Database

QDB2

IBMDB2version7.1andlater

QIBASE

BorlandInterBase

QMYSQL

MySQL

QOCI

ORACLE(OracleCallInterface)

QODBC

ODBC(includesMicrosoftSQLServer)

QPSQL

PostgreSQL7.3andlater

QSQLITE

SQLiteversion3

QSQLITE2

SQLiteversion2

QTDS

SybaseAdaptiveServer

Table 1: Qt Database Drivers

NotalldriversareshippedwithallversionsofQt. Somedriversonlyworkon
some platforms because the database only runs on some platforms. Other than an
ODBC driver, I don't know how you would access Oracle RDB from within Qt.
(Remember that Oracle and Oracle RDB are two completely different database
products.)Interestinglyenough,theOpenVMScommunityhasbeenportingQttothe
OpenVMSplatformforquitesometime.Trolltechdoesn'tseemwillingtoadoptthese
ports, so the community has been working diligently at them. You can find some
interestinginformationinthismessagethread:
http://groups.google.com/group/comp.os.vms/browse_thread/thread/4e1bacfc0f33584c/
00c4ad8634cf9f0e?hl=en&lnk=gst&q=Qt#00c4ad8634cf9f0e

Chapter1Introduction

15

Qtcomes(onmostplatforms)withitsownversionofSQLite. Thisislikethe
default MySQL for many Linux distributions, without the additional administration
issues. It is an SQL interface wrapped around an indexed file system. (MySQL
defaults to an indexed file system on most platforms. You have to actually force
InnoDBattimeofdatabasecreationtogetarelationalengine.)SinceSQLiteismuch
lighterthanMySQL,itwillletyoucreateyourindexedfilesprettymuchwhereever
youhappentobe.MySQLrequiresdatabasestobecreatedwhereitsstartupscriptor
systemlogicalpoints(atleastfortheserverversion.) ItispopularwithPCminded
developersbecauseitallowsthem togainthebenefitsofanSQL interfacewithout
havingtolearnhowtoactuallydesignanddeveloparoundadatabase.(Thoseofyou
lookingformoreinformationaboutlogicalsshouldreadISBN139780977086634.)
Intheory,thewrapperclassesprotectyoufromthequirksfoundineachdatabase.I
sayintheorybecausethehigherlevelwrappersdon'tworkcleanlyyet,soyouendup
writingquiteabitofSQLandusingqueryfunctions. Eachtargetdatabasehassome
minordifferencesintheirquerylanguagesandconnectionmethods. Whyhasn'tthis
becomeamajorissue? Quitesimply,veryfewapplicationsevergetportedbetween
databaseproducts.TheprimarydevelopmenteffortwithQthasbeentouseitwriting
KDE(theKDesktopEnvironmentforUnix/Linux),manytexteditors,andafewIDEs.
AmajorsecondarymarkethasbeentodevelopapplicationsforNokiaphonesandother
handhelddevices.Veryfewapplicationsareportedbetweenthetwoenvironments,but
thatisstartingtochange.Mostofthosewhichareportedcangetbywiththelimited
capabilitiesofSQLite.
These days, many companies will start development with a cross platform tool
knowingtheywillhavetoporttoanotherplatforminthefuture,eveniftheydon'twant
toport.Howisthatpossible?TakealookattheDOSmarket.Therewerearashof
DOSspecificGUIlibrariesduringDOS'sheyday. ThencameWindows(GUIDOS),
Windows for Work Groups (even more GUI DOS), Windows 3.x (still more GUI
DOS),Windows95(GUIerDOS),Windows98(DOSlockedinthedungeonstilldoing
the bulk of the work for the GUI),WindowsNT, Windows XP,and most recently
WindowsVista.(Yes,IskippedOS/2,abetterDOSthanDOSandabetterWindows
thanWindows.)Everythreetotenyears,theunderlyingenvironmentchanged.Across
platformlibrarycanminimizetheimpacttoyourapplication. Inabestcasescenario

16

Chapter1Introduction

yousimplyhavetorecompileandcreateanewinstallationprocedure.
WhenZinccameout,itstraddledtheDOS,OS/2,Mac,andWindowsmarkets.The
underlyingproblemswiththeproductallstemmedfromnothavinganOpenSource
version.Thevendorwaslefttryingtodevelopandsupportarashofmovingtargets,
and was always hampered by the limited address space of the DOS environment
combinedwithnearzerofreeDOSextenders.Bythetimesomeofthebetterquality
DOSextendertoolsbecamefreeorcheaplylicensed,themarkethadmoved.During
the first round of cross platform libraries we also had a large number of compiler
vendorscombinedwithevolvingstandardsforbothCandC++.Todaytherearevery
fewcompilervendorsandthestandardshavesettled.Whiletherestillarecommercial
C++compilersoutthere,mostdevelopersaremigratingtotheOpenSourcecompilers
of either GNU or Watcom (which oddly enough used to be the highest quality
commercialcompilerbackintheday.OpenWatcomnowhasaLinuxversionbesides
all of the other versions it used to have.
http://www.openwatcom.org/index.php/Main_Page)
Howmanyofyouhavebeenaroundlongenoughtorememberthepainofporting
fromWindows3.xtoWindows95toWindows98andthelaterports? Ifyoudidn't
haveawrapperlibrarythingswereugly. BecauseVistatooksolongtogetintothe
market,peopleforgotandstartedusingalotofnativetoolswhendevelopingonXP.
ThereleaseofVistataughtanothergenerationofdevelopersthesadlessonofusing
platformandvendorspecifictoolsets.Themuch(andrightfully)malignedVistaalso
openedthedoorforLinuxdistributionsonthedesktop. Peopledecidedtheyweren't
goingtogothroughthisyetagainsimplytostaycurrent.
Theotherbenefitofusingatoolsetwhichrunsonmanydifferentoperatingsystems
(noticeIavoidedsayingcrossplatform)isthatyourpoolofpotentialdevelopersgets
much wider. Yes, many are contributing things to the Qt library itself, but what
happenswhenyouneedtohirenewdevelopers?Mostcompanieshavelearnedthepain
ofusingproprietarysystemslikeWindowsandMicrosoftspecifictoolsets.Colleges
quitteachingthingsthataren'tfreewhentheyarestrappedforcash. Yourpoolof
possible employees shrinks every semester once that trend starts. Vendors keep
increasingthepricingoftheirtoolssotheonlywayyoucangetnewtalentistosteal

Chapter1Introduction

17

someoneelse'soldtalent.Alonewolfsimplyisn'tgoingtoplunkdownthousandsof
dollarstoobtainallofthetoolsinthehopetheywilllandagoodpayingcontract.
Ihaveseenjobopeningspostedformonthslistingsixtotwelvevendorspecific
tools. When the posting stays open in excess of a year they claim there is an IT
shortage.Thereisnoshortage.Theyweresimplypostingajobopeninglookingtopay
$60,000/yearforaskillsetcommanding$120/hour.Hadtheybeenwillingtopaythe
actual market rate, they would have gotten the talent they wanted. Had they been
willingtohireaprogrammerwhohadnoneofthoseskills,butwaswillingandableto
learn,theywouldhavegottensomebodyforthemoneytheyoffered.
A lot of companies are moving to Qt to avoid being trapped by a proprietary
platformlikeWindowsanditsproprietarytoolsets.Ifadeveloperalreadyknowshow
todevelopwithQt,theyonlyhavetolearnafewofthequirksandtheeditorsonthe
newplatform.Thisis,ofcourse,makingtheassumptionyouhavemanagedtomigrate
allofyourdataintorelationaldatabaseswhichhaveplugins/compileinsavailable.
1.7 ATaleofTwoMarketSegments
What is really the fundamental difference between the Mac market and the PC
market?Applehascompletelytossedoutpriorarchitectureonmorethanoneoccasion,
yetAppleisnowtheleadingcollegecampuscomputermanufacturer. Vistarequired
suchamassivehardwareupgradeformostexistingWindowsusersthattheindustry
refused to upgrade. After officially dropping support for Windows XP, Microsoft
begansellingaBusinessEditionVistalicense. Isetuponeofthesecomputersfor
someone.DoyouwanttoknowwhattheBusinessEditionreallyis?Itcomeswith
bootablemediawhichwillwipeVistaoffyourmachineandinstallWindowsXPPro.
MicrosoftcountsthisasaVistalicensesale,buttheyreallyjustsoldanothercopyof
WindowsXPPro.Oh,let'snotforgetaboutwhenthenetbookscameout!Theydidn't
haveenoughresourcesforVista,soMicrosoftbegansellingXPonthemagain. So
muchforthesunsettingofXP.

18

Chapter1Introduction

HowisitthattheApplecustomersarewillingtoendurethis,buttheMicrosoft
customersarenot?WhenApplethrowsoutthepriortechnology,theythrowitoutlock
stockandbarrel.Amajorimprovementinboththeoperatingsystemandthehardware
shippingwithitoccurs.Thereisacompellingreasontoditchtheoldstuffforthenew
becauseitalwaysfeelslikeyouareupgradingfromaModelTtoafullyloadedCaprice
Classic. Applealsotargetsaniche. Appledoesnotmarketitselfasageneraloffice
supplylikeastaplerorahandheldcalculator.
Vista was not a major improvement. Microsoft doesn't control the computer
hardwarelikeAppledoes.AnyonewhohasseenanyoftheI'maMacandI'maPC
commercialswherethePCuserwasrejoicingaboutbeingerrorfreefornearlyaweek
cangetagoodunderstandingofjusthowbadVistahasbeen. Sobad,Microsofthas
comeupwithanewcampaignwheretheywon'tevenusethenameVistaanymore.Too
badforthemtheeBookversionofthisbookwillremainforsaleonserverslongafter
Microsofthasceasedtoexist.
Over the course of its existence, Microsoft had managed to compel people and
companiestoupgradeandstaycurrentwiththelatesttoys.ThatworkeduntilthePC
becameastandardofficesupply.Companieslooktospendaslittleaspossiblewhenit
comestostandardofficesupplies.Ifanyofyouhavetriedtogetanamebrandsurge
protectorbecauseofsomedelicateequipmentthroughyourlocalofficesupplygroup,
youknowwhatImean. Whattheyshipyouisa$2genericthatmayormaynot
suppressanything.
WindowsXPProbecamesuccessfulbecauseitremainedvirtuallyunchangedfor
sixyears.Companiesdevelopedalotofsoftwareforit,andmostemployeesalready
hadsomeideaofhowtocheckemailandsurftheWebonitwhentheywerehired.Itis
a heritage system. The very thing Microsoft (and most everybody trying to sell
somethingnewintoIT)railsagainst,claimingitisholdingpeopleandcompaniesback
fromreachingtheirfullpotential. Whatthemarketingtypesdon'tunderstandiswhy
heritagesystemsexist:

Chapter1Introduction

19

1. Theyareasunkcost.
2. Theydotheirjob.
3. Mostcompaniesdon'tmakemoneychangingsoftwareorhardwareplatforms.
Applecangetawaywithacompleteredesignoftheirplatformbecausetheoffice
deskstheyarefoundonneed/wantalloftheimprovementsthenewplatformprovides.
Theyoccupycreativenichesatcompaniesandreallyarestillpersonalmachines.The
PCtargetedthemuchwiderbusinesstoolmarket.WhatMicrosoftshareholdersand
leadershaven'trealizedyetisthatonceabeancounterisinchargeofbuyinghammers
forthecarpenters,thecarpenterswillallbecarryingthecheapesthammeroutthere.
WhenApplemovedtoIntelbasedchipsitopenedthedoorforgoingheadtoheadwith
Microsoftandsomecompanieswarmedtotheproductline.Itshouldalsobenotedthat
Apple recently exceeded Microsoft in market cap size per the SEC formula of
share_price*authorized_shares. Thisisn'tbecauseAppleistakingoverthebusiness
desktop, rather it is because Microsoft is losing the business desktop to Linux, the
cheapesthammeroutthere.
VistamadeApplelooklikeabetterdealintheeyesofmanybeancountersaswell.
Vista required significant hardware upgrades and was deservedly maligned in the
industrytradepress.Whenthebeancountersstartedaddinguptheupgradecostsand
totalinginthecostofVistaproblemresolutionalongwithlostbusinessproductivitythe
higherpricedAppleproductsstartedlookingcheaperbytheminute;ofcourseLinux,
withitsfreeofficesoftwaresuitewhichcouldrunontheexistingdesktopswithoutany
hardwareupgradeslookedthebestofall,butthatrequiredabitmoreselling.

20

Chapter1Introduction

1.8 TheMutantNinjaDataModel
Manyofyoumightbelongenoughinthetoothtorememberwhenyoucouldn't
pick up a weekly geek magazine without seeing a bunch of ads and reading more
articlesabouthowobjectdatabasesweregoingtocompletelyreplaceallotherdatabase
engines.Well,thatdidn'thappen,buttheindustrydidgethungryforsomecapabilities
whichonlyobjectdatabasescoulddeliver.Likewise,hierarchicaldatabasesfelloutof
favor,butneverdisappeared.Wearenowbeginningtoseewhatsomegeekswillcall
theMutantNinjadatabasemodel,butotherswillcalltheobjectrelationalembedded
hierarchical model. Some even try to leave out the fact there is an embedded
hierarchicaldatabasebysimplycallingittheobjectrelationalmodel.Iprefertocallit
theMutantNinjaDataModel.
Therearen'treallyanyoftheseforustoplaywithintheOpenSourceworld.For
somestrangereasontheonlypeopleworkingonthem arethecommercialdatabase
providers.GiventhenatureoftheOpenSourceworlditwouldbealotsmartertocome
up with a design in the commercial world, then Open Source it to shake out the
problems.
Forawhilewehadsomedatabaseenginesusingrelationalastheouterengine,
accessingblobsviaanobjectmodelofsomekind,andstillprovidinghashedindexfile
services. DBAs,toalargeextent,handpickeachstoragetype,databasemodel,and
location.Wearelessthantenyearsawayfromthatchangingonapermanentbasis.
Inanottoodistantfuture,aDBAwillrunaninteractivetoolwhentheywantto
createadatabase. Itwillaskthemsomequestionsaboutinitialrowcounts,column
namesanddatatypes,foreignkeys,andkey/indexcolumnsneeded. TheDBAwill
chooseexistingtablespace,orbeallowedtocreateshinynewtablespace,then,whenall
questionshavebeenanswered,thetoolwillcreateadatabaseintheformatitchooses
keepinghiddenallknowledgeofitsrealorganization.Wewillnolongeruseadatabase
model,butaDataStorageSystemthatwilldetermineallofthehowyetpromiseus
wecanviewtheinformationaswewishaslongasweaccessitviatheURLitprovides.

Chapter1Introduction

21

Thereisnothingwhichcanbedoneaboutthis.Itistheonlylogicalprogression.
Eachdatabasemodelhasaclassofproblemitisuniquelyperfectatsolving.XMLis
used nearly universally for external communications and companies are seeking
methods of storing this beast. An XML document is, by definition, a hierarchical
document. Whenyouhavealimitednumberofrowsinareferencetable,nothingis
fasterthanasortedsequentialfileandabinarysearch,unlessthedataisreallyshort,
thenlinearsearch.Thealmostsmallrowcountdatasetlendsitselftoahashtable,
butalmostsmalltendstobedefinedbythequalityoftheavailablehash.Whenyou
have a mediumsized data set with less than four segmented keys, VSAM (RMS
Indexed) rules. When you have real time data coming in that requires embedded
triggerstoautomaticallyfireoncecertainmethodsreturntrue,objectmakesitapieceof
cake.Whenyouhavetableswithmassiveamountsofdatarequiringseriousamountsof
referentialintegrity,relationaliscurrentlytheanswer.
Wehavespentthelast20yearsmigratingperfectlygoodbusinesssystemsto
relationaldatabasesystemsfornootherreasonthantogetSQLaccess.
While it is true that some of those systems had their lifespan greatly extended
becausetherelationalmodeldidn'thavethesamefilesizelimitationstheirprevious
storagemodelshad,itisalsotruethatwehad4GLtoolstheindustrychosetoshitupon.
Oneofthemostimpressive4GLtoolsIeverworkedwithwasCognosPowerHouse.It
ranonmanyplatformsandyoucouldlinkanativeindexedfiletoanRDBdatabaseto
anOracledatabasetoaSybasedatabasetoNotherdatabasesallinthesamereport,
batchupdatemodule,ordataentryscreen.Thetoolisstillavailabletoday,butCognos
isnowownedbyIBM.YoureallyshouldtryitbeforeyourITcareerends.
Whathappenedwastheindustrywentthroughaperiodwhereproprietarythings
wereconsideredbad(yetforsomeoddreasonproprietarydatabasesystemslikeOracle
andDB2wereconsideredgood.) Theonlyreasonforitatthetimewasthefactthe
GartnerGroupwaspaidtomarketOpensystems. ThedefinitionofOpenwas
whatevertheywerepaidtosellthisweekbecausesomehowMicrosoft,oneofthe
mostproprietaryoperatingsystemsonthefaceoftheplanethavingasinglevendorand
damnedlittleaccuratekerneldocumentation,wasalsobrandedasOpen.

22

Chapter1Introduction

TheendresultofthisdisasterwasanMBAmindsetthatyoucouldn'tpayasupport
contractforacrossplatform4GL,butyoucouldpayoneforaproprietaryrelational
databasebecauseSQLwasanopenstandard,nevermindthateverydatabasehadits
owndialectofSQLandthatverylittleproductioncodecouldevermigrate.Sincethe
MBAswantedtobeabletoaccessallinformationthecompanyhadviaanooohlookat
the colors dashboard application, all data had to be in the relational database that
dashboardused.
NowyoufindmanySQLfrontendtoolsandlibrariesforolddatastorageformats.
Itwasn'tuntilrecentlythatuppermanagementlearnedtheSQLwrapperprovidedby
MySQLreallyworkedwithanindexedfilesystemratherthanarelationaldatabaseby
defaultonmostplatforms.Mostdidn'tknowthatuntilOracleboughtInnoDB,which
wastherelationalengineshippingwithMySQLandapublicbrewhahaensuedinthe
tradepress.
Long hours combined with massive quantities of caffeine and nicotine are now
happeningbehindcloseddoorsatallofthemajordatabasevendors,andprobablysome
oftheOpenSourceprojects.Ofcoursetheywillhavetocomeupwithasexiername
thanDSS(didn'tIBMcallsomeancientformofDASDDSS?)andtheycannotuse
DataWarehouse because that's already been used for other things, but the solution,
howevernamed,willbethesame. TheDBAwilluseatoolwhichletsthemspecify
howthecolumnswillbeviewedbytheoutsideworld,butthedatabaseenginewill
choose how to store them. More importantly, it will dynamically switch from one
formattoanotherwhenitsensesalimitbeingreachedandautomaticallyrebuildindexes
when it notices performance degradation. We will no longer have any low level
recoveryoptions. Wewillusethebackupandrecoverytoolsprovidedbythesystem
andthepeoplewhocreatedSOXwillbehappybecausenowwehaveonlyonetoolfor
datastorageandtheirauditswillbeeasy.
Ofcourse,someofyoumightthinkthisissoundingalltoofamiliar.Well,itis,if
youhappentoeverworkwiththePICKoperatingsystem.
http://en.wikipedia.org/wiki/Pick_operating_system

Chapter1Introduction

23

Wewillhaveadifferentdatabaseengine,butwewillbeheadedbackthereshortly.
WhatdoyouwanttobetweendupwithamishmashofBASIC andSQLasthe
primarylanguagesagain?
1.9 Exercises
1. Whydoheritagesystemsexist(3reasons)?
2. WhatdoesWSDLstandfor?
3. Withoutanetworkandwithouttheabilitytoexposepartofyourdiskfarmto
outsiders,howdoyouhavetosharedata?
4. What are thethreemain reasonswhichwould force aclientserver solution
insteadofaWebbasedsolution?
5. Whatwasthereallybigproblemwithclientservercomputingduringthe1980s
throughthemid1990s.
6. WhatdoesYASTstandfor?

24

Chapter1Introduction

Pageleftblankintentionally.

Chapter2

CautionaryInformation
2.1 ScopeofThisChapter
InthischapterwewillcoversomeofthemajorissuesencounteredwhenusingQt
and/ordatabases. Thesewillbethekindofthingswhichshouldn'thappen. When
theydohappenyouchaseyourtailforhoursandsometimesdaystrackingtheproblem
down.
2.2 DevelopmentRestrictions
If you come from a development environment on a robust platform which was
centeredaroundacodemanagementlibrary,developingwiththeQttoolsetisgoingto
seemlikeanactofextremesacrilege.TheUnix,andbyextensionLinux,platformsdo
nothaveanyconceptsoflogicalnamesordistributedarchitectures.Here,youhaveat
best,aPCmindset. Thatmindset,beingsmall,simple,andnotlookingfurtherahead
thantheweekend,developedatoolsetwhichmatcheditsthoughtpatterns. Thereis
onlyoneguidingprinciple.
Everythinginasingledirectorytree.
You will find this philosophy wrapped up in every Open Source project you
downloadoruse.Backinthedayswhenweonlyhadindexedfileswecouldmuddle
through. When you start actually running software as an installed image and need
multipleversions,thisphilosophystartstobreakdown. Itgoescompletelyoutthe
window when you need to run D,T,S, and P versions of databases. (Development,
integration Testing,production Support,and Production,allonthesamemachineor
cluster. GenerallyrunningProductiononyourdevelopmentmachine(s)ispartofa
disasterrecoveryplan.) IcoveredthebulkoftheDTSPissuesinISBN13:9780
977086603,pleaserefertoitforfurtherinformation.

26

Chapter2CautionaryInformation

2.3 BuildingaQtProgram
Onceyouhaveallofyoursourcefilesinasingledirectory,youneedtocreateaQt
projectfile. IfQthasbeencorrectlyinstalledonyourmachine,youcandothisby
simplytyping:
1)
2)
3)

qmake -project

Afileusingthecurrentdirectorynamewitha.proextensionwillbegenerated.
Thenameoftheexecutablefilewhichcomesoutoftheentirebuildprocesswillalsobe
thesameasthedirectory. (IthinkyouarenowstartingtoseewhyIharpedonthe
Everything in a single directory tree philosophy.) It was never a good idea on
midrangeplatformstohaveadirectoryprovidethenameforanexecutablecontained
withinthedirectory,anditwasanevenworseideaonthemainframeplatform,buthey,
the Open Source community is all gaga over using a convention which has failed
miserablyoneveryotherplatform.
WhendealingwitharelationaldatabaseviatheQtlibraries,youneedtomanually
edittheresultingPROfiletoaddthefollowinghighlightedlinepriortogeneratingthe
Makefile.
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)

######################################################################
# Automatically generated by qmake (2.01a) Sun Jun 6 19:10:04 2010
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
QT += sql
# Input

Idon'tknowwhytheqmaketoolisn'tsmartenoughtodoaquickscaninsideofthe
filesanddetermineanapplicationusestheSQLclasses.Onceyou'veaddedthelineand
savedthefileyoucanrunqmakeonemoretimewithoutanyparameterstogeneratea
Makefile. Yourfinalactofbuildinganexecutableistotypemakeandwatchitrun.
Assuming you don't have any typos or other compilation errors, you will get an
executablewiththesamenameasyourdirectory.

Chapter2CautionaryInformation

27

Becausethissectionofthebookisaboutdevelopmentrestrictions,takealookatthe
other lines above our highlighted line. The . stands for the current directory.
Everything is assumed to be in your current directory. If you want to change the
resultingexecutablenameyoumustprovideavaluetotheTARGETline.Whenyou
are using third party libraries and need to force recompilation due to a header file
changing,youneedtoaddthedirectorytreeholdingtheheaderfilestoDEPENDPATH.
Toensurethecompilerlooksinthatthirdpartydirectoryforheaderfiles,youneedto
alsoaddittotheINCLUDEPATH.Useaspaceora;toseparateentriesinthepath
list.Thereisalonglistofvariableswhichcanbedefinedinthisfile.Useyourfavorite
Websearchengine(hopefullynotGoogle)andsearchforQtincludepathwithoutthe
quotes. You shouldseeatleastonelinktoaNokia/Trolltechdocumentationpage.
Hopefullythedocumentationpagehasaversionnumberclosetotheversionyouare
usingofQtsoeverythingworksthewayitisdescribed.
2.4 TheDatabaseUsernameRestriction
Oneofthemostfrustratingaspectsyouwillencounterisobtainingtheusername
andpassword fordatabaseaccess. Itsoundssimple. Usingaproceduraltool,you
wouldpopupawindowtopromptforthese,thendeletethatwindowanddisplayyour
mainformorforms.EvenwithourJavaappletinTheMinimumYouNeedtoKnow
AboutServiceOrientedArchitecturewehandledthiscleanly.Notsowhenitcomesto
designingyourQtbasedapplications.Handlingthisonetaskdramaticallyimpactsyour
applicationdesign. MostexamplesyouwillfindforQtconcentrateonlyontheGUI
aspectsinceitwasoriginallyaGUIlibrary.Eachwillhaveamain.cppfilewhichlooks
muchlikethefollowing.
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)

#include <QApplication>
#include "mydialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyDialog *dialog = new MyDialog;
dialog->show();
return app.exec();
}

28

Chapter2CautionaryInformation

WhentheinitialGUIobjectisadialogyoutypicallyallocateitbycalling newand
lettheQtenvironmentdeletetheattachedobjectwhentheapplicationexits.Whatyou
havetorememberisthatQtprogramsaren'tjustGUIOOPprograms,theyareevent
driven.EverythingoccursintheeventloophandledbytheQApplicationobjectmethod
exec().I'vebeenthroughthismanytimeswithotherproducts.I'mverygladIamnot
coveringdeviceIOinthisbooklikeIdidinmyfirstZincbook. Whenyouendup
handlingIOfrom32+portserialcardsandcontrollingrelaysviadigitalIOandOpto22
componentsyouenduphavingtocodeyourowneventlooptohandlesuchthings.
Addingadatabasetothismixopensnearlythesamecanofworms,notbecauseyou
havetohackyourowneventloop,butbecauseintheQtworldthedatabaseobjectis
globaldata. Itreallyneedstobeownedbythemain.cppfilesoitnevergoesoutof
scopeandisn'tgarbagecollectedonyou.KeepinmindwhatItoldyouaboutC++in
TheMinimumYouNeedtoKnowtoBeanOpenVMSApplicationDeveloper:Once
an object goes out of scope it is eligible for garbage collection. Some compilers
garbagecollectimmediatelyandotherspostponethecollectionuntilidletime. Java
developerswillhaveaneasiertimeunderstandingthisgiventhatJavaalmostalways
waitsuntilidletimetoperformgarbagecollection.
Your second major problem with adding a database to the mix is the method
differentdatabasesusetovalidateauser. Somerelationalenginesareconfiguredso
thatanyuseronthelocalmachinehasautomaticvalidation,otherrelationaldatabases
providetheirownuserauthentication.Somedatabasesusebothmethods.Thefirstfor
localusersandthesecondforusersaccessingviaTCP/IPservices.
Many of you are probably having trouble understanding what I'm complaining
about,soletmegiveyouasmallexample. Hereisthemain.cppfrom thexpnsqt
project.

Chapter2CautionaryInformation
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20)
21)
22)
23)
24)
25)
26)
27)
28)
29)
30)
31)
32)
33)
34)
35)
36)
37)

29

/****************************************************************************
* Originally created by Roland Hughes at Logikal Solutions.
* Project is being released under GPL2 license.
*
* At some point in the future either Logikal Solutions or Roland Hughes
* may elect to publish a book with this original source code in it. If
* the book is written it will be part of "The Minimum You Need to Know"
* book series.
****************************************************************************/
#include <QtGui>
#include <QtSql>
#include <QtDebug>
#include "XpnsLogonDialog.h"
int main(int argc, char *argv[])
{
int i_x;
QString qtDbName;
QTime theTime = QTime::currentTime();
QApplication app(argc, argv);
{
QString driverName = "QPSQL";

//
// Set up information for driver and see if driver is available
//
qtDbName = "xpns" + theTime.toString("hhmmsszzz");
QSqlDatabase db = QSqlDatabase::addDatabase(driverName, qtDbName);
if (!QSqlDatabase::isDriverAvailable( driverName)) {
QMessageBox::critical( 0, "Missing PostgreSQL Driver",
"The Qt driver for PostgreSQL "
+ driverName
+ " is not installed. You must install it before running
this program",
38)
QMessageBox::Ok);
39)
db = QSqlDatabase();
// reset to avoid warning
40)
return 0;
41)
}
42)
43)
XpnsLogonDialog *xpnsLogonDialog = new XpnsLogonDialog( 0, qtDbName);
44)
xpnsLogonDialog->show();
45)
i_x = app.exec();
46)
qDebug() << "About to delete dialog";
47)
delete xpnsLogonDialog;
48)
qDebug() << "Dialog deleted, now clearing database";
49)
50)
db = QSqlDatabase::database(); // clear connection to avoid error
51)
} // end scope of logon dialog
52)
53)
QSqlDatabase::removeDatabase(qtDbName);

30
54)
55)

Chapter2CautionaryInformation
return i_x;}

Payspecialattentiontothehighlighted{}surroundingtheinnercodeinthismodule
(listinglines25through51.)Thisisrequiredtoensurethedbobjectgoesoutofscope
andisgarbagecollected.JustpriortoexitingthatblockIhavetoassignthedbobjectto
thedefaultdatabase.Youcanonlyremoveadatabasefromtheglobalstorageareaonce
allreferencestoithavebeenremoved.
TheglobalQSqlDatabaseobjectcreatesadefaultdatabaseobjectwheneveryou
include SQL support in your program. Many developers will only use the default
database.Thedifferencebetweenadefaultdatabaseandotherdatabasesisthedefault
database doesn't have a name. Notice the highlighted line calling the method
addDataBase().Thefirstparameterisastringcontainingthedrivername.Thesecond
parameterisanameyougivethisdatabaseobject.Anyothermoduleinyourprogram
canuseanydatabaseobjectyouaddtotheglobaldatabaseobject.Inaffectitisalistof
databasesyourprogramhasaccesstoandisstoredglobally. Youcannotremovea
databasefromthislistaslongasthereisstillanobjectreferencingit.Oddlyenough,
thereferencecountisn'treducedwhenthedbobjectgoesoutofscopeandisgarbage
collected.Iassumetheywillfixthisbugeventually.Fornow,youneedtoremovethat
referencebyreassigningtheobjecttothedefaultdatabase.
Ifyoufailtoremovenameddatabaseobjects,orfailtoremoveallreferencesto
them, you will receive warning messages in the default output window when your
application closes. Many developers simply ignore these messages and set there
applicationsuptobelaunchedfromscreeniconsratherthanruninsideofaterminal
window.Ifthereisnodefaultoutputdevice(likeaterminalwindow)yousimplydon't
seethesemessages.
Notice how I had to launch a login dialog as the initial screen/form for this
application.Whenwegettothechaptercoveringthisapplicationyouwillseethatthe
logindialog hadtolaunchtheactualmain screen/form onceitcompleted thelogin
process.Thisisn'tthepinnacleofOOPbenefits.

Chapter2CautionaryInformation

31

WhydidIhavetolaunchthelogindialogastheinitialform?Applicationdesign.
Inordertohavecomboboxespopulatedfromvarioustablesinyourdatabase,youhave
toactuallyhaveaconnectiontothedatabaseandthosetables. Inordertogetthat
connection,youhavetoensureyouhaveausernameandpassword.Youcannotassume
thedatabasewillallowfreeaccesstoanyonewhowantsit.Infact,thecurrentwrapper
providedbyQtdoesn'tallowyoutoconnectwithoutausernameandpassword,even
whenPostgreSQLallowsthisaswehaveseen.Nowmylogindialogistightlycoupled
withtheapplication.
Ifyouchoosetonothaveanycomboboxesordatabasepopulatedpicklistsonyour
mainform,youcanwriteagenericlogindialogwhichcanbepoppedupafteryour
mainformhasbeencreatedanddisplayed.Thisgenericdialogwillbelooselycoupled
withyourapplicationandinfinitelyreusable. Theproblemisthatadeveloperrarely
getstomakethechoicesaboutwhatisandisn'tonaform.Userswantitallandthey
don'tcarehowyouhavetowarpthearchitecturetoproduceit.
2.5 GarbageCollectionRestrictions
Javarotsyourbrain.IreallyadmiredeveloperswhocaneasilyslidebetweenJava
inthemorningandC++intheafternoonwithoutevercreatingamemoryleakonany
systemandwithoutusingtoolstogrindtheirapplicationcheckingformemoryleaks.
Mypersonalguessisthatthereareaboutfivesuchpeopleinthesoftwaredevelopment
universe.Ihavenoideawhotheyare,butIfirmlybelievetherecannotbemorethan
fiveofthem.Iespeciallydoubtthatnumberisincreasing.
WhenyouareprogramminginJava,youhavetoallocateeverythingexceptthe
scalar types with new. The JVM keeps track of your references and does garbage
collection whenever it deems garbage collection is appropriate. C++ automatically
cleansupstackallocatedvariableswhentheygooutofscope,butvariablesallocated
vianewmustbephysicallykilledoffbyacalltodelete.

32

Chapter2CautionaryInformation

Qttriestoaidinthisbytellingyouitdeletesallobjectswhicharederivedfrom
QObject. Ihaveseenthisstatementinalotofdocumentation,andI'm notexactly
buyingit.Actually,itismostlytrue,buttellingpeoplethisiscausingmoreproblems
thanitissolving.ThemainproblemoccurswithQSqlDatabase.Youhavetocleanup
thereferencesyourself.EarlierIshowedyousomecodewhereIhadtoreassignour
pointertothedefaultinstanceinordertofreethereference,thenIwasabletoremove
thedatabasefromthelistofdatabases.
Whydowefindourselvesinthisbind?Location,location,location.QSqlDatabase
isglobal. Theinstancesyoudeclareofittendtobeonthestackratherthanonthe
dynamicheap.
QSqlDatabase db = QSqlDatabase::addDatabase(driverName, qtDbName);

OnlydynamicallyallocatedobjectscanberemovedbythedeleteoperatorinC++.
Objectsallocatedonthestackaresupposedtobecleanedupwiththestackcleanup
priortoreturn,butyoucannotremovethem. Becauseyoucannotphysicallydelete
stackallocatedobjectsyourself,youcannotgetridofthereferencestoglobalobjects
cleanly.
Butwhentheobjectwentoutofscopeitshouldhavebeengarbagecollected,right?
No.Itwasmadeavailabletogarbagecollection,buteachplatformimplementsgarbage
collectionatatimeandplaceofitsownchoosing.Theonlywaytoclearthereference
wastoreassignmystackobjecttothedefaultdatabaseconnectionpriortoexitingthe
enclosingblock.
db = QSqlDatabase::database();

// clear connection to avoid error

Chapter2CautionaryInformation

33

2.6 MoreDatabaseIssues
AlmostalloftheexamplesIfindfloatingaroundtheWebshowhowtousethe
defaultdatabaseconnection,notanamedconnection.Whyshouldyouavoidusingthe
defaultdatabaseconnection?Comeon,thinkaboutit,youknowtheanswer.Module
reusability.IfyoualwayspassinastringwiththenameofthedatabaseasQthasit
stored,youcancallyourmodules/classesfromanyapplicationyoudevelop.Itdoesn't
matterifthatconnectionisthefirstorthe50thconnection.
You will find the xpnsqt application is coded quite differently from the
mega_zillionareapplicationusedinotherbooksinthisseries.Themain.cppforxpnsqt
appearsearlierinthischapter. Itistheonewehavebeendiscussingwithrespectto
databaseissues.Ididn'tnoticeitwhenworkingonUbuntu8.10,butwhenworkingon
SuSE11.xIwasseeingerrormessageslikethefollowing:
roland@roland-desktop:~/megaqt$ ./megaqt
QSqlDatabasePrivate::database: unable to open database: Access denied for user
'roland'@'localhost' (using password: NO) QMYSQL: Unable to connect
Successfully connected to database

Theinstructionwhichtossedthiserrorwasn'ttheopenstatement.Itwasthecallto
makeacopyofthedatabaseinstance.
QSqlDatabase db = QSqlDatabase::database( m_qtDbName);

ImustsayIwasreallytakenabackbythis.Alloftheinformationyoucouldfind
onlinesaidyouneededthemain.cppfiletodeclareyourfirstinstanceofQSqlDatabase
soitwouldbeownedbythelastmoduletocompleteexecution.MostexamplesIcould
finddiditprettymuchthewayIwasdoing.Itappearsthatwhenyouattempttogeta
copyorinstanceofanexistingdatabaseconnection,themethodreturningthatinstance
automaticallytriestoopenitforyou.Quitethelittlehelperisn'tit.TheroadtoHell
ispavedwithgoodintentions.Alotoftruthinthatstatement.
TherewasaslightlylargershockwaitingformewhenIranthexpxnqtapplication
andsawthefollowinginmyterminalwindow:

34

Chapter2CautionaryInformation

roland@lgkl-destop:~/xpnsqt2$ ./xpnsqt2
QInotifyFileSystemWatcherEngine::addPaths: inotify_add_watch failed: No such file
or directory
QFileSystemWatcher: failed to add paths: /home/roland/.config/ibus/bus
Bus::open: Can not get ibus-daemon's address.
IBusInputContext::createInputContext: no connection to ibus-daemon
QSqlDatabasePrivate::database: unable to open database: "FATAL: database
"roland" does not exist
QPSQL: Unable to connect"
About to delete dialog
Dialog deleted, now clearing database

Whenyoulookthroughthevarioussupportforumsandemaillistsonthesubjectof
Qt programming, you will find quite a few hacks, suggestions, and work arounds.
Everybodyseemstobehappysimplygettingonetothreelinesofcodewhichmakesthe
errorgoaway.Theydon'tseemtobeinterestedinactuallyfixingtheproblem.Inthis
case, the problem was caused by someone trying to be helpful. It only became a
problembecausealotofotherpeopletriedtobehelpfultellingpeopletheyneededto
getaninstanceofthedatabaseobjectintheirmain.cppfiletokeepitfromgoingoutof
scopeduringthelifeoftheirprogram.
Whatwereallyneedtomakethingsworkcleanlyisourmain.cpptodeclareadriver
forthedefaultdatabaseconnectionandgetacopyoftheinstance. Weneedadriver
whichwillnotthrowanerrortryingtoconnectwhenamethodtriestogetacopyofa
partiallyconfiguredentry.
Iguessitshouldn'tbeanysurprisethatQtcomeswithitsownversionofSQLiteon
mostplatforms. IcouldnotfindanexampleofthisanywhereIsearched,butthisis
basicallywhatyouneedtodo.Wewillseethisdoneinthemegaqt(mega_zillionare)
example,notthexpnsqtexample.
Ifyoumakeatypoinyourdrivername,andyouhappentohavelaunchedyour
applicationfromaterminalwindow,youwillseeahelpfulerrormessagealongthelines
ofthefollowing:
roland@roland-desktop:~/megaqt$ ./megaqt
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL3 QMYSQL QPSQL7 QPSQL
roland@roland-desktop:~/megaqt$

Chapter2CautionaryInformation

35

Ofcourseyoumighthavethedrivertypedcorrectlyandevenseethenameofitin
thelistofavailabledrivers.Inthatcase,youareusingastaticQtlibrary.Inthesource
filewhichisactuallymakingreferencetothedriveryouneedtotypethefollowing
macro:
Q_IMPORT_plugin(qsqlmysql)

andinthe.profileyouneedtoadd:
QTPLUGIN +=qmysql

Here is another beautiful error message which will have your head slamming
againstthekeyboard:
QSqlDatabasePrivate::removeDatabase: connection 'blah' is still in use, all
queries will cease to work.

Ilovedseeingthismessage.Ittookalongtimetofigureitout.Thisisthemessage
yougetwhenyouforgottoremoveyourlastreferencetoadatabasebeforecalling
removeDatabase().I'vealreadyshownyouthefixforit.Simplyassignyourdatabase
variabletothedefaultdatabasebeforecallingremove.Thatwillclearthereferencein
mostcases. Yougetthiserrorevenifthedatabaseisclosedbecausethereference
counterisgreaterthanzero.
2.7 PitfallsofMandatoryPasswords
YouhavealreadyseenafewexamplesfromtheworldofPostgreSQLwhereitwill
letaproperlyconfigureduserconnecttoadatabasewithoutforcingapasswordprompt.
Thedatabaseitselfhastobetoldtotrustlocalsystemsecuritybeforesuchthingsare
allowed.
roland@lgkl-destop:~$ psql -d postgres
psql (8.4.4)
Type "help" for help.
postgres=#

36

Chapter2CautionaryInformation

Mostofyoursecuritytypegeeksmakeitapointtoturnthisfeatureoffonany
newlycreatedsystem.Thelastthingtheywantissomeonewithaccesstoasensitive
database(likeonefullofcreditcardinfo)walkingawayfromtheirterminalwithout
lockingit.Sometimestheusercommunitywinsandsometimesitdoesn't.
I'm pointing this out in the cautionary section because it can really screw your
design. Actually,itcanmakeentireapplicationsimpossibletodevelop. Inthenext
chapter you will encounter our XPNSQT application. While it does not try to
download,install,andconfigurePostgreSQLautomaticallyfortheuser,itdoestryto
createamissingdatabase.
Youshouldnotbesurprisedtolearnthatcreatingayearspecificexpensedatabase
willrequireuserinput. Itshouldsurpriseyouevenlesstolearnthattheapplication
buildsaSHELLcommandontheflyusingthetaxyearandotherinformationtocreate
thedatabase.
cmd = "createdb " + db.databaseName() + " 'Expense Information';";
int x = system( cmd.toAscii());

Thesystem()commandinC/C++doesnotcreateashinynewterminalwindow
whereausercouldviewandrespondtopromptsthatmightbeencounteredwhenthe
commandruns.Ifsomeoneneedsorwantsthatcapability,theywillhavetomodifythe
applicationtouseQProcessprovidedbyQt.
I need to point out that createdb is a helper program provided with the
PostgreSQL database package. It is expected to reside in a directory which is in
everyone'spath.Thereareactuallyquiteafewofthesehelperprograms.Wherethey
getinstalleddependsuponyourplatformanddatabaseversion.
roland@lgkl-destop:/$ dir
clusterdb
createlang
pg_dump
pg_resetxlog
createdb
createuser
pg_dumpall pg_restore
roland@lgkl-destop:/$

/usr/lib/postgresql/8.4/bin
dropdb
dropuser
oid2name pg_controldata
pg_standby postmaster reindexdb vacuumlo
droplang
initdb
pgbench
pg_ctl
postgres
psql
vacuumdb

Chapter2CautionaryInformation

37

2.8 DatabaseFrailty
If you work in IT long enough, you will find out there are reasons for most
everything. Manytimesyouwillfindthatnoneofthosereasonsweregood,buta
courseofactionwaschosenanyway.Thisisespeciallytrueofdatabaseproducts.
OntheOpenVMSplatform,wehaveawonderfuldatabasecalledRDB. Itwas
originallydevelopedbyDEC(DigitalEquipmentCorporation)andisnowownedby
Oracle. No, it isn't the Oracle database you now see running on useless Windows
boxes, I'm talking about a good database. RDB is completely integrated with the
operatingsystem.ItusestheACLs(AccessControlLists)andrightsidentifiersfrom
thesameUAF(UserAuthorizationFile)astheoperatingsystem. Inshort,itdoesn't
have to provide some hokey rollyourown user administration and rights identifier
systemlikealmosteveryotherdatabaseonthefaceoftheplanet.
OpenVMSistheonlyoperatingsystemwhichtrulyclusters,manyclaimit,butonly
OpenVMS actuallydeliversit. TheOS kernelunderstandstheconceptofarecord
alongwiththeconceptsofACLs,rightsidentifiers,logicals,anduserprivileges.This
enablesthekerneltosupportandutilizealockmanagerwhichhascompleterecovery
builtintothesystembootprocess.Thisenablestheclustertohaveadistributedlock
managerthatworksinunisonwitheverylockmanageroneverynodeinthecluster.
The lock managers provide the foundation for the distributed transaction manager
whichallowsatransactiontospanmultipledatabases,indexedfiles,journaledflatfiles,
andmessagequeueswithfullrollback/commitcapabilities.Itmaynotbesexy,butitis
dataNirvana.
RDBallowsyousomechoiceswithdatabasecreation.Youcancreateasinglefile
database,oryoucanspreadyourdatabaseacrossmultipledrivesanddirectories.Single
filedatabasesaren'tspeeddemons,buttheyareprettycool.Youcanlosethesystem
diskwhichhadRDBrunning,createashinynewsystemdiskwithRDBonit,boot,and
tellRDBtoopenyourdatabase.Alloftheinformationneededisstoredinthesingle
filedatabaseanditsdirectory.Youhavecompleterecovery.It'sabittrickierwiththe
multifilediskversion.

38

Chapter2CautionaryInformation

I'mbringingthisuptoyounowtoletyouknowthatnoneofthedatabaseswewill
becoveringhavethatcapability.MostPCusersthinkaboutthecheapnessoreaseof
usewhenchoosingadatabase,notaboutwhathappenswhenthingsgobad.Iguessthat
iskindofunderstandablesincealotofthosepeopleareusingacomputerwithasingle
harddrivetheyrarelybackupandnevertakethetimetocreateafullimagebackup.
Whenthespindlespitsaplatterontothefloortheyarewellandtrulypooched.
Professionals,ontheotherhand,havemadealivingdesigningsystemsonmidrange
andmainframecomputers.We'vehadtobuildinfaulttolerancetolevelswhichwould
maketheaveragePCuserfaintiftheytriedtowraptheirheadsaroundit.
I'm onmysoapboxnowsothatyouwillunderstandjusthowshakythingsare.
Everydatabasewewillbecoveringhastheexactsameflaw:
If you lose the disk containing the database engine directory and have to
reinstallonanewdrive,allofyourdataistoast.Itdoesn'tmatterhowmany
drivesyouspreadthedataacrossorhowreligiouslyyoubackeditupwith
somethirdpartytool.
Theflawiscausedbythedatabaseenginekeepingalloftheschema,user,access,
space, and proprietary information in the directory with the engine, or some
subdirectorythereof.Whenyoulosethatinformation,youlosetouchwiththedata,no
matterwhereitisstored.MySQL,aswellasmostoftheothers,triestohidethisfact
fromusersbymakingthedefaultlocationfordatabasecreationthesamedirectorytree
asthedatabaseenginewhichputsitonthesamespindleastheoperatingsystem.This
wayyouloseitalltogetheranddon'trealizetheflaw.Ofcourse,creatingaproduction
databaseontheoperatingsystemdiskdriveisasystemsmanagementnonoofbiblical
proportions.
IfoundthisoutaboutPostgreSQLthehardway.
Nobinarybackupmadewithanybackuptoolisofanyuseifitcannotrestoreyour
databaseenginedirectorytoexactlywhatitneedstobesodatabaseinformationisn't

Chapter2CautionaryInformation

39

lost.Thebackupmethodusedbyallofthesedatabasesisforyoutowriteascript(or
useaprovidedutility)whichdumpsthedataintoatextonlyCSV(CommaSeparated
Value)file.Onceyourecreateyourdatabasefromthescriptfiles(youdiduseascript
insteadofjustkeyingthedefinitionsinattheinteractivepromptdidn'tyou?)theCSV
filesaretobeimportedintotheircorrespondingtables.
Ifyouaren'tanITpersonbytrade,thatpitfallprobablydoesn'tseemsobad.Iam
anITpersonbytrade. Thefirsttwoversionsofmyexpensetrackingsoftwareused
standaloneindexedfileswhichIkeptindifferentdirectoriesfordifferentyearsona
separatediskdrive.Ialsobackedthatdriveupreligiously.Icouldeasilyrecoverfrom
anysituationpriortomovingtoanOpenSourcerelationaldatabase.Now,inorderto
beabletorecover,Ihavetoquintuplemystoragerequirements.Onceallofthedatais
entered and verified, I must export each table to aCSV and backup that expanded
versionalongwiththescriptstocreateandrestoretheCSV.
WhatI'mshowingyouwillbegoodenoughforyourownpersonaldevelopment.It
mayevenbegoodenoughforasmallcompanyaslongasyouarereligiousaboutCSV
exportandbackup.Somecompanieshavebeenburnedprettybadbythisfrailty.Ihave
seensomecompaniesmapthesystembootdisk(whichcontainsthedatabaseengineand
proprietaryinformation)toaRAID6volumeontheirSANsotheycouldbeprotected
fromtwospindlesburningoutatthesametime.
Onlyyoucandecidewhatyourdataisworth.Don'tmakethatdecisionafteryou've
developedtheapplication.
It'sdifficultforaregularPCuseroreventheaverageOpenSourcedeveloperto
graspjusthowcriticalthisissueisinaproductionenvironment. Ifyourdatabaseis
currentlyapproaching1TBinsizeandisfilledwithbinarydata(mostlynumbers,not
muchtext)youwillneed5TBofcontiguousfreediskspacetocreateyourCSVfile.
RightnowthebiggestSATAdriveyoucanbuyforyourPCis2TBsounlessyoulink
threeofthemtogetherwithaRAIDcontroller,youcannotbackup.

40

Chapter2CautionaryInformation

2.9 Main.cppforWhat?
OneofthelargerissuesIhavewiththeacceptedmethodofQtdevelopmentand
itsonedirectorytreephilosophyistheconceptthateveryapplicationshouldhavea
main.cppfile.IfindthisinallofthebooksIlookatandmostoftheonlineexamples.
ItappearstobethewilloftheQtprogrammingcommunity.
Letmebethefirsttotellyou,thisisadisasterwaitingtohappen!
Yes,Ireallywantedonesentenceallbyitselflikethat.Youneedtopayattentionto
thisissue. Ifeveryprojecthastohaveamain.cppandtheyareallcustomforthat
project,youhaveatickingtimebombonyourhands.Letmetellyouastory.
Backinthedaysofmyyoot,IworkedataDECVAR(ValueAddedReseller.)
Besides selling DEC equipment, OpenVMS, and its layered products, we sold a
customizedERPsystem. ItwaslikemanyoftheERPsystemsofitsday. Allofthe
orderentrymoduleswerenamedOEnnnasothemainorderentrysourcefilewouldbe
somethinglikeOE710andthesupportingsourcefileswouldbenamedeitherOE710A,
OE710B,OE710CorOE711,OE712,OE713,dependinguponwhattheywhereand
how the developer split things. (There were rules, but they aren't relevant to this
discussion.)
Wehadnosourcecodecontrolsystem. Eachcustomerwouldhaveatemporary
directorytreesetupforthembytheSystemAnalystassignedtotheproject.Wewould
alltakesourcefromthattreeandworkonitinourownlittledevelopmentaccounts.
WhenwehadcompletedourcodingandtestingtheSystemsAnalystwouldbegiven
informationonwheretopullthesourcefromouraccountandwhatfilestopull.Ifit
passedtesting,theyputthenewsourceintotheclientsourcetree.
Ifbusinesswasgoinggood,weneverhadenoughdiskspacetogoaround.Oncea
clienthadbeeninstalledandafewdaysofshakeouttimehadpassed,theirtree
including source, data, etc. was backed up to a removable platter and/or tape, then
deletedfromthesystem.Manytimestherewereracesbetweenataperestoreprocess
and getting the developers to delete enough items for the restore to complete
successfully.Someofourclientsactuallypurchasedenoughdiskspacethatwecould
storeacopyofthesourcefileswhichhadbeenmodifiedforthemoutontheirsystems

Chapter2CautionaryInformation

41

aswell.Atokenfewevendroppedthe$1200itcostforthat300baudmodemsowe
coulddialintosupporttheirsystems.
Sonow,scatteredacrossthreediskdrivesandliterallythousandsofsubdirectories,
werevariouscopiesofeverysourcefileinthesystem.Youcanguesswhathappened
whenanewprogrammerstarted.Theywouldbegivenalistofcustomchangestoone
moduleandwouldinvariablypluckthatmodulefromadirectorytreeotherthantheone
theSystemAnalyst(SA)hadcreated.Sincetheyhadnoideaastowhatchangeshad
alreadybeenmadeforthatclient,theyhadnoideathatsomethingsweremissingfrom
this version. The SA would find that out the hard way when they got around to
integrationtesting.
Oh,therewasoneotherthingwhichwouldhappenwithnewpeople.Theywouldn't
createadifferentsubdirectoryforeachprojecttheywereworkingonandleaveituntil
theclienthadbeenremovedfromthesystem.Givenallofourtimewasbillable,we
didn'tjustgettositaroundwaitingforbugreportstocomebackfromtheSA. We
handedoffourstuffandgotanewassignmentatthattime.Ifyouhadjustgottendone
modifyingIN110forclientA,thenextSAmaygiveyoutheexactsamemoduleto
modifyforClientBsinceyouwerealreadyfamiliarwithit. Halfwaythroughyour
modificationsthefirstSAwouldcomebackwithbugstheyneededfixedrightawaydue
toaninstallationdatecomingup.Youguessedit.Theywouldpullthatmoduleinto
theirworkingdirectoryandwalkoneverythingtheyhaddoneforClientBwithout
knowingit.
I'mpointingthisoutbecauseitisamassivepetpeeveofmine. Itisphysically
impossibletosayenoughbadanddegradingthingsaboutcodemanagementsystems
whichallowdirectoriestobecreatedinsideofthemorwhichenforcesomekindof
directorystructureonaproject. Thesearenotcodemanagementtools,theyarecode
disastersinthemaking.NaturallyCVSandtheothertoolswhichsupportthishappento
bethefreestuffusedbymany,ifnotall,OpenSourceprojects.Thesearebadbadbad
badbadbadthings.

42

Chapter2CautionaryInformation

Aqualitycodemanagementpackagewillallowasourcefilenametoexist once
within a code library. If you want to fork off development, you have to create a
completelydifferentcodelibrarywhichisopticallyisolatedfromthefirstcodelibrary.
Adeveloper(orbuildprocedure)canpullaspecificgenerationofanyelementatany
timewithouthavingtoknowwhatdirectorythethingwassquirreledawayin. When
you issue a command like SHOW HISTORY OE710.BAS you get the complete
historyfortheelementandeachgenerationiseitheralinearorflatprogression.(Linear
= put back with changes from prior version, Flat = put back without any changes,
sometimesprogrammererror,othertimesresultofmigrationfromDevelopmenttoTest
toProductiontoSupport.)
Forabetterexplanationonhowaqualitycodemanagementsystemworksread,
TheMinimumYouNeedtoKnowtoBeanOpenVMSApplicationDeveloperISBN
139780977086603.InparticularyouwanttofocusonthechaptercoveringCMS.
Readerslooking for generalinformationon codemanagement systemscan start
here:http://en.wikipedia.org/wiki/Revision_controlthoughtheauthor(s)don'tappearto
understandtheproblemsofallowingbranchesandforks within thesamerepository.
WhattheydotalkquiteabitaboutishowCVScan,inmanycases,mergethecode
fromonefork/branchwithanother.That'sascarythingMaynard.
2.10 TransactionIntegrity
ThistopicissoimportantthatIbrokeitoutintoitsownsection.WhiletheSQL
interfaceprovidedbyQtcandoagreatmanythings,onethingitisnotdesignedtodois
providetransactionintegrity.Thedatabaseengine,orthedriverprovidingaccesstothe
database,hasbeenmadecompletelyresponsiblefortransactionintegrity.Thismeans
youarepersonallyresponsible.
Far too many developers expect any SQL interface to provide them complete
transactionintegrity.Ihaveseenmanyacatastrophecausedbystupiddeveloperswho
breakthecardinalruleofdatabaseapplicationdevelopment.
Keepyourtransactionsshort.

Chapter2CautionaryInformation

43

That'sit.That'stheoneruleyouhavetorememberanditistheonerulemostof
youignore.I'veevenbeenguiltyofit.ActuallyI'vehadbusinessunitsatclientsites
force upon me application design rules which meant I either created a massive
transactionorIwroteanawfullotofrecoverycodewhichmayormaynotworkinthe
eventofasystemorhardwarefailure.
Pleaseconsidertheapplicationsbeingpresentedinthisbook.Whiletheycould
bemultiuser,theyarereallydesignedtobestandalone.(Iftwodifferentuserswiththe
XPNSQTapplicationtrytocreateanewtaxyeardatabaseattheexactsameinstant,one
ofthemisgoingtohavealessthanhappyexperiencewiththeapplication.)
Whenwedesignstandaloneapplications,weseldomworryaboutmultiuserissues
orthesizeofourtransactions.Oneclientsiteusedtohavearulewhichwasenforcedin
codereviewsthatnodatabasewritetransactioncouldcontainmorethan300records
beforeacommithadtooccur.Whilehardwareimproved,theykeptthisruleinplace
andcurrentlyhaveapplicationsinproductionwhichstilladheretothisrule. Whileit
mightsoundstupidtoday,thatruleexistedfortworeasons:
1. Databaselocktimeouts
2. Snapshotfilesize
Ifyouhave400differentusersallattemptingtodosomebulkinsertof20300
recordsonthesametableatthesametime,locktimeoutcanbeanissue.Itcertainly
wasanissuebackintheearly1990swhendiskaccesswascomparativelyslowsoeach
ofthoseusersneedonetothreesecondsoflockonthesametable.Youdon'thavetobe
amathmajortofigureoutthatattwosecondseach:300*2=600seconds/60=10
minutes.Basicallyeveryuserpastthe2minutemarkwasgoingtogointoarollback
stateduetotimeoutwhich,atthetime,wasevenlessefficientthanacommit.
TodaywehavesimilarissueswiththefreelyavailabledatabasesonourPCs.While
theseissuesaren'tmultiuserperse,theyamounttothesamething,especiallyifyour
applicationisbeingaccessedviaaWebsitewitheachconnectionhavingitsownthread.
ImaginehowthingswouldfailateBaywith100,000+biddersalltryingtoupdatethe
bidhistorytablesimultaneouslyiftheyeachneededaonesecondlockontheentire

44

Chapter2CautionaryInformation

table?Theydon'thavetobebiddingonthesameitem,justbiddingonANitemtoforce
theneedforanentry.

HaveyoueverbeenaProjectManager?

HaveyoueverbeenaBusinessAnalyst?

Have you ever read SDLC (Systems Development Life Cycle)


documentationforashinynewprojectwhichinvolvesadatabase?

HaveyoueversatinameetingwiththeITteamandtheMBAswherethey
werearguingaboutwhatnewthingsneededtobepurchasedtomakethis
projectwork?

Whatistheonethingwhichgetsquestionedoneveryproject?
Ohcomeon.Wehaveallsatincollegedatabaseclassesandbeentoldthereason
relational databases can provide a rollback capability is because all of the pending
changesarekeptoffinasnapshotfileorareauntiltheCOMMIToccurs.Wehaveall
satthereatourterminalandtypedsomethinglike:
DELETE * FROM PERSONNEL;
xxxx rows deleted
ROLLBACK;
SELECT * FROM PERSONNEL;

Andsimplybeenamazedtoseeallofthedataonscreen. Thenwerepeatedthe
sameexercisewithaCOMMITinsteadofaROLLBACKandsawzerorowsmakeitto
thereportattheend.Nomatterwhatwedid,thatdatawasgoneforgoodandhadtobe
reloadedfromthesourcefile.Someoftheolderdatabasesystemswouldevenletusdo
theSELECTaftertheDELETEwithouteitheraCOMMITorROLLBACKsowecould
seetheimpactoftheDELETEpriortodecidingwhethertoCOMMITorROLLBACK.
IreallyfeelsorryforDBAswhenwearesittinginthosemeetings.Itdoesn'tmatter
howmanytimesIpointthisdatabaseexampleout,orhowmanytimestheywritethe
formulafromthedatabasevendorontheboard,MBAssimplycan'tunderstandwhywe
need to spend so much money on disk space that will never be allocated to actual
databasestorage.

Chapter2CautionaryInformation

45

PerhapsitistimetorehashanotherlessonheresincemanyofyouwillbePCusers
andhavenoideawhatdiskstorageforrealcomputerscosts. Yes,youcanfind1TB
SATAharddrivesforunder$100throughmanymajorretailersthesedays.Theprice
seemstodroponthemeverycoupleofmonths. Thesedrives,however,don'treally
workforrealcomputers.Realcomputersystemsobtainmuchoftheirstoragefroma
SAN(StorageAreaNetwork)device.Thisdevicemaybecolocatedinthecomputer
room,orsomewhereelseinthecountry.
Manyofthe1TBSANunitsrequireaforklifttobemovedandtheirown200AMP
servicetooperate. Thesearehighavailabilitydevices. Everydrive,exceptthehot
swapincabinetspares,staysspinningwhiletheunithaspowerforaslongasithas
power.Whentheunitsensesafailure,itcanrecreatethefailing/faileddriveononeof
theinternalspareswhiletheoperatorreplacesthefailing/faileddrivewithanewoneoff
theshelf.Whentheunitsensesaquiettimeitwillrecreatetheoriginaldriveinits
originalplacereturningtheincabinetsparetothesparepool.(Okay,somerequirea
systemmanagertoissueafewcommands,otherswillonlyrecreateinplace,butsome
arethiswaytoo.Let'snotchasearedherringandlosesightofthelesson.)
TheSANallowsthesystemsmanagerstoconfigureanynumberofspindlesasa
logicaldiskdriveavailabletooneormorecomputersystems. Thosespindlescanbe
configuredasrawstorageorasvariousRAIDlevels(whichiswhatallowsthehotswap
withoutdataloss.)ManyofthemostrobustSANsystemsusedonlySCSIdiskdrives
becausetheywerebuiltfartougherthantheirIDEcounterparts.Itwasnotunusualto
seea$300+pricedifferencebetweenSCSIandIDEforthesamesizedrivesfromthe
samemanufacturer.Later,somesleazydrivemanufacturerstriedsimplyputtingtheir
SCSIlogiccardsontopoftheirIDEmanufactureddrivesasawayofincreasingprofits.
Theywerequicklyweededoutofthemarketplacewhenreplacementdrivesfromthem
startedlastingdaysinsteadofyearsinsideoftheSANcabinet.
You see, the IDE drive (which is now morphed into SATA and PATA drive
interfacesoverthesameplatterset)achievesitsfiveyearMTBbyspendingmuchofits
life powered off. Of course, marketing has been able to play this up as Green
technology and many have even gotten Energy Star ratings. Once again, spending

46

Chapter2CautionaryInformation

muchofyourlifepoweredoffmakesthispossible.
LifeintheSANisdifferent.TheSANmustprovidehighavailability.Thereason
SANswereinventedwastoprovidehighavailabilityandmakediskmanagementeasier.
Justhowmanyusersdoyouthinkwillsitaroundwaitingtoplaceabidiftheyhaveto
wait30secondsforoneofthespindlesthedatabaseisspreadacrosstospinupandfor
thedatabasetoverifyitsownstructuralintegrity?
Somevendorsarenowtryingtoblendhighavailabilitywithcheaptechnologyin
newerSANdevices.Thisrequirestwoormoresetsofstandbyrackssinceeachstandby
needstomatchthedevicetypeitwilldynamicallyreplace.
Othervendorsaresimplyrelyingoncheapindividualspindlepricesandmassive
replication.Whiletheimplementationsvary,mostsimplyrequireonetosixadditional
coldmirrorspindlesforeveryhotspindle.Fromthesystemmanagerperspectivethey
simplyassignedaprimary1TBspindleandamirrorspindle,butinternallytheSAN
allocatedtwelveadditionalspindlestocoldmirrorthose. ThesoftwareintheSAN
attemptstoregulatetheamountofhourseachspindlehastobehot.Whenitgetsclose
totimeitspinsuponeofthecoldmirrorsandbeginstheupdateprocessdirectingmost
newtraffictothenewspindle. Oncetheupdateiscomplete,theoriginalhotspindle
shutsdownandcoolswhileawaitingitsnextactivetime.Eventuallysomeonewillget
thistechnologytoworkreallywell,butI'vebeenhearingmixedthingsaboutitfora
whilenow.
OneofthebigdrawbackswithSANingeneralisthebandwidthrequiredtoflash
copy1TBbetweenhotandcoldspindles. Bythetimethatissuegetsresolved2TB
driveswillprobablycostaround$50andtheproblemswillstartalloveragain.Amajor
reasonalotofSANtechnologystuckwith36GBSCSIdriveswasthefacttheycould
copythatmuchinjustafewsecondsanddoalloftheworkontheSCSIcontroller
withoutinvolvingtheSANCPU.Partofthehighavailabilityrequirementmeansthat
adatabaserequestwon'ttimeoutwhileyouareinthemiddleofahotswap.
Howmanyofyouhaveactuallysatthroughafullformatofyour1TBSATAdrive?
Notoneofthosequickformatswhichjustwritesoutthefileallocationtableand
updatestheMBR,butonewhichactuallywritesapatterntoeachdrivesector? You
aren't even doing IO at that point for most of these new drives. The drive has a

Chapter2CautionaryInformation

47

commandwhichtellsittodothatanditstillseemstotakeforever.Imaginejusthow
widespreadSANtechnologywouldbeifthatwashowlongittooktohotswapa
spindle?
EnterourprojectDBA.Heorshehastositintheprojectmeetingandsaythatif
youplantohave2TBofdatainyourdatabase,oroneofthetablescouldhaveinexcess
ofhalfabillionrecords,thattheyneed1TBofsnapshotspacetoensureallusersaccess.
TheyhavetosellthattoanMBAwithahalfamilliondollarpricetagfortheSANplus
thecostofgettingyetanother200AMPserviceinstalledinthecomputerroomandthe
additionalairconditioningcapacity.InsomecasesitmeanshavingtoputtheSANina
differentdatacenterorhavingtobuildanadditionaldatacenterbecauseyouarealready
drawingthemaximumamountofpoweryourmunicipalitywillallowanofficebusiness
todraw.
Laughallyouwantatnotbeingabletogettheelectricitytorunthedevice.Ihave
seenthathappenmorethanonce. Oneclientbuiltanofficebuildinginashinynew
officeparkwherethebuildingswerelimitedto1000AMPSeach.Itdidn'tsoundbad
until upper management started putting more people in the building than originally
planned.Beforeeverybodygotmovedin,thedatacenterdidn'thaveenoughpowerto
run the single SAN it was supposed to run. Adding insult to injury, the Business
RecoveryPlanonfilestatedthatthedatacenterinthenewofficebuildingwouldrun
thecompanyintheeventtheylosttheprimarydatacenterandIneversawitget
updated.
ManypeopleerroneouslybelievethatitistheresponsibilityoftheDBAtoensure
integrity in the database. Nothing could be further from the truth. It is the
responsibility of the DBA to ensure all of the constraints are in place to ensure
referentialintegrityexistswithinthedatabase,butitistheapplicationdeveloperand
BusinessAnalyst'sresponsibilitytoensurethatreferentialanddomainintegritymatches
thebusinessrequirements.ItistheresponsibilityoftheDBAgrouptoputinplacea
backupprocedurewhichensuresrecoverablearchivesofthedatabasegetcreated,butit
istheresponsibilityofthesystemmanagerstoensurethoseproceduresgetexecutedand
tocoordinatethehandlingofbackupmedia. ItistheresponsibilityoftheBusiness
Analysttoensurethebackupandrecoveryprocessoccurwithinanoutagewindowthe

48

Chapter2CautionaryInformation

businessunitiswillingtolivewithandtoobtainthefundingnecessarytogetbackup
andrecoverytooccurwithintheoutagewindowthebusinessiswillingtolivewith.
Lastly, it is the responsibility of the application developer to ensure their software
providestransactionalintegrity.
Ifyou,theapplicationdeveloper,arenotapartnerandfriendtotheDBAonyour
project,youaregoingtohaveamiserableprojectexperience.Ifyoustartatransaction
atthesametimeyouinitiallyconnecttothedatabase,butdon'tactuallyhavedataor
completethetransactionuntilauserprovidessomeresponse,youaregoingtobecalled
onthecarpetwhenyourprogramhasresourceslockedforhours.Ifyoutrytoselect
everyrowinthedatabaseforupdatewithoutfirstobtainingacounttoseeifthatis
reasonable,youaregoingtohaveaverybadday.
Perhapsthebiggestproblemyouwilleverencounterinyourcareeristhedifferent
definitionstransactionhasthroughoutanygivenbusiness.

TotheDBA,atransactionisawriteordeleteandcommittoasingletable.
Youmightgetthemtoagreetomultipletableswithinasingledatabase,butnot
somethingspanningmultipledatabasesordatasources.

Yourbusinesspeopleviewanentirebusinessprocessasasingletransaction.If
youareworkinginawholesalebusinesssupplyingretail/dealerlocations,the
businessisgoingtoconsiderastockordercominginfromthedealerallthe
waythroughtheorderacknowledgmentwithinstockandbackorderindicators
alongwithpricinggoingbacktothedealerasasinglebusinesstransaction.
This transaction not only encompasses checking the inventory at all
warehouses,butalsocheckingavailabledealercreditandavailableshipping
methods. Itshouldn'ttakearocketscientisttofigureoutthatmuchofthis
informationwillbeprovidedbyeitherdifferentdatabasesorinterfacesprovided
bycompletelydifferentsystems.

ManyportionsofITwillrefertothebusinesstransactionasamultisegmented
transaction.Thatdefinitionisimportant,becauseonceyoudefinethesegments
youcanbustuptheoveralltransactionintorestartableunitsofwork.Eachunit
ofworkwillbeatransactionaDBAcanlikebutyourlogicaltransactionwill
require the successful commit of many physical transactions before it can

Chapter2CautionaryInformation

49

commitorbeconsideredsuccessful.
IfyouareluckyenoughtobeworkingonOpenVMS,ithasadistributedtransaction
manager(DECdtm)whichallowsyoutocommitandrollbacktransactionsspanning
multiple data sources without having nasty database locking issues. When you are
working on lesser platforms, you have to provide your own recovery logic for
segmentedtransactions. Whatyoucannotdoiskeepadifferenttransactionopenand
pendingineverydatabaseyouareusinguntilyouknowyouarecompletelydone.
ManydevelopersareabsolutegeniuseswhenitcomestocodinginC/C++,Boost,
STL,orahostofothertoolsandlanguages. ManyofthemhaveonlycodedOpen
SourceorstandaloneapplicationsonaPCandwhiletheymaybecodingsavants,they
stillgetfiredwhentheytrytoworkforbigcompaniesusingrealcomputers. The
reasontheyarecannedisthattheydon'tunderstandtransactionalintegrity.
2.11 SQL!=SQL
I'vesaidthisbeforeandIreallycatchalotofgriefforit,butasaprofessional
developeryouhavetobelieveit.
ThereisnoSQLstandard.
Yes,IknowtheentireANSImembershipbeganhuntingmewithpitchforksand
torchestheinstanceItypedthatline,butit'sreallythetruth. Yes,thereweresome
standardspublishedandyes,everySQLengineforeverydatabasewillclaimtosupport
ANSIstandardSQL,butlessthanhalfofthecommandsyoutypewillworkonmore
thanonedatabaseproduct,evenifthedatabasetablesaredeclaredexactlythesame.
Every database you encounter supporting some flavor of SQL will exploit
implementation defined holes in the standard. They will also enhance the
standardateveryopportunity.

You have been reading an excerpt.


Please visit http://www.theminimumyouneedtoknow.com to learn more about this
book series. You can also find out how to purchase works by this author.

S-ar putea să vă placă și