arielpts / mysql-proxy-arel

A little experient written in Lua and Ruby that enables you to perform Arel "SQL" from your favorite MySQL tool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MySQL Proxy for Arel

What?

A little experient written in Lua and Ruby that enables you to perform Arel "SQL" from your favorite MySQL tool:

Screenshot (OK)

image

Screenshot (Returned Ruby Error)

image

Arel to SQL Samples

Simple Query

Original "SQL"

#arel
customer.select(*).where(customer[:name].eq('Ariel Patschiki'))

Rewritten SQL

SELECT * FROM `customer` WHERE `customer`.`name` = 'Ariel Patschiki'

More Query

Original SQL

#arel
customer.select(*). \
join(address).on(address[:id].eq(customer[:address_id])). \
where(customer[:name].eq('Ariel Patschiki'))

Rewritten SQL

SELECT *
FROM `customer`
INNER JOIN `address` ON `address`.`id` = `customer`.`address_id`
WHERE `customer`.`name` = 'Ariel Patschiki'

Complex Query

Original SQL

#arel

ids = [1, 2, 3]

c = customer.take(1)
ids.each do |customer_id|
	sql = customer.select(subscription[:value].sum). \
		join(subscription).on(subscription[:customer_id].eq(customer[:id])). \
		where(customer[:id].eq(customer_id)). \
		where(subscription[:status].eq('Confirmed')).to_sql

	c = c.project(Arel::Nodes::SqlLiteral.new('(' + sql + ')').as('id_' + customer_id.to_s))
end

c

Rewritten SQL

SELECT  (SELECT SUM(`subscription`.`value`) AS sum_id FROM `customer` INNER JOIN `subscription` ON `subscription`.`customer_id` = `customer`.`id` WHERE `customer`.`id` = 1 AND `subscription`.`status` = 'Confirmed') AS id_1, (SELECT SUM(`subscription`.`value`) AS sum_id FROM `customer` INNER JOIN `subscription` ON `subscription`.`customer_id` = `customer`.`id` WHERE `customer`.`id` = 2 AND `subscription`.`status` = 'Confirmed') AS id_2, (SELECT SUM(`subscription`.`value`) AS sum_id FROM `customer` INNER JOIN `subscription` ON `subscription`.`customer_id` = `customer`.`id` WHERE `customer`.`id` = 3 AND `subscription`.`status` = 'Confirmed') AS id_3 FROM `customer`  LIMIT 1

How?

  1. Install MySQL Proxy. Eg.: brew install mysql-proxy
  2. bundle
  3. ./start.sh
  4. Connect using port 3307 instead of 3306
  5. Start your queries with #arel

Syntax Sugar

  • Replaces * by Arel.sql('*')
  • Method Missing my_table_name to Arel::Table.new(:my_table_name)
  • More intuitive method names: select instead of project

About

A little experient written in Lua and Ruby that enables you to perform Arel "SQL" from your favorite MySQL tool


Languages

Language:Ruby 72.8%Language:Lua 23.4%Language:Shell 3.8%