Friday, April 26, 2013

Arel Sample (1)

I study Ruby on Rails and I have to use Arel directly.
So I write some SQL patterns on this page for my memo.

The database schema is the following diagram.


I write using of Arel as Rspec.


  it "where_sample1" do
    order = Arel::Table.new(:orders)
    q = order.
      where(order[:order_date].eq(Date.new(2013, 7, 24))).
      project(Arel.star)

    q.to_sql.should eq "SELECT *" +
      " FROM `orders` " +
      " WHERE `orders`.`order_date` = '2013-07-24'"
  end

  it "table_alias_sample1" do
    o = Arel::Table.new(:orders)
    o.table_alias = "o"

    q = o.
      where(o[:order_date].eq(Date.new(2013, 7, 24))).
      project(Arel.star)

    q.to_sql.should eq "SELECT *" +
      " FROM `orders` `o` " +
      " WHERE `o`.`order_date` = '2013-07-24'"
  end

  it "join_sample1" do
    o = Arel::Table.new(:orders)
    o.table_alias = "o"

    od = Arel::Table.new(:order_details)
    od.table_alias = "od"

    q = o.
      join(od).on(od[:order_no].eq(o[:order_no])).
      where(o[:order_date].eq(Date.new(2013, 7, 24))).
      where(od[:detail_no].lteq(3)).
      project(o[Arel.star],
        od[Arel.star])

    q.to_sql.should eq "SELECT `o`.*," +
      " `od`.*" +
      " FROM `orders` `o`" +
      " INNER JOIN `order_details` `od`" +
      " ON `od`.`order_no` = `o`.`order_no`" +
      " WHERE `o`.`order_date` = '2013-07-24'" +
      " AND `od`.`detail_no` <= 3"
  end

  it "outer_join_sample1" do
    od = Arel::Table.new(:order_details)
    od.table_alias = "od"

    i = Arel::Table.new(:items)
    i.table_alias = "i"

    q = od.
      join(i, Arel::Nodes::OuterJoin).
      on(i[:item_no].eq(od[:item_no])).
      project(od[Arel.star],
        i[Arel.star])

    q.to_sql.should eq "SELECT `od`.*," +
      " `i`.*" +
      " FROM `order_details` `od`" +
      " LEFT OUTER JOIN `items` `i`" +
      " ON `i`.`item_no` = `od`.`item_no`"
  end

  it "join_sample2" do
    o = Arel::Table.new(:orders)
    o.table_alias = "o"

    od = Arel::Table.new(:order_details)
    od.table_alias = "od"

    i = Arel::Table.new(:items)
    i.table_alias = "i"

    q = o.
      join(od).on(od[:order_no].eq(o[:order_no])).
      join(i).on(i[:item_no].eq(od[:item_no])).
      project(o[Arel.star],
        od[Arel.star],
        i[Arel.star])

    q.to_sql.should eq "SELECT `o`.*," +
      " `od`.*," +
      " `i`.*" +
      " FROM `orders` `o`" +
      " INNER JOIN `order_details` `od`" +
      " ON `od`.`order_no` = `o`.`order_no`" +
      " INNER JOIN `items` `i`" +
      " ON `i`.`item_no` = `od`.`item_no`"
  end

Monday, October 10, 2011

An idea for generating test data (3)

I told the progress making the tool was 10% but it is wrong and it is not easy.

I got some tips. I describe the tip for labeled for statement.

We can write the for statement with label and the example is following:

for1: for(int i = 0; i < 10; i++) {
  for2: for(int j = 0; j < 5; j++) {
    int k = getValue(i, j);
    if (k == 1)
      continue for1;
    else if (k == 2)
      break for1;
  }
}


But the labeled for statement is not allowed in C#. So we need to write it :

for (int i = 0; i < 10; i++) {
  for (int j = 0; j < 5; j++) {
    int k = getValue(i, j);
    if (k == 1)
      goto continue_for_for1;
    else if (k == 2)
      goto break_for_for1;
  }
continue_for_for1:
}
break_for_for1:

Tuesday, October 4, 2011

An idea for generating test data (2)

I decided to use Irony to make the tool for converting Java sources to C# sources.

It is difficult for me to use ANTLR because I cannot understand building Java AST by using ANTLR.
And I can understand building Java AST by using Irony.

I am making the tool and the progress is about 10% toward completion.

Sunday, September 25, 2011

An idea for generating test data

An idea come to me and it is that we convert the Java source to C# source and apply Pex the C# source and the generated test data is usable for original Java source.

But I don't know the tools that convert Java source to C# source.

I will make the tool by ANTLR or Irony. Which is easier to make tools?

Sunday, September 18, 2011

The arrows in Haskell

Can the arrows in Haskell considered by a category?
I will investigate it later.

Sunday, September 11, 2011

The kinds of Categories usable for computer science

I want to systematize the categories but will think later.


  • cartesian closed category
  • freyd category
  • kleisli category
  • monoidal category
  • premonoidal category
  • traced monoidal category
  • braided monoidal category
  • balanced monoidal category
  • symmetric monoidal category
  • ribbon category = tortile monoidal category
  • compact closed category

Sunday, September 4, 2011

Should the copy of variable be less?

The lambda calculus with types equals to intuitionistic logic. (from the Curry-Howard Isomorphism)
Intuitionistic logic is asymmetric and is constructive.
If we want the logic to be symmetric and constructive we should restrict the copy of assumption from the linear logic.
So we can introduce the symmetry to the calculus by the copy of variable.
I think that a symmetric structure is more beautiful than asymmetric one and symmetric one is  more maintenanceable.
So I think that it is good that we restrict the copy of variable or wrap that by a library. Is it right?