-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_class.applescript
More file actions
155 lines (137 loc) · 5.29 KB
/
sqlite_class.applescript
File metadata and controls
155 lines (137 loc) · 5.29 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
(*
Example Usage:
# Change the path to location of this file
set path_to_this_file to ((path to desktop as string) & "sqlite.scpt")
set sql_class_script to load script file path_to_this_file
set SQLite to sql_class_script's SQLite
set SQLite's FOLDER_NAME to "base_folder"
set SQLite's DATABASE_NAME to "data_base"
SQLite's createBaseFolder()
SQLite's setHead()
SQLite's create_table("people", {"name", "age"})
SQLite's insert("people", {"Tom", "44"})
SQLite's insert("people", {"Annie", "34"})
set theValues to SQLite's select_({"name", "age"}, "people", "name", "Annie")
*)
script SQLite
property SUPPORT_FOLDER : missing value
property FOLDER_NAME : missing value
property DATABASE_NAME : missing value
property FILE_PATH : missing value
property HEAD : missing value
property TAIL : quote
#==============================================================
# Setup
#==============================================================
on createBaseFolder()
set SUPPORT_FOLDER to (path to application support from user domain)
tell application "Finder"
set folder_path to (SUPPORT_FOLDER & FOLDER_NAME) as string
if not (exists folder folder_path) then
make folder at SUPPORT_FOLDER with properties {name:FOLDER_NAME}
end if
end tell
end createBaseFolder
on setHead()
set f_path to SUPPORT_FOLDER & FOLDER_NAME & ":" & DATABASE_NAME & ".db" as string
set FILE_PATH to quoted form of POSIX path of f_path
set file_location to space & FILE_PATH & space
set HEAD to "sqlite3" & file_location & quote
end setHead
#==============================================================
# Functions
#==============================================================
on create_table(table_name, column_names_array)
set column_names_string to my commaSepQuotedString(column_names_array)
set statement to "create table if not exists " & table_name & "(" & column_names_string & "); "
executeSQL(statement)
end create_table
on insert(table_name, the_values)
try
set the_values to my commaSepQuotedString(the_values)
set statement to "insert into " & table_name & " values(" & the_values & "); "
executeSQL(statement)
on error e
display dialog "There was an error while inserting." & return & e
end try
end insert
on update(table_name, the_fields, the_values, search_field, search_value)
repeat with i from 1 to count of the_fields
set this_item to item i of the_fields
set statement to ("UPDATE " & table_name & " set " & this_item & " = '" & ¬
item i of the_values & "' WHERE " & search_field & " = '" & search_value & "'; " as string)
executeSQL(statement)
end repeat
end update
on addColumn(table_name, col_name)
set statement to ("ALTER table " & table_name & " add " & col_name & "; " as string)
executeSQL(statement)
end addColumn
on select_(column_names_array, table_name, search_field, search_value)
set column_names_string to my commaSepString(column_names_array)
set statement to ("SELECT " & column_names_string & " FROM " & table_name & ¬
" WHERE " & search_field & " = '" & search_value & "'; " as string)
return executeSQL(statement)
end select_
on select_all(table_name)
set statement to ("SELECT * FROM " & table_name & " ; " as string)
set execute to (executeSQL(statement))
return my tidStuff(return, execute)
end select_all
on select_all_where(table_name, search_field, search_value)
set statement to ("SELECT * FROM " & table_name & " WHERE " & ¬
search_field & " = " & search_value & " ; " as string)
set execute to (executeSQL(statement))
return my tidStuff(return, execute)
end select_all_where
on delete_where(table_name, search_field, search_value)
set statement to ("DELETE FROM " & table_name & " WHERE " & ¬
search_field & " = " & search_value & " ; " as string)
executeSQL(statement)
end delete_where
on delete_every_row(table_name)
set statement to ("DELETE FROM " & table_name & "; " as string)
executeSQL(statement)
end delete_every_row
on delete_table(table_name)
set statement to ("DELETE " & table_name & "; " as string)
executeSQL(statement)
end delete_table
#==============================================================
# Private
#==============================================================
on executeSQL(statement)
return (do shell script HEAD & statement & TAIL)
end executeSQL
on tidStuff(paramHere, textHere)
set OLDtid to AppleScript's text item delimiters
set AppleScript's text item delimiters to paramHere
set theItems to text items of textHere
set AppleScript's text item delimiters to OLDtid
return theItems
end tidStuff
on commaSepQuotedString(the_array)
set return_string to ""
if length of the_array > 1 then
repeat with i from 1 to count of the_array
set this_item to item i of the_array
set return_string to return_string & "'" & this_item & "', "
end repeat
return text 1 thru -3 of return_string as string
else
return item 1 of the_array
end if
end commaSepQuotedString
on commaSepString(the_array)
set return_string to ""
if length of the_array > 1 then
repeat with i from 1 to count of the_array
set this_item to item i of the_array
set return_string to return_string & this_item & ", "
end repeat
return text 1 thru -3 of return_string as string
else
return item 1 of the_array
end if
end commaSepString
end script