使用jQuery解析SQL CREATE TABLE语句? [英] Parsing SQL CREATE TABLE statement using jQuery?

查看:135
本文介绍了使用jQuery解析SQL CREATE TABLE语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用SQL作为输入的Web应用程序.该SQL必须始终为CREATE TABLE语句,我的应用程序需要从中获取以下属性:

I am writing a web app which uses SQL as input. This SQL must always be a CREATE TABLE statement, and what my app needs to do is getting the following attributes from it:

  • 字段名称
  • 类型
  • 长度(如果有)
  • 二进制(如果有)
  • 允许NULL(如果可用)
  • 自动递增(如果有)

示例SQL:

CREATE TABLE customer
(First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)

我不知道从哪里开始,而且我也不擅长编写这样的解析器.我是否可以使用任何适用于JavaScript/jQuery的SQL解析器,或任何示例代码?谢谢.

I have no idea where to start, and I'm not that good at writing such parsers. Are there any SQL parsers for JavaScript/jQuery that I can use, or any example code? Thanks.

推荐答案

这不是完整的解决方案,但希望可以帮助您入门.

This isn't a complete solution, but hopefully something to get you started.

您可以检查以下正则表达式:

You could check the following regex:

/^CREATE\s+TABLE\s+(\S*)\s*\((.*)\)$/

很粗糙,但是应该匹配.

It's crude, but it should match.

然后您可以拔出第一个匹配的组(假设您需要表的名称),然后拔出字段列表的第二个匹配的组

You could then pull out the first matched group (assuming you need the name of the table) and then the second matched group for the field list

然后可以在逗号上拆分字段列表,以获取每个字段的信息.除此之外,您可以通过匹配/^(.)\s(.)$/中的第一组来获得字段名称.语句的后半部分(类型,长度,null/not null,默认值,identity(1,1)),但是如果您可以找出始终适用的某种模式,则可以肯定.请注意,上面还假设对空白进行了适当的修剪,但这很容易.

You can then split the field list on the comma to get the information for each field. Outside of that you could get the field name by matching the first group in /^(.)\s(.)$/ ... You'd have to get a bit more creative in parsing the second half of the statement (types, lengths, null/not null, default, identity(1,1) ) but if you can figure out some sort of pattern that would always apply, I'm sure it could be done. Note that the above is also assuming that whitespace is properly trimmed, but that is easy enough.

这篇关于使用jQuery解析SQL CREATE TABLE语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