PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Gettext.CsUtils/Tools/Gettext.DatabaseResourceGenerator/DatabaseInterface.cs

https://code.google.com/p/gettext-cs-utils/
C# | 275 lines | 213 code | 42 blank | 20 comment | 7 complexity | d16ff2f6adb19dd7efa4c3e04510d0e2 MD5 | raw file
  1. /**
  2. * gettext-cs-utils
  3. *
  4. * Copyright 2011 Manas Technology Solutions
  5. * http://www.manas.com.ar/
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. **/
  21. ???using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Data.SqlClient;
  26. namespace Gettext.DatabaseResourceGenerator
  27. {
  28. class DatabaseInterface : IDisposable
  29. {
  30. string connString;
  31. SqlConnection conn;
  32. SqlTransaction trans;
  33. public string KeyField { get; set; }
  34. public string ValueField { get; set; }
  35. public string CultureField { get; set; }
  36. public string KeyParam { get { return "@" + KeyField; } }
  37. public string ValueParam { get { return "@" + ValueField; } }
  38. public string CultureParam { get { return "@" + CultureField; } }
  39. public string TableName { get; set; }
  40. public string InsertSP { get; set; }
  41. public string DeleteSP { get; set; }
  42. public string GetSP { get; set; }
  43. public DatabaseInterface(string connString)
  44. {
  45. this.connString = connString;
  46. this.conn = new SqlConnection(connString);
  47. }
  48. public DatabaseInterface Init()
  49. {
  50. this.conn.Open();
  51. this.trans = conn.BeginTransaction();
  52. return this;
  53. }
  54. public void Commit()
  55. {
  56. this.trans.Commit();
  57. }
  58. public void InsertResource(string culture, string key, string value)
  59. {
  60. try
  61. {
  62. var command = new SqlCommand() { CommandText = InsertSP, CommandType = System.Data.CommandType.StoredProcedure, Connection = conn, Transaction = trans };
  63. command.Parameters.AddWithValue(CultureParam, culture);
  64. command.Parameters.AddWithValue(KeyParam, key);
  65. command.Parameters.AddWithValue(ValueParam, value);
  66. command.ExecuteNonQuery();
  67. }
  68. catch (Exception ex)
  69. {
  70. Console.Error.WriteLine("Error inserting resource for culture {0} key '{1}' value '{2}': {3}", culture, key, value, ex.Message);
  71. throw;
  72. }
  73. }
  74. public void DeleteResourceSet(string culture)
  75. {
  76. try
  77. {
  78. var command = new SqlCommand() { CommandText = DeleteSP, CommandType = System.Data.CommandType.StoredProcedure, Connection = conn, Transaction = trans };
  79. command.Parameters.AddWithValue(CultureParam, culture);
  80. command.ExecuteNonQuery();
  81. }
  82. catch (Exception ex)
  83. {
  84. Console.Error.WriteLine("Error deleting resource set for culture {0}: {1}", culture, ex.Message);
  85. throw;
  86. }
  87. }
  88. public void Prepare()
  89. {
  90. CheckI18NTable();
  91. if (!ExistsSP(GetSP)) CreateGetSP();
  92. if (!ExistsSP(InsertSP)) CreateInsertSP();
  93. if (!ExistsSP(DeleteSP)) CreateDeleteSP();
  94. }
  95. private void CreateDeleteSP()
  96. {
  97. Console.WriteLine(string.Format("Delete string resources stored procedure named {0} does not exist. Creating for table {1}...", DeleteSP, TableName));
  98. string cmd = String.Empty;
  99. try
  100. {
  101. cmd = String.Format(@"
  102. CREATE PROCEDURE [dbo].[{0}]
  103. @{2} VARCHAR(5)
  104. AS
  105. BEGIN
  106. DELETE FROM {1}
  107. WHERE {2} = @{2}
  108. END", DeleteSP, TableName, CultureField);
  109. var command = conn.CreateCommand();
  110. command.CommandText = cmd;
  111. command.Transaction = trans;
  112. command.ExecuteScalar();
  113. Console.WriteLine("Created {0} stored procedure with parameter @{1} on table {2}.", DeleteSP, CultureField, TableName);
  114. }
  115. catch (Exception ex)
  116. {
  117. Console.Error.WriteLine("Error creating delete stored procedure: {0}\n\nCommand: {1}", ex.Message, cmd);
  118. throw;
  119. }
  120. }
  121. private void CreateGetSP()
  122. {
  123. Console.WriteLine(string.Format("Get resource set stored procedure named {0} does not exist. Creating for table {1}...", GetSP, TableName));
  124. string cmd = String.Empty;
  125. try
  126. {
  127. cmd = String.Format(@"
  128. CREATE PROCEDURE [dbo].[{0}]
  129. @{2} VARCHAR(5)
  130. AS
  131. BEGIN
  132. SELECT [{3}], [{4}] FROM [{1}]
  133. WHERE [{2}] = @{2}
  134. END", GetSP, TableName, CultureField, KeyField, ValueField);
  135. var command = conn.CreateCommand();
  136. command.CommandText = cmd;
  137. command.Transaction = trans;
  138. command.ExecuteScalar();
  139. Console.WriteLine("Created {0} stored procedure with parameter @{1} on table {2}.", GetSP, CultureField, TableName);
  140. }
  141. catch (Exception ex)
  142. {
  143. Console.Error.WriteLine("Error creating get stored procedure: {0}\n\nCommand: {1}", ex.Message, cmd);
  144. throw;
  145. }
  146. }
  147. private void CreateInsertSP()
  148. {
  149. Console.WriteLine(string.Format("Insert string resource stored procedure named {0} does not exist. Creating for table {1}...", InsertSP, TableName));
  150. string cmd = String.Empty;
  151. try
  152. {
  153. cmd = String.Format(@"
  154. CREATE PROCEDURE [dbo].[{0}]
  155. @{2} VARCHAR(5),
  156. @{3} VARCHAR(4000),
  157. @{4} VARCHAR(4000)
  158. AS
  159. BEGIN
  160. INSERT INTO [{1}] ([{2}], [{3}], [{4}])
  161. VALUES (@{2}, @{3}, @{4})
  162. END", InsertSP, TableName, CultureField, KeyField, ValueField);
  163. var command = conn.CreateCommand();
  164. command.CommandText = cmd;
  165. command.Transaction = trans;
  166. command.ExecuteScalar();
  167. Console.WriteLine("Created {0} stored procedure with parameters @{1}, @{2}, @{3} on table {4}.", InsertSP, CultureField, KeyField, ValueField, TableName);
  168. }
  169. catch (Exception ex)
  170. {
  171. Console.Error.WriteLine("Error creating insert stored procedure: {0}\n\nCommand: {1}", ex.Message, cmd);
  172. throw;
  173. }
  174. }
  175. private void CheckI18NTable()
  176. {
  177. Console.WriteLine("Checking if the given table {0} exists...", TableName);
  178. var command = conn.CreateCommand();
  179. command.CommandText = "select case when exists((select * from information_schema.tables where table_name = '" + TableName + "')) then 1 else 0 end";
  180. command.Transaction = trans;
  181. bool existsTable = (int)command.ExecuteScalar() == 1;
  182. if (!existsTable)
  183. {
  184. try
  185. {
  186. Console.WriteLine(string.Format("Table {0} does not exist. Creating table with fields {1} {2} {3}...", TableName, CultureField, KeyField, ValueField));
  187. command.CommandText = string.Format("CREATE TABLE [dbo].[{0}] ([{1}] varchar(5) NOT NULL, [{2}] varchar(4000) NOT NULL, [{3}] varchar(4000))", TableName, CultureField, KeyField, ValueField);
  188. command.ExecuteNonQuery();
  189. }
  190. catch (Exception ex)
  191. {
  192. Console.Error.WriteLine("Error creating table: {0}\n\nCommand: {1}", ex.Message, command.CommandText);
  193. throw;
  194. }
  195. try
  196. {
  197. command.CommandText = string.Format("ALTER TABLE [{0}] ADD CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED ([{1}], [{2}])", TableName, CultureField, KeyField);
  198. command.ExecuteNonQuery();
  199. }
  200. catch (Exception ex)
  201. {
  202. Console.Error.WriteLine("Error creating table primary key: {0}\n\nCommand: {1}", ex.Message, command.CommandText);
  203. throw;
  204. }
  205. }
  206. }
  207. private bool ExistsSP(string sp)
  208. {
  209. Console.WriteLine("Checking if stored procedure {0} exists...", sp);
  210. try
  211. {
  212. string cmd = String.Format("SELECT COUNT(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{0}]') AND type in (N'P', N'PC')", sp);
  213. var command = new SqlCommand(cmd, conn, trans);
  214. var count = (int)command.ExecuteScalar();
  215. return count > 0;
  216. }
  217. catch (Exception ex)
  218. {
  219. Console.Error.WriteLine("Error checking SP {0}: {1}", sp, ex.Message);
  220. throw;
  221. }
  222. }
  223. #region IDisposable Members
  224. public void Dispose()
  225. {
  226. if (this.conn != null)
  227. {
  228. this.conn.Dispose();
  229. }
  230. }
  231. #endregion
  232. }
  233. }