File: setup_tutorial.sql

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (349 lines) | stat: -rw-r--r-- 8,930 bytes parent folder | download
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
--  
--  $Id: setup_tutorial.sql,v 1.7.2.2 2011/07/20 09:50:18 source Exp $
--  
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--  
--  Copyright (C) 1998-2006 OpenLink Software
--  
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--  
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--  
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--  
--  

create procedure
sql_user_password (in name varchar)
{
  declare pass varchar;
  pass := NULL;
  whenever not found goto none;
  select pwd_magic_calc (U_NAME, U_PASSWORD, 1) into pass
      from SYS_USERS where U_NAME = name and U_SQL_ENABLE = 1 and U_IS_ROLE = 0;
none:
  return pass;
}
;

create procedure
sql_user_password_check (in name varchar, in pass varchar)
{
  if (exists (select 1 from SYS_USERS where U_NAME = name and U_SQL_ENABLE = 1 and U_IS_ROLE = 0 and
	pwd_magic_calc (U_NAME, U_PASSWORD, 1) = pass))
    return 1;
  return 0;
}
;

create procedure
demo_user_password (in name varchar)
{
	return pwd_magic_calc('demo','demo',1);
};

create procedure
demo_user_password_check (in name varchar, in pass varchar)
{
  return 1;
}
;

create procedure t_file_stat (in path varchar, in pdav int := null)
{
  declare ret any;
  ret := 0;

  if ( pdav is null) pdav := http_map_get ('is_dav');

  if (pdav)
    {
      ret := coalesce ((select cast (res_mod_time as varchar) from WS.WS.SYS_DAV_RES where RES_FULL_PATH = path), 0);
    }
  else
    {
      ret := file_stat (path);
    }
  return ret;
}
;

create procedure t_file_to_string (in path varchar, in pdav int := null)
{
  declare ret any;
  if ( pdav is null) pdav := http_map_get ('is_dav');

  if (pdav)
    {
      ret := (select blob_to_string (RES_CONTENT) from WS.WS.SYS_DAV_RES where RES_FULL_PATH = t_normalize_path(path));
    }
  else
    {
      ret := file_to_string (path);
    }
  return ret;
}
;

create procedure t_normalize_path (in path varchar)
{
  declare res varchar;
  res := path;
  
  while(regexp_match('\\.\\.',res)){
    res := regexp_replace(res,'/[^/.]*/\\.\\./','/');
  }
  return res;
}
;

create procedure t_sys_dirlist (in path varchar, in files int, in err any, in sorts int, in pdav int := null)
{
  declare ret any;
  ret := vector ();
  if ( pdav is null) pdav := http_map_get ('is_dav');

  if (pdav)
    {
      declare col any;
      if (path[length(path)-1] <> ascii ('/'))
	path := path || '/';

      col := DAV_SEARCH_ID (path, 'C');
      if (files)
	{
	  for select RES_NAME from WS.WS.SYS_DAV_RES where RES_COL = col do
	    {
	      ret := vector_concat (ret, vector (RES_NAME));
	    }
        }
      else
	{
	  for select COL_NAME from WS.WS.SYS_DAV_COL where COL_PARENT = col do
	    {
	      ret := vector_concat (ret, vector (COL_NAME));
	    }
	}
    }
  else
    {
      ret := sys_dirlist (path, files, err, sorts);
    }
  return ret;
}
;

create procedure t_get_pwd (in path varchar, in pdav int := null)
{
  if ( pdav is null) pdav := http_map_get ('is_dav');

  if (pdav)
    {
      return path;
    }
  else
    {
      return concat (http_root (), path);
    }
}
;


create procedure t_load_script (in path varchar, in pdav int := null)
{
  declare cnt, parts, errors any;
  cnt := t_file_to_string (path, pdav);
  parts := sql_split_text (cnt);
  errors := vector ();
  foreach (varchar s in parts) do
    {
      declare stat, msg any;
      stat := '00000';
      exec (s, stat, msg);
      if (stat <> '00000')
	{
	  --dbg_obj_print (stat, msg);
	  if (lower (trim (s, ' \r\n')) not like 'drop %')
	    {
	      errors := vector_concat (errors, vector (vector (stat, msg)));
	    }
	  rollback work;
	}
      else
	{
	  commit work;
	}
    }
  return errors;
}
;

create procedure tcheck_package (in pname varchar)
{
  declare aXML any;
  declare i integer;

  i := 0;
  aXML := VAD.DBA.VAD_GET_PACKAGES ();
  while(i<length(aXML))
  {
    if (aXML[i][1] = pname)
       return 1;
    i := i + 1;
  };
  return 0;
}
;

create procedure ensure_tutorial_demo_user ()
{
  declare id int;
  if (not exists (select 1 from SYS_USERS where U_NAME = 'tutorial_demo'))
    {
      id := DB.DBA.USER_CREATE ('tutorial_demo', 'secret',
	  vector ('SQL_ENABLE', 0, 'DAV_ENABLE', 1, 'DISABLED', 0, 'HOME', '/DAV/home/tutorial_demo/'));
      DB.DBA.DAV_MAKE_DIR ('/DAV/home/', http_dav_uid (), http_admin_gid (), '110100100R');
      DB.DBA.DAV_COL_CREATE_INT ('/DAV/home/tutorial_demo/', '110100100R',
	  id, http_admin_gid (), 'dav', null, 1, 0, 1, null, null);
    }
  else
  	{
      id := (select U_ID from SYS_USERS where U_NAME = 'tutorial_demo');
      DB.DBA.DAV_MAKE_DIR ('/DAV/home/', http_dav_uid (), http_admin_gid (), '110100100R');
      DB.DBA.DAV_COL_CREATE_INT ('/DAV/home/tutorial_demo/', '110100100R',
	  id, http_admin_gid (), 'dav', null, 1, 0, 1, null, null);
    };
  		
};

