zeoslib  UNKNOWN
 All Files
ZSqlMetadata.pas
Go to the documentation of this file.
1 {*********************************************************}
2 { }
3 { Zeos Database Objects }
4 { SQL Metadata Dataset component }
5 { }
6 { Originally written by Sergey Seroukhov }
7 { }
8 {*********************************************************}
9 
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
12 { }
13 { License Agreement: }
14 { }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
20 { }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
39 { }
40 { }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
45 { }
46 { http://www.sourceforge.net/projects/zeoslib. }
47 { }
48 { }
49 { Zeos Development Group. }
50 {********************************************************@}
51 
52 unit ZSqlMetadata;
53 
54 interface
55 
56 {$I ZComponent.inc}
57 
58 uses
59  SysUtils, Classes, ZDbcIntfs, ZAbstractRODataset;
60 
61 type
62 
63  {** Defines an enumiration for SQL metadata resultsets. }
64  TZMetadataType = (mdProcedures, mdProcedureColumns, mdTables, mdSchemas,
65  mdCatalogs, mdTableTypes, mdColumns, mdColumnPrivileges, mdTablePrivileges,
66  mdBestRowIdentifier, mdVersionColumns, mdPrimaryKeys, mdImportedKeys,
67  mdExportedKeys, mdCrossReference, mdTypeInfo, mdTriggers, mdIndexInfo, mdSequences,
68  mdUserDefinedTypes);
69 
70  {**
71  Abstract dataset component which works with one specified table.
72  }
73  TZSQLMetadata = class(TZAbstractRODataset)
74  private
75  FMetadataType: TZMetadataType;
76  FCatalog: string;
77  FSchema: string;
78  FTableName: string;
79  FTriggerName: string;
80  FColumnName: string;
81  FProcedureName: string;
82  FScope: Integer;
83  FNullable: Boolean;
84  FForeignCatalog: string;
85  FForeignSchema: string;
86  FForeignTableName: string;
87  FUnique: Boolean;
88  FApproximate: Boolean;
89  FTypeName: string;
90  FSequenceName: string;
91 
92  procedure SetMetadataType(Value: TZMetadataType);
93  protected
94  function CreateResultSet(const SQL: string; MaxRows: Integer): IZResultSet;
95  override;
96  procedure CheckSQLQuery; override;
97  published
98  property MetadataType: TZMetadataType read FMetadataType write SetMetadataType;
99  property Catalog: string read FCatalog write FCatalog;
100  property Schema: string read FSchema write FSchema;
101  property TableName: string read FTableName write FTableName;
102  property ColumnName: string read FColumnName write FColumnName;
103  property ProcedureName: string read FProcedureName write FProcedureName;
104  property Scope: Integer read FScope write FScope default 0;
105  property Nullable: Boolean read FNullable write FNullable default False;
106  property ForeignCatalog: string read FForeignCatalog write FForeignCatalog;
107  property ForeignSchema: string read FForeignSchema write FForeignSchema;
108  property ForeignTableName: string read FForeignTableName write FForeignTableName;
109  property Unique: Boolean read FUnique write FUnique default False;
110  property Approximate: Boolean read FApproximate write FApproximate default False;
111  property TypeName: string read FTypeName write FTypeName;
112  property SequenceName: string read FSequenceName write FSequenceName;
113  property TriggerName: String read FTriggerName write FTriggerName;
114  property Active;
115  property MasterFields;
116  property MasterSource;
117  property LinkedFields; {renamed by bangfauzan}
118  end;
119 
120 implementation
121 
122 { TZSQLMetadata }
123 
124 {**
125  Sets a new SQL metadata type.
126  @param Value a new SQL metadata type.
127 }
128 procedure TZSQLMetadata.SetMetadataType(Value: TZMetadataType);
129 begin
130  if FMetadataType <> Value then
131  begin
132  Active := False;
133  FMetadataType := Value;
134  end;
135 end;
136 
137 {**
138  Creates a DBC resultset for the query.
139  @param SQL an SQL query.
140  @param MaxRows a maximum rows number (-1 for all).
141  @returns a created DBC resultset.
142 }
143 {$IFDEF FPC}
144  {$HINTS OFF}
145 {$ENDIF}
146 function TZSQLMetadata.CreateResultSet(const SQL: string; MaxRows: Integer):
147  IZResultSet;
148 var
149  Metadata: IZDatabaseMetadata;
150 begin
151  Connection.ShowSQLHourGlass;
152  try
153  Metadata := Connection.DbcConnection.GetMetadata;
154 
155  case FMetadataType of
156  mdProcedures:
157  Result := Metadata.GetProcedures(FCatalog, FSchema, FProcedureName);
158  mdProcedureColumns:
159  Result := Metadata.GetProcedureColumns(FCatalog, FSchema,
160  FProcedureName, FColumnName);
161  mdTables:
162  Result := Metadata.GetTables(FCatalog, FSchema, FTableName, nil);
163  mdSchemas:
164  Result := Metadata.GetSchemas;
165  mdCatalogs:
166  Result := Metadata.GetCatalogs;
167  mdTableTypes:
168  Result := Metadata.GetTableTypes;
169  mdTriggers:
170  Result := Metadata.GetTriggers(FCatalog, FSchema, FTableName, FTriggerName);
171  mdColumns:
172  Result := Metadata.GetColumns(FCatalog, FSchema, FTableName,
173  FColumnName);
174  mdColumnPrivileges:
175  Result := Metadata.GetColumnPrivileges(FCatalog, FSchema, FTableName,
176  FColumnName);
177  mdTablePrivileges:
178  Result := Metadata.GetTablePrivileges(FCatalog, FSchema, FTableName);
179  mdBestRowIdentifier:
180  Result := Metadata.GetBestRowIdentifier(FCatalog, FSchema, FTableName,
181  FScope, FNullable);
182  mdVersionColumns:
183  Result := Metadata.GetVersionColumns(FCatalog, FSchema, FTableName);
184  mdPrimaryKeys:
185  Result := Metadata.GetPrimaryKeys(FCatalog, FSchema, FTableName);
186  mdImportedKeys:
187  Result := Metadata.GetImportedKeys(FCatalog, FSchema, FTableName);
188  mdExportedKeys:
189  Result := Metadata.GetExportedKeys(FCatalog, FSchema, FTableName);
190  mdCrossReference:
191  Result := Metadata.GetCrossReference(FCatalog, FSchema, FTableName,
192  FForeignCatalog, FForeignSchema, FForeignTableName);
193  mdTypeInfo:
194  Result := Metadata.GetTypeInfo;
195  mdIndexInfo:
196  Result := Metadata.GetIndexInfo(FCatalog, FSchema, FTableName, FUnique,
197  FApproximate);
198  mdSequences:
199  Result := Metadata.GetSequences(FCatalog, FSchema, FSequenceName);
200  mdUserDefinedTypes:
201  Result := Metadata.GetUDTs(FCatalog, FSchema, FTypeName, nil);
202  end;
203  finally
204  Connection.HideSQLHourGlass;
205  end;
206 end;
207 {$IFDEF FPC}
208  {$HINTS ON}
209 {$ENDIF}
210 
211 {**
212  Checks the SQL query. The query has no meaning for this class.
213 }
214 procedure TZSQLMetadata.CheckSQLQuery;
215 begin
216  // Here sql has no meaning. So it should not be tested.
217 end;
218 
219 end.