Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeMirror: How add tables to sql-hint?

I can't figure how add tables to codemirror. I have sql-hint.js included, and work for keywords but don't understand how add tables and columns...

like image 469
mamcx Avatar asked Nov 16 '13 20:11

mamcx


2 Answers

Sadly, this does not appear to be documented anywhere.

With some trial and error I was able to figure out that you can pass in a structure of table and column names as options when invoking the hinter, like this:

CodeMirror.commands.autocomplete = function(cm) {
    CodeMirror.showHint(cm, CodeMirror.hint.sql, { 
        tables: {
            "table1": [ "col_A", "col_B", "col_C" ],
            "table2": [ "other_columns1", "other_columns2" ]
        }
    } );
}
like image 161
Matthew Simon Cavalletto Avatar answered Oct 20 '22 08:10

Matthew Simon Cavalletto


I know that this question is somewhat old but..I found an interesting way, valid in 4.3 release (I don't know anything about older releases): Just add the "CodeMirror.hint.sql" value (without quotes, as its a function) as "hint" option and add the "tables" object as a sub-object defined in "hintOptions" object.

Something like:

CodeMirror.fromTextArea(document.getElementsByTagName("textarea")[0], {
        mode: "text/x-sql",
        extraKeys: {"Ctrl-Space": "autocomplete"}, // To invoke the auto complete
        hint: CodeMirror.hint.sql,
        hintOptions: {
            tables: {
                "table1": [ "col_A", "col_B", "col_C" ],
                "table2": [ "other_columns1", "other_columns2" ]
            }
        }
}); 

That's it. Note that the "extraKeys" is absolutely not needed, but I found it to be great to test the autocomplete more easily. =)

Good luck. :)

like image 21
Fernando Jorge Mota Avatar answered Oct 20 '22 08:10

Fernando Jorge Mota