-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparql-query.c
129 lines (120 loc) · 2.68 KB
/
sparql-query.c
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
/* SPARQL client library: simple command-line query tool
*
* Author: Mo McRoberts <[email protected]>
*
* Copyright (c) 2014-2017 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include "libsparqlclient.h"
static char *node_to_string(SPARQL *sparql, librdf_node *node);
void
logger(int priority, const char *format, va_list ap)
{
if(priority > LOG_INFO)
{
return;
}
fprintf(stderr, "SPARQL<%d>: ", priority);
vfprintf(stderr, format, ap);
}
int
main(int argc, char **argv)
{
SPARQL *sparql;
SPARQLRES *result;
SPARQLROW *row;
size_t index, count;
librdf_node *node;
if(argc != 3)
{
fprintf(stderr, "Usage: %s URI QUERY\n", argv[0]);
return 1;
}
sparql = sparql_create(argv[1]);
sparql_set_logger(sparql, logger);
sparql_set_verbose(sparql, 1);
result = sparql_query(sparql, argv[2], strlen(argv[2]));
if(!result)
{
fprintf(stderr, "query failed\n");
sparql_destroy(sparql);
return 1;
}
if(sparqlres_is_boolean(result))
{
if(sparqlres_boolean(result))
{
puts("true");
}
else
{
puts("false");
}
sparqlres_destroy(result);
sparql_destroy(sparql);
return 0;
}
count = sparqlres_variables(result);
for(row = sparqlres_next(result); row; row = sparqlres_next(result))
{
for(index = 0; index < count; index++)
{
node = sparqlrow_binding(row, index);
if(node)
{
printf("%s: %s\n", sparqlres_variable(result, index), node_to_string(sparql, node));
}
else
{
printf("%s: NULL\n", sparqlres_variable(result, index));
}
}
printf("\n");
}
sparqlres_destroy(result);
sparql_destroy(sparql);
return 0;
}
static char *
node_to_string(SPARQL *sparql, librdf_node *node)
{
raptor_world *world;
raptor_iostream *stream;
char *buf;
int r;
world = librdf_world_get_raptor(sparql_world(sparql));
buf = NULL;
stream = raptor_new_iostream_to_string(world, (void **) &buf, NULL, malloc);
if(!stream)
{
return NULL;
}
r = librdf_node_write(node, stream);
raptor_free_iostream(stream);
if(r)
{
free(buf);
return NULL;
}
return buf;
}