Skip to content

Commit

Permalink
add columns.lua which return all columns of the table
Browse files Browse the repository at this point in the history
  • Loading branch information
junjiemars committed Feb 13, 2016
1 parent 5192975 commit cbeccbb
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
33 changes: 33 additions & 0 deletions resources/columns.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local _t_ = '_T_'
local _t_n_ = '_T_N_'
local na = #ARGV
local level = redis.LOG_NOTICE
local v = {}

local table_exists = function(t)
return 1 == redis.call('sismember', _t_n_, t)
end

if (0 == na) then
v = {0, 'should provides table name argument(>=1)'}
redis.log(level, v[#v])
return v
end

local t = ARGV[1]
if (not table_exists(t)) then
v = {0, string.format('table %s does not exists', t)}
redis.log(level, v[#v])
return v
end

local d = string.format(_t_..'[%s]_C_', t)
local cc = redis.call('smembers', d)
if (nil == cc) then
v = {0, 'can not find column defition'}
else
v = {#cc, cc}
end

return v

33 changes: 25 additions & 8 deletions resources/describe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,35 @@ local table_exists = function(t)
end

if (0 == na) then
v[#v+1] = redis.call('hgetall', _t_)
local d = redis.call('hgetall', _t_)
if (nil == d) then
v = {0, 'scheme does not exists'}
redis.log(level, v[#v])
else
v = {#d, d}
end
return v
end

local t = ARGV[1]
if (table_exists(t)) then
local d = string.format(_t_..'[%s]_C_', t)
local cc = redis.call('smembers', d)
for i=1,#cc do
local dc = string.format(d..'[%s]_', cc[i])
v[#v+1] = redis.call('hgetall', dc)
end
if (not table_exists(t)) then
v = {0, string.format('table %s does not exists', t)}
redis.log(level, v[#v])
return v
end

local d = string.format(_t_..'[%s]_C_', t)
local cc = redis.call('smembers', d)
if (nil == cc) then
v = {0, string.format('column\'s definition of table %s does not exists', t)}
redis.log(level, v[#v])
return v
end

v[#v+1] = #cc
for i=1,#cc do
local dc = string.format(d..'[%s]_', cc[i])
v[#v+1] = redis.call('hgetall', dc)
end

return v
Expand Down
2 changes: 1 addition & 1 deletion resources/insert.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local _t_n_ = '_T_N_'
local nk = #KEYS
local na = #ARGV
local level = redis.LOG_NOTICE
local m = 'not enough columns or values'
Expand All @@ -26,6 +25,7 @@ end

if (3 > na) or (0 == na % 2) then
m = 'not enough columns or values(>=3)'
redis.log(level, m)
return {00947, m}
end

Expand Down
17 changes: 9 additions & 8 deletions resources/scheme.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local v = {}
local _t_ = '_T_'
local _t_n_ = '_T_N_'
local _t_t_c_ = '_T_[T]_C_'
local _t_t_d_c = '_T_[T]_<C>_'
local _t_t_d_c_ = '_T_[T]_C_[C]_'
local _t_t_d_c_pk_ = '_T_[T]_C_PK_'
local level = redis.LOG_NOTICE
local m = 'create redisql\'s scheme %s'

Expand All @@ -11,15 +11,16 @@ local t = redis.call('hmset', _t_,
'TABLES_COMMENT', 'The set hold tables name',
'CLOUMNS', _t_t_c_,
'COLUMNS_COMMENT', 'The set hold columns name, [T] will be replaced with Table name',
'COLUMN_DEFINE', _t_t_d_c,
'COLUMN_DEFINE_COMMENT', 'The hash hold column definition, [C] will be replaced with Column name')
'COLUMN_DEFINE', _t_t_d_c_,
'COLUMN_DEFINE_COMMENT', 'The hash hold column definition, [C] will be replaced with Column name',
'PRIMARY_KEYS', _t_t_d_c_pk_,
'PRIMARY_KEYS_DEFINE_COMMENT', 'The set hold primary keys of table [T]')

local v = {}
if ('OK' == t['ok']) then
v[#v+1] = 0
v[#v+1] = string.format(m, 'ok')
v = {0, string.format(m, 'ok')}
else
v[#v+1] = -1
v[#v+1] = string.format(m, 'failed')
v = {-1, string.format(m, 'failed')}
end

redis.log(level, v[#v])
Expand Down
5 changes: 3 additions & 2 deletions resources/table.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local _t_n_ = '_T_N_'
local nk = #KEYS
local na = #ARGV
local level = redis.LOG_NOTICE

Expand Down Expand Up @@ -41,7 +40,9 @@ local def_attribute = function(cd, an, av)
end

if (3 > na) or (0 == na % 2) then
return {-1, 'should provides table, column, value arguments(>=3)'}
local m = 'should provides table, column, value arguments(>=3)'
redis.log(level, m)
return {-1, m}
end

local t = def_table(ARGV[1])
Expand Down
5 changes: 5 additions & 0 deletions src/redisql/redis.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
:insert ""
:select ""
:describe ""
:columns ""
:test ""}))

(def ^:dynamic ^:private ^JedisPool *pool* (atom nil))
Expand Down Expand Up @@ -189,3 +190,7 @@
(evalsha l nil)
(let [t1 (norm (first t))]
(evalsha l nil t1))))))

(defn columns
[t]
(evalsha (:columns @*lua*) nil (norm t)))

0 comments on commit cbeccbb

Please sign in to comment.