Created
April 20, 2012 11:54
-
-
Save ThomasLocke/2428003 to your computer and use it in GitHub Desktop.
SQL_Select - the cleaner version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ------------------------------------------------------------------------------- | |
| -- -- | |
| -- dbtest -- | |
| -- -- | |
| -- Copyright (C) 2012-, AdaHeads K/S -- | |
| -- -- | |
| -- This is free software; you can redistribute it and/or modify it -- | |
| -- under terms of the GNU General Public License as published by the -- | |
| -- Free Software Foundation; either version 3, or (at your option) any -- | |
| -- later version. This library is distributed in the hope that it will be -- | |
| -- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -- | |
| -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -- | |
| -- You should have received a copy of the GNU General Public License and -- | |
| -- a copy of the GCC Runtime Library Exception along with this program; -- | |
| -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- | |
| -- <http://www.gnu.org/licenses/>. -- | |
| -- -- | |
| ------------------------------------------------------------------------------- | |
| with Ada.Text_IO; | |
| with Database; | |
| with GNATCOLL.SQL; | |
| with GNATCOLL.SQL.Exec; | |
| with GNATCOLL.SQL.Postgres; | |
| with GNATCOLL.Traces; | |
| procedure DBtest is | |
| use Ada.Text_IO; | |
| use Database; | |
| use GNATCOLL.SQL; | |
| use GNATCOLL.Traces; | |
| Cursor : Exec.Forward_Cursor; | |
| DB_Description : Exec.Database_Description := | |
| Postgres.Setup (Database => "db", | |
| User => "bob", | |
| Host => "yay.com", | |
| Password => "secret"); | |
| DB_Connection : Exec.Database_Connection := | |
| Exec.Get_Task_Connection (DB_Description); | |
| Query : SQL_Query; | |
| Tags : constant SQL_Left_Join_Table := | |
| Join (Tag, | |
| Contactentity_Tag, | |
| Contactentity_Tag.Tag_Id = Tag.Tag_Id); | |
| Contacts : constant SQL_Left_Join_Table := | |
| Left_Join (Contactentity, | |
| Tags, | |
| Contactentity.Ce_Id = | |
| Contactentity_Tag.Ce_Id); | |
| Org_Contacts : constant SQL_Left_Join_Table := | |
| Join (Contactentity_Organization, | |
| Contacts, | |
| Contactentity_Organization.Ce_Id = | |
| Contactentity.Ce_Id); | |
| Org_And_Contacts : constant SQL_Left_Join_Table := | |
| Left_Join (Organization, | |
| Org_Contacts, | |
| Organization.Org_Id = | |
| Contactentity_Organization.Org_Id); | |
| begin | |
| Parse_Config_File (".gnatdebug"); | |
| -- Activate all GNATCOLL.SQL related log traces. | |
| Query := SQL_Select | |
| (Fields => | |
| Organization.Org_Id & | |
| Organization.Org_Name & | |
| Organization.Sip_Uri & | |
| Contactentity.Ce_Id & | |
| Contactentity.Ce_Name & | |
| Contactentity.Isperson & | |
| Tag.Tag_Id & | |
| Tag.Tag_Name, | |
| From => Org_And_Contacts, | |
| Where => Organization.Org_Id = 1); | |
| Cursor.Fetch (DB_Connection, Query); | |
| if DB_Connection.Success then | |
| while Exec.Has_Row (Cursor) loop | |
| New_Line; | |
| Put_Line (Cursor.Field_Name (0) & ": " & Cursor.Value (0)); | |
| Put_Line (Cursor.Field_Name (1) & ": " & Cursor.Value (1)); | |
| Put_Line (Cursor.Field_Name (2) & ": " & Cursor.Value (2)); | |
| Put_Line (Cursor.Field_Name (3) & ": " & Cursor.Value (3)); | |
| Put_Line (Cursor.Field_Name (4) & ": " & Cursor.Value (4)); | |
| Put_Line (Cursor.Field_Name (5) & ": " & Cursor.Value (5)); | |
| Put_Line (Cursor.Field_Name (6) & ": " & Cursor.Value (6)); | |
| Put_Line (Cursor.Field_Name (7) & ": " & Cursor.Value (7)); | |
| Cursor.Next; | |
| end loop; | |
| else | |
| Put_Line (DB_Connection.Error); | |
| end if; | |
| -- Free connection and description. | |
| Exec.Free (DB_Connection); | |
| Exec.Free (DB_Description); | |
| end DBtest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment