qscilexersql.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // This module implements the QsciLexerSQL class.
  2. //
  3. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of QScintilla.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. #include "Qsci/qscilexersql.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerSQL::QsciLexerSQL(QObject *parent)
  25. : QsciLexer(parent),
  26. at_else(false), fold_comments(false), fold_compact(true),
  27. only_begin(false), backticks_identifier(false),
  28. numbersign_comment(false), backslash_escapes(false),
  29. allow_dotted_word(false)
  30. {
  31. }
  32. // The dtor.
  33. QsciLexerSQL::~QsciLexerSQL()
  34. {
  35. }
  36. // Returns the language name.
  37. const char *QsciLexerSQL::language() const
  38. {
  39. return "SQL";
  40. }
  41. // Returns the lexer name.
  42. const char *QsciLexerSQL::lexer() const
  43. {
  44. return "sql";
  45. }
  46. // Return the style used for braces.
  47. int QsciLexerSQL::braceStyle() const
  48. {
  49. return Operator;
  50. }
  51. // Returns the foreground colour of the text for a style.
  52. QColor QsciLexerSQL::defaultColor(int style) const
  53. {
  54. switch (style)
  55. {
  56. case Default:
  57. return QColor(0x80,0x80,0x80);
  58. case Comment:
  59. case CommentLine:
  60. case PlusPrompt:
  61. case PlusComment:
  62. case CommentLineHash:
  63. return QColor(0x00,0x7f,0x00);
  64. case CommentDoc:
  65. return QColor(0x7f,0x7f,0x7f);
  66. case Number:
  67. return QColor(0x00,0x7f,0x7f);
  68. case Keyword:
  69. return QColor(0x00,0x00,0x7f);
  70. case DoubleQuotedString:
  71. case SingleQuotedString:
  72. return QColor(0x7f,0x00,0x7f);
  73. case PlusKeyword:
  74. return QColor(0x7f,0x7f,0x00);
  75. case Operator:
  76. case Identifier:
  77. break;
  78. case CommentDocKeyword:
  79. return QColor(0x30,0x60,0xa0);
  80. case CommentDocKeywordError:
  81. return QColor(0x80,0x40,0x20);
  82. case KeywordSet5:
  83. return QColor(0x4b,0x00,0x82);
  84. case KeywordSet6:
  85. return QColor(0xb0,0x00,0x40);
  86. case KeywordSet7:
  87. return QColor(0x8b,0x00,0x00);
  88. case KeywordSet8:
  89. return QColor(0x80,0x00,0x80);
  90. }
  91. return QsciLexer::defaultColor(style);
  92. }
  93. // Returns the end-of-line fill for a style.
  94. bool QsciLexerSQL::defaultEolFill(int style) const
  95. {
  96. if (style == PlusPrompt)
  97. return true;
  98. return QsciLexer::defaultEolFill(style);
  99. }
  100. // Returns the font of the text for a style.
  101. QFont QsciLexerSQL::defaultFont(int style) const
  102. {
  103. QFont f;
  104. switch (style)
  105. {
  106. case Comment:
  107. case CommentLine:
  108. case PlusComment:
  109. case CommentLineHash:
  110. case CommentDocKeyword:
  111. case CommentDocKeywordError:
  112. #if defined(Q_OS_WIN)
  113. f = QFont("Comic Sans MS",9);
  114. #elif defined(Q_OS_MAC)
  115. f = QFont("Comic Sans MS", 12);
  116. #else
  117. f = QFont("Bitstream Vera Serif",9);
  118. #endif
  119. break;
  120. case Keyword:
  121. case Operator:
  122. f = QsciLexer::defaultFont(style);
  123. f.setBold(true);
  124. break;
  125. case DoubleQuotedString:
  126. case SingleQuotedString:
  127. case PlusPrompt:
  128. #if defined(Q_OS_WIN)
  129. f = QFont("Courier New",10);
  130. #elif defined(Q_OS_MAC)
  131. f = QFont("Courier", 12);
  132. #else
  133. f = QFont("Bitstream Vera Sans Mono",9);
  134. #endif
  135. break;
  136. default:
  137. f = QsciLexer::defaultFont(style);
  138. }
  139. return f;
  140. }
  141. // Returns the set of keywords.
  142. const char *QsciLexerSQL::keywords(int set) const
  143. {
  144. if (set == 1)
  145. return
  146. "absolute action add admin after aggregate alias all "
  147. "allocate alter and any are array as asc assertion "
  148. "at authorization before begin binary bit blob "
  149. "boolean both breadth by call cascade cascaded case "
  150. "cast catalog char character check class clob close "
  151. "collate collation column commit completion connect "
  152. "connection constraint constraints constructor "
  153. "continue corresponding create cross cube current "
  154. "current_date current_path current_role current_time "
  155. "current_timestamp current_user cursor cycle data "
  156. "date day deallocate dec decimal declare default "
  157. "deferrable deferred delete depth deref desc "
  158. "describe descriptor destroy destructor "
  159. "deterministic dictionary diagnostics disconnect "
  160. "distinct domain double drop dynamic each else end "
  161. "end-exec equals escape every except exception exec "
  162. "execute external false fetch first float for "
  163. "foreign found from free full function general get "
  164. "global go goto grant group grouping having host "
  165. "hour identity if ignore immediate in indicator "
  166. "initialize initially inner inout input insert int "
  167. "integer intersect interval into is isolation "
  168. "iterate join key language large last lateral "
  169. "leading left less level like limit local localtime "
  170. "localtimestamp locator map match minute modifies "
  171. "modify module month names national natural nchar "
  172. "nclob new next no none not null numeric object of "
  173. "off old on only open operation option or order "
  174. "ordinality out outer output pad parameter "
  175. "parameters partial path postfix precision prefix "
  176. "preorder prepare preserve primary prior privileges "
  177. "procedure public read reads real recursive ref "
  178. "references referencing relative restrict result "
  179. "return returns revoke right role rollback rollup "
  180. "routine row rows savepoint schema scroll scope "
  181. "search second section select sequence session "
  182. "session_user set sets size smallint some| space "
  183. "specific specifictype sql sqlexception sqlstate "
  184. "sqlwarning start state statement static structure "
  185. "system_user table temporary terminate than then "
  186. "time timestamp timezone_hour timezone_minute to "
  187. "trailing transaction translation treat trigger "
  188. "true under union unique unknown unnest update usage "
  189. "user using value values varchar variable varying "
  190. "view when whenever where with without work write "
  191. "year zone";
  192. if (set == 3)
  193. return
  194. "param author since return see deprecated todo";
  195. if (set == 4)
  196. return
  197. "acc~ept a~ppend archive log attribute bre~ak "
  198. "bti~tle c~hange cl~ear col~umn comp~ute conn~ect "
  199. "copy def~ine del desc~ribe disc~onnect e~dit "
  200. "exec~ute exit get help ho~st i~nput l~ist passw~ord "
  201. "pau~se pri~nt pro~mpt quit recover rem~ark "
  202. "repf~ooter reph~eader r~un sav~e set sho~w shutdown "
  203. "spo~ol sta~rt startup store timi~ng tti~tle "
  204. "undef~ine var~iable whenever oserror whenever "
  205. "sqlerror";
  206. if (set == 5)
  207. return
  208. "dbms_output.disable dbms_output.enable dbms_output.get_line "
  209. "dbms_output.get_lines dbms_output.new_line dbms_output.put "
  210. "dbms_output.put_line";
  211. return 0;
  212. }
  213. // Returns the user name of a style.
  214. QString QsciLexerSQL::description(int style) const
  215. {
  216. switch (style)
  217. {
  218. case Default:
  219. return tr("Default");
  220. case Comment:
  221. return tr("Comment");
  222. case CommentLine:
  223. return tr("Comment line");
  224. case CommentDoc:
  225. return tr("JavaDoc style comment");
  226. case Number:
  227. return tr("Number");
  228. case Keyword:
  229. return tr("Keyword");
  230. case DoubleQuotedString:
  231. return tr("Double-quoted string");
  232. case SingleQuotedString:
  233. return tr("Single-quoted string");
  234. case PlusKeyword:
  235. return tr("SQL*Plus keyword");
  236. case PlusPrompt:
  237. return tr("SQL*Plus prompt");
  238. case Operator:
  239. return tr("Operator");
  240. case Identifier:
  241. return tr("Identifier");
  242. case PlusComment:
  243. return tr("SQL*Plus comment");
  244. case CommentLineHash:
  245. return tr("# comment line");
  246. case CommentDocKeyword:
  247. return tr("JavaDoc keyword");
  248. case CommentDocKeywordError:
  249. return tr("JavaDoc keyword error");
  250. case KeywordSet5:
  251. return tr("User defined 1");
  252. case KeywordSet6:
  253. return tr("User defined 2");
  254. case KeywordSet7:
  255. return tr("User defined 3");
  256. case KeywordSet8:
  257. return tr("User defined 4");
  258. case QuotedIdentifier:
  259. return tr("Quoted identifier");
  260. case QuotedOperator:
  261. return tr("Quoted operator");
  262. }
  263. return QString();
  264. }
  265. // Returns the background colour of the text for a style.
  266. QColor QsciLexerSQL::defaultPaper(int style) const
  267. {
  268. if (style == PlusPrompt)
  269. return QColor(0xe0,0xff,0xe0);
  270. return QsciLexer::defaultPaper(style);
  271. }
  272. // Refresh all properties.
  273. void QsciLexerSQL::refreshProperties()
  274. {
  275. setAtElseProp();
  276. setCommentProp();
  277. setCompactProp();
  278. setOnlyBeginProp();
  279. setBackticksIdentifierProp();
  280. setNumbersignCommentProp();
  281. setBackslashEscapesProp();
  282. setAllowDottedWordProp();
  283. }
  284. // Read properties from the settings.
  285. bool QsciLexerSQL::readProperties(QSettings &qs, const QString &prefix)
  286. {
  287. int rc = true;
  288. at_else = qs.value(prefix + "atelse", false).toBool();
  289. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  290. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  291. only_begin = qs.value(prefix + "onlybegin", false).toBool();
  292. backticks_identifier = qs.value(prefix + "backticksidentifier", false).toBool();
  293. numbersign_comment = qs.value(prefix + "numbersigncomment", false).toBool();
  294. backslash_escapes = qs.value(prefix + "backslashescapes", false).toBool();
  295. allow_dotted_word = qs.value(prefix + "allowdottedword", false).toBool();
  296. return rc;
  297. }
  298. // Write properties to the settings.
  299. bool QsciLexerSQL::writeProperties(QSettings &qs, const QString &prefix) const
  300. {
  301. int rc = true;
  302. qs.value(prefix + "atelse", at_else);
  303. qs.value(prefix + "foldcomments", fold_comments);
  304. qs.value(prefix + "foldcompact", fold_compact);
  305. qs.value(prefix + "onlybegin", only_begin);
  306. qs.value(prefix + "backticksidentifier", backticks_identifier);
  307. qs.value(prefix + "numbersigncomment", numbersign_comment);
  308. qs.value(prefix + "backslashescapes", backslash_escapes);
  309. qs.value(prefix + "allowdottedword", allow_dotted_word);
  310. return rc;
  311. }
  312. // Set if ELSE blocks can be folded.
  313. void QsciLexerSQL::setFoldAtElse(bool fold)
  314. {
  315. at_else = fold;
  316. setAtElseProp();
  317. }
  318. // Set the "fold.sql.at.else" property.
  319. void QsciLexerSQL::setAtElseProp()
  320. {
  321. emit propertyChanged("fold.sql.at.else", (at_else ? "1" : "0"));
  322. }
  323. // Set if comments can be folded.
  324. void QsciLexerSQL::setFoldComments(bool fold)
  325. {
  326. fold_comments = fold;
  327. setCommentProp();
  328. }
  329. // Set the "fold.comment" property.
  330. void QsciLexerSQL::setCommentProp()
  331. {
  332. emit propertyChanged("fold.comment", (fold_comments ? "1" : "0"));
  333. }
  334. // Set if folds are compact
  335. void QsciLexerSQL::setFoldCompact(bool fold)
  336. {
  337. fold_compact = fold;
  338. setCompactProp();
  339. }
  340. // Set the "fold.compact" property.
  341. void QsciLexerSQL::setCompactProp()
  342. {
  343. emit propertyChanged("fold.compact", (fold_compact ? "1" : "0"));
  344. }
  345. // Set if BEGIN blocks only can be folded.
  346. void QsciLexerSQL::setFoldOnlyBegin(bool fold)
  347. {
  348. only_begin = fold;
  349. setOnlyBeginProp();
  350. }
  351. // Set the "fold.sql.only.begin" property.
  352. void QsciLexerSQL::setOnlyBeginProp()
  353. {
  354. emit propertyChanged("fold.sql.only.begin", (only_begin ? "1" : "0"));
  355. }
  356. // Enable quoted identifiers.
  357. void QsciLexerSQL::setQuotedIdentifiers(bool enable)
  358. {
  359. backticks_identifier = enable;
  360. setBackticksIdentifierProp();
  361. }
  362. // Set the "lexer.sql.backticks.identifier" property.
  363. void QsciLexerSQL::setBackticksIdentifierProp()
  364. {
  365. emit propertyChanged("lexer.sql.backticks.identifier", (backticks_identifier ? "1" : "0"));
  366. }
  367. // Enable '#' as a comment character.
  368. void QsciLexerSQL::setHashComments(bool enable)
  369. {
  370. numbersign_comment = enable;
  371. setNumbersignCommentProp();
  372. }
  373. // Set the "lexer.sql.numbersign.comment" property.
  374. void QsciLexerSQL::setNumbersignCommentProp()
  375. {
  376. emit propertyChanged("lexer.sql.numbersign.comment", (numbersign_comment ? "1" : "0"));
  377. }
  378. // Enable/disable backslash escapes.
  379. void QsciLexerSQL::setBackslashEscapes(bool enable)
  380. {
  381. backslash_escapes = enable;
  382. setBackslashEscapesProp();
  383. }
  384. // Set the "sql.backslash.escapes" property.
  385. void QsciLexerSQL::setBackslashEscapesProp()
  386. {
  387. emit propertyChanged("sql.backslash.escapes", (backslash_escapes ? "1" : "0"));
  388. }
  389. // Enable dotted words.
  390. void QsciLexerSQL::setDottedWords(bool enable)
  391. {
  392. allow_dotted_word = enable;
  393. setAllowDottedWordProp();
  394. }
  395. // Set the "lexer.sql.allow.dotted.word" property.
  396. void QsciLexerSQL::setAllowDottedWordProp()
  397. {
  398. emit propertyChanged("lexer.sql.allow.dotted.word", (allow_dotted_word ? "1" : "0"));
  399. }