|
| 1 | +!SLIDE |
| 2 | +# Chapter 6 |
| 3 | +## The Shoulders of Giants |
| 4 | + |
| 5 | +!SLIDE |
| 6 | +# Installation |
| 7 | +## Install postgresql-contrib |
| 8 | + |
| 9 | +!SLIDE |
| 10 | +# chkpass |
| 11 | +## Encrypted column type designed for passwords |
| 12 | + @@@ bash |
| 13 | + psql database_name -f \ |
| 14 | + /usr/share/postgresql/8.4/contrib/chkpass.sql |
| 15 | + |
| 16 | +!SLIDE |
| 17 | +# chkpass |
| 18 | + @@@ sql |
| 19 | + create table users |
| 20 | + (name varchar(256), password chkpass); |
| 21 | + |
| 22 | + insert into users |
| 23 | + (name, password) values ('nick', 'monkey'); |
| 24 | + |
| 25 | + select * from users; |
| 26 | + name | password |
| 27 | + ------+---------------- |
| 28 | + nick | :ggn3QyV4c5VKw |
| 29 | + |
| 30 | +!SLIDE |
| 31 | +# chkpass |
| 32 | + @@@ sql |
| 33 | + select * from users |
| 34 | + where name = 'nick' and password = 'monkey'; |
| 35 | + name | password |
| 36 | + ------+---------------- |
| 37 | + nick | :ggn3QyV4c5VKw |
| 38 | + (1 row) |
| 39 | + |
| 40 | + select * from users |
| 41 | + where name = 'nick' and password = 'secret'; |
| 42 | + name | password |
| 43 | + ------+---------- |
| 44 | + (0 rows) |
| 45 | + |
| 46 | +!SLIDE |
| 47 | +# hstore |
| 48 | +## Store and query hashes |
| 49 | + @@@ bash |
| 50 | + psql database_name -f \ |
| 51 | + /usr/share/postgresql/8.4/contrib/hstore.sql |
| 52 | + |
| 53 | +!SLIDE |
| 54 | +# hstore |
| 55 | + @@@ sql |
| 56 | + create table user_profiles |
| 57 | + (name varchar(256), profile hstore); |
| 58 | + insert into user_profiles (name, profile) |
| 59 | + values |
| 60 | + ('nick', '"color"=>"blue", "music"=>"indie"'), |
| 61 | + ('joe', '"color"=>"blue", "music"=>"rock"'); |
| 62 | + |
| 63 | + select * from user_profiles; |
| 64 | + name | profile |
| 65 | + ------+---------------------------------- |
| 66 | + nick | "color"=>"blue", "music"=>"indie" |
| 67 | + joe | "color"=>"blue", "music"=>"rock" |
| 68 | + (2 rows) |
| 69 | + |
| 70 | +!SLIDE |
| 71 | +# hstore |
| 72 | + @@@ sql |
| 73 | + select * from user_profiles |
| 74 | + where profile::hstore->'color' = 'blue'; |
| 75 | + name | profile |
| 76 | + ------+----------------------------------- |
| 77 | + nick | "color"=>"blue", "music"=>"indie" |
| 78 | + joe | "color"=>"blue", "music"=>"rock" |
| 79 | + (2 rows) |
| 80 | + |
| 81 | + select * from user_profiles |
| 82 | + where profile::hstore->'music' = 'rock'; |
| 83 | + name | profile |
| 84 | + ------+---------------------------------- |
| 85 | + joe | "color"=>"blue", "music"=>"rock" |
| 86 | + (1 row) |
| 87 | + |
| 88 | + |
| 89 | +!SLIDE |
| 90 | +# ltree |
| 91 | +## Tree structure |
| 92 | +### Examples from PostgreSQL docs (not mine) |
| 93 | + |
| 94 | +!SLIDE |
| 95 | +# ltree |
| 96 | + @@@ sql |
| 97 | + CREATE TABLE test (path ltree); |
| 98 | + INSERT INTO test VALUES |
| 99 | + ('Top'), |
| 100 | + ('Top.Science'), |
| 101 | + ('Top.Science.Astronomy'), |
| 102 | + ('Top.Science.Astronomy.Astrophysics'), |
| 103 | + ('Top.Science.Astronomy.Cosmology'), |
| 104 | + ('Top.Hobbies'), |
| 105 | + ('Top.Hobbies.Amateurs_Astronomy'), |
| 106 | + ('Top.Collections'), |
| 107 | + ('Top.Collections.Pictures'), |
| 108 | + ('Top.Collections.Pictures.Astronomy'), |
| 109 | + ('Top.Collections.Pictures.Astronomy.Stars'), |
| 110 | + ('Top.Collections.Pictures.Astronomy.Galaxies'), |
| 111 | + ('Top.Collections.Pictures.Astronomy.Astronauts'); |
| 112 | + |
| 113 | +!SLIDE |
| 114 | +# ltree |
| 115 | + @@@ sql |
| 116 | + select path from test |
| 117 | + where path <@ 'Top.Science'; |
| 118 | + path |
| 119 | + ------------------------------------ |
| 120 | + Top.Science |
| 121 | + Top.Science.Astronomy |
| 122 | + Top.Science.Astronomy.Astrophysics |
| 123 | + Top.Science.Astronomy.Cosmology |
| 124 | + (4 rows) |
| 125 | + |
| 126 | +!SLIDE |
| 127 | +# ltree |
| 128 | + @@@ sql |
| 129 | + select path from test |
| 130 | + where path @ 'Astro*% & !pictures@'; |
| 131 | + path |
| 132 | + ------------------------------------ |
| 133 | + Top.Science.Astronomy |
| 134 | + Top.Science.Astronomy.Astrophysics |
| 135 | + Top.Science.Astronomy.Cosmology |
| 136 | + Top.Hobbies.Amateurs_Astronomy |
| 137 | + (4 rows) |
| 138 | + |
| 139 | + |
| 140 | +!SLIDE |
| 141 | +# isn |
| 142 | +## EAN13, UPC, ISBN (books), ISMN (music), and ISSN (serials) |
| 143 | +### Auto validation + check bits |
| 144 | +### Auto hyphenation on output |
| 145 | + |
| 146 | +!SLIDE |
| 147 | +# XML Functions |
| 148 | +## Let's convert post names to tag cloud |
| 149 | +## Based on number of comments |
| 150 | + |
| 151 | +!SLIDE |
| 152 | +# Setup some data |
| 153 | + @@@ sql |
| 154 | + create table posts |
| 155 | + (id int, name varchar(256), body text); |
| 156 | + create table comments |
| 157 | + (id int, post_id int, body text); |
| 158 | + insert into posts (id, name, body) values |
| 159 | + (1, 'cool code', 'here is some cool code'), |
| 160 | + (2, 'lunch today', 'I ate a sandwich'); |
| 161 | + insert into comments (id, post_id, body) values |
| 162 | + (1, 1, 'that is cool'), |
| 163 | + (2, 1, 'pretty sweet'), |
| 164 | + (3, 2, 'sandwiches rule'); |
| 165 | + |
| 166 | +!SLIDE |
| 167 | +# Query our cloud |
| 168 | + @@@ sql |
| 169 | + select posts.name, count(comments.id) |
| 170 | + from posts join comments on |
| 171 | + posts.id = comments.post_id |
| 172 | + group by posts.name; |
| 173 | + name | count |
| 174 | + -------------+------- |
| 175 | + cool code | 2 |
| 176 | + lunch today | 1 |
| 177 | + (2 rows) |
| 178 | + |
| 179 | +!SLIDE |
| 180 | +# Output XML |
| 181 | + @@@ sql |
| 182 | + select xmlelement( |
| 183 | + name span, |
| 184 | + xmlattributes( |
| 185 | + count(comments.id) as "data-count" |
| 186 | + ), |
| 187 | + posts.name |
| 188 | + ) |
| 189 | + from posts join comments |
| 190 | + on posts.id = comments.post_id |
| 191 | + group by posts.name; |
| 192 | + |
| 193 | + xmlelement |
| 194 | + ----------------------------------------- |
| 195 | + <span data-count="1">lunch today</span> |
| 196 | + <span data-count="2">cool code</span> |
| 197 | + (2 rows) |
| 198 | + |
0 commit comments