-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathactive_record.lua
More file actions
167 lines (148 loc) · 5.8 KB
/
active_record.lua
File metadata and controls
167 lines (148 loc) · 5.8 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- USAGE
--
-- Create a Lua file for your module. The file should look like this:
--
-- require 'ActiveRecord'
-- Product = ActiveRecord:subclass('ActiveRecord')
-- Product.tableName = 'products'
-- Product.tableFields = {
-- id = {type = 'integer', flags = {'primary_key', 'autoincrement', 'not_null'} },
-- name = {type = 'string', flags = {'not_null'} }
-- }
--
-- If the table does not yet exist, you can create it in your app initialization with this call:
--
-- orm.initialize()
-- Product.createTable()
--
-- Sample API calls
--
-- local products = Product.findAll
--
-- p = Product.new{id = 1, name = 'test', description = ''} (NOT YET IMPLEMENTED)
-- p.save
--
-- p.updateAttribute('name', 'newName')
-- p.updateAttributes{name = 'newName', description = 'newDescription'} (NOT YET IMPLEMENTED)
--
-- p = Product.find(1)
-- test_products = Product.where("name = 'test'")
--
-- numberOfProducts = Product.count()
--
require 'middleclass'
local orm = require 'orm'
local sql = require 'sql'
ActiveRecord = class('ActiveRecord')
------------------------------------------------------------------------------
-- CLASS (STATIC) METHODS - START
------------------------------------------------------------------------------
function ActiveRecord:initialize(newRecord)
for k,v in pairs(newRecord) do
self[k] = v
end
end
------------------------------------------------------------------------------
-- Returns the number of rows in the table
------------------------------------------------------------------------------
function ActiveRecord.static:count()
return orm.getTableRowCount(self.tableName)
end
------------------------------------------------------------------------------
-- Creates the table
-- TODO: If options.recreate = true, it drops the table if it already exists
------------------------------------------------------------------------------
function ActiveRecord.static:createTable(options)
local createSql = sql.generateCreateTable(self.tableName, self.tableFields)
db:exec( createSql )
end
------------------------------------------------------------------------------
-- Returns the record matching the given id. Returns nil if no match is found.
--
-- NOTE: Until I figure out how to determine the caller's class,
-- I'll have to resort to this ugliness of using the klass parameter
------------------------------------------------------------------------------
function ActiveRecord.static:find(klass, id)
local record = orm.selectOne(klass.tableName, 'id', id)
if not( record == nil ) then
result = klass:new(record)
end
return result
end
------------------------------------------------------------------------------
-- Returns all rows in the table that match the given filter
------------------------------------------------------------------------------
function ActiveRecord.static:findAll( klass, params )
local result = nil
if params == nil then
params = {}
end
if params.where == nil then
result = orm.selectAll( klass.tableName, params )
else
result = orm.selectWhere( klass.tableName, params )
end
return result
end
------------------------------------------------------------------------------
-- Updates all rows in the table that match the given filter
------------------------------------------------------------------------------
function ActiveRecord.static:updateAll( klass, updateSql, filter )
if filter == nil then
orm.updateAll( klass.tableName, updateSql )
else
orm.updateWhere( klass.tableName, updateSql, filter )
end
end
------------------------------------------------------------------------------
-- CLASS (STATIC) METHODS - END
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- INSTANCE METHODS - START
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- Reloads the record values from the database
------------------------------------------------------------------------------
function ActiveRecord:reload()
local updatedRecord = orm.selectOne( self.class.tableName, 'id', self.id )
if updatedRecord ~= nil then
for k,v in pairs(updatedRecord) do
self[k] = v
end
end
end
------------------------------------------------------------------------------
-- Saves the content of the object to the database.
-- If a matching record already exists in the database, an UPDATE is done.
-- Otherwise an INSERT is done.
------------------------------------------------------------------------------
function ActiveRecord:save()
local updateTable = {}
for k in pairs(self.class.tableFields) do
updateTable[k] = self[k]
end
orm.createOrUpdate( self.class.tableName, updateTable )
end
------------------------------------------------------------------------------
-- Updates one column value
------------------------------------------------------------------------------
function ActiveRecord:updateAttribute( columnName, columnValue )
local filter = "id = " .. self.id
orm.updateAttribute( self.class.tableName, filter, columnName, columnValue )
end
------------------------------------------------------------------------------
-- Updates an array of columns
------------------------------------------------------------------------------
function ActiveRecord:updateAttributes( updateTable )
local filter = "id = " .. self.id
local columns = {}
local columnValues = {}
for k,v in pairs(updateTable) do
table.insert( columns, k )
table.insert( columnValues, v )
end
orm.updateAttributes( self.class.tableName, filter, columns, columnValues )
end
------------------------------------------------------------------------------
-- INSTANCE METHODS - END
------------------------------------------------------------------------------