Browse Source

- Initial implementation of several SQLdb examples and tests

git-svn-id: trunk@1207 -
joost 20 years ago
parent
commit
fb32bf2cd5

+ 9 - 0
.gitattributes

@@ -695,6 +695,15 @@ fcl/db/sdf/testfix.pp svneol=native#text/plain
 fcl/db/sdf/testsdf.pp svneol=native#text/plain
 fcl/db/sqldb/Makefile svneol=native#text/plain
 fcl/db/sqldb/Makefile.fpc svneol=native#text/plain
+fcl/db/sqldb/examples/alisttables.pp svneol=native#text/plain
+fcl/db/sqldb/examples/bcreatetable.pp svneol=native#text/plain
+fcl/db/sqldb/examples/cfilltable.pp svneol=native#text/plain
+fcl/db/sqldb/examples/database.ini svneol=native#text/plain
+fcl/db/sqldb/examples/dshowtable.pp svneol=native#text/plain
+fcl/db/sqldb/examples/efilltableparams.pp svneol=native#text/plain
+fcl/db/sqldb/examples/fedittable.pp svneol=native#text/plain
+fcl/db/sqldb/examples/gfiltertable.pp svneol=native#text/plain
+fcl/db/sqldb/examples/sqldbexampleunit.pp svneol=native#text/plain
 fcl/db/sqldb/fpmake.inc svneol=native#text/plain
 fcl/db/sqldb/fpmake.pp svneol=native#text/plain
 fcl/db/sqldb/interbase/Makefile svneol=native#text/plain

+ 59 - 0
fcl/db/sqldb/examples/alisttables.pp

