| 1 |
|
| 2 |
BEGIN; |
| 3 |
|
| 4 |
/** |
| 5 |
* Samples from PostgreSQL src/tutorial/basics.source |
| 6 |
*/ |
| 7 |
CREATE TABLE weather ( |
| 8 |
city varchar(80), |
| 9 |
temp_lo int, -- low temperature |
| 10 |
temp_hi int, -- high temperature |
| 11 |
prcp real, -- precipitation |
| 12 |
"date" date |
| 13 |
); |
| 14 |
|
| 15 |
CREATE TABLE cities ( |
| 16 |
name varchar(80), |
| 17 |
location point |
| 18 |
); |
| 19 |
|
| 20 |
|
| 21 |
INSERT INTO weather |
| 22 |
VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27'); |
| 23 |
|
| 24 |
INSERT INTO cities |
| 25 |
VALUES ('San Francisco', '(-194.0, 53.0)'); |
| 26 |
|
| 27 |
INSERT INTO weather (city, temp_lo, temp_hi, prcp, "date") |
| 28 |
VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29'); |
| 29 |
|
| 30 |
INSERT INTO weather (date, city, temp_hi, temp_lo) |
| 31 |
VALUES ('1994-11-29', 'Hayward', 54, 37); |
| 32 |
|
| 33 |
|
| 34 |
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, "date" FROM weather; |
| 35 |
|
| 36 |
SELECT city, temp_lo, temp_hi, prcp, "date", location |
| 37 |
FROM weather, cities |
| 38 |
WHERE city = name; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Dollar quotes starting at the end of the line are colored as SQL unless |
| 44 |
* a special language tag is used. Dollar quote syntax coloring is implemented |
| 45 |
* for Perl, Python, JavaScript, and Json. |
| 46 |
*/ |
| 47 |
create or replace function blob_content_chunked( |
| 48 |
in p_data bytea, |
| 49 |
in p_chunk integer) |
| 50 |
returns setof bytea as $$ |
| 51 |
-- Still SQL comments |
| 52 |
declare |
| 53 |
v_size integer = octet_length(p_data); |
| 54 |
begin |
| 55 |
for i in 1..v_size by p_chunk loop |
| 56 |
return next substring(p_data from i for p_chunk); |
| 57 |
end loop; |
| 58 |
end; |
| 59 |
$$ language plpgsql stable; |
| 60 |
|
| 61 |
|
| 62 |
-- pl/perl |
| 63 |
CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $perl$ |
| 64 |
# perl comment... |
| 65 |
my ($x,$y) = @_; |
| 66 |
if (! defined $x) { |
| 67 |
if (! defined $y) { return undef; } |
| 68 |
return $y; |
| 69 |
} |
| 70 |
if (! defined $y) { return $x; } |
| 71 |
if ($x > $y) { return $x; } |
| 72 |
return $y; |
| 73 |
$perl$ LANGUAGE plperl; |
| 74 |
|
| 75 |
-- pl/python |
| 76 |
CREATE FUNCTION usesavedplan() RETURNS trigger AS $python$ |
| 77 |
# python comment... |
| 78 |
if SD.has_key("plan"): |
| 79 |
plan = SD["plan"] |
| 80 |
else: |
| 81 |
plan = plpy.prepare("SELECT 1") |
| 82 |
SD["plan"] = plan |
| 83 |
$python$ LANGUAGE plpythonu; |
| 84 |
|
| 85 |
-- pl/v8 (javascript) |
| 86 |
CREATE FUNCTION plv8_test(keys text[], vals text[]) RETURNS text AS $javascript$ |
| 87 |
var o = {}; |
| 88 |
for(var i=0; i<keys.length; i++){ |
| 89 |
o[keys[i]] = vals[i]; |
| 90 |
} |
| 91 |
return JSON.stringify(o); |
| 92 |
$javascript$ LANGUAGE plv8 IMMUTABLE STRICT; |
| 93 |
|
| 94 |
-- json |
| 95 |
select * from json_object_keys($json$ |
| 96 |
{ |
| 97 |
"f1": 5, |
| 98 |
"f2": "test", |
| 99 |
"f3": {} |
| 100 |
} |
| 101 |
$json$); |
| 102 |
|
| 103 |
|
| 104 |
-- psql commands |
| 105 |
\df cash* |
| 106 |
|
| 107 |
|
| 108 |
-- Some string samples. |
| 109 |
select 'don''t do it now;' || 'maybe later'; |
| 110 |
select E'dont\'t do it'; |
| 111 |
select length('some other''s stuff' || $$cat in hat's stuff $$); |
| 112 |
|
| 113 |
select $$ strings |
| 114 |
over multiple |
| 115 |
lines - use dollar quotes |
| 116 |
$$; |
| 117 |
|
| 118 |
END; |
| 119 |
|
| 120 |
-- Anonymous code block |
| 121 |
DO LANGUAGE plpgsql $$ |
| 122 |
BEGIN |
| 123 |
-- code block |
| 124 |
END $$; |
| 125 |
|
| 126 |
DO $$ |
| 127 |
BEGIN |
| 128 |
-- code block |
| 129 |
END; $$; |