/
SQL.apl
161 lines (140 loc) · 4.28 KB
/
SQL.apl
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
#!apl -f
⍝ This file contains helper definititions to make the SQL API more easy
⍝ to use. Example use:
⍝
⍝ db ← 'sqlite' SQL∆Connect '/path/to/db/file'
⍝
⍝ Connects to the database of type X with connect arguments Y.
⍝ This call returns the ID of the connection.
⍝
⍝ results ← 'select * from foo where a > ?' SQL∆Select[db] 100
⍝
⍝ Executes the query given in X with the bind parameters in Y.
⍝
⍝ SQL∆Disconnect db
⍝
⍝ Disconnects from the database connection given as X.
∇Z←type SQL∆Connect arg
⍝⍝ Connect to database of type L using connection arguments R.
⍝⍝
⍝⍝ L must be a string indicating the database type. Current supported
⍝⍝ values are 'postgresql' and 'sqlite'.
⍝⍝
⍝⍝ R is the connection parameters which depends on the type of
⍝⍝ database:
⍝⍝
⍝⍝ - For type≡'sqlite': the argument is string pointing to the
⍝⍝ database file.
⍝⍝
⍝⍝ - For type≡'postgresql', the argument is a standard connect string
⍝⍝ as described in the PostgreSQL documentation.
⍝⍝
⍝⍝ This function returns a database handle that should be used when
⍝⍝ using other SQL functions. This value should be seen as an opaque
⍝⍝ handle. It is, however, guaranteed that the handle is a scalar
⍝⍝ value.
Z←type SQL[1] arg
∇
∇Z←SQL∆Disconnect db
⍝⍝ Disconnect from database R.
⍝⍝
⍝⍝ R is the database handle that should be disconnected. After this
⍝⍝ function has been called, no further operations are to be performed
⍝⍝ on this handle. Future calls to SQL∆Connect may reuse previously
⍝⍝ disconnected handles.
Z←SQL[2] db
∇
∇Z←statement SQL∆Select[db] args
⍝⍝ Execute a select statement and return the result table.
⍝⍝
⍝⍝ The axis parameter indicates the database handle.
⍝⍝
⍝⍝ L is a select statement to be executed. Positional parameters can be
⍝⍝ supplied by specifying a question mark "?" in the statemement.
⍝⍝
⍝⍝ R is an array containing the values for the positional parameters.
⍝⍝ If the array is of rank 2, the statement will be executed multiple
⍝⍝ times with each row being the values for each call.
⍝⍝
⍝⍝ The return value is a rank-2 array representing the result of the
⍝⍝ select statement. Null values are returned as ⍬ and empty strings
⍝⍝ are returned as ''.
Z←statement SQL[3,db] args
∇
∇Z←statement SQL∆Exec[db] args
⍝⍝ Execute an SQL statement that does not return a result.
⍝⍝
⍝⍝ This function is identical to SQL∆Select with the exception that it
⍝⍝ is used on statements which do not return a result table.
Z←statement SQL[4,db] args
∇
∇Z←SQL∆Begin db
⍝⍝ Begin a transaction.
Z←SQL[5] db
∇
∇Z←SQL∆Commit db
⍝⍝ Commit a transaction.
Z←SQL[6] db
∇
∇Z←SQL∆Rollback db
⍝⍝ Rolls back the current transaction.
Z←SQL[7] db
∇
∇Z←SQL∆Tables db
⍝⍝ Return an array containing the name of all tables.
Z←SQL[8] db
∇
∇Z←db SQL∆Columns table
⍝⍝ Return an array containing information about the columns in the
⍝⍝ given table. Currently, the column layout is as follows:
⍝⍝
⍝⍝ Name
⍝⍝ Type
⍝⍝
⍝⍝ More columns containing extra information may be added in a future
⍝⍝ release.
Z←db SQL[9] table
∇
∇Z←X (F SQL∆WithTransaction FINDDB) Y;result
SQL∆Begin FINDDB
→(0≠⎕NC 'X')/dyadic
result ← '→rollback' ⎕EA 'F Y'
→commit
dyadic:
result ← '→rollback' ⎕EA 'X F Y'
commit:
SQL∆Commit FINDDB
Z ← result
→end
rollback:
SQL∆Rollback FINDDB
⎕ES "Transaction rolled back"
end:
∇
⍝
⍝ Metadata for library
⍝
∇Z←SQL⍙metadata
Z ← ,[0.5] 'Author' 'Elias Mårtenson'
Z ← Z,[1] 'BugEmail' 'bug-apl@gnu.org'
Z ← Z,[1] 'Documentation' ''
Z ← Z,[1] 'Download' 'https://github.com/lokedhs/apl-sqlite'
Z ← Z,[1] 'License' 'LGPL'
Z ← Z,[1] 'Portability' 'L3'
Z ← Z,[1] 'Provides' 'SQL'
Z ← Z,[1] 'Requires' ''
Z ← Z,[1] 'Version' '1.0'
∇
⍝
⍝ Load the native library
⍝
∇sql⍙load_library;result
→(0≠⎕NC 'SQL')/skip
result ← 'lib_sql' ⎕FX 'SQL'
→('SQL'≡result)/skip
⎕ES 'Error loading native library'
skip:
∇
sql⍙load_library
)erase sql⍙load_library
⎕←'SQL lib loaded'