@@ -0,0 +1,59 @@
+(******************************************************************************
+ *                                                                            *
+ *  (c) 2005 CNOC v.o.f.                                                      *
+ *                                                                            *
+ *  File:        aListTables.pp                                               *
+ *  Author:      Joost van der Sluis ([email protected])                          *
+ *  Description: SQLDB example and test program                               *
+ *  License:     GPL                                                          *
+ *                                                                            *
+ ******************************************************************************)
+
+program aListTables;
+
+{$mode objfpc}{$H+}
+
+uses
+  Classes,
+  sqldb, pqconnection, mysql4conn, IBConnection,
+  SqldbExampleUnit;
+
+var Tables : TStringList;
+    i      : integer;
+
+begin
+  ReadIniFile;
+
+// create FConnection
+  if dbtype = 'mysql' then Fconnection := tMySQLConnection.Create(nil);
+  if dbtype = 'postgresql' then Fconnection := tpqConnection.Create(nil);
+  if dbtype = 'interbase' then Fconnection := tIBConnection.Create(nil);
+
+  if not assigned(Fconnection) then ExitWithError('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
+
+  with Fconnection do
+    begin
+    DatabaseName := dbname;
+    UserName := dbuser;
+    Password := dbpassword;
+    open;
+    end;
+
+// create FTransaction
+  Ftransaction := tsqltransaction.create(nil);
+  with Ftransaction do
+    begin
+    database := Fconnection;
+    StartTransaction;
+    end;
+
+  Fconnection.Transaction := Ftransaction;
+
+  Tables := TStringList.Create;
+  Fconnection.GetTableNames(Tables);
+  for i := 0 to Tables.Count -1 do writeln(Tables[i]);
+
+  Tables.Free;
+  Ftransaction.Free;
+  Fconnection.Free;
+end.

+ 40 - 0
fcl/db/sqldb/examples/bcreatetable.pp

@@ -0,0 +1,40 @@
+(******************************************************************************
+ *                                                                            *
+ *  (c) 2005 CNOC v.o.f.                                                      *
+ *                                                                            *
+ *  File:        bCreateTable.pp                                              *
+ *  Author:      Joost van der Sluis ([email protected])                          *
+ *  Description: SQLDB example and test program                               *
+ *  License:     GPL                                                          *
+ *                                                                            *
+ ******************************************************************************)
+
+program bCreateTable;
+
+{$mode objfpc}{$H+}
+
+uses
+  Classes,
+  sqldb, pqconnection, mysql4conn, IBConnection,
+  SqldbExampleUnit;
+
+begin
+  ReadIniFile;
+  
+  CreateFConnection;
+  CreateFTransaction;
+
+  Fconnection.Transaction := Ftransaction;
+  
+  Fconnection.ExecuteDirect('recreate table FPDEV (       ' +
+                            '  id INT NOT NULL,           ' +
+                            '  Name VARCHAR(50),          ' +
+                            '  Email CHAR(50),            ' +
+                            '  Birthdate Date,            ' +
+                            '  PRIMARY KEY (id)           ' +
+                            ');                           ');
+
+  FTransaction.Commit;
+  Ftransaction.Free;
+  Fconnection.Free;
+end.

+ 60 - 0
fcl/db/sqldb/examples/cfilltable.pp

@@ -0,0 +1,60 @@
+(******************************************************************************
+ *                                                                            *
+ *  (c) 2005 CNOC v.o.f.                                                      *
+ *                                                                            *
+ *  File:        cFillTable.pp                                                *
+ *  Author:      Joost van der Sluis ([email protected])                          *
+ *  Description: SQLDB example and test program                               *
+ *  License:     GPL                                                          *
+ *                                                                            *
+ ******************************************************************************)
+
+program CFillTable;
+
+{$mode objfpc}{$H+}
+
+uses
+  Classes,
+  sqldb, pqconnection, mysql4conn, IBConnection,
+  SqldbExampleUnit;
+
+begin
+  ReadIniFile;
+
+  CreateFConnection;
+  CreateFTransaction;
+
+// create FQuery
+  Fquery := TSQLQuery.create(nil);
+  with Fquery do
+    begin
+    database := Fconnection;
+    transaction := Ftransaction;
+    end;
+
+  with Fquery do
+    begin
+
+    SQL.Clear;
+    SQL.Add('insert into FPDEV (       ');
+    SQL.Add('  id,                        ');
+    SQL.Add('  Name,                      ');
+    SQL.Add('  Email,                     ');
+    SQL.Add('  Birthdate)                 ');
+    SQL.Add('values (                     ');
+    SQL.Add('  1,                         ');
+    SQL.Add('  ''Florian Klaempfl'',      ');
+    SQL.Add('  ''[email protected]'',');
+    SQL.Add('  ''1-1-1975''               ');
+    SQL.Add(');                           ');
+
+    ExecSql;
+
+    end;
+  Ftransaction.CommitRetaining;
+
+  Fquery.Free;
+  Ftransaction.Free;
+  Fconnection.Free;
+end.
+

+ 33 - 0
fcl/db/sqldb/examples/database.ini

@@ -0,0 +1,33 @@
+[Database]
+
+# type
+# gives the type of the database-engine. Valid values are:
+# * interbase
+# * mysql
+# * postgresql
+
+type=interbase
+
+# name
+# gives the name of the database that should be used.
+# This could be a file-name or an alias, dependent on which database-engine is
+# used. More information about how to create a dabatase can be find in the
+# documentation of the database-engine.
+
+name=/opt/firebird/examples/employee.fdb
+
+# user
+# name is the name of a user which must have all rights on the selected
+# database. If the user has insufficient rights, all or one of the test could
+# fail.
+# How to set up users and their rights can be found in the database-engine
+# documentation.
+
+user=sysdba
+
+# password
+# password is the password of the provided user. If the password is incorrect,
+# all or one  of the test could fail.
+
+password=masterkey
+

+ 57 - 0
fcl/db/sqldb/examples/dshowtable.pp

@@ -0,0 +1,57 @@
+(******************************************************************************
+ *                                                                            *
+ *  (c) 2005 CNOC v.o.f.                                                      *
+ *                                                                            *
+ *  File:        dShowTable.pp                                                *
+ *  Author:      Joost van der Sluis ([email protected])                          *
+ *  Description: SQLDB example and test program                               *
+ *  License:     GPL                                                          *
+ *                                                                            *
+ ******************************************************************************)
+
+program dShowTable;
+
+{$mode objfpc}{$H+}
+
+uses
+  Classes,
+  sqldb, pqconnection, mysql4conn, IBConnection,
+  SqldbExampleUnit;
+
+begin
+  ReadIniFile;
+
+  CreateFConnection;
+  CreateFTransaction;
+  CreateFQuery;
+
+  with Fquery do
+    begin
+
+    ReadOnly := True;
+
+    SQL.Clear;
+    SQL.Add('select * from fpdev');
+
+    Writeln('Id;Name;Email;birthdate');
+
+    Open;
+
+    while not eof do
+      begin
+      write(fieldbyname('ID').asstring+';');
+      write(fieldbyname('Name').asstring+';');
+      write(fieldbyname('Email').asstring+';');
+      writeln(fieldbyname('Birthdate').asstring);
+      next;
+      end;
+
+    close;
+
+    end;
+
+  Fquery.Free;
+  Ftransaction.Free;
+  Fconnection.Free;
+end.
+

+ 63 - 0
fcl/db/sqldb/examples/efilltableparams.pp

@@ -0,0 +1,63 @@
+(******************************************************************************
+ *                                                                            *
+ *  (c) 2005 CNOC v.o.f.                                                      *
+ *                                                                            *
+ *  File:        eFillTableParams.pp                                          *
+ *  Author:      Joost van der Sluis ([email protected])                          *
+ *  Description: SQLDB example and test program                               *
+ *  License:     GPL                                                          *
+ *                                                                            *
+ ******************************************************************************)
+
+program eFillTableParams;
+
+{$mode objfpc}{$H+}
+
+uses
+  Classes, sysutils,
+  sqldb, pqconnection, mysql4conn, IBConnection,
+  SqldbExampleUnit;
+  
+var i : integer;
+
+begin
+  ReadIniFile;
+
+  CreateFConnection;
+  CreateFTransaction;
+  CreateFQuery;
+
+  with Fquery do
+    begin
+
+    SQL.Clear;
+    
+    SQL.Add('insert into FPDEV ( id, Name, Email, BirthDate)       ');
+    SQL.Add('           values ( :id, :name, :email, :birthdate )   ');
+
+    for i := 2 to 4 do
+      begin
+      params.ParamByName('id').asinteger := i;
+      params.ParamByName('name').asstring := FPdevNames[i];
+      params.ParamByName('email').AsString := FPdevEmails[i];
+      params.ParamByName('birthdate').AsDateTime := FPdevBirthDates[i];
+      ExecSql;
+      end;
+
+    for i := 5 to 7 do
+      begin
+      sql[1] := 'values ('+inttostr(i)+ ', ' +
+                      '''' +FPdevNames[i]+ ''', ' +
+                      '''' +FPdevEmails[i]+ ''', ' +
+                      '''' +FormatDateTime('MM-DD-YYYY',FPdevBirthDates[i])+ ''')';
+      ExecSql;
+      end;
+
+    end;
+  Ftransaction.CommitRetaining;
+
+  Fquery.Free;
+  Ftransaction.Free;
+  Fconnection.Free;
+end.
+

