PageRenderTime 56ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Program.cs

https://bitbucket.org/lokkju/sql-cli
C# | 339 lines | 302 code | 9 blank | 28 comment | 20 complexity | d7df46ad1c00b92c4520172d77497934 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * SQL_CLI - A simple command line client for processing SQL files, against a given DSN
  3. * Copyright (C) 2005 Lokkju, Inc
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * @version 1.0
  20. * @author Lokkju
  21. *
  22. */
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Text;
  26. using System.Data.Odbc;
  27. using System.Data.OleDb;
  28. using System.IO;
  29. namespace ODBC_CLI
  30. {
  31. class Program
  32. {
  33. static void Main(string[] args)
  34. {
  35. string dbc = "";
  36. string fname = "";
  37. string ctype = "";
  38. if (args.Length < 3)
  39. {
  40. usage();
  41. }
  42. else
  43. {
  44. ctype = args[0];
  45. dbc = args[1];
  46. fname = args[2];
  47. switch (ctype.ToLower())
  48. {
  49. case "ole":
  50. do_cli_ole(dbc, fname);
  51. break;
  52. case "odbc":
  53. do_cli_odbc(dbc, fname);
  54. break;
  55. }
  56. }
  57. }
  58. static void do_cli_ole(string strDbc, string strFname)
  59. {
  60. File.Delete(strFname + ".err");
  61. StreamWriter errlog = File.CreateText(strFname + ".err");
  62. if (!file_exists(strFname))
  63. {
  64. System.Console.WriteLine("Fatal Error: The specified sql command file does not exist.");
  65. return;
  66. }
  67. if (!dbc_exists_ole(strDbc))
  68. {
  69. System.Console.WriteLine("Fatal Error: The specified Connection String is invalid.");
  70. return;
  71. }
  72. OleDbConnection dbc = new OleDbConnection(strDbc);
  73. StreamReader sqlf = File.OpenText(strFname);
  74. try
  75. {
  76. dbc.Open();
  77. OleDbCommand cmd;
  78. int i = 0;
  79. TimeSpan tmr = new TimeSpan();
  80. Int64 tic = DateTime.Now.Ticks;
  81. OleDbTransaction tr = dbc.BeginTransaction();
  82. int n = 1000;
  83. while (sqlf.Peek() >= 0)
  84. {
  85. //if (i < 39100) { sqlf.ReadLine(); i++; continue; }
  86. if ((i > n))
  87. {
  88. n += 1000;
  89. tr.Commit();
  90. tr.Dispose();
  91. dbc.Close();
  92. dbc.Open();
  93. tr = dbc.BeginTransaction();
  94. tmr = new TimeSpan(DateTime.Now.Ticks - tic);
  95. Console.Write("\r\n");
  96. Console.WriteLine("i:" + i + "\t\tTime:" + tmr.TotalSeconds);
  97. }
  98. string cmdtxt = "";
  99. string ln = sqlf.ReadLine();
  100. cmdtxt = ln;
  101. if (ln.StartsWith("--")) { Console.Write("\r\nLine " + i + ":\tSkipping (--)\r\n"); i++; continue; }
  102. else if (ln.Trim().Length < 5) { Console.Write("\r\nLine " + i + ":\tSkipping (<5)\r\n"); i++; continue; }
  103. else if (ln.StartsWith("SET")) { Console.Write("\r\nLine " + i + ":\tSkipping (SET)\r\n"); i++; continue; }
  104. else if (ln.StartsWith("GO")) { Console.Write("\r\nLine " + i + ":\tSkipping (GO)\r\n"); i++; continue; }
  105. //else if (!ln.StartsWith("INSERT")) { Console.Write("Line " + i + ":\tInvalid SQL Statement\r\n"); i++; continue; }
  106. //else { cmdtxt += ln; }
  107. cmd = dbc.CreateCommand();
  108. try
  109. {
  110. cmd.CommandText = ln;
  111. cmd.CommandTimeout = 3;
  112. cmd.Transaction = tr;
  113. cmd.ExecuteNonQuery();
  114. cmd.Dispose();
  115. Console.Write(".");
  116. }
  117. catch (OleDbException oex)
  118. {
  119. cmd.Dispose();
  120. errlog.Write("Error:\tLine Number " + i + " (this transaction lost!)\r\n" + cmdtxt + "\r\n" + oex.ToString() + "\r\n\r\n");
  121. Console.Write("\r\nLine " + i + ":\tOLE Error (this transaction lost!)\r\n");
  122. errlog.Flush();
  123. dbc.Close();
  124. dbc.Open();
  125. tr = dbc.BeginTransaction();
  126. }
  127. catch (System.AccessViolationException ave)
  128. {
  129. cmd.Dispose();
  130. errlog.Write("Error:\tLine Number " + i + "\r\n" + cmdtxt + "\r\n" + ave.ToString() + "\r\n\r\n");
  131. Console.Write("\r\nLine " + i + ":\t Unknown Error\r\n");
  132. errlog.Flush();
  133. dbc.Close();
  134. dbc.Open();
  135. tr = dbc.BeginTransaction();
  136. }
  137. catch (Exception ex)
  138. {
  139. cmd.Dispose();
  140. errlog.Write("Error:\tLine Number " + i + "\r\n" + cmdtxt + "\r\n" + ex.ToString() + "\r\n\r\n");
  141. Console.Write("\r\nLine " + i + ":\t Unknown Error\r\n");
  142. errlog.Flush();
  143. dbc.Close();
  144. dbc.Open();
  145. tr = dbc.BeginTransaction();
  146. }
  147. finally
  148. {
  149. i++;
  150. }
  151. }
  152. tr.Commit();
  153. }
  154. catch (OleDbException oex)
  155. {
  156. Console.WriteLine(oex.ToString());
  157. }
  158. catch (AccessViolationException ave)
  159. {
  160. Console.WriteLine(ave.ToString());
  161. }
  162. catch (Exception oex)
  163. {
  164. Console.WriteLine(oex.ToString());
  165. }
  166. finally
  167. {
  168. dbc.Close();
  169. sqlf.Close();
  170. errlog.Flush();
  171. errlog.Close();
  172. }
  173. }
  174. static void do_cli_odbc(string strDbc,string strFname) {
  175. File.Delete(strFname + ".err");
  176. StreamWriter errlog = File.CreateText(strFname + ".err");
  177. if (!file_exists(strFname)) {
  178. System.Console.WriteLine("Fatal Error: The specified sql command file does not exist.");
  179. return;
  180. }
  181. if (!dbc_exists(strDbc)) {
  182. System.Console.WriteLine("Fatal Error: The specified DSN does not exist.");
  183. return;
  184. }
  185. OdbcConnection dbc = new OdbcConnection(strDbc);
  186. StreamReader sqlf = File.OpenText(strFname);
  187. try
  188. {
  189. dbc.ConnectionTimeout = 3;
  190. dbc.Open();
  191. OdbcCommand cmd;
  192. int i = 0;
  193. TimeSpan tmr = new TimeSpan();
  194. Int64 tic = DateTime.Now.Ticks;
  195. OdbcTransaction tr = dbc.BeginTransaction();
  196. int n = 1000;
  197. while (sqlf.Peek() >= 0)
  198. {
  199. //if (i < 39100) { sqlf.ReadLine(); i++; continue; }
  200. if ((i > n))
  201. {
  202. n += 1000;
  203. tr.Commit();
  204. tr.Dispose();
  205. dbc.Close();
  206. dbc.Open();
  207. tr = dbc.BeginTransaction();
  208. tmr = new TimeSpan(DateTime.Now.Ticks - tic);
  209. Console.Write("\r\n");
  210. Console.WriteLine("i:" + i + "\t\tTime:" + tmr.TotalSeconds);
  211. }
  212. string cmdtxt = "";
  213. string ln = sqlf.ReadLine();
  214. if (ln.StartsWith("--")) { Console.Write("\r\nLine " + i + ":\tInvalid SQL Statement (--)\r\n"); i++; continue; }
  215. else if (ln.Length < 5) { Console.Write("\r\nLine " + i + ":\tInvalid SQL Statement (<5)\r\n"); i++; continue; }
  216. //else if (!ln.StartsWith("INSERT")) { Console.Write("Line " + i + ":\tInvalid SQL Statement\r\n"); i++; continue; }
  217. //else { cmdtxt += ln; }
  218. cmd = dbc.CreateCommand();
  219. try
  220. {
  221. cmd.CommandText = ln;
  222. cmd.CommandTimeout = 3;
  223. cmd.Transaction = tr;
  224. cmd.ExecuteNonQuery();
  225. cmd.Dispose();
  226. Console.Write(".");
  227. }
  228. catch (OdbcException oex)
  229. {
  230. cmd.Dispose();
  231. errlog.Write("Error:\tLine Number " + i + "\r\n" + cmdtxt + "\r\n" + oex.ToString() + "\r\n\r\n");
  232. Console.Write("\r\nLine " + i + ":\tODBC Error\r\n");
  233. errlog.Flush();
  234. dbc.Close();
  235. dbc.Open();
  236. tr = dbc.BeginTransaction();
  237. }
  238. catch (System.AccessViolationException ave)
  239. {
  240. cmd.Dispose();
  241. errlog.Write("Error:\tLine Number " + i + "\r\n" + cmdtxt + "\r\n" + ave.ToString() + "\r\n\r\n");
  242. Console.Write("\r\nLine " + i + ":\t Unknown Error\r\n");
  243. errlog.Flush();
  244. dbc.Close();
  245. dbc.Open();
  246. tr = dbc.BeginTransaction();
  247. }
  248. catch (Exception ex)
  249. {
  250. cmd.Dispose();
  251. errlog.Write("Error:\tLine Number " + i + "\r\n" + cmdtxt + "\r\n" + ex.ToString() + "\r\n\r\n");
  252. Console.Write("\r\nLine " + i + ":\t Unknown Error\r\n");
  253. errlog.Flush();
  254. dbc.Close();
  255. dbc.Open();
  256. tr = dbc.BeginTransaction();
  257. }
  258. finally
  259. {
  260. i++;
  261. }
  262. }
  263. tr.Commit();
  264. }
  265. catch (OdbcException oex)
  266. {
  267. Console.WriteLine(oex.ToString());
  268. }
  269. catch (AccessViolationException ave)
  270. {
  271. Console.WriteLine(ave.ToString());
  272. }
  273. catch (Exception oex)
  274. {
  275. Console.WriteLine(oex.ToString());
  276. }
  277. finally
  278. {
  279. dbc.Close();
  280. sqlf.Close();
  281. errlog.Flush();
  282. errlog.Close();
  283. }
  284. }
  285. static void usage()
  286. {
  287. System.Console.WriteLine("Usage: odbc_cli [odbc|ole] [DSN] [filename]");
  288. }
  289. static bool file_exists(string fname)
  290. {
  291. return System.IO.File.Exists(fname);
  292. }
  293. static bool dbc_exists(string dbc)
  294. {
  295. System.Data.Odbc.OdbcConnection ddbc = new OdbcConnection(dbc);
  296. try
  297. {
  298. ddbc.Open();
  299. }
  300. catch (OdbcException oex)
  301. {
  302. System.Console.WriteLine(oex.ToString());
  303. return false;
  304. }
  305. finally
  306. {
  307. ddbc.Close();
  308. }
  309. return true;
  310. }
  311. static bool dbc_exists_ole(string dbc)
  312. {
  313. System.Data.OleDb.OleDbConnection ddbc = new OleDbConnection(dbc);
  314. try
  315. {
  316. ddbc.Open();
  317. }
  318. catch (OleDbException oex)
  319. {
  320. System.Console.WriteLine(oex.ToString());
  321. return false;
  322. }
  323. finally
  324. {
  325. ddbc.Close();
  326. }
  327. return true;
  328. }
  329. }
  330. }