SqlEditorSharp.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. //
  2. // SqlEditor.cs - writen in C# using GTK#
  3. //
  4. // Authors:
  5. // Daniel Morgan <[email protected]>
  6. // Rodrigo Moya <[email protected]>
  7. //
  8. // (c)copyright 2002 Daniel Morgan
  9. // (c)copyright 2002 Rodrigo Moya
  10. //
  11. // SqlEditorSharp is based on the gnome-db-sql-editor.c in libgnomedb.
  12. // SqlEditorSharp falls under the GPL license and is included
  13. // in SQL# For GTK#. SQL# For GTK# is a database query tool for Mono.
  14. //
  15. namespace SqlEditorSharp
  16. {
  17. using System;
  18. using Gtk;
  19. using Gdk;
  20. using GLib;
  21. using System.Collections;
  22. using System.IO;
  23. using System.Text;
  24. using System.Runtime.InteropServices;
  25. using System.Diagnostics;
  26. using Mono.Data.SqlSharp.Gui.GtkSharp;
  27. /// <summary> SqlEditor Class</summary>
  28. /// <remarks>
  29. /// </remarks>
  30. public class SqlEditorSharp : Gtk.VBox
  31. {
  32. // Fields
  33. // text tags for TextTagTable in TextBuffer
  34. private TextTag freecomment_tag;
  35. private TextTag linecomment_tag;
  36. private TextTag singlequotedconstant_tag;
  37. private TextTag sql_tag;
  38. private TextTag normaltext_tag;
  39. // determine if something has changed beyond a line
  40. // updating one line is faster than the whole buffer
  41. //private int line_last_changed;
  42. //private int last_freecomment_count;
  43. // settings
  44. private bool use_hi_lighting;
  45. private string family;
  46. // widgets
  47. private ScrolledWindow scroll;
  48. private TextView sqlTextView;
  49. private TextBuffer sqlTextBuffer;
  50. private EditorTab tab = null;
  51. // Constructors
  52. public SqlEditorSharp() : base(false, 4) {
  53. scroll = new ScrolledWindow (
  54. new Adjustment (0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
  55. new Adjustment (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
  56. scroll.HscrollbarPolicy = Gtk.PolicyType.Automatic;
  57. scroll.VscrollbarPolicy = Gtk.PolicyType.Automatic;
  58. scroll.ShadowType = Gtk.ShadowType.In;
  59. this.PackStart (scroll, true, true, 0);
  60. // default font famly for SQL editor
  61. family = "courier";
  62. // other default settings
  63. use_hi_lighting = false;
  64. // create text tag table
  65. TextTagTable textTagTable = new TextTagTable ();
  66. // anything else is normaltext
  67. normaltext_tag = new TextTag ("normaltext");
  68. normaltext_tag.Family = family;
  69. normaltext_tag.Foreground = "black";
  70. normaltext_tag.Style = Pango.Style.Normal;
  71. textTagTable.Add (normaltext_tag);
  72. // SQL Keywords - SELECT FROM WHERE, etc
  73. sql_tag = new TextTag ("sql");
  74. sql_tag.Family = family;
  75. sql_tag.Foreground = "blue";
  76. sql_tag.Style = Pango.Style.Normal;
  77. textTagTable.Add (sql_tag);
  78. // c like free comment - used within a SQL statement
  79. freecomment_tag = new TextTag ("freecomment");
  80. freecomment_tag.Family = family;
  81. freecomment_tag.Foreground = "darkgreen";
  82. freecomment_tag.Style = Pango.Style.Italic;
  83. textTagTable.Add (freecomment_tag);
  84. // c++ like line comment, but using two hyphens
  85. linecomment_tag = new TextTag ("linecomment");
  86. linecomment_tag.Family = family;
  87. linecomment_tag.Foreground = "darkgreen";
  88. linecomment_tag.Style = Pango.Style.Italic;
  89. textTagTable.Add (linecomment_tag);
  90. /* single quoted constant - WHERE COL1 = 'ABC' */
  91. singlequotedconstant_tag = new TextTag ("singlequotedconstant");
  92. singlequotedconstant_tag.Family = family;
  93. singlequotedconstant_tag.Foreground = "red";
  94. singlequotedconstant_tag.Style = Pango.Style.Normal;
  95. textTagTable.Add (singlequotedconstant_tag);
  96. // create TextBuffer and TextView
  97. sqlTextBuffer = new TextBuffer (textTagTable);
  98. sqlTextView = new TextView (sqlTextBuffer);
  99. // allow it to be edited
  100. sqlTextView.Editable = true;
  101. //line_last_changed = -1;
  102. //last_freecomment_count = -1;
  103. // attach OnTextChanged callback function
  104. // to "changed" signal so we can do something
  105. // when the text has changed in the buffer
  106. sqlTextBuffer.Changed += new EventHandler (OnTextChanged);
  107. // add the TextView to the ScrolledWindow
  108. scroll.Add (sqlTextView);
  109. }
  110. // Public Properties
  111. public TextBuffer Buffer {
  112. get {
  113. return sqlTextBuffer;
  114. }
  115. }
  116. public TextView View {
  117. get {
  118. return sqlTextView;
  119. }
  120. }
  121. public EditorTab Tab {
  122. get {
  123. return tab;
  124. }
  125. set {
  126. tab = value;
  127. }
  128. }
  129. public bool UseSyntaxHiLighting {
  130. get {
  131. return use_hi_lighting;
  132. }
  133. set {
  134. use_hi_lighting = value;
  135. }
  136. }
  137. // Private Methods
  138. void OnTextChanged (object o, EventArgs args)
  139. {
  140. if(tab != null)
  141. tab.label.Text = tab.basefilename + " *";
  142. SqlSharpGtk.DebugWriteLine ("[[[[[ Syntax Hi-Light Text BEGIN ]]]]]");
  143. if (use_hi_lighting == true) {
  144. SyntaxHiLightText ();
  145. }
  146. SqlSharpGtk.DebugWriteLine ("[[[[[ Syntax Hi-Light Text END ]]]]]\n");
  147. }
  148. void SyntaxHiLightText ()
  149. {
  150. TextIter start_iter, end_iter,
  151. iter, insert_iter;
  152. TextIter match_start1, match_end1,
  153. match_start2, match_end2;
  154. int char_count = 0;
  155. int hyphen = 0, single_quotes = 0;
  156. string text = String.Empty;
  157. int i = 0, start_con = 0, end_con = 0;
  158. //int line = 0;
  159. //int freecomment_count = 0;
  160. int start_word = -1;
  161. TextMark insert_mark;
  162. char ch = ' ';
  163. insert_mark = sqlTextBuffer.InsertMark;
  164. sqlTextBuffer.GetIterAtMark (out insert_iter, insert_mark);
  165. //line = insert_iter.Line;
  166. /* get the starting and ending text iterators */
  167. sqlTextBuffer.GetIterAtOffset (out start_iter, 0);
  168. char_count = sqlTextBuffer.CharCount;
  169. sqlTextBuffer.GetIterAtOffset (out end_iter, char_count);
  170. SqlSharpGtk.DebugWriteLine ("char_count: " + char_count);
  171. /* since line is not same - redo all */
  172. //if (line != line_last_changed) {
  173. /* remove all previously applied tags */
  174. sqlTextBuffer.RemoveAllTags (start_iter, end_iter);
  175. /* apply the entire buffer to the normaltext tag */
  176. sqlTextBuffer.ApplyTag (normaltext_tag, start_iter, end_iter);
  177. //}
  178. //else { /* just worry about current insertion line */
  179. // /* get start iter */
  180. // if (insert_iter.StartsLine () == true) {
  181. // start_iter = insert_iter;
  182. // }
  183. // else {
  184. // start_iter = insert_iter;
  185. // start_iter.LineOffset = 0;
  186. // }
  187. // /* get end iter */
  188. // end_iter.ForwardToLineEnd ();
  189. // char_count = start_iter.CharsInLine;
  190. //
  191. // /* remove all previously applied tags */
  192. // sqlTextBuffer.RemoveAllTags (start_iter, end_iter);
  193. //
  194. // /* apply the entire buffer to the normaltext tag */
  195. // sqlTextBuffer.ApplyTag (normaltext_tag,
  196. // start_iter, end_iter);
  197. //
  198. // /* get the starting and ending text iterators */
  199. // sqlTextBuffer.GetIterAtOffset (out start_iter, 0);
  200. // char_count = sqlTextBuffer.CharCount;
  201. // sqlTextBuffer.GetIterAtOffset (out end_iter, char_count);
  202. //}
  203. /* ------------------------------------
  204. * Free Comments (sort of like c style)
  205. * ------------------------------------
  206. * except in SQL, a c like comment occurs within
  207. * a SQL statement
  208. */
  209. match_start1 = start_iter; // dummy
  210. match_end1 = end_iter; // dummy
  211. match_start2 = start_iter; // dummy
  212. match_end2 = end_iter; // dummy
  213. while (start_iter.IsEnd == false) {
  214. // FIXME: match_start1, match_end1, end_iter
  215. // need to be set to have ref in front
  216. // Problem with TextIter's ForwardSearch()
  217. // in GTK# (not GTK+)
  218. if (start_iter.ForwardSearch (
  219. "/*",
  220. TextSearchFlags.TextOnly,
  221. out match_start1,
  222. out match_end1,
  223. end_iter) == true) {
  224. /* beginning of free comment found */
  225. //freecomment_count++;
  226. // FIXME: fix match_start2, match_end2, end_iter
  227. // with ref if front
  228. if (match_end1.ForwardSearch (
  229. "*/",
  230. TextSearchFlags.TextOnly,
  231. out match_start2,
  232. out match_end2,
  233. end_iter) == true) {
  234. // ending of free comment found,
  235. // now hi-light comment
  236. sqlTextBuffer.ApplyTag (
  237. freecomment_tag,
  238. match_start1,
  239. match_end2);
  240. match_end2.ForwardChars (1);
  241. start_iter = match_end2;
  242. }
  243. else {
  244. // if no end found,
  245. // hi-light to the end,
  246. // to let the user know
  247. // the ending asterisk slash is missing
  248. ApplyTag (
  249. freecomment_tag,
  250. normaltext_tag,
  251. match_start1,
  252. end_iter);
  253. break;
  254. }
  255. }
  256. else
  257. break;
  258. }
  259. /* if free comments is different than last time,
  260. * invalidate line_last_changed - causes
  261. * a complete redo (instead hi-lighting just the current line -
  262. * do the whole buffer)
  263. * THIS IS JUST AN ATTEMPT FOR SPEED
  264. */
  265. //if (freecomment_count != last_freecomment_count) {
  266. // line_last_changed = -1;
  267. //}
  268. /*********************************************************************
  269. * See if the following needs hi-lighting:
  270. * - Line Comments (sort of like C++ slash slash comments
  271. * but uses hypen hyphen and it is based at the beginning of a line)
  272. * - Single-Quoted Constants ( WHERE COL1 = 'ABC' )
  273. * - SQL keywords (SELECT, FROM, WHERE, UPDATE, etc)
  274. *********************************************************************/
  275. //if (line != line_last_changed) {
  276. sqlTextBuffer.GetIterAtOffset (out start_iter, 0);
  277. //}
  278. //else {
  279. // if (insert_iter.StartsLine () == true) {
  280. // start_iter = insert_iter;
  281. // }
  282. // else {
  283. // start_iter = insert_iter;
  284. // start_iter.LineOffset = 0;
  285. // }
  286. //}
  287. // get starting and ending iters
  288. // and character count of line
  289. char_count = sqlTextBuffer.CharCount;
  290. sqlTextBuffer.GetIterAtOffset (out end_iter, char_count);
  291. // for each line, look for:
  292. // line comments, constants, and keywoards
  293. do {
  294. iter = start_iter;
  295. iter.ForwardToLineEnd ();
  296. text = sqlTextBuffer.GetText (
  297. start_iter, iter, false);
  298. // look for line comment
  299. char_count = start_iter.CharsInLine;
  300. hyphen = 0;
  301. for (i = 0; i < char_count - 1; i++) {
  302. switch (text[i]) {
  303. case '-':
  304. if (hyphen == 1) {
  305. hyphen = 2;
  306. // line comment found
  307. i = char_count;
  308. ApplyTag (
  309. linecomment_tag,
  310. normaltext_tag,
  311. start_iter,
  312. iter);
  313. }
  314. else {
  315. hyphen = 1;
  316. }
  317. break;
  318. case ' ':
  319. // continue
  320. break;
  321. default:
  322. // this line is not line commented
  323. i = char_count; // break out of for loop
  324. break;
  325. }
  326. }
  327. // if not line commented,
  328. // look for singled quoted constants
  329. // and keywords
  330. if (hyphen < 2) {
  331. if (start_iter.IsEnd == true)
  332. break; // break out of for loop
  333. start_word = -1;
  334. single_quotes = 0;
  335. LookForSingleQuotesAndWords (
  336. ref start_iter,
  337. text, char_count,
  338. ref start_word,
  339. ref single_quotes,
  340. ref start_con,
  341. ref end_con);
  342. }
  343. } while (start_iter.ForwardLine () == true);
  344. // POOR ATTEMPTS AT SPEED - last_freecomment_count
  345. // and line_last_changed
  346. //
  347. //last_freecomment_count = freecomment_count;
  348. //line_last_changed = line;
  349. }
  350. void LookForSingleQuotesAndWords (ref TextIter start_iter,
  351. string text, int char_count,
  352. ref int start_word, ref int single_quotes,
  353. ref int start_con, ref int end_con)
  354. {
  355. TextIter match_start1, match_end1;
  356. int i;
  357. char ch;
  358. for (i = 0; i < char_count; i++) {
  359. match_start1 = start_iter;
  360. match_end1 = start_iter;
  361. if (match_end1.IsEnd == true)
  362. break;
  363. if (CharHasTag (start_iter,
  364. freecomment_tag, i)
  365. == false) {
  366. if (single_quotes == 0 &&
  367. start_word == -1) {
  368. ch = text[i];
  369. if (ch == '\'') {
  370. single_quotes = 1;
  371. start_con = i + 1;
  372. }
  373. else if (Char.IsLetter (ch)) {
  374. start_word = i;
  375. }
  376. else {
  377. // continue
  378. }
  379. }
  380. else if (single_quotes == 1) {
  381. ch = text[i];
  382. switch (ch) {
  383. case '\'':
  384. // single quoted constant
  385. end_con = i;
  386. // get starting and
  387. // ending of constant
  388. // excluding quotes
  389. ApplyTagOffsets (
  390. start_iter,
  391. start_con, i,
  392. singlequotedconstant_tag,
  393. normaltext_tag);
  394. single_quotes = 0;
  395. break;
  396. default:
  397. break;
  398. }
  399. }
  400. else if (start_word != -1) {
  401. ch = text[i];
  402. // is character alphabetic, numeric, or '_'
  403. if (Char.IsLetterOrDigit (ch) ||
  404. ch == '_') {
  405. // continue
  406. }
  407. else {
  408. // using start_word
  409. // and i offsets,
  410. // get word
  411. if (IsTextSQL (text, start_word, i)) {
  412. // word is a SQL keyword,
  413. // hi-light word
  414. ApplyTagOffsets (
  415. start_iter,
  416. start_word, i,
  417. sql_tag,
  418. normaltext_tag);
  419. }
  420. start_word = -1;
  421. switch (text[i]) {
  422. case '\'':
  423. single_quotes = 1;
  424. start_con = i + 1;
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. if( start_word != -1) {
  434. if (IsTextSQL (text, start_word, i)) {
  435. // word is a SQL keyword,
  436. // hi-light word
  437. ApplyTagOffsets(
  438. start_iter,
  439. start_word, i,
  440. sql_tag,
  441. normaltext_tag);
  442. }
  443. }
  444. }
  445. void ApplyTag ( TextTag apply_tag, TextTag remove_tag,
  446. TextIter start_iter, TextIter end_iter )
  447. {
  448. #if DEBUG
  449. DebugText(start_iter, end_iter, "ApplyTag() " +
  450. "remove: " + remove_tag.Name +
  451. " apply: " + apply_tag.Name);
  452. #endif // DEBUG
  453. sqlTextBuffer.RemoveTag (
  454. remove_tag,
  455. start_iter, end_iter);
  456. sqlTextBuffer.ApplyTag (
  457. apply_tag,
  458. start_iter, end_iter);
  459. }
  460. void ApplyTagOffsets (TextIter start_iter,
  461. int start_offset, int end_offset,
  462. TextTag apply_tag,
  463. TextTag remove_tag)
  464. {
  465. TextIter begin_iter, end_iter;
  466. begin_iter = start_iter;
  467. end_iter = start_iter;
  468. begin_iter.LineOffset = start_offset;
  469. end_iter.LineOffset = end_offset;
  470. #if DEBUG
  471. DebugText(start_iter, end_iter, "ApplyTagOffsets() " +
  472. "remove: " + remove_tag.Name +
  473. " apply: " + apply_tag.Name +
  474. " start: " + start_offset.ToString() +
  475. " end: " + end_offset.ToString());
  476. #endif
  477. sqlTextBuffer.RemoveTag (remove_tag,
  478. begin_iter, end_iter);
  479. sqlTextBuffer.ApplyTag (apply_tag,
  480. begin_iter, end_iter);
  481. }
  482. /* is word a SQL keyword? */
  483. bool IsTextSQL (string text, int begin, int end)
  484. {
  485. string keyword = "";
  486. int i;
  487. int text_len;
  488. if(text.Equals(String.Empty))
  489. return false;
  490. if(begin < 0)
  491. return false;
  492. if(end < 2)
  493. return false;
  494. if(begin >= end)
  495. return false;
  496. text_len = end - begin;
  497. if(text_len < 1)
  498. return false;
  499. #if DEBUG
  500. SqlSharpGtk.DebugWriteLine("IsTextSQL - " +
  501. "begin: " + begin.ToString() +
  502. " end: " + end.ToString() +
  503. " text_len: " + text_len);
  504. SqlSharpGtk.DebugWriteLine("[TEXT BEGIN]");
  505. SqlSharpGtk.DebugWriteLine(text);
  506. SqlSharpGtk.DebugWriteLine("[TEXT END ]");
  507. #endif // DEBUG
  508. for (i = 0; sql_keywords[i] != String.Empty; i++) {
  509. if(text_len == sql_keywords[i].Length) {
  510. SqlSharpGtk.DebugWriteLine(
  511. "Test length: " + text_len +
  512. " keyword: " + keyword);
  513. try {
  514. keyword = text.Substring (begin, text_len);
  515. }
  516. catch(ArgumentOutOfRangeException a) {
  517. Console.WriteLine ("Internal Error: SqlSharpGtk: text.Substring() ArgumentOutOfRange");
  518. }
  519. keyword = keyword.ToUpper();
  520. if(keyword.Equals (sql_keywords [i]))
  521. return true;
  522. }
  523. }
  524. return false;
  525. }
  526. // does the character at offset in the GtkTextIter has
  527. // this text tag applied?
  528. bool CharHasTag(TextIter iter,
  529. TextTag tag, int char_offset_in_line)
  530. {
  531. TextIter offset_iter;
  532. offset_iter = iter;
  533. offset_iter.LineOffset = char_offset_in_line;
  534. return offset_iter.HasTag (tag);
  535. }
  536. public void Clear()
  537. {
  538. TextIter start, end;
  539. start = sqlTextBuffer.StartIter;
  540. end = sqlTextBuffer.EndIter;
  541. sqlTextBuffer.Delete(start,end);
  542. }
  543. public void LoadFromFile(string inFilename)
  544. {
  545. StreamReader sr = new StreamReader(inFilename);
  546. Clear();
  547. string NextLine;
  548. string line;
  549. while((NextLine = sr.ReadLine()) != null) {
  550. line = NextLine + "\n";
  551. sqlTextBuffer.Insert (sqlTextBuffer.EndIter, line);
  552. }
  553. sr.Close();
  554. }
  555. public void SaveToFile(string outFilename)
  556. {
  557. TextIter start_iter, iter;
  558. string text;
  559. StreamWriter sw = null;
  560. sw = new StreamWriter(outFilename);
  561. sqlTextBuffer.GetIterAtOffset (out iter, 0);
  562. start_iter = iter;
  563. while (iter.ForwardLine()) {
  564. text = sqlTextBuffer.GetText(start_iter, iter, false);
  565. sw.Write(text);
  566. start_iter = iter;
  567. }
  568. text = sqlTextBuffer.GetText(start_iter, iter, false);
  569. sw.Write(text);
  570. sw.Close();
  571. sw = null;
  572. }
  573. void DebugText (TextIter iter_start, TextIter iter_end,
  574. string debugMessage)
  575. {
  576. #if DEBUG
  577. string text = sqlTextBuffer.GetText (
  578. iter_start, iter_end, false);
  579. string msg =
  580. "[DEBUG-TEXT]: " +
  581. debugMessage +
  582. " (" +
  583. text +
  584. ")";
  585. SqlSharpGtk.DebugWriteLine(msg);
  586. #endif // DEBUG
  587. }
  588. static readonly string[] sql_keywords =
  589. new string[] {
  590. "DELETE",
  591. "FROM",
  592. "SELECT",
  593. "UPDATE",
  594. "SET",
  595. "INSERT",
  596. "INTO",
  597. "VALUES",
  598. "WHERE",
  599. "COUNT",
  600. "SUM",
  601. "MAX",
  602. "MIN",
  603. "AVG",
  604. "DROP",
  605. "ALTER",
  606. "CREATE",
  607. "VIEW",
  608. "TABLE",
  609. "AS",
  610. "AND",
  611. "OR",
  612. "ORDER",
  613. "GROUP",
  614. "BY",
  615. "HAVING",
  616. "IS",
  617. "NULL",
  618. "NOT",
  619. "COMMIT",
  620. "ROLLBACK",
  621. "EXISTS",
  622. "IN",
  623. "LIKE",
  624. "GRANT",
  625. "REVOKE",
  626. "ON",
  627. "TO",
  628. String.Empty
  629. };
  630. }
  631. }