+ 61 - 0
fcl/db/sqldb/examples/fedittable.pp

@@ -0,0 +1,61 @@
+(******************************************************************************
+ *                                                                            *
+ *  (c) 2005 CNOC v.o.f.                                                      *
+ *                                                                            *
+ *  File:        fEditTable.pp                                                *
+ *  Author:      Joost van der Sluis ([email protected])                          *
+ *  Description: SQLDB example and test program                               *
+ *  License:     GPL                                                          *
+ *                                                                            *
+ ******************************************************************************)
+
+program fEditTable;
+
+{$mode objfpc}{$H+}
+
+uses
+  Classes, sysutils,
+  sqldb, pqconnection, mysql4conn, IBConnection,
+  SqldbExampleUnit;
+  
+
+begin
+  ReadIniFile;
+
+  CreateFConnection;
+  CreateFTransaction;
+  CreateFQuery;
+
+  Fconnection.Transaction := Ftransaction; //all updates are performed in this transaction
+
+  with Fquery do
+    begin
+
+    SQL.Clear;
+    
+    SQL.Add('select * from FPDEV');
+
+    open;
+    
+    Edit;
+    FieldByName('name').AsString := FPdevNames[1];
+    FieldByName('birthdate').AsDateTime := FPdevBirthDates[1];
+    Post;
+    
+    Append;
+    FieldByName('id').AsInteger := 8;
+    FieldByName('name').AsString := FPdevNames[8];
+    FieldByName('email').AsString := FPdevEmails[8];
+    FieldByName('birthdate').AsDateTime := FPdevBirthDates[8];
+    post;
+    
+    ApplyUpdates;
+
+    end;
+  Ftransaction.CommitRetaining;
+
+  Fquery.Free;
+  Ftransaction.Free;
+  Fconnection.Free;
+end.
+