ensure_tutorial_demo_user ();


create procedure t_populate_sioc (in path varchar)
{
  declare graph varchar;
  declare data varchar;
  declare s,e integer;
  
  graph := cfg_item_value (virtuoso_ini_path(), 'URIQA', 'DefaultHost');
  if (graph is null)
    return;
  graph := 'http://' || ltrim(graph,'/') || '/tutorial';
  
  data := xml_uri_get(path,'');
  
  data := replace(blob_to_string(data),'<?V _path ?>',graph||'/');
  
  s := strstr(data,'<?vsp');
  e := strstr(data,'<rdf:RDF');
  
  if (s is not null)
  data := substring(data,1,s) || subseq(data,e);
  
  DELETE FROM DB.DBA.RDF_QUAD WHERE G = DB.DBA.RDF_MAKE_IID_OF_QNAME (graph);
  
  DB.DBA.RDF_LOAD_RDFXML(data,graph,graph);
    
};


create procedure tut_generate_tomcat_url (in tut_name any, in lines any)
{

   -- First get the tomcat HTTP port.

   declare path, ini_s, idx, pos, ini_f, port, temp, line, fhost any;

   declare exit handler for sqlstate '*'
     {
	connection_set ('TomcatStatus', 'BAD');
	return '';
     };

   idx := 1;
   ini_s := '';
   path := '';

   while (ini_s is not NULL)
    {
	ini_s := cfg_item_value(virtuoso_ini_path(), 'Parameters','JavaVMOption' || cast (idx as varchar));
	if (strstr (ini_s, 'catalina.home'))
	   {
		pos := strstr (ini_s, '=');
		path := "RIGHT" (ini_s, length (ini_s) - pos - 1);
		ini_s := NULL;
	   }
	idx := idx + 1;
    }

   if (path = '' and tut_name <> '')
	signal ('TUT01', 'Can''t get Catalina home dir.');

   ini_f := file_to_string (path || '/conf/server.xml');
   ini_f := xml_tree_doc (ini_f);
   port := xpath_eval ('//Service/Connector/@port', ini_f, 1);

   if (port is NULL and tut_name <> '')
	signal ('TUT02', 'Can''t get tomcat HTTP port.');

   port := cast (port as varchar);
   fhost := http_request_header (lines, 'Host', null, 'localhost');
   fhost := split_and_decode (fhost, 0, ':=:');
   fhost := fhost[0];

   -- Make list of samples

   declare exit handler for sqlstate '08001'
     {
	connection_set ('TomcatStatus', 'BAD');
	return '';
	signal ('TUT03', 'Tomcat is not started. Please Start it in order to continue whit sample.');
     };

   temp := http_get ('http://' || fhost || ':' || port || '/jsp-examples/');
   temp := xml_tree_doc (xml_tree (temp, 2));
   temp := xpath_eval ('//@href', temp, 0);

   if (not exists (select 1 from DB.DBA.HTTP_PATH where HP_LPATH = '/jsp_tutorial'))
     VHOST_DEFINE (lpath=>'/jsp_tutorial',ppath=>'http://' || fhost || ':' || port || '/jsp-examples/');

   for (declare x any, x := 0; x < length (temp); x := x + 1)
     {
	 line := cast (temp[x] as varchar);
	 if (strstr (line, tut_name))
  	   return 'http://' || fhost || ':' || server_http_port () || '/jsp_tutorial/' || line;
     }

  return '';
}
;

create procedure DB.DBA.rd_v_1_localize()
{
  declare file_text, uriqa varchar;
  uriqa := registry_get('URIQADefaultHost');
  file_text := (select blob_to_string (RES_CONTENT) from WS.WS.SYS_DAV_RES 
   where RES_FULL_PATH='/DAV/VAD/tutorial/rdfview/rd_v_1/rd_v_1.sql');
  file_text := replace(file_text, 'URIQA2_MACRO', uriqa);
  update WS.WS.SYS_DAV_RES set RES_CONTENT=file_text where RES_FULL_PATH='/DAV/VAD/tutorial/rdfview/rd_v_1/rd_v_1.sql';
  
  file_text := (select blob_to_string (RES_CONTENT) from WS.WS.SYS_DAV_RES 
    where RES_FULL_PATH='/DAV/VAD/tutorial/rdfview/rd_v_1/rd_v_1.owl');
  file_text := replace(file_text, 'URIQA_MACRO', uriqa);
  update WS.WS.SYS_DAV_RES set RES_CONTENT=file_text where RES_FULL_PATH='/DAV/VAD/tutorial/rdfview/rd_v_1/rd_v_1.owl';
}
;
			
DB.DBA.rd_v_1_localize()
;
			
drop procedure DB.DBA.rd_v_1_localize
;