- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_class.applescript
More file actions
Latest commit
155 lines (137 loc) · 5.29 KB
/
Copy pathsqlite_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")
*)
scriptSQLite
propertySUPPORT_FOLDER : missing value
propertyFOLDER_NAME : missing value
propertyDATABASE_NAME : missing value
propertyFILE_PATH : missing value
propertyHEAD : missing value
propertyTAIL : quote
#==============================================================
# Setup
#==============================================================
oncreateBaseFolder()
setSUPPORT_FOLDERto (path toapplication support from user domain)
tellapplication"Finder"
setfolder_pathto (SUPPORT_FOLDER & FOLDER_NAME) asstring
ifnot (exists folder folder_path) then
make folder at SUPPORT_FOLDER withproperties {name:FOLDER_NAME}
end if
end tell
end createBaseFolder
onsetHead()
setf_pathto SUPPORT_FOLDER & FOLDER_NAME &":"& DATABASE_NAME &".db"asstring
setFILE_PATHtoquoted formofPOSIX pathof f_path
setfile_locationtospace& FILE_PATH &space
setHEADto"sqlite3"& file_location "e
end setHead
#==============================================================
# Functions
#==============================================================
oncreate_table(table_name, column_names_array)
setcolumn_names_stringtomy commaSepQuotedString(column_names_array)
setstatementto"create table if not exists "& table_name &"("& column_names_string &"); "
executeSQL(statement)
end create_table
oninsert(table_name, the_values)
try
setthe_valuestomy commaSepQuotedString(the_values)
setstatementto"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
onupdate(table_name, the_fields, the_values, search_field, search_value)
repeatwithifrom1tocountof the_fields
setthis_itemtoitem i of the_fields
setstatementto ("UPDATE "& table_name &" set "& this_item &" = '"& ¬
item i of the_values &"' WHERE "& search_field &" = '"& search_value &"'; "asstring)
executeSQL(statement)
end repeat
end update
onaddColumn(table_name, col_name)
setstatementto ("ALTER table "& table_name &" add "& col_name &"; "asstring)
executeSQL(statement)
end addColumn
onselect_(column_names_array, table_name, search_field, search_value)
setcolumn_names_stringtomy commaSepString(column_names_array)
setstatementto ("SELECT "& column_names_string &" FROM "& table_name & ¬
" WHERE "& search_field &" = '"& search_value &"'; "asstring)
return executeSQL(statement)
end select_
onselect_all(table_name)
setstatementto ("SELECT * FROM "& table_name &" ; "asstring)
setexecuteto (executeSQL(statement))
returnmy tidStuff(return, execute)
end select_all
onselect_all_where(table_name, search_field, search_value)
setstatementto ("SELECT * FROM "& table_name &" WHERE "& ¬
search_field &" = "& search_value &" ; "asstring)
setexecuteto (executeSQL(statement))
returnmy tidStuff(return, execute)
end select_all_where
ondelete_where(table_name, search_field, search_value)
setstatementto ("DELETE FROM "& table_name &" WHERE "& ¬
search_field &" = "& search_value &" ; "asstring)
executeSQL(statement)
end delete_where
ondelete_every_row(table_name)
setstatementto ("DELETE FROM "& table_name &"; "asstring)
executeSQL(statement)
end delete_every_row
ondelete_table(table_name)
setstatementto ("DELETE "& table_name &"; "asstring)
executeSQL(statement)
end delete_table
#==============================================================
# Private
#==============================================================
onexecuteSQL(statement)
return (do shell script HEAD & statement & TAIL)
end executeSQL
ontidStuff(paramHere, textHere)
setOLDtidtoAppleScript's text item delimiters
setAppleScript's text item delimitersto paramHere
settheItemstotextitemsof textHere
setAppleScript's text item delimitersto OLDtid
return theItems
end tidStuff
oncommaSepQuotedString(the_array)
setreturn_stringto""
iflengthof the_array >1then
repeatwithifrom1tocountof the_array
setthis_itemtoitem i of the_array
setreturn_stringto return_string &"'"& this_item &"', "
end repeat
returntext1thru-3of return_string asstring
else
returnitem1of the_array
end if
end commaSepQuotedString
oncommaSepString(the_array)
setreturn_stringto""
iflengthof the_array >1then
repeatwithifrom1tocountof the_array
setthis_itemtoitem i of the_array
setreturn_stringto return_string & this_item &", "
end repeat
returntext1thru-3of return_string asstring
else
returnitem1of the_array
end if
end commaSepString
end script