Difference between revisions of "Zeos tutorial"

From Lazarus wiki
Jump to navigationJump to search
 
(20 intermediate revisions by 12 users not shown)
Line 1: Line 1:
 
{{Zeos tutorial}}
 
{{Zeos tutorial}}
  
== Overview ==
+
{{Infobox databases}}
 +
 
 +
= Overview =
  
 
This tutorial is about getting, installing and using [http://zeos.firmos.at/portal.php Zeoslib] with [[Glossary#Lazarus|Lazarus]] and [[Glossary#FreePascal|FPC]].
 
This tutorial is about getting, installing and using [http://zeos.firmos.at/portal.php Zeoslib] with [[Glossary#Lazarus|Lazarus]] and [[Glossary#FreePascal|FPC]].
<br>
 
<br>
 
'''<font color=navy>The Zeos team has recently made a new forum. Please visit the new forum for more information about ZeosLib and to ask/answer questions about using Zeos and report bugs. The new forum can be found here [http://zeos.firmos.at Forum for ZeosLib].</font>'''
 
  
== Getting Zeos ==
+
= Getting Zeos =
  
 
Zeos has recently been ported to [[Glossary#Lazarus|Lazarus]] and there are no releases yet that officially support it but you can easily get it from SVN if you follow these steps:
 
Zeos has recently been ported to [[Glossary#Lazarus|Lazarus]] and there are no releases yet that officially support it but you can easily get it from SVN if you follow these steps:
 +
 +
==SVN==
  
 
=== Windows ===
 
=== Windows ===
Line 16: Line 17:
 
* get a SVN client [http://tortoisesvn.tigris.org TortoiseSVN] and install
 
* get a SVN client [http://tortoisesvn.tigris.org TortoiseSVN] and install
 
* see [http://tortoisesvn.net/docs/release/TortoiseSVN_en/help-onepage.html#tsvn-dug-general Getting Started with TortoiseSVN]
 
* see [http://tortoisesvn.net/docs/release/TortoiseSVN_en/help-onepage.html#tsvn-dug-general Getting Started with TortoiseSVN]
* do Checkout from Windows Explorer: svn://zeos.firmos.at/zeos/trunk  
+
* do Checkout from Windows Explorer: http://svn.code.sf.net/p/zeoslib/code-0/trunk
  
 
=== Linux/BSD ===
 
=== Linux/BSD ===
  
* get a SVN client (esvn, kdesvn, etc.)
+
* [[Portal:FreeBSD|FreeBSD]] comes with '''svnlite'' (an svn client) preinstalled
 +
* get an SVN client (esvn, kdesvn, etc.)
 
* create zeosdbo directory, go in that directory and do
 
* create zeosdbo directory, go in that directory and do
* svn checkout https://zeoslib.svn.sourceforge.net/svnroot/zeoslib/trunk
+
* svn checkout http://svn.code.sf.net/p/zeoslib/code-0/trunk
  (old development until revision 903:  svn checkout svn://zeos.firmos.at/zeos/trunk)
 
  
== Installing the components ==
+
==ZIP==
 +
 
 +
You can download the latest version as a ZIP file from sourceforge.net:
 +
* [http://sourceforge.net/projects/zeoslib/ http://sourceforge.net/projects/zeoslib/]
 +
 
 +
= Installing the components =
  
 
This is a tricky part so you should have a little patience and read this part carefully.
 
This is a tricky part so you should have a little patience and read this part carefully.
 
 
* Make sure you have the latest [http://www.de.freepascal.org/lazarus/ Lazarus snapshot] and the at least FPC 2.0.3 not older than 6th March 2006.
 
* Make sure you have the latest [http://www.de.freepascal.org/lazarus/ Lazarus snapshot] and the at least FPC 2.0.3 not older than 6th March 2006.
 
 
* Start one instance of Lazarus.
 
* Start one instance of Lazarus.
 
 
# Use '''Components/Open Package File(.lpk)''' from the main menu.
 
# Use '''Components/Open Package File(.lpk)''' from the main menu.
# Go to '''zeosdbo_rework\packages\lazarus\''' and open '''zcomponent.lpk'''
+
# Go to '''zeosdbo_rework\packages\lazarus\''' and open '''zcomponent.lpk'''. Note: Starting with Zeos 7.3 / 8.0 Zeos has a new zcomponentdesign package for Lazarus. Please use this package on these versions.
 
# Press '''[Compile]''' only if you don't want to install the components into the IDE
 
# Press '''[Compile]''' only if you don't want to install the components into the IDE
 
# Press '''[Install]'''
 
# Press '''[Install]'''
Line 46: Line 49:
 
If you receive the error "Cannot find unit ZClasses" or something similar, then you need to carefully check the case of the filenames in your Zeos source distribution.
 
If you receive the error "Cannot find unit ZClasses" or something similar, then you need to carefully check the case of the filenames in your Zeos source distribution.
 
* Even if the cases match exactly, the autogenerated package source file can generate the wrong case name in the uses clause (Lazarus 0.9.18), i.e.:
 
* Even if the cases match exactly, the autogenerated package source file can generate the wrong case name in the uses clause (Lazarus 0.9.18), i.e.:
 
 
    
 
    
<delphi>{ This file was automatically created by Lazarus. Do not edit!
+
<syntaxhighlight lang=pascal>
 +
{ This file was automatically created by Lazarus. Do not edit!
 
   This source is only used to compile and install the package.
 
   This source is only used to compile and install the package.
 
}
 
}
Line 58: Line 61:
 
   Zvariant;  
 
   Zvariant;  
 
implementation
 
implementation
end.</delphi>
+
end.
 
+
</syntaxhighlight>
  
 
* Notice that Lazarus has named the unit ZClasses Zclasses, resulting in a naming conflict. Presumably this is a bug in Lazarus, not the Zeos packages. One way around this is to rename all the zeos source files to lowercase. Trawl through each subdirectory under src/ and execute this command in a bash window:
 
* Notice that Lazarus has named the unit ZClasses Zclasses, resulting in a naming conflict. Presumably this is a bug in Lazarus, not the Zeos packages. One way around this is to rename all the zeos source files to lowercase. Trawl through each subdirectory under src/ and execute this command in a bash window:
Line 68: Line 71:
 
* The package should compile now.
 
* The package should compile now.
  
== Comments ==
+
= Installing Zeos components in MSEide =
 
+
In order to install the Zeos components:
Currently Zeos CVS for Lazarus has broken packages.
+
* add the path to the Zeos source to 'Project'-'Options'-'Make'-'Directories'
Some comments from IgD:
+
* and compile the IDE with '''-dmse_with_zeoslib -dMSEgui'''.
 +
* There is a predefined IDE project '''apps/ide/mseide_zeos.prj''', update 'Project'- 'Options'-'Macros' according to your installation.
 +
* A simple demo is in [https://github.com/mse-org/mseuniverse/tree/main/samples/db/zeos mseuniverse].
  
Using a fresh install of Laz 0.9.12 (latest stable version) I was able to install ZeosLib dated 13 Oct 05. First fix the following bugs in ZeosLib:
+
= Make your first Zeos application =
 
 
* 1. Change ZplainPostgreSql81.pas to ZPlainPostgresSql8x.pas in zplain.lpk
 
 
<Item22>
 
  <Filename Value="../../src/plain/ZPlainPostgreSql81.pas"/>
 
  <UnitName Value="ZPlainPostgreSql8x"/>
 
</Item22>
 
 
 
 
* 2. Fix the following typos in zdbc.lpk:
 
 
 
a. Remove extra "/" from "build/" (remove the one that is inside the quotes)
 
 
 
<SearchPaths>
 
  <OtherUnitFiles Value="../../src/dbc/"/>
 
  <UnitOutputDirectory Value="build/"/>
 
</SearchPaths>
 
 
b. Add missing quote to unitname value
 
 
 
<Item43>
 
  <Filename Value="../../src/dbc/ZDbcASA.pas"/>
 
  <UnitName Value="ZDbcASA/>
 
</Item43>
 
 
 
Now, here are the installation instructions:
 
 
 
Before Lazarus 0.9.13 you must open each of the zeos packages via Components/Open Package File in the following order:
 
 
 
# zcore.lpk
 
# zparsesql.lpk
 
# zplain.lpk
 
# zdbc.lpk
 
 
 
Then finally open zcomponent.lpk and compile and install this package.
 
0.9.13 can simply open zcomponent.lpk. The IDE will open the rest automatically.
 
 
 
Note: The first four packages only need to be compiled and cannot be installed.
 
 
 
--[[User:Forest|Forest]] 17:41, 2 Nov 2005 (CET)
 
 
 
* 3. If you compile and install with sucessfull Zeos, but Lazarus, after rebuilded, start with:
 
[[Image:Zeoserror.gif]]
 
 
 
Then you can do:
 
 
 
In Zeos.Inc file replace, in section "// Settings for Debug mode":
 
{$R-}
 
to
 
{$R+}
 
 
 
--[[User:Junior|Junior]] 19:48, 15 Apr 2006 (GMT)
 
 
 
== Make your first Zeos application ==
 
  
 
* Drop a '''ZConnection'''.
 
* Drop a '''ZConnection'''.
Line 145: Line 96:
 
** If all is ok you should now be able to see the records from your table.
 
** If all is ok you should now be able to see the records from your table.
  
== More information about working with Zeos ==
+
= Possible Bugs and Issues =
  
Here you can find some really useful articles explaining Zeos usage and it's internals, some of them  also talk about Delphi specific stuff but mostly the same techniques can be used in Lazarus too.<br>
+
* I have noticed that sometimes when building Lazarus it cannot find some Zeos files, as a quick workaround try this:
 
 
* [http://zeos.firmos.at/kb.php?mode=article&k=6 The ZeosLib DBOs 6.1.5 - With Delphi 7 and Firebird 1.5]
 
* [http://zeos.firmos.at/kb.php?mode=article&k=4 Overview of the Zeos Database Objects Architecture]
 
* [http://zeos.firmos.at/kb.php?mode=article&k=5 An Introduction To ZDBC API]
 
* Also check the [http://zeos.firmos.at/downloads.php Downloads] section for real examples wich can be easily ported to Lazarus.
 
 
 
<br>
 
'''Please complete me :)'''
 
 
 
== Possible Bugs and Issues ==
 
 
 
* I have noticed that somethimes when building Lazarus it cannot find some Zeos files, as a quick workaround try this:
 
 
** Use '''Components/Package Graph''' from the main menu.
 
** Use '''Components/Package Graph''' from the main menu.
 
** Open the '''ZComponent''' package.
 
** Open the '''ZComponent''' package.
Line 167: Line 106:
 
** Recompile Lazarus normally (with packages).<br><br>
 
** Recompile Lazarus normally (with packages).<br><br>
  
* There is a bug in [[Glossary#Freepascal|FPC's]] [http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/ansistrcomp.html AnsiStrComp] wich causes unreliable PChar comparison, this is obvious when using a DBGrid to edit data, '''AnsiStrComp('abc', 'abcd')''' can't detect them as being different but it detects '''AnsiStrComp('abd', 'abcd')''' ignoring multiple chars from the second string. This bug was found by [[User:Jesusrmx|Jesusrmx]]. It will probably be fixed soon, see [http://www.freepascal.org/bugs/showrec.php3?ID=4325 Bug Report].<br><br>
+
=See also=
 
 
* Trying to compile revision 903 of ZeosDBO trunk (20110806) on GNU/Linux amd64 x86_64 gives errors on ZClasses.pas file like:
 
** Error: No matching Implementation for interface method "IUnknown.QueryInterface (constref TGuid, out <Formal type>) LongInt; Cdecl;"
 
** The problem is due to changes in FPC related to compatibility between platforms (http://wiki.freepascal.org/User_Changes_2.6.0#IInterface.QueryInterface.2C_._AddRef_and_._Release_definitions_have_been_changed)
 
** and the solution is to edit the ZClasses.pas file as the following patch:
 
 
 
-    function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; virtual;                                                     
 
-    function _AddRef: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};                                                                                                                                               
 
-    function _Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};                                                                                                                                               
 
+    function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult; {$IF (not defined(WINDOWS)) AND (FPC_FULLVERSION>=20501)}cdecl{$ELSE}stdcall{$IFEND}; virtual;
 
+    function _AddRef: Integer; {$IF (not defined(WINDOWS)) AND (FPC_FULLVERSION>=20501)}cdecl{$ELSE}stdcall{$IFEND};
 
+    function _Release: Integer; {$IF (not defined(WINDOWS)) AND (FPC_FULLVERSION>=20501)}cdecl{$ELSE}stdcall{$IFEND};
 
  public
 
    constructor Create(const Controller: IInterface);
 
    property Controller: IInterface read GetController;
 
@@ -89,7 +93,8 @@
 
  TContainedObject = class(TAggregatedObject, IInterface)
 
  protected
 
-    function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; override;
 
+    function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult; {$IF (not defined(WINDOWS)) AND (FPC_FULLVERSION>=20501)}cdecl{$ELSE}stdcall{$IFEND}; override;
 
  end;
 
  {$ENDIF}
 
@@ -221,7 +226,8 @@
 
  Result := IInterface(FController)._AddRef;
 
end;
 
-function TAggregatedObject._Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
 
+function TAggregatedObject._Release: Integer; {$IF (not defined(WINDOWS)) AND (FPC_FULLVERSION>=20501)}cdecl{$ELSE}stdcall{$IFEND};
 
 
 
<br>
 
* Trying to compile revision 903 of ZeosDBO trunk on GNU/Linux with FPC 2.6.0 or higher gives the following error in ZAbstractRODataset.pas: "Error: Expected another 2 array elements". The problem is FPC2_1UP is not defined because the file src/Zeos.inc doesn't check for the newer versions. If we are using only newer versions of FPC (2.6, 2.7 and up) its safe to modify src/Zeos.inc adding the following lines just before {$IFDEF VER2_5}, and the error is gone.
 
 
 
+  {$DEFINE FPC2_5UP}
 
+  {$DEFINE FPC2_4UP}
 
+  {$DEFINE FPC2_3UP}
 
+  {$DEFINE FPC2_2UP}
 
+  {$DEFINE FPC2_1UP}
 
+  {$DEFINE FPC2_UP}
 
+
 
    {$IFDEF VER2_5}
 
      {$DEFINE FPC2_5UP}
 
 
 
  
[[Category:Tutorials]]
+
* [http://zeoslib.sourceforge.net/index.php Forum for ZeosLib]
[[Category:Components]]
+
* [[ZeosDBO]]
[[Category:Databases]]
+
* [https://lazarus.intern.es/tutorial_firebird_lazarus_zeos_2.html Tutorial Lazarus/Zeos/Firebird (Windows)] German/Parts in English [https://lazarus.intern.es/download_tutorials_lazarus_zeos_firebird.html download site]

Latest revision as of 09:03, 28 March 2022

Deutsch (de) English (en) español (es) français (fr) português (pt) русский (ru) 中文(中国大陆)‎ (zh_CN)

Databases portal

References:

Tutorials/practical articles:

Databases

Advantage - MySQL - MSSQL - Postgres - Interbase - Firebird - Oracle - ODBC - Paradox - SQLite - dBASE - MS Access - Zeos

Overview

This tutorial is about getting, installing and using Zeoslib with Lazarus and FPC.

Getting Zeos

Zeos has recently been ported to Lazarus and there are no releases yet that officially support it but you can easily get it from SVN if you follow these steps:

SVN

Windows

Linux/BSD

ZIP

You can download the latest version as a ZIP file from sourceforge.net:

Installing the components

This is a tricky part so you should have a little patience and read this part carefully.

  • Make sure you have the latest Lazarus snapshot and the at least FPC 2.0.3 not older than 6th March 2006.
  • Start one instance of Lazarus.
  1. Use Components/Open Package File(.lpk) from the main menu.
  2. Go to zeosdbo_rework\packages\lazarus\ and open zcomponent.lpk. Note: Starting with Zeos 7.3 / 8.0 Zeos has a new zcomponentdesign package for Lazarus. Please use this package on these versions.
  3. Press [Compile] only if you don't want to install the components into the IDE
  4. Press [Install]
  5. You are asked if you want to recompile Lazarus.
  • Answer [Yes] this time.
  • Wait until compilation ends, Lazarus should restart itself after that.
  • If all is ok you should now be able to see the [Zeos Access] tab in the Component Palette.

Zeos Components.png

If you receive the error "Cannot find unit ZClasses" or something similar, then you need to carefully check the case of the filenames in your Zeos source distribution.

  • Even if the cases match exactly, the autogenerated package source file can generate the wrong case name in the uses clause (Lazarus 0.9.18), i.e.:
{ This file was automatically created by Lazarus. Do not edit!
  This source is only used to compile and install the package.
}
unit Zcore; 
interface
uses
  Zclasses, Zcollections, Zcompatibility, Zexprparser, Zexprtoken, Zexpression, 
  Zfunctions, Zmatchpattern, Zmessages, Zsysutils, Ztokenizer, Zvariables, 
  Zvariant; 
implementation
end.
  • Notice that Lazarus has named the unit ZClasses Zclasses, resulting in a naming conflict. Presumably this is a bug in Lazarus, not the Zeos packages. One way around this is to rename all the zeos source files to lowercase. Trawl through each subdirectory under src/ and execute this command in a bash window:
 rename -v 'y/A-Z/a-z/' *
  • Then, in Lazarus, reopen the package (.lpk) and fix the filename cases by clicking on "More..."/"Fix Files Case"
  • The package should compile now.

Installing Zeos components in MSEide

In order to install the Zeos components:

  • add the path to the Zeos source to 'Project'-'Options'-'Make'-'Directories'
  • and compile the IDE with -dmse_with_zeoslib -dMSEgui.
  • There is a predefined IDE project apps/ide/mseide_zeos.prj, update 'Project'- 'Options'-'Macros' according to your installation.
  • A simple demo is in mseuniverse.

Make your first Zeos application

  • Drop a ZConnection.
    • Set your User, Password, Host, Port and Protocol (and any other params if needed).
    • Set Connected to True.
  • Drop a ZQuery (do not mistake with ZReadOnlyQuery).
    • Set the Connection to your active ZConnection.
    • Set the Sql property to something like SELECT * FROM MyTable
    • Set Active to True.
  • Drop a DataSource from the [Data Access] tab.
    • Set the DataSet to your active ZQuery.
  • Drop a DBGrid from the [Data Controls] tab.
    • Set the Datasource to your DataSource.
    • If all is ok you should now be able to see the records from your table.

Possible Bugs and Issues

  • I have noticed that sometimes when building Lazarus it cannot find some Zeos files, as a quick workaround try this:
    • Use Components/Package Graph from the main menu.
    • Open the ZComponent package.
    • Right Click on the Files item in the list.
    • Choose [Recompile all required].
    • When asked "Re-Compile this and all required packages?" answer [Yes].
    • Recompile Lazarus normally (with packages).

See also