+ 72 - 0
fcl/db/sqldb/examples/gfiltertable.pp

@@ -0,0 +1,72 @@
+(******************************************************************************
+ *                                                                            *
+ *  (c) 2005 CNOC v.o.f.                                                      *
+ *                                                                            *
+ *  File:        gFilterTable.pp                                              *
+ *  Author:      Joost van der Sluis ([email protected])                          *
+ *  Description: SQLDB example and test program                               *
+ *  License:     GPL                                                          *
+ *                                                                            *
+ ******************************************************************************)
+
+program gFilterTable;
+
+{$mode objfpc}{$H+}
+
+uses
+  Classes,
+  sqldb, pqconnection, mysql4conn, IBConnection,
+  SqldbExampleUnit;
+
+begin
+  ReadIniFile;
+
+  CreateFConnection;
+  CreateFTransaction;
+  CreateFQuery;
+
+  with Fquery do
+    begin
+
+    ReadOnly := True;
+
+    SQL.Clear;
+    SQL.Add('select * from fpdev');
+
+    Writeln('Id;Name;Email;birthdate');
+
+    Filter := 'id > 4';
+    Filtered := True;
+
+    Open;
+
+    while not eof do
+      begin
+      write(fieldbyname('ID').asstring+';');
+      write(fieldbyname('Name').asstring+';');
+      write(fieldbyname('Email').asstring+';');
+      writeln(fieldbyname('Birthdate').asstring);
+      next;
+      end;
+
+    Filter := 'id < 5';
+    First;
+
+    while not eof do
+      begin
+      write(fieldbyname('ID').asstring+';');
+      write(fieldbyname('Name').asstring+';');
+      write(fieldbyname('Email').asstring+';');
+      writeln(fieldbyname('Birthdate').asstring);
+      next;
+      end;
+
+    close;
+
+    end;
+
+  Fquery.Free;
+  Ftransaction.Free;
+  Fconnection.Free;
+end.
+

+ 33 - 0
fcl/db/sqldb/examples/readme.txt

@@ -0,0 +1,33 @@
+
+In this directory you can find some examples for SQLdb. They can also be used
+to test functionality and new connections.
+
+To use these examples you need a working login to a DB-Server and have the
+appropiate client installed. You have to change 'database.ini' to work with the
+right database-engine and login-credentials.
+
+You can check if everything works fine by compiling & running 'alisttables'. If
+everything works well, you'll get a list of all tables in the database provided
+in database.ini.
+
+I suggest to compile and run the examples in this order:
+
+alisttables.pp      - shows all tables in the current database
+bcreatetable.pp     - creates the 'fpdev' table, if it doesn't exist already
+cfilltable.pp       - populates the fpdev table with one record
+dshowtable.pp       - shows all records in the fpdev table
+efilltableparams.pp - add more entries to fpdev, first with and then without
+                      using parameters (use dshowtable again to see the results)
+fedittable          - edits the first record, and adds another one, without
+                      using update/insert-queries. (dshowtable to see results)
+gfiltertable        - shows how to use filtering
+
+As you can see all programs start with a letter in alphabetical order.
+
+I think that these examples could be used to write some sort of SQLdb tutorial,
+but i don't have time to do that myself. So if someone is interested... Be my
+guest.
+
+
+Joost van der Sluis, 26-sept-2005
+

