= SQL
== DESCRIPTION:
Ruby library for parsing and generating SQL statements.
== FEATURES/PROBLEMS:
* Parse arbitrary SQL strings into an AST (abstract syntax tree), which can
then be traversed.
* Allows your code to understand and manipulate SQL in a deeper way than
just using string manipulation.
== SYNOPSIS:
Here's an example of manually building an AST. It's extremely verbose at the
moment, but that should be mitigated a bit when I come up with a DSL for
building these trees.
>> require 'rubygems'
>> require 'sql'
# Let's build a tree representing the SQL statement
# "SELECT * FROM users WHERE id = 1"
# We'll start from the rightmost side, and work our way left as we go.
# First, the integer constant, "1"
>> integer_constant = SQL::Statement::Integer.new(1)
>> integer_constant.to_sql
=> "1"
# Now the column reference, "id"
>> column_reference = SQL::Statement::Column.new('id')
>> column_reference.to_sql
=> "id"
# Now we'll combine the two using an equals operator, to create a search
# condition
>> search_condition = SQL::Statement::Equals.new(column_reference, integer_constant)
>> search_condition.to_sql
=> "id = 1"
# Next we'll feed that search condition to a where clause
>> where_clause = SQL::Statement::WhereClause.new(search_condition)
>> where_clause.to_sql
=> "WHERE id = 1"
# Next up is the FROM clause. First we'll build a table reference
>> users = SQL::Statement::Table.new('users')
>> users.to_sql
=> "users"
# Now we'll feed that table reference to a from clause
>> from_clause = SQL::Statement::FromClause.new(users)
>> from_clause.to_sql
=> "FROM users"
# Now to combine the FROM and WHERE clauses to form a table expression
>> table_expression = SQL::Statement::TableExpression.new(from_clause, where_clause)
>> table_expression.to_sql
=> "FROM users WHERE id = 1"
# Now we need to represent the asterisk "*"
>> all = SQL::Statement::All.new
>> all.to_sql
=> "*"
# Now we're ready to hand off these objects to a select statement
>> select_statement = SQL::Statement::Select.new(all, table_expression)
>> select_statement.to_sql
=> "SELECT * FROM users WHERE id = 1"
== ROADMAP
These features aren't currently implemented, but I have plans to add them in
the future. Stay tuned.
Eventually I plan to implement a SQL parser that will generate the tree
structure for you, based on an input string of SQL.
>> require 'rubygems'
>> require 'sql'
>> parser = SQL::Parser.new
# Build the AST from a SQL statement
>> ast = parser.scan_str('SELECT * FROM users WHERE id = 1')
# Find which columns where selected in the FROM clause
>> ast.select_list.to_sql
=> "*"
# Output the table expression as SQL
>> ast.table_expression.to_sql
=> "FROM users WHERE id = 1"
# Drill down into the WHERE clause, to examine every piece
>> ast.table_expression.where_clause.to_sql
=> "WHERE id = 1"
>> ast.table_expression.where_clause.search_condition.to_sql
=> "id = 1"
>> ast.table_expression.where_clause.search_condition.left.to_sql
=> "id"
>> ast.table_expression.where_clause.search_condition.right.to_sql
=> "1"
== REQUIREMENTS:
* Ruby (duh)
* RubyGems
* Hoe >= 1.6.0
== INSTALL:
$ sudo gem install omghax-sql --source http://gems.github.com
SQL-用于解析+生成SQL语句的Ruby库.zip
需积分: 5 38 浏览量
2024-02-25
19:33:10
上传
评论
收藏 24KB ZIP 举报
m0_57195758
- 粉丝: 763
- 资源: 252
最新资源
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- Highlight Plus v20.0.1
- 基于MIC+NE555光敏电阻的声光控电路Multisim仿真原理图
- python tkinter-08-盒子模型.ev4.rar
- Doozy UI Manager 2023
- 基于matlab实现夜间车牌识别程序(1).rar
- 基于matlab实现无线传感器网络无需测距定位算法matlab源代码 包括apit,dv-hop,amorphous在内的共7个
- 基于python的yolov5实现的旋转目标检测
- 基于matlab实现无线传感器网络 CAB定位仿真程序 这是无线传感器节点定位CAB算法的仿真程序,由matlab完成.rar
- 基于matlab实现图像处理,本程序使用背景差分法对来往车辆进行检测和跟踪.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