| 1 | # $Id: Tutorial.pm,v 1.20 2005-04-22 12:19:17 mike Exp $ | ||
| 2 | |||
| 3 | package Net::Z3950::DBIServer::Tutorial; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Net::Z3950::DBIServer::Tutorial - The zSQLgate Tutorial | ||
| 10 | |||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | server$ B<mysql> | ||
| 15 | |||
| 16 | mysql> create database books; | ||
| 17 | mysql> use books; | ||
| 18 | mysql> GRANT ALL PRIVILEGES ON books.* TO ""@"localhost"; | ||
| 19 | mysql> CREATE TABLE author(id INT, name TEXT); | ||
| 20 | mysql> INSERT INTO author(id, name) VALUES('1','Douglas Adams'); | ||
| 21 | mysql> INSERT INTO author(id, name) VALUES('2','Stephen Notley'); | ||
| 22 | mysql> INSERT INTO author(id, name) VALUES('3','Dave Barry'); | ||
| 23 | |||
| 24 | server$ B<cat trivial.nzd> | ||
| 25 | |||
| 26 | datasource = "DBI:mysql:dbname=books" | ||
| 27 | database "artists" { | ||
| 28 | table = "author" | ||
| 29 | search attrset bib1 { | ||
| 30 | access 12 = "id" | ||
| 31 | access 1 = "name" | ||
| 32 | } | ||
| 33 | data format xml { | ||
| 34 | record = "author" | ||
| 35 | field "authorId" = "id" | ||
| 36 | field "authorName" = "name" | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | server$ B<zSQLgate trivial.nzd @:3950> | ||
| 41 | |||
| 42 | 13:04:54-12/04 [server] Adding dynamic Z3950 listener on tcp:@:9999 | ||
| 43 | 13:04:54-12/04 [server] Starting server zSQLgate pid=28488 | ||
| 44 | 13:04:57-12/04 zSQLgate(1) [session] Starting session from tcp:127.0.0.1 (pid=28490) | ||
| 45 | ... | ||
| 46 | |||
| 47 | client$ B<yaz-client @:3950> | ||
| 48 | |||
| 49 | Z> base artists | ||
| 50 | Z> find @attr 1=12 @attr 2=1 3 | ||
| 51 | Number of hits: 2 | ||
| 52 | Z> format xml | ||
| 53 | Z> show 1+2 | ||
| 54 | <author> | ||
| 55 | <authorId>1</authorId> | ||
| 56 | <authorName>Douglas Adams</authorName> | ||
| 57 | </author> | ||
| 58 | <author> | ||
| 59 | <authorId>2</authorId> | ||
| 60 | <authorName>Stephen Notley</authorName> | ||
| 61 | </author> | ||
| 62 | |||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | This document is a tutorial for C<zSQLgate>, the Z39.50 server for | ||
| 67 | relational databases. | ||
| 68 | |||
| 69 | If you don't know what C<zSQLgate> is, then you should start by | ||
| 70 | reading | ||
| 71 | L<Net::Z3950::DBIServer::Intro> | ||
| 72 | (the introduction to C<zSQLgate>) and | ||
| 73 | L<Net::Z3950::DBIServer::LICENCE>, | ||
| 74 | (the licence under which it is available). | ||
| 75 | |||
| 76 | This tutorial informally walks through the main features of | ||
| 77 | C<zSQLgate>, and for many people will be the only part of the | ||
| 78 | C<zSQLgate> documentation that they ever read. That's fine - much of | ||
| 79 | what you need to know will be here. But please be aware that some of | ||
| 80 | the more esoteric features will not be discussed here: if you don't | ||
| 81 | find what you want, you may need to dig into two more rigorous | ||
| 82 | documents that are supplied with this tutorial: | ||
| 83 | |||
| 84 | =over 4 | ||
| 85 | |||
| 86 | =item * | ||
| 87 | |||
| 88 | L<Net::Z3950::DBIServer::Run> | ||
| 89 | describes the command-line options available to tailor the behaviour | ||
| 90 | of C<zSQLgate> at run-time. | ||
| 91 | |||
| 92 | Z<> | ||
| 93 | |||
| 94 | =item * | ||
| 95 | |||
| 96 | L<Net::Z3950::DBIServer::Spec> | ||
| 97 | describes the C<zSQLgate> configuration file format in more detail | ||
| 98 | than this document, explaining the precise meaning of its various | ||
| 99 | directives. | ||
| 100 | |||
| 101 | Z<> | ||
| 102 | |||
| 103 | =back | ||
| 104 | |||
| 105 | In general, those two documents describe the sorts of things you can | ||
| 106 | say to C<zSQLgate> and what they mean - but this one explains why you | ||
| 107 | might want to say those things. The reference documents are there to | ||
| 108 | answer I<what> questions; this tutorial aims to tell you I<how>. | ||
| 109 | |||
| 110 | |||
| 111 | =head1 A WALK THROUGH THE SYNOPSIS | ||
| 112 | |||
| 113 | The synopsis shows the process of | ||
| 114 | creating a database, a C<zSQLgate> configuration file for that | ||
| 115 | database, how to start a Z39.50 server for it, and a session with a | ||
| 116 | Z39.50 client accessing it. | ||
| 117 | |||
| 118 | Let's look at the four sections separately. They will introduce us to | ||
| 119 | pretty much all of the main C<zSQLgate> features: everything else is | ||
| 120 | just refinement of these core facilities. | ||
| 121 | |||
| 122 | =head2 Database Creation | ||
| 123 | |||
| 124 | server$ B<mysql> | ||
| 125 | |||
| 126 | mysql> create database books; | ||
| 127 | mysql> use books; | ||
| 128 | mysql> GRANT ALL PRIVILEGES ON books.* TO ""@"localhost"; | ||
| 129 | mysql> CREATE TABLE author(id INT, name TEXT); | ||
| 130 | mysql> INSERT INTO author(id, name) VALUES('1','Douglas Adams'); | ||
| 131 | mysql> INSERT INTO author(id, name) VALUES('2','Stephen Notley'); | ||
| 132 | mysql> INSERT INTO author(id, name) VALUES('3','Dave Barry'); | ||
| 133 | |||
| 134 | Here, we are creating a relational database. This is the one part | ||
| 135 | of the whole process that C<zSQLgate> can't really help you with, | ||
| 136 | because the details of how you create and populate a database are very | ||
| 137 | much down to the specific RDBMS that you happen to be using. The | ||
| 138 | commands above create a tiny but self-contained database called | ||
| 139 | C<books> in the MySQL RDBMS: the actual SQL commands to do | ||
| 140 | this will be very similar in most or all relational databases, but the | ||
| 141 | front-end program will be different. | ||
| 142 | |||
| 143 | How do you choose which RDBMS to use? Well, for most projects, this | ||
| 144 | will be a non-issue: if you're be putting a C<zSQLgate> front-end on an | ||
| 145 | existing database, that choice will already have been made for you. | ||
| 146 | (You also won't need to worry about building and populating the database!) | ||
| 147 | |||
| 148 | If you don't already have a database, and you're building one from | ||
| 149 | scratch, then one good choice is PostgreSQL, which is included in | ||
| 150 | recent Red Hat Linux distributions, along with support for using it in | ||
| 151 | PHP3, Perl, Java (via JDBC), C (via native or ODBC interfaces), Perl, | ||
| 152 | Python and Tcl. If you're not using Red Hat, you can get it from | ||
| 153 | http://www.postgresql.org/ - but there are plenty of other good RDBMSs | ||
| 154 | around, and I'm not really in a position to recommend one above | ||
| 155 | another. | ||
| 156 | |||
| 157 | Anyway. You need to get a database from somewhere if you're going to | ||
| 158 | build a Z39.50 interface to it. | ||
| 159 | |||
| 160 | =head2 zSQLgate Configuration | ||
| 161 | |||
| 162 | server$ B<cat trivial.nzd> | ||
| 163 | |||
| 164 | datasource = "DBI:mysql:dbname=books" | ||
| 165 | database "artists" { | ||
| 166 | table = "author" | ||
| 167 | search attrset bib1 { | ||
| 168 | access 12 = "id" | ||
| 169 | access 1 = "name" | ||
| 170 | } | ||
| 171 | data format xml { | ||
| 172 | record = "author" | ||
| 173 | field "authorId" = "id" | ||
| 174 | field "authorName" = "name" | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | 90% of the work of setting up a C<zSQLgate> server is writing the | ||
| 179 | configuration file. Its job is to describe the relational database | ||
| 180 | that is to be made available, couching its tables in terms of Z39.50 | ||
| 181 | databases, and to express how the columns in those tables are mapped | ||
| 182 | to access points (for searching) and data elements (for retrieval). | ||
| 183 | |||
| 184 | What we have here is just about the simplest possible complete | ||
| 185 | C<zSQLgate> configuration file. Apart from specifying the datasource | ||
| 186 | (roughly, the relational database), it provides mapping details for a | ||
| 187 | single Z39.50 database (that is, in relational terms, a single table), to | ||
| 188 | be known as C<artists> in the Z39.50 world. The details for that | ||
| 189 | database consist of a statement of the name of the table that is to | ||
| 190 | provide its records (C<author>), plus sections about how that database | ||
| 191 | is to be searched, and how its data is to be formatted. | ||
| 192 | |||
| 193 | The search specification says that Z39.50 searching is done using the BIB-1 | ||
| 194 | attribute set, and that two access-points are | ||
| 195 | recognised: searches against access point 12 (``Local number'' in the | ||
| 196 | BIB-1 attribute set) are mapped into C<SELECT> statements on the C<id> | ||
| 197 | column, and those against access point 1 (``Personal name'') to the | ||
| 198 | C<name> column. | ||
| 199 | |||
| 200 | The data specification says that C<zSQLgate> is to support record | ||
| 201 | retrieval using the XML format, building records wrapped in an | ||
| 202 | <C<artist>>...<C</artist>> pair, and that each record should contain | ||
| 203 | the contents of the C<id> and C<name> columns, wrapped in | ||
| 204 | <C<artistId>>...<C</artistId>> and C<<artistName>>...C<</artistName>> | ||
| 205 | tag-pairs respectively. | ||
| 206 | |||
| 207 | See? It's easy! | ||
| 208 | |||
| 209 | Yes, it's possible to say much, much more in a configuration file; but | ||
| 210 | you can start by writing a simple one, and add in the | ||
| 211 | more sophisticated stuff as you need it, rather than needing to learn | ||
| 212 | everything in one go. | ||
| 213 | |||
| 214 | =head2 Running the zSQLgate server | ||
| 215 | |||
| 216 | server$ B<zSQLgate trivial.nzd @:3950> | ||
| 217 | |||
| 218 | 13:04:54-12/04 [server] Adding dynamic Z3950 listener on tcp:@:9999 | ||
| 219 | 13:04:54-12/04 [server] Starting server zSQLgate pid=28488 | ||
| 220 | 13:04:57-12/04 zSQLgate(1) [session] Starting session from tcp:127.0.0.1 (pid=28490) | ||
| 221 | ... | ||
| 222 | |||
| 223 | Once you've got your configuration file written, it's time to start | ||
| 224 | the server. Generally, you'll need to give it two arguments: the name | ||
| 225 | of the configuration file you want to use, and the ``listener | ||
| 226 | address'' which specifies which port to receive incoming connections | ||
| 227 | on. Generally, this should be C<@:>I<number>, where I<number> is | ||
| 228 | the IP port number. This is conventionally 210 for Z39.50 servers, | ||
| 229 | but you'll need to pick a number above 1024 if your server is not | ||
| 230 | running as root, as the lower-number ports are considered to be | ||
| 231 | ``privileged''. | ||
| 232 | |||
| 233 | If there's something wrong with your configuration file, the server | ||
| 234 | will refuse to start up, giving a hopefully useful message to help you | ||
| 235 | track down what you've done wrong. If all is well, it will go into a | ||
| 236 | loop waiting for connections, C<fork()>ing for each incoming client | ||
| 237 | and serving its requests in a subprocess. Unless you've turned | ||
| 238 | logging off, you'll see startup messages similar to those above. | ||
| 239 | |||
| 240 | =head2 Connecting to the zSQLgate server | ||
| 241 | |||
| 242 | client$ B<yaz-client @:3950> | ||
| 243 | |||
| 244 | Z> base artists | ||
| 245 | Z> find @attr 1=12 @attr 2=1 3 | ||
| 246 | Number of hits: 2 | ||
| 247 | Z> format xml | ||
| 248 | Z> show 1+2 | ||
| 249 | <author> | ||
| 250 | <authorId>1</authorId> | ||
| 251 | <authorName>Douglas Adams</authorName> | ||
| 252 | </author> | ||
| 253 | <author> | ||
| 254 | <authorId>2</authorId> | ||
| 255 | <authorName>Stephen Notley</authorName> | ||
| 256 | </author> | ||
| 257 | |||
| 258 | Once your C<zSQLgate> server is up, you'll want to connect a Z39.50 | ||
| 259 | client to it to check that it's doing what you intended. You can use | ||
| 260 | whatever client you're used to - that's the point of an interoperable | ||
| 261 | standard like Z39.50. | ||
| 262 | |||
| 263 | In this example, I'm using the simple command-line client provided by | ||
| 264 | with the YAZ toolkit, because it's the one client you're guaranteed to | ||
| 265 | have. (You wouldn't have been able to build C<zSQLgate> and the | ||
| 266 | libraries it depends on without YAZ.) Start the client with a | ||
| 267 | single command-line argument which specifies what host and port to connect to. | ||
| 268 | (The hostname C<@> is shorthand for the same host the client's running on.) | ||
| 269 | |||
| 270 | Once the client is connected, you can do what you like. In the short | ||
| 271 | session above, we've set the name of the database we want to use to | ||
| 272 | C<artists> (which is the Z39.50 database name that we specified in the | ||
| 273 | C<zSQLgate> configuration file), then fired off a search in the rather | ||
| 274 | opaque but general Prefix Query Format. The search C<@attr 1=12 | ||
| 275 | @attr 2=1 3> says to search for the term C<3> with two attributes | ||
| 276 | attached: the first, C<1=12>, means to use access point 12 (which we | ||
| 277 | defined to mean a C<SELECT> condition on the C<id> column); and the second, | ||
| 278 | C<2=1> is a relation attribute, meaning to search for values less than | ||
| 279 | the specified one. Inside C<zSQLgate>, this search is translated into | ||
| 280 | |||
| 281 | SELECT * FROM author WHERE id < 3 | ||
| 282 | |||
| 283 | Finally, having received a response to our search (which found two | ||
| 284 | records, presumably with C<id>s 1 and 2), we set the desired retrieval | ||
| 285 | format to XML, and fetch the records, which are rendered to the | ||
| 286 | screen. (The YAZ client includes more protocol information than this | ||
| 287 | in its output, but the substance of it is the two tiny XML records | ||
| 288 | listed above.) | ||
| 289 | |||
| 290 | =head2 Conclusion | ||
| 291 | |||
| 292 | An ounce of example is worth a ton of explanation, isn't it? | ||
| 293 | |||
| 294 | |||
| 295 | =head1 BUILDING ON THE FIRST EXAMPLE | ||
| 296 | |||
| 297 | =head2 Comments | ||
| 298 | |||
| 299 | The C<zSQLgate> configuration file can contain comments, which are | ||
| 300 | introduced by a hash character (C<#>), and run to the end of the line. | ||
| 301 | They are ignored. Comments are the only part of the configuration | ||
| 302 | file syntax that treats lines specially: otherwise the syntax is | ||
| 303 | totally free-form. | ||
| 304 | |||
| 305 | For example, we could begin our configuration file like this: | ||
| 306 | |||
| 307 | # books.nzd - a sample configuration file for Net::Z39.50::DBIServer | ||
| 308 | # $Id: Tutorial.pm,v 1.20 2005-04-22 12:19:17 mike Exp $ | ||
| 309 | # | ||
| 310 | # See Net::Z3950::DBIServer::Spec for a formal description of this | ||
| 311 | # file's format, if you need it. But it's actually pretty obvious. | ||
| 312 | |||
| 313 | (The Id line allows a version-control system such as CVS to insert | ||
| 314 | information into the file, such as the date and time it was last | ||
| 315 | checked in.) | ||
| 316 | |||
| 317 | Comments need not start at the beginning of a line: for example, we | ||
| 318 | might want to add a note that the datasource is MySQL-specific, and | ||
| 319 | perhaps to add the equivalent line for PostgreSQL, commented out: | ||
| 320 | |||
| 321 | datasource = "DBI:mysql:dbname=books" # If using MySQL | ||
| 322 | #datasource = "DBI:Pg:dbname=books" # If using PostgreSQL | ||
| 323 | |||
| 324 | Similarly, we might annotate the access-point lines with comments | ||
| 325 | indicating the interpretation of the BIB-1 access points, since they | ||
| 326 | are specified as rather opaque numbers: | ||
| 327 | |||
| 328 | access 12 = "id" # local number | ||
| 329 | access 1 = "name" # author | ||
| 330 | |||
| 331 | =head2 Authentication | ||
| 332 | |||
| 333 | If the relational database requires a username and password, these can | ||
| 334 | be specified at the top level: | ||
| 335 | |||
| 336 | username = "simon" # If needed | ||
| 337 | password = "pieman" # If needed | ||
| 338 | |||
| 339 | =head2 Multiple Databases | ||
| 340 | |||
| 341 | A Z39.50 database corresponds roughly to a single table from a | ||
| 342 | relational database. Within a relational database, a single | ||
| 343 | C<zSQLgate> process can serve data from multiple tables, representing | ||
| 344 | multiple Z39.50 databases. So let's add another table to the | ||
| 345 | database, and add some records: | ||
| 346 | |||
| 347 | mysql> CREATE TABLE book(id INT, author_id INT, name TEXT, | ||
| 348 | year INT, notes TEXT, fulltext index (name)); | ||
| 349 | mysql> INSERT INTO book(id,author_id,name,year) | ||
| 350 | VALUES('1','1','The Hitch Hiker''s Guide to the Galaxy','1979'); [etc.] | ||
| 351 | |||
| 352 | Now we can add a new C<database> section to the C<zSQLgate> | ||
| 353 | configuration, indicating how to search in, and retrieve from, the new | ||
| 354 | table: | ||
| 355 | |||
| 356 | database "works" { | ||
| 357 | table = "book" | ||
| 358 | search { | ||
| 359 | attrset bib1 { | ||
| 360 | access 12 = "id" # local number | ||
| 361 | access 4 = "name" # title | ||
| 362 | access 30 = "year" # year | ||
| 363 | } | ||
| 364 | } | ||
| 365 | data format xml { | ||
| 366 | record = "book" | ||
| 367 | field "bookId" = "id" | ||
| 368 | field "authorId" = "author_id" | ||
| 369 | field "authorName" = "author.name" | ||
| 370 | field "bookName" = "name" | ||
| 371 | field "bookYear" = "year" | ||
| 372 | field "bookNotes" = "?notes" | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | This works exactly the same way as the similar section for the | ||
| 377 | "artists" database: Z39.50 searches against the BIB-1 access-points 12 | ||
| 378 | (local number), 4 (title) and 30 (year) are implemented using the | ||
| 379 | C<id>, C<name> and C<year> columns respectively. XML records are | ||
| 380 | returned as a C<<book>> element containing C<<bookId>> derived from | ||
| 381 | the C<id> column, etc. | ||
| 382 | |||
| 383 | =head2 Searching in Multiple Columns | ||
| 384 | |||
| 385 | The specification on the right-hand side of an C<access> declaration | ||
| 386 | may be a comma-separated list of multiple columns to search when the | ||
| 387 | specified access point is used: | ||
| 388 | |||
| 389 | access 1016 = "name,year,notes" # any | ||
| 390 | |||
| 391 | =head2 Searching with Multiple Attribute Sets | ||
| 392 | |||
| 393 | It is possible to support access points drawn from more than one | ||
| 394 | attribute set. Often this is not necessary, as the BIB-1 attribute | ||
| 395 | set is so nearly ubiquitous. However, supporting Z39.50 profiles | ||
| 396 | developed in accordance with the Attribute Architecture, such as the | ||
| 397 | Zthes and ZeeRex profiles, does require that attributes from multiple | ||
| 398 | sets can be mixed - for example, the use of both a Dublin Core "title" | ||
| 399 | and a Network Metadata "host" access points. Another use for multiple | ||
| 400 | attribute sets is to support different access points that refer to the | ||
| 401 | same columns - for example, BIB-1 "title" (4) and the attribute | ||
| 402 | architecture cross-domain set's "title" (1). | ||
| 403 | |||
| 404 | In this case, simply provide multiple C<attrset> clauses within the | ||
| 405 | database's C<search> section, enclosed in curly braces: | ||
| 406 | |||
| 407 | search { | ||
| 408 | attrset bib1 { | ||
| 409 | access 4 = "name" # BIB-1 title | ||
| 410 | access 30 = "year" # BIB-1 year | ||
| 411 | # ... etc. ... | ||
| 412 | } | ||
| 413 | attrset xd1 { # test for multiple attrsets | ||
| 414 | access 1 = "name" # AA cross-domain title | ||
| 415 | access 5 = "year" # AA cross-domain date | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | Then the following searches are equivalent: they are both title | ||
| 420 | searches, using the attributes from the two different sets: | ||
| 421 | |||
| 422 | @attr 1=4 flower | ||
| 423 | @attr xd1 1=1 flower | ||
| 424 | |||
| 425 | =head2 Specifying Attribute Sets by OID | ||
| 426 | |||
| 427 | C<zSQLgate> knows the names of the most commonly used attribute sets; | ||
| 428 | however, if it is necessary to support access points in an attribute | ||
| 429 | set that it doesn't know - for example, a private one - that attribute | ||
| 430 | set can be specfied by its OID rather than its name: | ||
| 431 | |||
| 432 | attrset 1.2.840.10003.3.1000.169.42 { # Private attribute-set | ||
| 433 | access 609 = "notes" | ||
| 434 | } | ||
| 435 | |||
| 436 | This can be useful for providing a way to search against a column that | ||
| 437 | can't be mapped to any of the access points in the standard sets: for | ||
| 438 | example, the C<notes> column of our books database. | ||
| 439 | |||
| 440 | Depending on what Z39.50 client you use, it should be possible to | ||
| 441 | search using any attribute set by spelling out its OID in the query. | ||
| 442 | For example, the YAZ command-line client lets you use: | ||
| 443 | |||
| 444 | @attr 1.2.840.10003.3.1000.169.42 1=609 guess | ||
| 445 | |||
| 446 | =head2 Specifying Default Attributes for Searching | ||
| 447 | |||
| 448 | It's possible to specify a set of default attributes that are used in | ||
| 449 | every query unless overridden by attributes explicitly provided by the | ||
| 450 | query itself. The attributes are listed after the C<defaultattrs> | ||
| 451 | keyword within the C<search> section. This facility can be used to | ||
| 452 | choose a default access point: | ||
| 453 | |||
| 454 | defaultattrs 1=4 | ||
| 455 | |||
| 456 | Or default truncation: | ||
| 457 | |||
| 458 | defaultattrs 5=3 | ||
| 459 | |||
| 460 | Or indeed both: | ||
| 461 | |||
| 462 | defaultattrs 1=4, 5=3 | ||
| 463 | |||
| 464 | B<Note that default attributes specified in this manner are always | ||
| 465 | taken from the BIB-1 attribute set. To make an access point in a | ||
| 466 | different attribute set the default, make a "private" BIB-1 access | ||
| 467 | point that searches in the same column, and make that the default.> | ||
| 468 | |||
| 469 | =head2 Restricting Searches to a Subset of Available Records | ||
| 470 | |||
| 471 | it is sometime useful to hide some records from all searching - for | ||
| 472 | example, records that have a "deleted" flag set, or that are marked as | ||
| 473 | requiring revision before they can be published. This kind of | ||
| 474 | restriction can be specified within a C<database> section, specifying | ||
| 475 | an SQL clause to be ANDed with the query generated from what the | ||
| 476 | Z39.50 client submits. For example: | ||
| 477 | |||
| 478 | restriction = "author_id != 1" | ||
| 479 | |||
| 480 | ensures that only records whose C<author_id> is different from 1 are | ||
| 481 | included in the result sets returned to the client. | ||
| 482 | |||
| 483 | =head2 Linking Between Tables | ||
| 484 | |||
| 485 | We now approach the key to C<zSQLgate>'s power, which is the ability | ||
| 486 | to exploit relational links between tables (or, in Z39.50 terms, | ||
| 487 | between databases). This is done using an C<auxiliary> clause within | ||
| 488 | a C<database> sections, specifying what other table is related to the | ||
| 489 | one in question, and by what relations. For example, in our toy | ||
| 490 | database, every C<book> record has an C<author_id> which is the C<id> | ||
| 491 | of a record in the C<author> table. This is expressed as follows: | ||
| 492 | |||
| 493 | database "works" { | ||
| 494 | table = "book" | ||
| 495 | auxiliary "author" with "author.id = book.author_id" | ||
| 496 | # ... etc. ... | ||
| 497 | |||
| 498 | This opens up the use of the auxiliary table's columns in both | ||
| 499 | searching and retrieval. For example, it's possible to search the | ||
| 500 | C<works> database by author name as follows: | ||
| 501 | |||
| 502 | access 1 = "author.name" # personal name | ||
| 503 | |||
| 504 | And to include the author's name in the retrieved records as follows: | ||
| 505 | |||
| 506 | field "authorName" = "author.name" | ||
| 507 | |||
| 508 | B<This only works correctly for many-to-one and one-to-many links. If | ||
| 509 | an auxiliary table is specified that has many matches for the | ||
| 510 | condition, then too many records will be generated - one for each | ||
| 511 | linked record rather than one for each record in the primary table | ||
| 512 | associated with the Z39.50 database.> | ||
| 513 | |||
| 514 | =head2 Setting Attributes on XML Records | ||
| 515 | |||
| 516 | By default, the XML elements in the records generated by C<zSQLgate> | ||
| 517 | are wrapped in a simple document element, whose name is specified by | ||
| 518 | the C<record=> directive. Sometimes, though, it is useful to include | ||
| 519 | attributes in this top-level element. This can be done using the | ||
| 520 | C<attrs=> directive. | ||
| 521 | |||
| 522 | For example, the I<Guidelines for Implementing Dublin Core in XML> - | ||
| 523 | http://dublincore.org/documents/dc-xml-guidelines/index.shtml | ||
| 524 | - specify that Dublin Core elements should be in the XML namespace | ||
| 525 | http://purl.org/dc/elements/1.1/ . | ||
| 526 | This namespace can be declared with a C<dc> prefix in the document | ||
| 527 | element by specifying an C<xmlns:dc> attribute as follows: | ||
| 528 | |||
| 529 | attrs = "xmlns:dc='http://purl.org/dc/elements/1.1/'" | ||
| 530 | |||
| 531 | =head2 Transforming XML Records with XSLT | ||
| 532 | |||
| 533 | The facilities for constructing XML records in C<zSQLgate> allow only | ||
| 534 | simple records to be created: a flat list of elements, each containing | ||
| 535 | a field from the database, wrapped in a top-level element that may | ||
| 536 | have some attributes specified. For many applications, this is | ||
| 537 | sufficient. But more flexibility is required if it's necessary to | ||
| 538 | construct records according to a pre-defined schema: for example, the | ||
| 539 | RDF representation of Dublin Core records as described in | ||
| 540 | I<Expressing Simple Dublin Core in RDF/XML> - | ||
| 541 | http://dublincore.org/documents/dcmes-xml/index.shtml | ||
| 542 | - describe a structure in which the data elements are contained in a | ||
| 543 | subelement of the document element, like this: | ||
| 544 | |||
| 545 | <rdf:RDF ...> | ||
| 546 | <rdf:Description ...> | ||
| 547 | <dc:title>Dave Beckett's Home Page</dc:title> | ||
| 548 | <dc:creator>Dave Beckett</dc:creator> | ||
| 549 | </rdf:Description> | ||
| 550 | </rdf:RDF> | ||
| 551 | |||
| 552 | In order to generate complex XML document such as this, zSQLgate | ||
| 553 | allows an arbitrary XSLT stylesheet to be applied to the generated | ||
| 554 | XML, and delivers the result of this transformation to the client. | ||
| 555 | This functionality is requested using the C<transform> directive | ||
| 556 | |||
| 557 | transform = "dc-rdf.xsl" | ||
| 558 | |||
| 559 | within the C<format xml> section of the relevant database. The | ||
| 560 | argument is the name of a file containing the stylesheet to be | ||
| 561 | applied, interpreted relative to the working directory of the | ||
| 562 | C<zSQLgate> server process. | ||
| 563 | |||
| 564 | Here is an example stylesheet, C<dc-rdf.xml>, which generates Dublin | ||
| 565 | Core RDF/XML records from the raw XML records generated by the | ||
| 566 | C<zsQLgate> core: | ||
| 567 | |||
| 568 | <?xml version="1.0"?> | ||
| 569 | <!-- $Id: Tutorial.pm,v 1.20 2005-04-22 12:19:17 mike Exp $ --> | ||
| 570 | |||
| 571 | <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | ||
| 572 | <xsl:output method="xml" indent="yes" | ||
| 573 | doctype-public="-//DUBLIN CORE//DCMES DTD 2002/07/31//EN" | ||
| 574 | doctype-system="http://dublincore.org/documents/2002/07/31/dcmes-xml/dcmes-xml-dtd.dtd"/> | ||
| 575 | <xsl:template match="/book"> | ||
| 576 | <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
| 577 | xmlns:dc="http://purl.org/dc/elements/1.1/"> | ||
| 578 | <rdf:Description> | ||
| 579 | <dc:title><xsl:value-of select="bookName"/></dc:title> | ||
| 580 | <dc:creator><xsl:value-of select="authorName"/></dc:creator> | ||
| 581 | </rdf:Description> | ||
| 582 | </rdf:RDF> | ||
| 583 | </xsl:template> | ||
| 584 | </xsl:stylesheet> | ||
| 585 | |||
| 586 | =head2 Conditional Fields | ||
| 587 | |||
| 588 | I<"?notes"> | ||
| 589 | |||
| 590 | =head2 Compound Fields | ||
| 591 | |||
| 592 | I<"%{field1} - %{field2}"> | ||
| 593 | |||
| 594 | =head2 Complex String Constants | ||
| 595 | |||
| 596 | I<"foo" + "bar"> | ||
| 597 | |||
| 598 | =head2 Generating MARC Records | ||
| 599 | |||
| 600 | I<format marc> | ||
| 601 | |||
| 602 | =head2 Generating GRS-1 Records | ||
| 603 | |||
| 604 | I<format grs1> | ||
| 605 | |||
| 606 | =head2 Generating Cutomised SUTRS Records | ||
| 607 | |||
| 608 | I<format sutrs> | ||
| 609 | |||
| 610 | =head2 Tuning Cache Size | ||
| 611 | |||
| 612 | I<cachesize> | ||
| 613 | |||
| 614 | |||
| 615 | =head1 PUTTING IT TOGETHER | ||
| 616 | |||
| 617 | Here, then, is the complete configuration file using all of the | ||
| 618 | facilities we've discussed: | ||
| 619 | |||
| 620 | # books.nzd - a sample configuration file for Net::Z39.50::DBIServer | ||
| 621 | # $Id: Tutorial.pm,v 1.20 2005-04-22 12:19:17 mike Exp $ | ||
| 622 | # | ||
| 623 | # See Net::Z3950::DBIServer::Spec for a formal description of this | ||
| 624 | # file's format, if you need it. But it's actually pretty obvious. | ||
| 625 | |||
| 626 | datasource = "DBI:mysql:dbname=books" # If using MySQL | ||
| 627 | #datasource = "DBI:Pg:dbname=books" # If using PostgreSQL | ||
| 628 | #username = "simon" # If needed | ||
| 629 | #password = "pieman" # If needed | ||
| 630 | |||
| 631 | database "artists" { | ||
| 632 | table = "author" | ||
| 633 | search attrset bib1 { | ||
| 634 | access 12 = "id" # local number | ||
| 635 | access 1 = "name" # author | ||
| 636 | } | ||
| 637 | data format xml { | ||
| 638 | record = "author" | ||
| 639 | field "authorId" = "id" | ||
| 640 | field "authorName" = "name" | ||
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | database "works" { | ||
| 645 | table = "book" | ||
| 646 | auxiliary "author" with "author.id = book.author_id" | ||
| 647 | restriction = "author_id != 1" # Omit Douglas Adams books | ||
| 648 | search { | ||
| 649 | defaultattrs 1=4 | ||
| 650 | attrset bib1 { | ||
| 651 | access 12 = "id" # local number | ||
| 652 | access 4 = fulltext "name" # title (fulltext for MySQL) | ||
| 653 | access 30 = "year" # year | ||
| 654 | access 1016 = "name,year,notes" # any | ||
| 655 | } | ||
| 656 | attrset xd1 { # test for multiple attrsets | ||
| 657 | access 1 = "name" # AA cross-domain title | ||
| 658 | access 5 = "year" # AA cross-domain date | ||
| 659 | } | ||
| 660 | attrset 1.2.840.10003.3.1000.169.42 { # Private attribute-set | ||
| 661 | access 609 = "notes" | ||
| 662 | } | ||
| 663 | } | ||
| 664 | data { | ||
| 665 | cachesize = 2 | ||
| 666 | format grs1 { | ||
| 667 | field (1,14) = "id" # tagSet-M local control number | ||
| 668 | field (3,"artistId") = "author_id" | ||
| 669 | field (2,1) = "name" # tagSet-G title | ||
| 670 | field (2,4) = "year" # tagSet-G date | ||
| 671 | field (3,"notes") = "?notes" | ||
| 672 | } | ||
| 673 | format xml { | ||
| 674 | record = "book" | ||
| 675 | #attrs = "xmlns='http://sql.z3950.org/schema/books/1.0'" | ||
| 676 | transform = "default.xsl" | ||
| 677 | field "bookId" = "id" | ||
| 678 | field "authorId" = "author_id" | ||
| 679 | field "authorName" = "author.name" | ||
| 680 | field "bookName" = "name" | ||
| 681 | field "bookYear" = "year" | ||
| 682 | field "bookNotes" = "?notes" | ||
| 683 | } | ||
| 684 | format marc { | ||
| 685 | field "001" = "id" | ||
| 686 | field "100/1$a" = "author.name" | ||
| 687 | field "245/1/0$a" = "name" | ||
| 688 | field "260$c" = "year" | ||
| 689 | field "500$a" = "*MARC record generated by zSQLgate from MySQL" | ||
| 690 | field "500$a" = "?notes" | ||
| 691 | } | ||
| 692 | format sutrs { | ||
| 693 | field "LocalIdentifier" = "id" | ||
| 694 | field "Title" = "name" | ||
| 695 | field "Author" = "author.name" | ||
| 696 | field "Published" = "year" | ||
| 697 | field "Notes" = "?notes" | ||
| 698 | field "titleStatement" = "<b>%{name}</b> -- <i>%{author.name}</i>" | ||
| 699 | } | ||
| 700 | } | ||
| 701 | } | ||
| 702 | |||
| 703 | |||
| 704 | =head1 AUTHOR | ||
| 705 | |||
| 706 | Mike Taylor E<lt>mike@miketaylor.org.ukE<gt> | ||
| 707 | |||
| 708 | First version Sunday 24th February 2002. | ||
| 709 | |||
| 710 | =cut | ||
| 711 | |||
| 712 | 1; |