Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 82 lines (68 sloc) 1.873 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
# lambda-sql : using closures to compose SQL statements.
# Eugene Koontz <ekoontz@hiro-tan.org>
# Licensed under the GPL version 3.
#
# Note: uses ruby 1.8 "lambda" syntax (not 1.9 "->" syntax)
#
# <library functions>

schematized_where = lambda{|select,from,where|
  "SELECT '" + schema + "' AS schema, " + select +
  " FROM " + schema + "." + from +
  " WHERE " + where
}

from_where = lambda{|from,where|
  lambda{|schema|
    "SELECT '" + schema + "' AS schema, " + select +
    " FROM " + schema + "." + from +
    " WHERE " + where
  }
}

from = lambda{|from|
  lambda{|where|
    lambda{|select|
      lambda{|schema|
        "SELECT '" + schema + "' AS schema, " + select +
        " FROM " + schema + "." + from +
        " WHERE " + where
      }
    }
  }
}

union2 = lambda{|schf|
  lambda{|sch2,sch1|
    schf.call(sch1) +
    " UNION " +
    schf.call(sch2)
  }
}

set_window = lambda{|query,limit,offset,orderby|
  query + " ORDER BY " + orderby + " LIMIT " + limit + " OFFSET " + offset
}

# </library functions>

# <example usages>

kernel = from.
  call(
       " project " +
       "INNER JOIN person manager " +
       " ON project.manager_id = manager.person_id " +
       "INNER JOIN location " +
       " ON location.location_id = project.location_id"
       )

kernel_with_where = kernel.
  call(
       "project.created_on > '2009-08-01'"
       )

kernel_with_select = kernel_with_where.
  call(
       "project_id,project.name,created_on, " +
       "manager.name, " +
       "location.street_number || ' ' || location.street AS address"
       )

kernel_union = union2.call(kernel_with_select).
  call("software",
       "finance")

my_query = set_window.call(kernel_union,
                           "20",
                           "10",
                           "created_on DESC")

# </example usages>
  
Something went wrong with that request. Please try again.