+ 121 - 0
fcl/db/sqldb/examples/sqldbexampleunit.pp

@@ -0,0 +1,121 @@
+(******************************************************************************
+ *                                                                            *
+ *  (c) 2005 CNOC v.o.f.                                                      *
+ *                                                                            *
+ *  File:        SqldbExampleUnit.pp                                          *
+ *  Author:      Joost van der Sluis ([email protected])                          *
+ *  Description: This unit is used by the SQLdb examples                      *
+ *  License:     GPL                                                          *
+ *                                                                            *
+ ******************************************************************************)
+unit SqldbExampleUnit;
+
+{$mode objfpc}{$H+}
+
+interface
+
+uses
+  Classes,
+  sqldb, pqconnection, mysql4conn, IBConnection;
+
+var dbtype,
+    dbname,
+    dbuser,
+    dbpassword   : string;
+
+    Fconnection  : tSQLConnection;
+    Ftransaction : tSQLTransaction;
+    Fquery       : tSQLQuery;
+
+const
+ FPdevNames : Array[1..8] of string = ('Florian Klämpfl', 'Carl Eric Codère',
+                  'Daniël Mantione', 'Jonas Maebe', 'Michael Van Canneyt',
+                  'Peter Vreman', 'Pierre Muller', 'Marco van de Voort'
+                 );
+ FPdevEmails : Array[1..8] of string = ('[email protected]', '[email protected]',
+                  '[email protected]', '[email protected]', '[email protected]',
+                  '[email protected]', '[email protected]', '[email protected]'
+                 );
+// I know, this results in bogus-values...
+ FPdevBirthDates : Array[1..8] of TDateTime = (1-1-1991,2-2-1992,
+                   3-3-1993, 4-4-1994, 5-5-1995,
+                   6-6-1996, 7-7-1997, 8-8-1998
+                  );
+
+
+procedure ExitWithError(s : string);
+procedure ReadIniFile;
+
+procedure CreateFConnection;
+procedure CreateFTransaction;
+procedure CreateFQuery;
+
+implementation
+
+uses
+  inifiles;
+
+procedure ExitWithError(s : string);
+
+begin
+  writeln(s);
+  writeln('Execution aborted');
+  halt;
+end;
+
+procedure ReadIniFile;
+
+var IniFile : TIniFile;
+
+begin
+  IniFile := TIniFile.Create('database.ini');
+  dbtype := IniFile.ReadString('Database','Type','');
+  dbname := IniFile.ReadString('Database','Name','');
+  dbuser := IniFile.ReadString('Database','User','');
+  dbpassword := IniFile.ReadString('Database','Password','');
+  IniFile.Free;
+end;
+
+procedure CreateFConnection;
+
+begin
+  if dbtype = 'mysql' then Fconnection := tMySQLConnection.Create(nil);
+  if dbtype = 'postgresql' then Fconnection := tpqConnection.Create(nil);
+  if dbtype = 'interbase' then Fconnection := tIBConnection.Create(nil);
+
+  if not assigned(Fconnection) then ExitWithError('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
+
+  with Fconnection do
+    begin
+    DatabaseName := dbname;
+    UserName := dbuser;
+    Password := dbpassword;
+    open;
+    end;
+end;
+
+procedure CreateFTransaction;
+
+begin
+  Ftransaction := tsqltransaction.create(nil);
+  with Ftransaction do
+    begin
+    database := Fconnection;
+    StartTransaction;
+    end;
+end;
+
+procedure CreateFQuery;
+
+begin
+  Fquery := TSQLQuery.create(nil);
+  with Fquery do
+    begin
+    database := Fconnection;
+    transaction := Ftransaction;
+    end;
+end;
+
+
+end.
+