PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Npgsql/Npgsql/NpgsqlSchema.cs

https://github.com/iainlane/mono
C# | 286 lines | 177 code | 46 blank | 63 comment | 11 complexity | 38626725f7a0d1dd71d8507dc71d617e MD5 | raw file
  1. // Npgsql.NpgsqlCommand.cs
  2. //
  3. // Author:
  4. // Josh Cooley <jbnpgsql@tuxinthebox.net>
  5. //
  6. // Copyright (C) 2002-2005 The Npgsql Development Team
  7. // npgsql-general@gborg.postgresql.org
  8. // http://gborg.postgresql.org/project/npgsql/projdisplay.php
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2.1 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the Free Software
  22. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. using System;
  24. using System.Data;
  25. using System.Text;
  26. namespace Npgsql
  27. {
  28. /// <summary>
  29. /// Provides the underlying mechanism for reading schema information.
  30. /// </summary>
  31. internal sealed class NpgsqlSchema
  32. {
  33. private NpgsqlConnection _connection;
  34. /// <summary>
  35. /// Creates an NpgsqlSchema that can read schema information from the database.
  36. /// </summary>
  37. /// <param name="connection">An open database connection for reading metadata.</param>
  38. internal NpgsqlSchema(NpgsqlConnection connection)
  39. {
  40. _connection = connection;
  41. }
  42. /// <summary>
  43. /// Returns the MetaDataCollections that lists all possible collections.
  44. /// </summary>
  45. /// <returns>The MetaDataCollections</returns>
  46. internal static DataTable GetMetaDataCollections()
  47. {
  48. DataTable metaDataCollections = new DataTable("MetaDataCollections");
  49. metaDataCollections.Columns.AddRange(new DataColumn[] {
  50. new DataColumn("CollectionName"),
  51. new DataColumn("NumberOfRestrictions", typeof(int)),
  52. new DataColumn("NumberOfIdentifierParts", typeof(int)) });
  53. // Add(object[] { CollectionName, NumberOfRestrictions, NumberOfIdentifierParts })
  54. metaDataCollections.Rows.Add(new object[]{"MetaDataCollections",0,0});
  55. metaDataCollections.Rows.Add(new object[]{"Restrictions",0,0});
  56. metaDataCollections.Rows.Add(new object[]{"Databases",1,1});
  57. metaDataCollections.Rows.Add(new object[]{"Tables",4,3});
  58. metaDataCollections.Rows.Add(new object[]{"Columns",4,4});
  59. metaDataCollections.Rows.Add(new object[]{"Views",3,3});
  60. metaDataCollections.Rows.Add(new object[]{"Users",1,1});
  61. return metaDataCollections;
  62. }
  63. /// <summary>
  64. /// Returns the Restrictions that contains the meaning and position of the values in the restrictions array.
  65. /// </summary>
  66. /// <returns>The Restrictions</returns>
  67. internal static DataTable GetRestrictions()
  68. {
  69. DataTable restrictions = new DataTable("Restrictions");
  70. restrictions.Columns.AddRange(new DataColumn[] {
  71. new DataColumn("CollectionName"),
  72. new DataColumn("RestrictionName"),
  73. new DataColumn("RestrictionDefault"),
  74. new DataColumn("RestrictionNumber", typeof(int)) });
  75. restrictions.Rows.Add(new object[]{"Databases","Name","Name",1});
  76. restrictions.Rows.Add(new object[]{"Tables","Catalog","table_catalog",1});
  77. restrictions.Rows.Add(new object[]{"Tables","Schema","table_schema",2});
  78. restrictions.Rows.Add(new object[]{"Tables","Table","table_name",3});
  79. restrictions.Rows.Add(new object[]{"Tables","TableType","table_type",4});
  80. restrictions.Rows.Add(new object[]{"Columns","Catalog","table_catalog",1});
  81. restrictions.Rows.Add(new object[]{"Columns","Schema","table_schema",2});
  82. restrictions.Rows.Add(new object[]{"Columns","Table","table_name",3});
  83. restrictions.Rows.Add(new object[]{"Columns","Column","column_name",4});
  84. restrictions.Rows.Add(new object[]{"Views","Catalog","table_catalog",1});
  85. restrictions.Rows.Add(new object[]{"Views","Schema","table_schema",2});
  86. restrictions.Rows.Add(new object[]{"Views","Table","table_name",3});
  87. return restrictions;
  88. }
  89. private NpgsqlCommand BuildCommand(StringBuilder query, string[] restrictions, params string[] names)
  90. {
  91. NpgsqlCommand command = new NpgsqlCommand();
  92. if (restrictions != null && names != null)
  93. {
  94. bool addWhere = true;
  95. for(int i=0; i<restrictions.Length && i<names.Length; ++i)
  96. {
  97. if (restrictions[i] != null && restrictions[i].Length != 0)
  98. {
  99. if (addWhere)
  100. {
  101. query.Append(" WHERE ");
  102. addWhere = false;
  103. }
  104. else
  105. {
  106. query.Append(" AND ");
  107. }
  108. query.AppendFormat("{0} = :{0}", names[i]);
  109. command.Parameters.Add(new NpgsqlParameter(names[i], restrictions[i]));
  110. }
  111. }
  112. }
  113. command.CommandText = query.ToString();
  114. command.Connection = _connection;
  115. return command;
  116. }
  117. /// <summary>
  118. /// Returns the Databases that contains a list of all accessable databases.
  119. /// </summary>
  120. /// <param name="restrictions">The restrictions to filter the collection.</param>
  121. /// <returns>The Databases</returns>
  122. internal DataTable GetDatabases(string[] restrictions)
  123. {
  124. DataTable databases = new DataTable("Databases");
  125. databases.Columns.AddRange(new DataColumn[] {
  126. new DataColumn("database_name"),
  127. new DataColumn("owner"),
  128. new DataColumn("encoding") });
  129. StringBuilder getDatabases = new StringBuilder();
  130. getDatabases.Append("SELECT d.datname AS database_name, u.usename AS owner, pg_catalog.pg_encoding_to_char(d.encoding) AS encoding FROM pg_catalog.pg_database d LEFT JOIN pg_catalog.pg_user u ON d.datdba = u.usesysid");
  131. using (NpgsqlCommand command = BuildCommand(getDatabases, restrictions, "datname"))
  132. using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command))
  133. {
  134. adapter.Fill(databases);
  135. }
  136. return databases;
  137. }
  138. /// <summary>
  139. /// Returns the Tables that contains table and view names and the database and schema they come from.
  140. /// </summary>
  141. /// <param name="restrictions">The restrictions to filter the collection.</param>
  142. /// <returns>The Tables</returns>
  143. internal DataTable GetTables(string[] restrictions)
  144. {
  145. DataTable tables = new DataTable("Tables");
  146. tables.Columns.AddRange(new DataColumn[] {
  147. new DataColumn("table_catalog"),
  148. new DataColumn("table_schema"),
  149. new DataColumn("table_name"),
  150. new DataColumn("table_type") });
  151. StringBuilder getTables = new StringBuilder();
  152. getTables.Append("SELECT table_catalog, table_schema, table_name, table_type FROM information_schema.tables");
  153. using (NpgsqlCommand command = BuildCommand(getTables, restrictions, "table_catalog", "table_schema", "table_name", "table_type"))
  154. using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command))
  155. {
  156. adapter.Fill(tables);
  157. }
  158. return tables;
  159. }
  160. /// <summary>
  161. /// Returns the Columns that contains information about columns in tables.
  162. /// </summary>
  163. /// <param name="restrictions">The restrictions to filter the collection.</param>
  164. /// <returns>The Columns.</returns>
  165. internal DataTable GetColumns(string[] restrictions)
  166. {
  167. DataTable columns = new DataTable("Columns");
  168. columns.Columns.AddRange(new DataColumn[] {
  169. new DataColumn("table_catalog"),
  170. new DataColumn("table_schema"),
  171. new DataColumn("table_name"),
  172. new DataColumn("column_name"),
  173. new DataColumn("ordinal_position", typeof(int)),
  174. new DataColumn("column_default"),
  175. new DataColumn("is_nullable"),
  176. new DataColumn("data_type"),
  177. new DataColumn("character_maximum_length", typeof(int)),
  178. new DataColumn("character_octet_length", typeof(int)),
  179. new DataColumn("numeric_precision", typeof(int)),
  180. new DataColumn("numeric_precision_radix", typeof(int)),
  181. new DataColumn("numeric_scale", typeof(int)),
  182. new DataColumn("datetime_precision", typeof(int)),
  183. new DataColumn("character_set_catalog"),
  184. new DataColumn("character_set_schema"),
  185. new DataColumn("character_set_name"),
  186. new DataColumn("collation_catalog") });
  187. StringBuilder getColumns = new StringBuilder();
  188. getColumns.Append("SELECT table_catalog, table_schema, table_name, column_name, ordinal_position, column_default, is_nullable, udt_name AS data_type, character_maximum_length, character_octet_length, numeric_precision, numeric_precision_radix, numeric_scale, datetime_precision, character_set_catalog, character_set_schema, character_set_name, collation_catalog FROM information_schema.columns");
  189. using (NpgsqlCommand command = BuildCommand(getColumns, restrictions, "table_catalog", "table_schema", "table_name", "column_name"))
  190. using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command))
  191. {
  192. adapter.Fill(columns);
  193. }
  194. return columns;
  195. }
  196. /// <summary>
  197. /// Returns the Views that contains view names and the database and schema they come from.
  198. /// </summary>
  199. /// <param name="restrictions">The restrictions to filter the collection.</param>
  200. /// <returns>The Views</returns>
  201. internal DataTable GetViews(string[] restrictions)
  202. {
  203. DataTable views = new DataTable("Views");
  204. views.Columns.AddRange(new DataColumn[] {
  205. new DataColumn("table_catalog"),
  206. new DataColumn("table_schema"),
  207. new DataColumn("table_name"),
  208. new DataColumn("check_option"),
  209. new DataColumn("is_updatable") });
  210. StringBuilder getViews = new StringBuilder();
  211. getViews.Append("SELECT table_catalog, table_schema, table_name, check_option, is_updatable FROM information_schema.views");
  212. using (NpgsqlCommand command = BuildCommand(getViews, restrictions, "table_catalog", "table_schema", "table_name"))
  213. using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command))
  214. {
  215. adapter.Fill(views);
  216. }
  217. return views;
  218. }
  219. /// <summary>
  220. /// Returns the Users containing user names and the sysid of those users.
  221. /// </summary>
  222. /// <param name="restrictions">The restrictions to filter the collection.</param>
  223. /// <returns>The Users.</returns>
  224. internal DataTable GetUsers(string[] restrictions)
  225. {
  226. DataTable users = new DataTable("Users");
  227. users.Columns.AddRange(new DataColumn[] {
  228. new DataColumn("user_name"),
  229. new DataColumn("user_sysid", typeof(int)) });
  230. StringBuilder getUsers = new StringBuilder();
  231. getUsers.Append("SELECT usename as user_name, usesysid as user_sysid FROM pg_catalog.pg_user");
  232. using (NpgsqlCommand command = BuildCommand(getUsers, restrictions, "usename"))
  233. using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command))
  234. {
  235. adapter.Fill(users);
  236. }
  237. return users;
  238. }
  239. }
  240. }