summaryrefslogtreecommitdiff
path: root/Redland/LowLevel.hs
blob: 75730a64205d2ddb1f171a677e5859c46aeb07cd (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
{- |
Module      :  Redland.LowLevel
Maintainer  :  defanor <defanor@uberspace.net>
Stability   :  unstable
Portability :  non-portable (GHC extensions are used)

Low-level <http://librdf.org/docs/api/index.html Redland RDF library>
bindings, a straightforward translation.

-}

{-# LANGUAGE ForeignFunctionInterface #-}

module Redland.LowLevel where

import Foreign
import Foreign.C


-- * World

data RedlandWorld

foreign import ccall "librdf_new_world"
  librdf_new_world :: IO (Ptr RedlandWorld)
foreign import ccall "librdf_free_world"
  librdf_free_world :: Ptr RedlandWorld -> IO ()
foreign import ccall "redland.h &librdf_free_world"
  p_librdf_free_world :: FinalizerPtr RedlandWorld

foreign import ccall "librdf_world_open"
  librdf_world_open :: Ptr RedlandWorld -> IO ()


-- * Hashes

data RedlandHash

foreign import ccall "librdf_new_hash"
  librdf_new_hash
  :: Ptr RedlandWorld
  -- ^ world
  -> CString
  -- ^ name
  -> IO (Ptr RedlandHash)
foreign import ccall "librdf_free_hash"
  librdf_free_hash :: Ptr RedlandHash -> IO ()
foreign import ccall "redland.h &librdf_free_hash"
  p_librdf_free_hash :: FinalizerPtr RedlandHash

foreign import ccall "librdf_hash_put_strings"
  librdf_hash_put_strings
  :: Ptr RedlandHash
  -- ^ hash
  -> CString
  -- ^ key
  -> CString
  -- ^ value
  -> IO CInt
  -- ^ non-zero on failure
foreign import ccall "librdf_hash_get"
  librdf_hash_get
  :: Ptr RedlandHash
  -- ^ hash
  -> CString
  -- ^ key
  -> IO CString
  -- ^ must be freed by the caller
foreign import ccall "librdf_hash_get_del"
  librdf_hash_get_del
  :: Ptr RedlandHash
  -- ^ hash
  -> CString
  -- ^ key
  -> IO CString
  -- ^ must be freed by the caller


-- * RDF Graph (librdf_model)

data RedlandModel

foreign import ccall "librdf_new_model"
  librdf_new_model
  :: Ptr RedlandWorld
  -- ^ world
  -> Ptr RedlandStorage
  -- ^ storage
  -> CString
  -- ^ options
  -> IO (Ptr RedlandModel)
foreign import ccall "librdf_free_model"
  librdf_free_model :: Ptr RedlandModel -> IO ()
foreign import ccall "redland.h &librdf_free_model"
  p_librdf_free_model :: FinalizerPtr RedlandModel

foreign import ccall "librdf_model_query_execute"
  librdf_model_query_execute
  :: Ptr RedlandModel
  -- ^ model
  -> Ptr RedlandQuery
  -- ^ query
  -> IO (Ptr RedlandQueryResults)
  -- ^ NULL on failure
foreign import ccall "librdf_model_sync"
  librdf_model_sync :: Ptr RedlandModel -> IO CInt
foreign import ccall "librdf_model_load"
  librdf_model_load
  :: Ptr RedlandModel
  -> Ptr RedlandURI
  -- ^ source URI
  -> CString
  -- ^ parser name, can be NULL
  -> CString
  -- ^ MIME type, can be NULL
  -> Ptr RedlandURI
  -- ^ type URI, can be NULL
  -> IO CInt

foreign import ccall "librdf_model_find_statements"
  librdf_model_find_statements
  :: Ptr RedlandModel
  -> Ptr RedlandStatement
  -> IO (Ptr RedlandStream)


-- * RDF term (librdf_node)

data RedlandNode

foreign import ccall "librdf_new_node"
  librdf_new_node :: Ptr RedlandWorld -> IO (Ptr RedlandNode)
foreign import ccall "librdf_free_node"
  librdf_free_node :: Ptr RedlandNode -> IO ()
foreign import ccall "redland.h &librdf_free_node"
  p_librdf_free_node :: FinalizerPtr RedlandNode

foreign import ccall "librdf_new_node_from_node"
  librdf_new_node_from_node
  :: Ptr RedlandNode
  -- ^ old node
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_new_node_from_blank_identifier"
  librdf_new_node_from_blank_identifier
  :: Ptr RedlandWorld
  -> CString
  -- ^ blank node identifier, can be NULL
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_new_node_from_literal"
  librdf_new_node_from_literal
  :: Ptr RedlandWorld
  -> CString
  -- ^ string value
  -> CString
  -- ^ literal XML language, can be NULL
  -> CInt
  -- ^ non-zero if literal is XML
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_new_node_from_typed_literal"
  librdf_new_node_from_typed_literal
  :: Ptr RedlandWorld
  -> CString
  -- ^ string value
  -> CString
  -- ^ literal XML language, can be NULL
  -> Ptr RedlandURI
  -- ^ typed literal datatype URI, can be NULL
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_new_node_from_uri"
  librdf_new_node_from_uri
  :: Ptr RedlandWorld
  -> Ptr RedlandURI
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_new_node_from_uri_string"
  librdf_new_node_from_uri_string
  :: Ptr RedlandWorld
  -> CString
  -> IO (Ptr RedlandNode)

foreign import ccall "librdf_node_get_blank_identifier"
  librdf_node_get_blank_identifier
  :: Ptr RedlandNode
  -> IO CString
  -- ^ identifier
foreign import ccall "librdf_node_get_literal_value"
  librdf_node_get_literal_value
  :: Ptr RedlandNode
  -> IO CString
  -- ^ Literal value, must be copied
foreign import ccall "librdf_node_get_literal_value_language"
  librdf_node_get_literal_value_language
  :: Ptr RedlandNode
  -> IO CString
  -- ^ Literal language value, must be copied
foreign import ccall "librdf_node_get_literal_value_datatype_uri"
  librdf_node_get_literal_value_datatype_uri
  :: Ptr RedlandNode
  -> IO (Ptr RedlandURI)
  -- ^ Literal datatype URI, must be copied
foreign import ccall "librdf_node_get_literal_value_is_wf_xml"
  librdf_node_get_literal_value_is_wf_xml
  :: Ptr RedlandNode
  -> IO CInt
  -- ^ 0 if it's not well formed XML
foreign import ccall "librdf_node_get_uri"
  librdf_node_get_uri
  :: Ptr RedlandNode
  -> IO (Ptr RedlandURI)
  -- ^ URI, must be copied
foreign import ccall "librdf_node_is_blank"
  librdf_node_is_blank :: Ptr RedlandNode -> IO CInt
foreign import ccall "librdf_node_is_literal"
  librdf_node_is_literal :: Ptr RedlandNode -> IO CInt
foreign import ccall "librdf_node_is_resource"
  librdf_node_is_resource :: Ptr RedlandNode -> IO CInt
foreign import ccall "librdf_node_to_string"
  librdf_node_to_string :: Ptr RedlandNode -> IO CString


-- * Parsers

data RedlandParser

foreign import ccall "librdf_new_parser"
  librdf_new_parser
  :: Ptr RedlandWorld
  -- ^ world
  -> CString
  -- ^ name
  -> CString
  -- ^ MIME type
  -> Ptr RedlandURI
  -- ^ type URI
  -> IO (Ptr RedlandParser)
foreign import ccall "librdf_free_parser"
  librdf_free_parser :: Ptr RedlandParser -> IO ()
foreign import ccall "redland.h &librdf_free_parser"
  p_librdf_free_parser :: FinalizerPtr RedlandParser

foreign import ccall "librdf_parser_parse_string_into_model"
  librdf_parser_parse_string_into_model
  :: Ptr RedlandParser
  -- ^ parser
  -> CString
  -- ^ string to parse
  -> Ptr RedlandURI
  -- ^ base URI
  -> Ptr RedlandModel
  -- ^ model
  -> IO CInt
  -- ^ non-zero on failure

-- librdf_parser_guess_name always fails, skipping it

foreign import ccall "librdf_parser_guess_name2"
  librdf_parser_guess_name2
  :: Ptr RedlandWorld
  -> CString
  -- ^ MIME type or NULL
  -> CString
  -- ^ content buffer or NULL
  -> CString
  -- ^ content identifier or NULL
  -> IO CString
  -- ^ parser name or NULL


-- * Querying

data RedlandQuery

foreign import ccall "librdf_new_query"
  librdf_new_query
  :: Ptr RedlandWorld
  -> CString
  -- ^ language name
  -> Ptr RedlandURI
  -- ^ language URI (can be NULL)
  -> CString
  -- ^ query string
  -> Ptr RedlandURI
  -- ^ base URI (can be NULL)
  -> IO (Ptr RedlandQuery)
foreign import ccall "librdf_free_query"
  librdf_free_query :: Ptr RedlandQuery -> IO ()
foreign import ccall "redland.h &librdf_free_query"
  p_librdf_free_query :: FinalizerPtr RedlandQuery

foreign import ccall "librdf_query_execute"
  librdf_query_execute
  :: Ptr RedlandQuery
  -- ^ query
  -> Ptr RedlandModel
  -- ^ model
  -> IO (Ptr RedlandQueryResults)
  -- ^ NULL on failure


-- * Query results

data RedlandQueryResults

foreign import ccall "librdf_free_query_results"
  librdf_free_query_results :: Ptr RedlandQueryResults -> IO ()
foreign import ccall "redland.h &librdf_free_query_results"
  p_librdf_free_query_results :: FinalizerPtr RedlandQueryResults

foreign import ccall "librdf_query_results_get_count"
  librdf_query_results_get_count :: Ptr RedlandQueryResults -> IO CInt
foreign import ccall "librdf_query_results_next"
  librdf_query_results_next :: Ptr RedlandQueryResults -> IO CInt
foreign import ccall "librdf_query_results_finished"
  librdf_query_results_finished :: Ptr RedlandQueryResults -> IO CInt
foreign import ccall "librdf_query_results_get_binding_value"
  librdf_query_results_get_binding_value
  :: Ptr RedlandQueryResults
  -- ^ results
  -> CInt
  -- ^ offset
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_query_results_get_binding_name"
  librdf_query_results_get_binding_name
  :: Ptr RedlandQueryResults
  -- ^ results
  -> CInt
  -- ^ offset
  -> IO CString
foreign import ccall "librdf_query_results_get_binding_value_by_name"
  librdf_query_results_get_binding_value_by_name
  :: Ptr RedlandQueryResults
  -- ^ results
  -> CString
  -- ^ variable name
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_query_results_get_bindings_count"
  librdf_query_results_get_bindings_count :: Ptr RedlandQueryResults -> IO CInt


-- * RDF Triple (librdf_statement)

data RedlandStatement

foreign import ccall "librdf_new_statement"
  librdf_new_statement :: Ptr RedlandWorld -> IO (Ptr RedlandStatement)
foreign import ccall "librdf_free_statement"
  librdf_free_statement :: Ptr RedlandStatement -> IO ()
foreign import ccall "redland.h &librdf_free_statement"
  p_librdf_free_statement :: FinalizerPtr RedlandStatement

-- | "The node objects become owned by the new statement (or freed on
-- error)."
foreign import ccall "librdf_new_statement_from_nodes"
  librdf_new_statement_from_nodes
  :: Ptr RedlandWorld
  -> Ptr RedlandNode
  -> Ptr RedlandNode
  -> Ptr RedlandNode
  -> IO (Ptr RedlandStatement)
foreign import ccall "librdf_new_statement_from_statement"
  librdf_new_statement_from_statement
  :: Ptr RedlandStatement
  -> IO (Ptr RedlandStatement)

foreign import ccall "librdf_statement_get_subject"
  librdf_statement_get_subject
  :: Ptr RedlandStatement
  -> IO (Ptr RedlandNode)
  -- ^ the returned node is shared, should be copied
foreign import ccall "librdf_statement_set_subject"
  librdf_statement_set_subject
  :: Ptr RedlandStatement
  -> Ptr RedlandNode
  -- ^ becomes owned by the statement
  -> IO ()
foreign import ccall "librdf_statement_get_predicate"
  librdf_statement_get_predicate
  :: Ptr RedlandStatement
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_statement_set_predicate"
  librdf_statement_set_predicate
  :: Ptr RedlandStatement
  -> Ptr RedlandNode
  -> IO ()
foreign import ccall "librdf_statement_get_object"
  librdf_statement_get_object
  :: Ptr RedlandStatement
  -> IO (Ptr RedlandNode)
foreign import ccall "librdf_statement_set_object"
  librdf_statement_set_object
  :: Ptr RedlandStatement
  -> Ptr RedlandNode
  -> IO ()


-- * Triple stores

data RedlandStorage

foreign import ccall "librdf_new_storage"
  librdf_new_storage
  :: Ptr RedlandWorld
  -- ^ world
  -> CString
  -- ^ storage type name (e.g., "hashes")
  -> CString
  -- ^ storage identifier
  -> CString
  -- ^ options
  -> IO (Ptr RedlandStorage)
foreign import ccall "librdf_free_storage"
  librdf_free_storage :: Ptr RedlandStorage -> IO ()
foreign import ccall "redland.h &librdf_free_storage"
  p_librdf_free_storage :: FinalizerPtr RedlandStorage

foreign import ccall "librdf_new_storage_with_options"
  librdf_new_storage_with_options
  :: Ptr RedlandWorld
  -- ^ world
  -> CString
  -- ^ storage type name (e.g., "hashes")
  -> CString
  -- ^ storage identifier
  -> Ptr RedlandHash
  -- ^ options
  -> IO (Ptr RedlandStorage)


-- * Stream of triples (librdf_statement)

data RedlandStream

foreign import ccall "librdf_free_stream"
  librdf_free_stream :: Ptr RedlandStream -> IO ()
foreign import ccall "redland.h &librdf_free_stream"
  p_librdf_free_stream :: FinalizerPtr RedlandStream

foreign import ccall "librdf_stream_end"
  librdf_stream_end :: Ptr RedlandStream -> IO CInt
foreign import ccall "librdf_stream_next"
  librdf_stream_next :: Ptr RedlandStream -> IO CInt
foreign import ccall "librdf_stream_get_object"
  librdf_stream_get_object
  :: Ptr RedlandStream
  -> IO (Ptr RedlandStatement)
  -- ^ a shared statement, should be copied


-- * URI

data RedlandURI

foreign import ccall "librdf_new_uri"
  librdf_new_uri
  :: Ptr RedlandWorld
  -- ^ world
  -> CString
  -- ^ URI string
  -> IO (Ptr RedlandURI)
foreign import ccall "librdf_free_uri"
  librdf_free_uri :: Ptr RedlandURI -> IO ()
foreign import ccall "redland.h &librdf_free_uri"
  p_librdf_free_uri :: FinalizerPtr RedlandURI

foreign import ccall "librdf_new_uri_from_uri"
  librdf_new_uri_from_uri
  :: Ptr RedlandURI
  -- ^ old URI
  -> IO (Ptr RedlandURI)
foreign import ccall "librdf_uri_as_string"
  librdf_uri_as_string :: Ptr RedlandURI -> IO CString