-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.func
102 lines (74 loc) · 2.93 KB
/
template.func
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
" Awesome Function
"
function! FunSqlCommand(x, bang, comment, copy, run)
normal! mx
if search('^--CWM--', 'ncw')
silent exec ':%s/^--CWM--//'
endif
if a:bang != 1 && a:x != 0
0
normal! my
normal! mz
" find bottom (z)
if search('WHERE TemplateID = '. a:x .';', 'sec')
exec 'normal! mz'
" find top (y)
if search('UPDATE Template SET', 'b')
exec 'normal! my'
if a:copy == 1 || a:run == 1
silent exec "normal! y'z"
" copy to clip
if a:copy == 1
call CygwinClip()
endif
" run
if a:run > 0
let currdir = escape(substitute(getcwd(),'\','/','ge'),"|[]*'\" #")
let initdir = currdir.'/.sql'
if !isdirectory(initdir)
call mkdir(initdir)
endif
let currfile = initdir.'/TemplateID_'. a:x .'.sql'
if a:run == 2
" let's run it
call writefile( split(substitute(@@, "\n", "\r\n", "g"), "\x0A", 1), l:currfile, 'b')
exec ':! /sql-template.bat ' . l:currfile
else
" create temp file (current copy in db)
let tempfile = currfile.'.tmp'
let xtemp = system('/sql-template.bat '. l:tempfile .' '. l:tempfile .' "SELECT TemplateBody FROM Template WHERE TemplateID = '. a:x .';" && sed -i "s/\x27/\x27\x27/g" '. l:tempfile ." && sed -i '$d' ". l:tempfile ." && sed -e :a -e '/^\\n*$/{$d;N;ba' -e '}' -i ". l:tempfile .' && sed -i "1s/^/UPDATE Template SET TemplateBody =\x0A\x27/" '. l:tempfile ." && sed -i '$s/$/\\x27\\x0AWHERE TemplateID = ". a:x .";/' ". l:tempfile )
if filereadable(currfile)
let diff = system('diff -uw ' . l:currfile .' '. l:tempfile)
if diff == ""
" not different let's run it
call writefile( split(substitute(@@, "\n", "\r\n", "g"), "\x0A", 1), l:currfile, 'b')
exec ':! /sql-template.bat ' . l:currfile
else
echo diff
endif
else
silent exec ":! mv ". l:tempfile .' '. l:currfile
echo "Created initial file. Re-run to update database"
endif
" drop temp file
silent exec ":! rm ". l:tempfile
endif
endif
endif
" comment
if a:comment == 1
silent exec ':%s/^/--CWM--/'
silent exec ":'y,'zs/^--CWM--//"
endif
exec "normal! 'y0"
endif
endif
endif
exec "normal! 'x0"
endfunction
command! -nargs=1 -bang SqlCom call FunSqlCommand(<args>,0+<bang>0,1,0,0)
command! -nargs=1 -bang SqlCopy call FunSqlCommand(<args>,0+<bang>0,0,1,0)
command! -nargs=1 -bang SqlRun call FunSqlCommand(<args>,0+<bang>0,0,1,1)
command! -nargs=1 -bang SqlRunX call FunSqlCommand(<args>,0+<bang>0,0,1,2)
" Testing line
" WHERE TemplateID = 82;