Skip to content

Latest commit

 

History

History
58 lines (44 loc) · 1.24 KB

File metadata and controls

58 lines (44 loc) · 1.24 KB

@tidbcloud/codemirror-extension-sql-parser

A codemirror extension listens the editor doc change, parses the content to SQL statements.

This extension is installed internally inside the SQLEditorInstance.

Installation

npm install @tidbcloud/codemirror-extension-sql-parser

You need to install its peer dependencies as well:

npm install @codemirror/view @codemirror/state @codemirror/language @codemirror/lang-sql

Usage

import { EditorView } from '@codemirror/view'
import { EditorState } from '@codemirror/state'
import { sql, MySQL } from '@codemirror/lang-sql'
import { sqlParser } from '@tidbcloud/codemirror-extension-sql-parser'

const editorView = new EditorView({
  state: EditorState.create({
    doc,
    extensions: [sql({ dialect: MySQL }), sqlParser()]
  })
})

API

type SqlStatement = {
  from: number
  to: number
  lineFrom: number
  lineTo: number
  content: string
  database: string
  type: 'use' | 'ddl' | 'other'
}

/* get all parsed statements */
function getSqlStatements(state: EditorState): SqlStatement[]

/* get the nearest statement before the pos */
function getNearbyStatement(
  state: EditorState,
  pos: number
): SqlStatement | undefined

function sqlParser(): Extension