* Makefile.am:
[monodevelop.git] / extras / MonoDevelop.Database / MonoDevelop.Database.Sql.Oracle / OracleSchemaProvider.cs
blob5c22ee4130dac81a3ac95ab80d12ec027cc4d70a
1 //
2 // Authors:
3 // Christian Hergert <chris@mosaix.net>
4 // Daniel Morgan <danielmorgan@verizon.net>
5 // Ben Motmans <ben.motmans@gmail.com>
6 //
7 // Copyright (C) 2005 Mosaix Communications, Inc.
8 // Copyright (C) 2005 Daniel Morgan
9 // Copyright (c) 2007 Ben Motmans
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
30 using System;
31 using System.Text;
32 using System.Data;
33 using System.Data.OracleClient;
34 using System.Collections.Generic;
35 namespace MonoDevelop.Database.Sql.Oracle
37 public class OracleSchemaProvider : AbstractSchemaProvider
39 public OracleSchemaProvider (IConnectionPool connectionPool)
40 : base (connectionPool)
42 //TODO: table, column, procedure, view, user, sequence, trigger capabilities
45 // public override ICollection<TableSchema> GetTables ()
46 // {
47 // CheckConnectionState ();
48 // List<TableSchema> tables = new List<TableSchema> ();
49 //
50 // IDbCommand command = connectionProvider.CreateCommand (
51 // "SELECT OWNER, TABLE_NAME, TABLESPACE_NAME " +
52 // "FROM ALL_TABLES " +
53 // "ORDER BY OWNER, TABLE_NAME"
54 // );
55 // using (command) {
56 // using (IDataReader r = command.ExecuteReader()) {
57 // while (r.Read ()) {
58 // TableSchema table = new TableSchema (this);
59 //
60 // table.OwnerName = r.GetValue (0).ToString();
61 // table.SchemaName = r.GetValue (0).ToString();
62 // table.Name = r.GetString (1).ToString();
63 // table.IsSystemTable = IsSystem (table.OwnerName);
64 // table.TableSpaceName = r.GetValue (2).ToString();
65 //
66 // StringBuilder sb = new StringBuilder();
67 // sb.AppendFormat ("-- Table: {0}\n", table.Name);
68 // sb.AppendFormat ("-- DROP TABLE {0};\n\n", table.Name);
69 // sb.AppendFormat ("CREATE TABLE {0} (\n", table.Name);
70 //
71 // ICollection<ColumnSchema> columns = table.Columns;
72 // string[] parts = new string[columns.Count];
73 // int i = 0;
74 // foreach (ColumnSchema col in columns)
75 // parts[i++] = col.Definition;
76 // sb.Append (String.Join (",\n", parts));
77 //
78 // ICollection<ConstraintSchema> constraints = table.Constraints;
79 // parts = new string[constraints.Count];
80 // if (constraints.Count > 0)
81 // sb.Append (",\n");
82 // i = 0;
83 // foreach (ConstraintSchema constr in constraints)
84 // parts[i++] = "\t" + constr.Definition;
85 // sb.Append (String.Join (",\n", parts));
86 //
87 // //sb.AppendFormat ("\n) COMMENT '{0}';", table.Comment);
88 // table.Definition = sb.ToString ();
89 //
90 // tables.Add (table);
91 // }
92 // r.Close ();
93 // }
94 // connectionProvider.Close (command.Connection);
95 // }
97 // return tables;
98 // }
99 //
100 // public override ICollection<ColumnSchema> GetTableColumns (TableSchema table)
101 // {
102 // CheckConnectionState ();
103 // List<ColumnSchema> columns = new List<ColumnSchema> ();
105 // IDbCommand command = connectionProvider.CreateCommand (
106 // "SELECT OWNER, TABLE_NAME, COLUMN_NAME, " +
107 // " DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, " +
108 // " NULLABLE, COLUMN_ID, DEFAULT_LENGTH, DATA_DEFAULT " +
109 // "FROM ALL_TAB_COLUMNS " +
110 // "WHERE OWNER = '" + table.OwnerName + "' " +
111 // "AND TABLE_NAME = '" + table.Name + "' " +
112 // "ORDER BY OWNER, TABLE_NAME, COLUMN_ID"
113 // );
114 // using (command) {
115 // using (IDataReader r = command.ExecuteReader()) {
116 // while (r.Read ()) {
117 // ColumnSchema column = new ColumnSchema (this);
119 // column.Name = GetCheckedString (r, 2);
120 // column.DataTypeName = GetCheckedString (r, 3);
121 // column.OwnerName = table.OwnerName;
122 // column.SchemaName = table.OwnerName;
123 // column.NotNull = GetCheckedString (r, 7) == "Y";
124 // column.Length = GetCheckedInt32 (r, 4);
125 // column.Precision = GetCheckedInt32 (r, 5);
126 // column.Scale = GetCheckedInt32 (r, 6);
127 // column.ColumnID = GetCheckedInt32 (r, 8);
129 // StringBuilder sb = new StringBuilder();
130 // sb.AppendFormat("{0} {1}{2}",
131 // column.Name,
132 // column.DataTypeName,
133 // (column.Length > 0) ? ("(" + column.Length + ")") : "");
134 // sb.AppendFormat(" {0}", column.NotNull ? "NOT NULL" : "NULL");
135 // //if (column.Default.Length > 0)
136 // // sb.AppendFormat(" DEFAULT {0}", column.Default);
137 // column.Definition = sb.ToString();
139 // columns.Add (column);
140 // }
141 // r.Close ();
142 // };
143 // connectionProvider.Close (command.Connection);
144 // }
146 // return columns;
147 // }
149 // public override ICollection<ViewSchema> GetViews ()
150 // {
151 // CheckConnectionState ();
152 // List<ViewSchema> views = new List<ViewSchema> ();
154 // IDbCommand command = connectionProvider.CreateCommand (
155 // "SELECT OWNER, VIEW_NAME, TEXT " +
156 // "FROM ALL_VIEWS " +
157 // "ORDER BY OWNER, VIEW_NAME"
158 // );
159 // using (command) {
160 // using (IDataReader r = command.ExecuteReader()) {
161 // while (r.Read ()) {
162 // ViewSchema view = new ViewSchema (this);
164 // view.Name = r.GetString(1);
165 // view.SchemaName = r.GetString (0);
166 // view.OwnerName = r.GetString (0);
167 // view.Definition = r.GetString (2);
168 // view.IsSystemView = IsSystem (view.OwnerName);
170 // views.Add (view);
171 // }
172 // r.Close ();
173 // }
174 // connectionProvider.Close (command.Connection);
175 // }
176 // return views;
177 // }
179 // public override ICollection<ColumnSchema> GetViewColumns (ViewSchema view)
180 // {
181 // CheckConnectionState ();
182 // List<ColumnSchema> columns = new List<ColumnSchema> ();
184 // IDbCommand command = connectionProvider.CreateCommand (
185 // "SELECT * " +
186 // " FROM " + view.Name +
187 // " WHERE 1 = 0"
188 // );
189 // using (command) {
190 // using (IDataReader r = command.ExecuteReader()) {
191 // while (r.Read ()) {
192 // for (int i = 0; i < r.FieldCount; i++) {
193 // ColumnSchema column = new ColumnSchema (this);
195 // column.Name = r.GetString (0);
196 // column.DataTypeName = r.GetString (1);
197 // column.SchemaName = view.SchemaName;
198 // column.NotNull = r.GetBoolean (3);
199 // column.Length = r.GetInt32 (2);
201 // columns.Add (column);
202 // }
203 // }
204 // r.Close ();
205 // };
206 // connectionProvider.Close (command.Connection);
207 // }
209 // return columns;
210 // }
212 // public override ICollection<ConstraintSchema> GetTableConstraints (TableSchema table)
213 // {
214 // CheckConnectionState ();
215 // List<ConstraintSchema> constraints = new List<ConstraintSchema> ();
217 // IDbCommand command = connectionProvider.CreateCommand (
218 // "SELECT k.owner, k.table_name, k.constraint_name, " +
219 // " k.constraint_type, k.status, k.validated " +
220 // "FROM all_constraints k " +
221 // "WHERE k.owner = '" + table.OwnerName + "' " +
222 // "AND k.table_name = '" + table.Name + "' " +
223 // "and k.constraint_type = 'P'"
224 // );
225 // using (command) {
226 // using (IDataReader r = command.ExecuteReader()) {
227 // while (r.Read ()) {
228 // ConstraintSchema constraint = null;
230 // switch (r.GetString(4)) {
231 // case "P":
232 // default:
233 // constraint = new PrimaryKeyConstraintSchema (this);
234 // break;
235 // }
237 // constraint.Name = r.GetString (3);
238 // constraint.Definition = "";
240 // constraints.Add (constraint);
241 // }
242 // r.Close ();
243 // }
244 // connectionProvider.Close (command.Connection);
245 // }
247 // return constraints;
248 // }
250 // public override DataTypeSchema GetDataType (string name)
251 // {
252 // if (name == null)
253 // throw new ArgumentNullException ("name");
254 // name = name.ToUpper ();
256 // DataTypeSchema dts = new DataTypeSchema (this);
257 // dts.Name = name;
258 // switch (name) {
259 // //TODO: IMPLEMENT
260 // case "":
261 // break;
262 // default:
263 // dts = null;
264 // break;
265 // }
267 // return dts;
268 // }
270 // private bool IsSystem (string owner)
271 // {
272 // switch (owner) {
273 // case "SYSTEM":
274 // case "SYS":
275 // case "DRSYS":
276 // case "CTXSYS":
277 // case "MDSYS":
278 // case "WKSYS":
279 // return true;
280 // default:
281 // return false;
282 // }
283 // }