-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLTable.lua
executable file
·188 lines (173 loc) · 5.28 KB
/
SQLTable.lua
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
--[[
@file SQLTable.lua
@brief Main part of 'SQLTable.lrplugin'
@note This plugin outputs file that SQL Server specified.
@author @remov_b4_flight
]]
local PluginTitle = 'SQLTable'
local LrApplication = import 'LrApplication'
local LrTasks = import 'LrTasks'
local LrProgress = import 'LrProgressScope'
local LrErrors = import 'LrErrors'
local LrPathUtils = import 'LrPathUtils'
local LrDialogs = import 'LrDialogs'
--local LrLogger = import 'LrLogger'
--local Logger = LrLogger(PluginTitle)
--Logger:enable('logfile')
-- Constants
DELM = ';'
FORMATTED = 1
RAW = 2
VIRTUAL = 3
-- Start of customizable part.
-- Define table name of SQL
local TABLE = 'photos'
-- Define matadata specs what you want to export to SQL script.
local metadefs = {
dateTime = {type = 'datetime', source = FORMATTED},
caption = {type = 'nvarchar(64)', source = FORMATTED},
folderName = {type = 'nvarchar(64)', source = FORMATTED},
fileName = {type = 'nvarchar(64)', source = FORMATTED},
fileType = {type = 'nvarchar(32)', source = FORMATTED},
cameraModel = {type = 'nvarchar(64)', source = FORMATTED},
lens = {type = 'nvarchar(64)', source = FORMATTED},
rating = {type = 'decimal(1)', source = RAW},
-- subjectDistance = {type = 'decimal(4,1)', source = FORMATTED},
aperture = {type = 'decimal(3,1)', source = RAW},
shutterSpeed = {type = 'decimal(10,6)', source = RAW},
exposureBias = {type = 'decimal(4,2)', source = RAW},
isoSpeedRating = {type = 'decimal(6)', source = RAW},
focalLength35mm = {type = 'decimal(5,1)', source = RAW},
flash = {type = 'nvarchar(32)', source = FORMATTED},
fileSize = {type = 'decimal(10)', source = RAW},
uuid = {type = 'varchar(64)', source = RAW},
collections = {type = 'decimal(2)', source = VIRTUAL},
collectionSet = {type = 'nvarchar(64)', source = VIRTUAL},
collectionName = {type = 'nvarchar(64)', source = VIRTUAL},
}
-- Define re-create or truncate table
local CREATE = false
-- END of cutomizable part
-- Define path delimiter
if WIN_ENV then
PATHDELM = '¥'
else
PATHDELM = '/'
end
function getMetadata(It,key)
local meta = metadefs[key].source
local val
if (meta == VIRTUAL) then
if (key == 'collections') then
local c = It:getContainedCollections()
if (c ~= nil) then
val = #c
end
elseif(key == 'collectionSet') then
local c = It:getContainedCollections()
if (#c == 1) then
local parent = c[1]:getParent()
if (parent ~= nil) then
val = parent:getName()
end
end
elseif(key == 'collectionName') then
local c = It:getContainedCollections()
if (#c == 1) then
val = c[1]:getName()
end
end
elseif (meta == FORMATTED) then
val = It:getFormattedMetadata(key)
elseif (meta == RAW) then
val = It:getRawMetadata(key)
end
if (val == nil or string.len(val) == 0) then
val = 'NULL'
end
return val
end
function chop(str)
local strlen = string.len(str)
return string.sub(str,1,strlen - 1)
end
-- Start of Main part
-- Making up
local CurrentCatalog = LrApplication.activeCatalog()
-- Open output SQL script file
local FileBaseName = PluginTitle .. '_' ..TABLE .. '.sql'
local OutputFile = LrPathUtils.getStandardFilePath('home') .. PATHDELM
OutputFile = OutputFile .. FileBaseName
fp = io.open(OutputFile,"w")
if fp == nil then
LrErrors.throwUserError(message)
end
-- Drop table
local SQL = 'use lightroom;\n'
if (CREATE == true) then
SQL = SQL .. 'drop table ' .. TABLE .. ';\ngo\n'
else
SQL = SQL .. 'truncate table ' .. TABLE .. ';\ngo\n'
end
fp:write(SQL)
-- Build column list
local SQLCOL = '('
local SQLCOLTYP = '('
for key,val in pairs(metadefs) do
if (key == 'fileName' or key == 'dateTime' or key == 'fileSize') then
key = '[' .. key .. ']'
end
SQLCOLTYP = SQLCOLTYP .. key .. ' ' .. val.type .. ','
SQLCOL = SQLCOL .. key .. ','
end
SQLCOLTYP = chop(SQLCOLTYP)
SQLCOL = chop(SQLCOL)
SQLCOL = SQLCOL ..')'
if (CREATE == true) then
-- create table statement
SQL = 'create table ' .. TABLE .. SQLCOLTYP .. ');\n'
fp:write(SQL)
-- create index statement
SQL = 'create index cap on ' .. TABLE .. "(caption);\n"
fp:write(SQL)
else
SQL = 'truncate table ' .. TABLE .. ';\n'
end
-- Build 'insert' statement
INSERT = 'insert into ' .. TABLE .. SQLCOL
-- Main part of this plugin.
LrTasks.startAsyncTask( function ()
local ProgressBar = LrProgress(
{title = 'making "' .. FileBaseName ..'"' }
)
local SelectedPhotos = CurrentCatalog:getTargetPhotos()
local countPhotos = #SelectedPhotos
--loops photos in selected
for i,PhotoIt in ipairs(SelectedPhotos) do
SQLVAL = ' values('
for key,val in pairs(metadefs) do
local metadata = getMetadata(PhotoIt,key)
if ((string.find(val.type,'varchar') ~= nil or string.find(val.type,'datetime') ~= nil)
and metadata ~= 'NULL') then
metadata = string.gsub(metadata,'\'','\'\'')
SQLVAL = SQLVAL .. '\'' .. metadata .. '\','
elseif (key == 'shutterSpeed' and metadata ~= 'NULL') then
SQLVAL = SQLVAL .. 'round(' .. metadata .. ',6),'
elseif (key == 'exposureBias' and metadata ~= 'NULL') then
SQLVAL = SQLVAL .. 'round(' .. metadata .. ',2),'
else
SQLVAL = SQLVAL .. metadata .. ','
end
end
SQLVAL = chop(SQLVAL)
SQL = INSERT .. SQLVAL .. ');\n'
fp:write(SQL)
if ((i % 90) == 0 ) then
fp:write('go\n')
end
ProgressBar:setPortionComplete(i,countPhotos)
end --end of for photos loop
ProgressBar:done()
fp:close()
end ) --end of startAsyncTask function()
return