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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" encoding="ISO-8859-1"/>
<!-- ================ Primary templates ================================ -->
<!-- Initialise database tables. -->
<xsl:template match="create">
<xsl:text>CREATE TABLE COOKBOOK(VERSION INT UNSIGNED NOT NULL,SUBVERSION INT UNSIGNED NOT NULL);
</xsl:text>
<!-- Always update the version number before
changing the database-structure!!!
Also update the version-macro in file 'configure.in'. -->
<xsl:text>INSERT INTO COOKBOOK VALUES(0,21);
</xsl:text>
<xsl:text>CREATE TABLE EDIBLE(ID INT UNSIGNED NOT NULL AUTO_INCREMENT,NAME VARCHAR(100) UNIQUE NOT NULL,PRIMARY KEY (ID));
</xsl:text>
<xsl:text>CREATE TABLE CATEGORY(RECIPEID INT UNSIGNED NOT NULL REFERENCES RECIPE,ID INT UNSIGNED NOT NULL,CATEGORYID INT UNSIGNED NOT NULL REFERENCES CATEGORIES,KEY(RECIPEID));
</xsl:text>
<xsl:text>CREATE TABLE CATEGORIES(ID INT UNSIGNED NOT NULL AUTO_INCREMENT,NAME VARCHAR(100) UNIQUE NOT NULL,PRIMARY KEY(ID),KEY(NAME));
</xsl:text>
<xsl:text>CREATE TABLE RECIPE(ID INT UNSIGNED NOT NULL AUTO_INCREMENT,TITLE VARCHAR(100) NOT NULL,AMOUNT INT UNSIGNED NOT NULL,UNIT VARCHAR(10) BINARY,LANGUAGE CHAR(2) NOT NULL,PRIMARY KEY(ID));
</xsl:text>
<xsl:text>CREATE TABLE INSTRUCTIONS(ID INT UNSIGNED NOT NULL,RECIPEID INT UNSIGNED NOT NULL REFERENCES RECIPE,TITLE VARCHAR(100),INSTRUCTIONS TEXT NOT NULL,PRIMARY KEY(RECIPEID,ID),KEY(RECIPEID));
</xsl:text>
<xsl:text>CREATE TABLE SECTION(ID INT UNSIGNED NOT NULL,RECIPEID INT UNSIGNED NOT NULL REFERENCES RECIPE,TITLE VARCHAR(100),PRIMARY KEY(RECIPEID,ID),KEY(RECIPEID));
</xsl:text>
<xsl:text>CREATE TABLE INGREDIENT(RECIPEID INT UNSIGNED NOT NULL REFERENCES RECIPE,SECTIONID INT UNSIGNED NOT NULL,ID INT UNSIGNED NOT NULL,EDIBLEID INT UNSIGNED NOT NULL REFERENCES EDIBLE,AMOUNTDOUBLE FLOAT,AMOUNTNOMINATOR INT UNSIGNED,AMOUNTDENOMINATOR INT UNSIGNED,UNIT VARCHAR(20) BINARY,PREP VARCHAR(100) NOT NULL,KEY(RECIPEID,SECTIONID,EDIBLEID));
</xsl:text>
<xsl:text>CREATE TABLE SELECTION(RECIPEID INT UNSIGNED NOT NULL REFERENCES RECIPE,USER VARCHAR(16) NOT NULL,PRIMARY KEY(USER,RECIPEID),KEY(RECIPEID));
</xsl:text>
</xsl:template>
<!-- Destruct database tables. -->
<xsl:template match="destruct">
<xsl:text>DROP TABLE IF EXISTS COOKBOOK;
</xsl:text>
<xsl:text>DROP TABLE IF EXISTS EDIBLE;
</xsl:text>
<xsl:text>DROP TABLE IF EXISTS CATEGORY;
</xsl:text>
<xsl:text>DROP TABLE IF EXISTS CATEGORIES;
</xsl:text>
<xsl:text>DROP TABLE IF EXISTS RECIPE;
</xsl:text>
<xsl:text>DROP TABLE IF EXISTS INSTRUCTIONS;
</xsl:text>
<xsl:text>DROP TABLE IF EXISTS SECTION;
</xsl:text>
<xsl:text>DROP TABLE IF EXISTS INGREDIENT;
</xsl:text>
<xsl:text>DROP TABLE IF EXISTS SELECTION;
</xsl:text>
</xsl:template>
<!-- Query version-number. -->
<xsl:template match="version">
<xsl:text># OPEN version;
</xsl:text>
<xsl:text>SELECT (VERSION*1000+SUBVERSION) FROM COOKBOOK;
</xsl:text>
<xsl:text># CLOSE version;
</xsl:text>
</xsl:template>
<!-- Update database version. -->
<xsl:template match="update">
<xsl:if test="version >= 13">
<xsl:if test="version <= 13">
<xsl:text>CREATE TABLE SELECTION(RECIPEID INT UNSIGNED NOT NULL REFERENCES RECIPE,USER VARCHAR(16) NOT NULL,PRIMARY KEY(USER,RECIPEID),KEY(RECIPEID));
</xsl:text>
<xsl:text>UPDATE COOKBOOK SET SUBVERSION=14;
</xsl:text>
</xsl:if>
<xsl:if test="version <= 14">
<!-- No values are specified (all IDs will be zero?). -->
<xsl:text>ALTER TABLE CATEGORY ADD ID INT UNSIGNED NOT NULL AFTER RECIPEID;
</xsl:text>
<xsl:text>UPDATE COOKBOOK SET SUBVERSION=15;
</xsl:text>
</xsl:if>
<xsl:if test="version <= 15">
<xsl:text>ALTER TABLE INGREDIENT ADD PREP VARCHAR(100) NOT NULL AFTER UNIT;
</xsl:text>
<xsl:text>UPDATE COOKBOOK SET SUBVERSION=16;
</xsl:text>
</xsl:if>
<xsl:if test="version <= 16">
<xsl:text>CREATE TABLE CATEGORIES(ID INT UNSIGNED NOT NULL AUTO_INCREMENT,NAME VARCHAR(100) UNIQUE NOT NULL,PRIMARY KEY(ID),KEY(NAME));
</xsl:text>
<xsl:text>INSERT INTO CATEGORIES SELECT NULL,C.NAME FROM CATEGORY C GROUP BY C.NAME;
</xsl:text>
<xsl:text>ALTER TABLE CATEGORY ADD CATEGORYID INT UNSIGNED NOT NULL REFERENCES CATEGORIES AFTER NAME;
</xsl:text>
<xsl:text>UPDATE CATEGORY,CATEGORIES SET CATEGORY.CATEGORYID=CATEGORIES.ID WHERE CATEGORIES.NAME=CATEGORY.NAME;
</xsl:text>
<xsl:text>ALTER TABLE CATEGORY DROP COLUMN NAME;
</xsl:text>
<xsl:text>UPDATE COOKBOOK SET SUBVERSION=17;
</xsl:text>
</xsl:if>
<xsl:if test="version <= 17">
<xsl:text>ALTER TABLE RECIPE ADD LANGUAGE CHAR(2) NOT NULL AFTER UNIT;
</xsl:text>
<xsl:text>UPDATE RECIPE SET LANGUAGE='en';
</xsl:text>
<xsl:text>UPDATE COOKBOOK SET SUBVERSION=18;
</xsl:text>
</xsl:if>
<xsl:if test="version <= 18">
<xsl:text>ALTER TABLE INGREDIENT ADD ID INT UNSIGNED NOT NULL AFTER SECTIONID;
</xsl:text>
<xsl:text>UPDATE COOKBOOK SET SUBVERSION=19;
</xsl:text>
</xsl:if>
<xsl:if test="version <= 19">
<!-- Resolve unit names for new database version. Unfortunately english only,
because I don't want to clutter this script with different languages. -->
<xsl:text>ALTER TABLE INGREDIENT CHANGE UNIT UNIT VARCHAR(20);
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='per serving' WHERE UNIT='x';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='small' WHERE UNIT='sm';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='medium' WHERE UNIT='md';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='large' WHERE UNIT='lg';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='can' WHERE UNIT='cn';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='package' WHERE UNIT='pk';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='pinch' WHERE UNIT='pn';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='drop' WHERE UNIT='dr';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='dash' WHERE UNIT='ds';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='carton' WHERE UNIT='ct';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='bunch' WHERE UNIT='bn';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='slice' WHERE UNIT='sl';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='each' WHERE UNIT='ea';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='teaspoon' WHERE UNIT='ts';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='tablespoon' WHERE UNIT='tb';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='tablespoon' WHERE UNIT='T';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='fluid ounce' WHERE UNIT='fl';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='cup' WHERE UNIT='c';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='pint' WHERE UNIT='pt';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='quart' WHERE UNIT='qt';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='gallon' WHERE UNIT='ga';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='ounce' WHERE UNIT='oz';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='pound' WHERE UNIT='lb';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='milliliter' WHERE UNIT='ml';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='cubic cm' WHERE UNIT='cb';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='centiliter' WHERE UNIT='cl';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='deciliter' WHERE UNIT='dl';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='liter' WHERE UNIT='l';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='milligram' WHERE UNIT='mg';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='centigram' WHERE UNIT='cg';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='decigram' WHERE UNIT='dg';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='gram' WHERE UNIT='g';
</xsl:text>
<xsl:text>UPDATE INGREDIENT SET UNIT='kilogram' WHERE UNIT='kg';
</xsl:text>
<xsl:text>UPDATE COOKBOOK SET SUBVERSION=20;
</xsl:text>
</xsl:if>
<xsl:if test="version <= 20">
<xsl:text>ALTER TABLE INGREDIENT CHANGE AMOUNT AMOUNTDOUBLE FLOAT;
</xsl:text>
<xsl:text>ALTER TABLE INGREDIENT ADD AMOUNTNOMINATOR INT UNSIGNED AFTER AMOUNTDOUBLE;
</xsl:text>
<xsl:text>ALTER TABLE INGREDIENT ADD AMOUNTDENOMINATOR INT UNSIGNED AFTER AMOUNTNOMINATOR;
</xsl:text>
<xsl:text>UPDATE COOKBOOK SET SUBVERSION=21;
</xsl:text>
</xsl:if>
</xsl:if>
</xsl:template>
<!-- Retrieve list of categories. -->
<xsl:template match="categories">
<xsl:text># OPEN categories;
</xsl:text>
<xsl:text>SELECT COUNT(CY.RECIPEID),CIES.NAME FROM CATEGORY CY,CATEGORIES CIES WHERE CIES.ID=CY.CATEGORYID GROUP BY CIES.ID;
</xsl:text>
<xsl:text># CLOSE categories;
</xsl:text>
</xsl:template>
<!-- Insert a bunch of recipes. -->
<xsl:template match="insert">
<xsl:apply-templates select="recipe"/>
</xsl:template>
<!-- Replace a single recipe. Calls delete-template. -->
<xsl:template match="replace">
<!-- Insert modified recipe. -->
<xsl:apply-templates select="recipe"/>
<!-- Update selection.. -->
<xsl:text>UPDATE SELECTION SET RECIPEID=@RECIPE WHERE RECIPEID=</xsl:text>
<xsl:value-of select="selection/single"/>
<xsl:text>;
</xsl:text>
<!-- Return new number of modified recipe. -->
<xsl:text># OPEN recipe;
</xsl:text>
<xsl:text>SELECT @RECIPE,'</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="recipe/title"/>
</xsl:call-template>
<xsl:text>';
</xsl:text>
<xsl:text># CLOSE recipe;
</xsl:text>
<!-- Remove old recipe. -->
<xsl:apply-templates select="selection"/>
<xsl:call-template name="delete"/>
</xsl:template>
<!-- Get number of recipes. -->
<xsl:template match="count">
<xsl:text># OPEN count;
</xsl:text>
<xsl:choose>
<!-- Optimized query for counting all recipes. -->
<xsl:when test="selection/all">
<xsl:text>SELECT COUNT(ID) FROM RECIPE;
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="selection"/>
<xsl:text>SELECT COUNT(ID) FROM TMP;
</xsl:text>
<xsl:text>DROP TEMPORARY TABLE TMP;
</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text># CLOSE count;
</xsl:text>
</xsl:template>
<!-- Extract some database values. -->
<xsl:template match="statistics">
<xsl:text># OPEN statistics;
</xsl:text>
<xsl:text>SELECT COUNT(ID) FROM RECIPE;
</xsl:text>
<xsl:text>SELECT COUNT(ID) FROM CATEGORIES;
</xsl:text>
<xsl:text>SELECT COUNT(ID) FROM EDIBLE;
</xsl:text>
<xsl:text># CLOSE statistics;
</xsl:text>
</xsl:template>
<!-- Delete recipes. Invokes delete-template. -->
<xsl:template match="delete">
<xsl:apply-templates select="selection"/>
<xsl:text># OPEN delete;
</xsl:text>
<xsl:call-template name="delete"/>
<xsl:text># CLOSE delete;
</xsl:text>
</xsl:template>
<!-- Retrieve recipes. -->
<xsl:template match="query">
<xsl:apply-templates select="selection"/>
<xsl:text># OPEN query;
</xsl:text>
<xsl:text># OPEN title;
</xsl:text>
<xsl:text>SELECT R.ID,R.TITLE,R.AMOUNT,R.UNIT FROM RECIPE R,TMP T WHERE R.ID=T.ID ORDER BY T.ID;
</xsl:text>
<xsl:text># CLOSE title;
</xsl:text>
<xsl:text># OPEN categories;
</xsl:text>
<xsl:text>SELECT CY.RECIPEID,CIES.NAME FROM CATEGORY CY,CATEGORIES CIES,TMP T WHERE CY.RECIPEID=T.ID AND CIES.ID=CY.CATEGORYID ORDER BY CY.ID;
</xsl:text>
<xsl:text># CLOSE categories;
</xsl:text>
<xsl:text># OPEN section;
</xsl:text>
<xsl:text>SELECT S.RECIPEID,S.ID,S.TITLE FROM SECTION S,TMP T WHERE S.RECIPEID=T.ID ORDER BY S.ID;
</xsl:text>
<xsl:text># CLOSE section;
</xsl:text>
<xsl:text># OPEN ingredient;
</xsl:text>
<xsl:text>SELECT I.RECIPEID,I.SECTIONID,I.AMOUNTDOUBLE,I.AMOUNTNOMINATOR,I.AMOUNTDENOMINATOR,I.UNIT,E.NAME,I.PREP FROM TMP T,INGREDIENT I,EDIBLE E WHERE I.RECIPEID=T.ID AND E.ID=I.EDIBLEID ORDER BY I.SECTIONID,I.ID;
</xsl:text>
<xsl:text># CLOSE ingredient;
</xsl:text>
<xsl:text># OPEN instructions;
</xsl:text>
<xsl:text>SELECT I.RECIPEID,I.TITLE,I.INSTRUCTIONS FROM INSTRUCTIONS I,TMP T WHERE I.RECIPEID=T.ID ORDER BY I.ID;
</xsl:text>
<xsl:text># CLOSE instructions;
</xsl:text>
<xsl:text># CLOSE query;
</xsl:text>
<xsl:text>DROP TEMPORARY TABLE TMP;
</xsl:text>
</xsl:template>
<!-- Change user's selection. -->
<xsl:template match="select">
<xsl:apply-templates select="selection"/>
<xsl:choose>
<xsl:when test="state=1">
<xsl:text>INSERT IGNORE INTO SELECTION SELECT T.ID,'</xsl:text>
<xsl:value-of select="@user"/>
<xsl:text>' FROM TMP T;
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>DELETE FROM SELECTION USING SELECTION,TMP WHERE SELECTION.USER='</xsl:text>
<xsl:value-of select="@user"/>
<xsl:text>' AND SELECTION.RECIPEID=TMP.ID;
</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text>DROP TEMPORARY TABLE TMP;
</xsl:text>
</xsl:template>
<!-- List recipe-id and title. -->
<xsl:template match="list">
<xsl:apply-templates select="selection"/>
<!-- Add selection-information. -->
<xsl:text>ALTER TABLE TMP ADD COLUMN SELECTED BIT NOT NULL;
</xsl:text>
<xsl:text>UPDATE TMP T,SELECTION S SET T.SELECTED=1 WHERE T.ID=S.RECIPEID AND USER='</xsl:text>
<xsl:value-of select="selection/@user"/>
<xsl:text>';
</xsl:text>
<xsl:text># OPEN results;
</xsl:text>
<!-- Extract id-numbers and titles of remaining recipes. -->
<xsl:text>SELECT T.ID,R.TITLE,T.SELECTED FROM TMP T,RECIPE R WHERE R.ID=T.ID </xsl:text>
<xsl:if test="@ordered='true'">
<xsl:text>ORDER BY R.TITLE </xsl:text>
</xsl:if>
<xsl:text>LIMIT 0,</xsl:text>
<xsl:value-of select="@limit"/>
<xsl:text>;
</xsl:text>
<xsl:text># CLOSE results;
</xsl:text>
<xsl:text>DROP TEMPORARY TABLE TMP;
</xsl:text>
</xsl:template>
<!-- =============== Secondary templates =========================== -->
<!-- Select recipes. TMP is used as return value. -->
<xsl:template match="selection">
<xsl:text>CREATE TEMPORARY TABLE TMP(ID INT UNSIGNED NOT NULL REFERENCES RECIPE,PRIMARY KEY(ID));
</xsl:text>
<xsl:choose>
<!-- Select single recipe. -->
<xsl:when test="single">
<xsl:text>INSERT INTO TMP VALUES(</xsl:text>
<xsl:value-of select="single"/>
<xsl:text>);
</xsl:text>
</xsl:when>
<!-- Select recipes with id within a certain range. -->
<xsl:when test="range">
<xsl:text>INSERT INTO TMP SELECT ID FROM RECIPE LIMIT </xsl:text>
<xsl:value-of select="range/skip"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="range/count"/>
<xsl:text>;
</xsl:text>
</xsl:when>
<xsl:when test="list">
<xsl:for-each select="list/id">
<xsl:text>INSERT INTO TMP VALUES(</xsl:text>
<xsl:value-of select="."/>
<xsl:text>);
</xsl:text>
</xsl:for-each>
</xsl:when>
<!-- Search recipes. -->
<xsl:when test="search">
<xsl:choose>
<xsl:when test="search/selection">
<xsl:text>INSERT INTO TMP SELECT RECIPEID FROM SELECTION WHERE USER='</xsl:text>
<xsl:value-of select="@user"/>
<xsl:text>';
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>INSERT INTO TMP SELECT ID FROM RECIPE;
</xsl:text>
</xsl:otherwise>
</xsl:choose>
<!-- Select by title keywords.
Mode 0: Single phrase.
Mode 1: All words.
Mode 2: Any word. -->
<xsl:if test="search/title">
<xsl:text>CREATE TEMPORARY TABLE TMP2(ID INT UNSIGNED NOT NULL REFERENCES RECIPE,PRIMARY KEY(ID));
</xsl:text>
<xsl:text>INSERT INTO TMP2 SELECT T.ID FROM TMP T,RECIPE R WHERE T.ID=R.ID AND </xsl:text>
<xsl:for-each select="search/title/word">
<xsl:text>R.TITLE </xsl:text>
<xsl:text>RLIKE '(^|[^0-9a-zA-Z])</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>([^0-9a-zA-Z]|$)'</xsl:text>
<xsl:choose>
<xsl:when test="not(position()=last())">
<xsl:text> AND </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>;
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!-- Drop old list ... -->
<xsl:text>DROP TEMPORARY TABLE TMP;
</xsl:text>
<!-- ... and replace with new filtered list. -->
<xsl:text>ALTER TABLE TMP2 RENAME TMP;
</xsl:text>
</xsl:if>
<!-- Filter by recipe's categories -->
<xsl:if test="search/category">
<xsl:text>CREATE TEMPORARY TABLE TMP2(ID INT UNSIGNED NOT NULL REFERENCES CATEGORIES,PRIMARY KEY(ID));
</xsl:text>
<xsl:text>INSERT INTO TMP2 SELECT ID FROM CATEGORIES WHERE NAME</xsl:text>
<!-- Perform positive or negative query ((not) in category list). -->
<xsl:if test="search/category/@positive='0'"> NOT</xsl:if>
<xsl:text> IN (</xsl:text>
<!-- The categories are forming a set. -->
<xsl:for-each select="search/category/word">
<xsl:text>'</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>'</xsl:text>
<xsl:if test="not(position()=last())">,</xsl:if>
</xsl:for-each>
<xsl:text>);
</xsl:text>
<xsl:text>CREATE TEMPORARY TABLE TMP3(ID INT UNSIGNED NOT NULL REFERENCES RECIPE,PRIMARY KEY(ID));
</xsl:text>
<xsl:text>INSERT INTO TMP3 SELECT T.ID FROM TMP T,CATEGORY C,TMP2 T2 WHERE C.RECIPEID=T.ID AND C.CATEGORYID=T2.ID GROUP BY T.ID;
</xsl:text>
<xsl:text>DROP TEMPORARY TABLE TMP2;
</xsl:text>
<!-- Drop old list ... -->
<xsl:text>DROP TEMPORARY TABLE TMP;
</xsl:text>
<!-- ... and replace with new filtered list. -->
<xsl:text>ALTER TABLE TMP3 RENAME TMP;
</xsl:text>
</xsl:if>
<!-- Filter recipes with ingredient-specification. -->
<xsl:if test="search/ingredient">
<xsl:for-each select="search/ingredient/word[@positive='1']">
<xsl:text>CREATE TEMPORARY TABLE TMP2(ID INT UNSIGNED NOT NULL REFERENCES RECIPE,PRIMARY KEY(ID));
</xsl:text>
<xsl:text>INSERT INTO TMP2 SELECT T.ID FROM TMP T,INGREDIENT I,EDIBLE E WHERE T.ID=I.RECIPEID AND I.EDIBLEID=E.ID AND E.NAME RLIKE '(^|[^0-9a-zA-Z])</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>([^0-9a-zA-Z]|$)' GROUP BY T.ID;
</xsl:text>
<xsl:text>DROP TEMPORARY TABLE TMP;
</xsl:text>
<xsl:text>ALTER TABLE TMP2 RENAME TMP;
</xsl:text>
</xsl:for-each>
<xsl:for-each select="search/ingredient/word[@positive='0']">
<xsl:text>DELETE FROM TMP USING TMP,INGREDIENT,EDIBLE WHERE TMP.ID=INGREDIENT.RECIPEID AND INGREDIENT.EDIBLEID=EDIBLE.ID AND EDIBLE.NAME RLIKE '(^|[^0-9a-zA-Z])</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>([^0-9a-zA-Z]|$)';
</xsl:text>
</xsl:for-each>
</xsl:if>
</xsl:when>
<!-- Select all recipes. -->
<xsl:when test="all">
<xsl:text>INSERT INTO TMP SELECT ID FROM RECIPE;
</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- Store recipe in database.
The variable @RECIPE is used as return-value. -->
<xsl:template match="recipe">
<!-- Generate entry with title, yield and instructions. -->
<xsl:text>INSERT INTO RECIPE VALUES(NULL,'</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="title"/>
</xsl:call-template>
<xsl:text>',</xsl:text>
<xsl:value-of select="servings/amount"/>
<xsl:text>,'</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="servings/unit"/>
</xsl:call-template>
<xsl:text>','</xsl:text>
<xsl:variable name="lang-scope"
select="(ancestor-or-self::*[@lang]
|ancestor-or-self::*[@xml:lang])[last()]"/>
<xsl:variable name="lang-attr"
select="($lang-scope/@lang | $lang-scope/@xml:lang)[1]"/>
<xsl:choose>
<xsl:when test="string($lang-attr) = ''">en</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$lang-attr"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>');
</xsl:text>
<!-- Memorize recipe's id. -->
<xsl:text>SET @RECIPE := LAST_INSERT_ID();
</xsl:text>
<xsl:for-each select="instructions/section">
<xsl:text>INSERT INTO INSTRUCTIONS VALUES(</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>,@RECIPE,'</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="title"/>
</xsl:call-template>
<xsl:text>','</xsl:text>
<xsl:for-each select="par">
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:if test="not(position()=last())">\n</xsl:if>
</xsl:for-each>
<xsl:text>');
</xsl:text>
</xsl:for-each>
<!-- Create links for categories. -->
<xsl:for-each select="categories/category">
<xsl:text>INSERT IGNORE INTO CATEGORIES VALUES(NULL,'</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>');
</xsl:text>
<xsl:text>SELECT @CATEGORY:=ID FROM CATEGORIES WHERE NAME='</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>';
</xsl:text>
<xsl:text>INSERT INTO CATEGORY VALUES(@RECIPE,</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>,@CATEGORY);
</xsl:text>
</xsl:for-each>
<!-- Store information about ingredients. -->
<xsl:for-each select="ingredients/section">
<!-- Memorize section-number. -->
<xsl:text>SET @SECTION := </xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>;
</xsl:text>
<!-- Store section title. -->
<xsl:text>INSERT INTO SECTION VALUES(@SECTION,@RECIPE,'</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="title"/>
</xsl:call-template>
<xsl:text>');
</xsl:text>
<xsl:for-each select="ingredient">
<!-- Store name of ingredient. -->
<xsl:text>INSERT IGNORE INTO EDIBLE VALUES(NULL,'</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="name"/>
</xsl:call-template>
<xsl:text>');
</xsl:text>
<!-- Get key of ingredient. -->
<xsl:text>SELECT @INGREDIENT:=ID FROM EDIBLE WHERE NAME='</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="name"/>
</xsl:call-template>
<xsl:text>';
</xsl:text>
<!-- Make entry in ingredient table. -->
<xsl:text>INSERT INTO INGREDIENT VALUES(@RECIPE,@SECTION,</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>,@INGREDIENT,</xsl:text>
<xsl:choose>
<xsl:when test="amount/float">
<xsl:value-of select="amount/float"/>
</xsl:when>
<xsl:otherwise>NULL</xsl:otherwise>
</xsl:choose>
<xsl:text>,</xsl:text>
<xsl:choose>
<xsl:when test="amount/fraction">
<xsl:value-of select="amount/fraction/nominator"/>
</xsl:when>
<xsl:otherwise>NULL</xsl:otherwise>
</xsl:choose>
<xsl:text>,</xsl:text>
<xsl:choose>
<xsl:when test="amount/fraction">
<xsl:value-of select="amount/fraction/denominator"/>
</xsl:when>
<xsl:otherwise>NULL</xsl:otherwise>
</xsl:choose>
<xsl:text>,'</xsl:text>
<xsl:value-of select="unit"/>
<xsl:text>','</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="prep"/>
</xsl:call-template>
<xsl:text>');
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<!-- Delete template. TMP is used as argument. -->
<xsl:template name="delete">
<xsl:text>DELETE FROM RECIPE USING RECIPE,TMP WHERE RECIPE.ID=TMP.ID;
</xsl:text>
<xsl:text>DELETE FROM CATEGORY USING CATEGORY,TMP WHERE CATEGORY.RECIPEID=TMP.ID;
</xsl:text>
<xsl:text>DELETE FROM INSTRUCTIONS USING INSTRUCTIONS,TMP WHERE INSTRUCTIONS.RECIPEID=TMP.ID;
</xsl:text>
<xsl:text>DELETE FROM SECTION USING SECTION,TMP WHERE SECTION.RECIPEID=TMP.ID;
</xsl:text>
<xsl:text>DELETE FROM INGREDIENT USING INGREDIENT,TMP WHERE INGREDIENT.RECIPEID=TMP.ID;
</xsl:text>
<xsl:text>DELETE FROM SELECTION USING SELECTION,TMP WHERE SELECTION.RECIPEID=TMP.ID;
</xsl:text>
<xsl:text>SELECT ID FROM TMP;
</xsl:text>
<xsl:text>DROP TEMPORARY TABLE TMP;
</xsl:text>
</xsl:template>
<!-- Prepare text for use in SQL script. -->
<xsl:template name="escape-ws">
<xsl:param name="text"/>
<xsl:choose>
<!-- Escape backslash. -->
<xsl:when test="contains($text,'\')">
<xsl:call-template name="escape-ws">
<xsl:with-param name="text">
<xsl:value-of select="substring-before($text,'\')"/>
</xsl:with-param>
</xsl:call-template>
<xsl:text>\\</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text"
select="substring-after($text,'\')"/>
</xsl:call-template>
</xsl:when>
<!-- Replace return by space -->
<xsl:when test="contains($text,'
')">
<xsl:call-template name="escape-ws">
<xsl:with-param name="text">
<xsl:value-of select="substring-before($text,'
')"/>
</xsl:with-param>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text"
select="substring-after($text,'
')"/>
</xsl:call-template>
</xsl:when>
<!-- Escape tabulator commands. -->
<xsl:when test="contains($text,'	')">
<xsl:call-template name="escape-ws">
<xsl:with-param name="text">
<xsl:value-of select="substring-before($text,'	')"/>
</xsl:with-param>
</xsl:call-template>
<xsl:text>\t</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text"
select="substring-after($text,'	')"/>
</xsl:call-template>
</xsl:when>
<!-- Escape quotation marks. -->
<xsl:when test='contains($text,"'")'>
<xsl:value-of select='substring-before($text,"'")'/>
<xsl:text>''</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text"
select='substring-after($text,"'")'/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
|