aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorDavid M. Syzdek <syzdek@bindlebinaries.com>2018-07-21 09:15:36 -0800
committerDavid M. Syzdek <syzdek@bindlebinaries.com>2018-07-21 09:15:36 -0800
commitc011686095effadac7a13aa875423281b9ba0a94 (patch)
tree5ebf783c088d1a09e7cde040fbd9eebb95052e02 /examples
parent63dd77aa1e23fea75da3a1aed7ac04fd9de9fcb0 (diff)
downloadscratchpad-c011686095effadac7a13aa875423281b9ba0a94.tar.gz
scratchpad-c011686095effadac7a13aa875423281b9ba0a94.tar.bz2
scratchpad-c011686095effadac7a13aa875423281b9ba0a94.tar.xz
scratchpad-c011686095effadac7a13aa875423281b9ba0a94.zip
reorganizing notes
Diffstat (limited to 'examples')
-rw-r--r--examples/sql/pg_function.sql57
1 files changed, 57 insertions, 0 deletions
diff --git a/examples/sql/pg_function.sql b/examples/sql/pg_function.sql
new file mode 100644
index 0000000..5edbb26
--- /dev/null
+++ b/examples/sql/pg_function.sql
@@ -0,0 +1,57 @@
1
2/*
3 * Source: https://stackoverflow.com/questions/21087710/how-to-write-update-function-stored-procedure-in-postgresql
4 */
5CREATE OR REPLACE FUNCTION update_user(
6 nickname LABEL,
7 full_name TEXT DEFAULT NULL,
8 email EMAIL DEFAULT NULL,
9 uri URI DEFAULT NULL,
10 twitter CITEXT DEFAULT NULL
11) RETURNS BOOLEAN LANGUAGE plpgsql SECURITY DEFINER AS $$
12/*
13 % SELECT update_user(
14 nickname := 'theory',
15 full_name := 'David E. Wheeler',
16 email := 'justatheory@pgxn.org',
17 uri := 'http://www.justatheory.com/',
18 twitter :- 'theory'
19 );
20 update_user
21 ─────────────
22 t
23
24Update the specified user. The user must be active. The nickname cannot be
25changed. The password can only be changed via `change_password()` or
26`reset_password()`. Pass other attributes as:
27
28full_name
29: The full name of the user.
30
31email
32: The email address of the user. Must be a valid email address as verified by
33 [Email::Valid](http://search.cpan.org/perldoc?Email::Valid).
34
35uri
36: Optional URI for the user. Should be a valid URI as verified by
37 [Data::Validate::URI](http://search.cpan.org/perldoc?Data::Validate::URI).
38
39twitter
40: Optional Twitter username. A leading "@" wil be removed.
41
42Returns true if the user was updated, and false if not.
43
44*/
45BEGIN
46 UPDATE users
47 SET full_name = COALESCE(update_user.full_name, users.full_name),
48 email = COALESCE(update_user.email, users.email),
49 uri = COALESCE(update_user.uri, users.uri),
50 twitter = COALESCE(trim(leading '@' FROM update_user.twitter), users.twitter),
51 updated_at = NOW()
52 WHERE users.nickname = update_user.nickname
53 AND users.status = 'active';
54 RETURN FOUND;
55END;
56$$;
57