//a side-by-side reference sheet//
# top[#grammar-invocation grammar and invocation] | [#variables-expressions variables and expressions] | [#arithmetic-logic arithmetic and logic] | [#strings strings] | [#regexes regexes] | [#dates-time dates and time] | [#arrays arrays] | [#user-defined-types user-defined types] | [#generic-types generic types] | [#functions functions] | [#execution-control execution control] | [#exceptions exceptions] | [#file-handles file handles] | [#files files] | [#directories directories] | [#processes-environment processes and environment] | [#libraries-namespaces libraries and namespaces]
||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# version-used[#version-used-note version used] _ @< >@||##gray|//Free Pascal 2.4//##||##gray|//GNAT GCC 4.6//## _ ##gray|//Ada 2012//##||##gray|//Postgres 9.1//##|| ||# show-version[#show-version-note show version] _ @< >@||$ fpc -v||$ gnatgcc -gnat12 @@–@@version||$ psql @@–@@version|| ||||||||~ # grammar-invocation[#grammar-invocation-note grammar and invocation]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# hello-world[#hello-world-note hello word]||$ cat hello.pas _ program Hello; _ begin _ @< >@WriteLn(‘hello world!’); _ end. _ _ $ fpc hello.pas _ _ $ ./hello||$ cat hello.adb _ with Text_IO; _ use Text_IO; _ _ procedure Hello is _ begin _ @< >@Put_Line(“Hello World!”); _ end Hello; _ _ $ gnatmake -gnat12 hello.adb _ _ $ ./hello _ Hello World!||##gray|//at psql prompt://## _ @@>@@ create or replace function hello() _ returns varchar as $$ _ begin _ @< >@return ‘Hello World!’; _ end; _ $$ language plpgsql; _ CREATE FUNCTION _ _ @@>@@ select hello(); _ hello _ @@————–@@ _ Hello World! _ (1 row)|| ||# file-suffixes[#file-suffixes-note file suffixes]||foo.pas _ foo.o _ foo||##gray|//source://## _ foo.ads: specification _ foo.adb: body _ _ ##gray|//compiler generated://## _ foo.adi: compilation information _ foo.o: object _ foo|| || ||# source-code-encoding[#source-code-encoding-note source code encoding]|| ||##gray|@@–@@ default is ISO 8859-1:## _ pragma WideCharacterEncoding(UTF8)|| || ||# eol-comment[#eol-comment-note end-of-line comment]||##gray|//not ISO Pascal://## _ @@//@@ ##gray|//comment line//## _ @@//@@ ##gray|//another line//##||@@–@@ ##gray|//comment line//## _ @@–@@ ##gray|//another line//##||@@–@@ ##gray|//comment line//## _ @@–@@ ##gray|//another line//##|| ||# multiple-line-comment[#multiple-line-comment-note multiple line comment]||(* ##gray|//comment line//## _ ##gray|//another line//## *) _ _ { ##gray|//comment line//## _ ##gray|//another line//## }||##gray|//none//##||##gray|//none//##|| ||||||||~ # variables-expressions[#variables-expressions-note variables and expressions]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# case-sensitive-id[#case-sensitive-id-note are identifiers case sensitive] _ @< >@||##gray|//no//##||##gray|//no//##||##gray|//no//##|| ||# declarations[#declarations-note declare constant, type, and variable]||program Foo; _ const _ @< >@PI: Real = 3.14159; _ type _ @< >@Customer = record _ @< >@@< >@Id: Integer; _ @< >@@< >@Name: String; _ @< >@end; _ var _ @< >@I: Integer; _ @< >@C: Customer; _ begin _ @< >@##gray|//body of program//##||procedure Foo is _ @< >@Pi: constant Float := 3.14; _ @< >@N: Integer; _ @< >@type Customer is Record _ @< >@@< >@Id: Integer; _ @< >@@< >@Name: String(1..4); _ @< >@end Record; _ @< >@C: Customer; _ begin _ @< >@##gray|//body of program//##|| create type customer as ( _ @< >@id integer, _ @< >@name text _ ); _ _ create or replace function foo() returns void as $$ _ declare _ @< >@pi numeric(10,4) = 3.14; _ @< >@i integer = 42; _ @< >@c customer%rowtype; _ begin _ @< >@return; _ end $$ language plpgsql;|| ||[#block block with local scope]|| ||declare _ @< >@N: Integer; _ begin _ @< >@N := 7; _ end;||declare _ @< >@i integer := 3; _ begin _ @< >@raise notice ‘i is %’, i; _ end;|| ||# assignment[#assignment-note assignment] _ @< >@||X := 1;||X := 1;|| x = 1;|| ||# pointer-declaration[#pointer-declaration-note pointer declaration]||IP: ^Integer;||type Integer_Ptr is access Integer; _ Ip: Integer_Ptr;||##gray|//none//##|| ||# allocate-heap[#allocate-heap-note allocate heap] _ @< >@||new(IP);||Ip := new Integer;||##gray|//none//##|| ||# free-heap[#free-heap-note free heap] _ @< >@||dispose(IP);||with Ada.Unchecked_Deallocation; _ _ procedure Free_Example is _ @< >@type Integer_Ptr is access Integer; _ @< >@procedure Free is new Ada.Unchecked_Deallocation( _ @< >@@< >@Integer, Integer_Ptr); _ @< >@Ip: Integer_Ptr; _ begin _ @< >@Ip := new Integer; _ @< >@Ip.all := 7; _ @< >@Free(Ip); _ end Hello;||##gray|//none//##|| ||# deference-pointer[#dereference-pointer-note dereference pointer]||IP^ := 7; _ Ans := 6 * IP^;||Ip.all := 7; _ ans := 6 * Ip.all;||##gray|//none//##|| ||# null[#null-note null literal]||##gray|//can only be assigned to pointers://## _ nil||##gray|//can only be assigned to access types://## _ null||NULL|| ||# null-test[#null-test-note null test]||X = nil||X = null||x is NULL _ ##gray|x = NULL //is always false//##|| ||# coalesce[#coalesce-note coalesce] _ @< >@||##gray|//none//##|| ||7 + coalesce(x, 0)|| ||# nullif[#nullif-note nullif] _ @< >@||##gray|//none//##|| ||nullif(x, 0)|| ||# conditional-expr[#conditional-expr-note conditional expression] _ @< >@||##gray|//none//##||##gray|@@–@@ Ada 2012:## _ (if X > 0 then X else -X)||case when x > 0 then x else -x end|| ||||||||~ # arithmetic-logic[#arithmetic-logic-note arithmetic and logic]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# boolean-type[#boolean-type-note boolean type] _ @< >@||Boolean||Boolean||BOOL BOOLEAN|| ||# true-false[#true-false-note true and false] _ @< >@||True False||True False||TRUE FALSE|| ||# falsehoods[#falsehoods-note falsehoods]||False _ ##gray|//non booleans cause error in boolean context//##||False _ ##gray|//non booleans cause error in boolean context//##||FALSE NULL 0 _ ##gray|//strings and floats cause error in boolean context//##|| ||# logical-op[#logical-op-note logical operators] _ @< >@||and or xor not||and or xor not||AND OR ##gray|//none//## NOT|| ||# short-circuit-op[#short-circuit-op-note short circuit operators]||##gray|{ in Free Pascal ‘and’ and ‘or’ also short circuit }## _ and_then _ or_else||and then _ or else||AND _ OR|| ||# int-type[#int-type-note integer type]||Integer||Integer||smallint: ##gray|//2 bytes//## _ integer: ##gray|//4 bytes//## _ bigint: ##gray|//8 bytes//##|| ||# float-type[#float-type-note float type]||Real||Float||real: ##gray|//4 bytes//## _ double precision: ##gray|//8 bytes//##|| ||# fixed-type[#fixed-type-note fixed type] _ @< >@||##gray|//four fractional digits://## _ Currency|| type Currency is delta 0.01 digits 18; _ Amt: Currency; _ _ Amt := 3.99;||numeric(##gray|//digits//##, ##gray|//fractional_digits//##)|| ||# relational-op[#relational-op-note relational operators] _ @< >@||@@=@@ <> < > <= >=||@@=@@ /= < > <= >=||@@=@@ != ##gray|//also://## <> < > <= >=|| ||# min-max[#min-max-note min and max]||uses Math; _ _ Min(1, 2) _ Max(1, 2) _ MinIntValue([1, 2, 3]) _ MaxIntValue([1, 2, 3]) _ MinValue([1.0, 2.0, 3.0]) _ MaxValue([1.0, 2.0, 3.0])||Integer’Min(1, 2) _ Integer’Max(1, 2) _ Integer’Min(1, Integer’Min(2, 3)) _ Integer’Max(1, Integer’Max(2, 3)) _ Float’Min(1.0, Float’Min(2.0, 3.0)) _ Float’Max(1.0, Float’Max(2.0, 3.0))||least(1,2,3) _ greatest(1,2,3)|| ||# arith-op[#arith-op-note arithmetic operators] _ @< >@||+ - * / div mod||+ - * ##gray|//none//## / mod ##gray|//or//## rem||+ - * ##gray|//none//## / %|| ||# int-div[#int-div-note integer division] _ @< >@||7 div 3||7 / 3||7 / 3|| ||# int-div-zero[#int-div-zero-note integer division by zero] _ @< >@||##gray|//raises//## EDivByZero||##gray|//raises//## ConstraintError||##gray|//raises//## divisionby_zero|| ||# float-div[#float-div-note float division] _ @< >@||7 / 3||Float(7) / Float(3)||7 * 1.0 / 3|| ||# float-div-zero[#float-div-zero-note float division by zero] _ @< >@||+Inf||##gray|//raises//## ConstraintError||##gray|//raises//## divisionby_zero|| ||# power[#power-note power]||uses Math; _ _ Power(2, 16);||2 ** 16||2 ^ 16|| ||# sqrt[#sqrt-note sqrt] _ @< >@||Sqrt(2)||with Ada.Numerics.Elementary_Functions; _ use Ada.Numerics.Elementary_Functions; _ _ ##gray|@@–@@ no implicit type conversion of Integer to Float:## _ Sqrt(2.0)||sqrt(2)|| ||# sqrt-negative-one[#sqrt-negative-one-note sqrt -1] _ @< >@||##gray|//raises//## EInvalidOp||##gray|//raises//## Ada.Numerics.ArgumentError||##gray|//raises//## invalidargumentforpower_function|| ||# transcendental-func[#transcendental-func-note transcendental functions]||uses Math; _ _ Exp Ln Sin Cos Tan ArcSin ArcCos ArcTan ArcTan2||with Ada.Numerics.Elementary_Functions; _ use Ada.Numerics.Elementary_Functions; _ _ Exp Log Sin Cos Tan Arcsin Arccos ##gray|//none//## Arctan||exp ln sin cos tan asin acos atan atan2|| ||# float-truncation[#float-truncation-note float truncation] _ ##gray|//towards zero, to nearest integer, down, up//##||uses Math; _ _ Trunc Round Floor Ceil||##gray|//return Float://## _ ##gray|//??//## Float’Rounding Float’Floor Float’Ceiling||trunc round floor ceil|| ||# absolute-val[#absolute-val-note absolute value] _ ##gray|//and sign//##||Abs(-7) _ ##gray|//none//##||Abs(-7) _ ##gray|//none//##||abs(-7) _ sign(-7)|| ||# int-overflow[#int-overflow-note integer overflow] _ @< >@||##gray|//modular arithmetic//##||##gray|//modular arithmetic//##||##gray|//raises//## numericvalueoutofrange|| ||# float-overflow[#float-overflow-note float overflow] _ @< >@||##gray|//raises//## EOverflow||+Inf@@*******@@||##gray|//raises//## numericvalueoutofrange|| ||# random-num[#random-num-note random number] _ ##gray|//integer, float//##||Random(100) _ Random||with Ada.Numerics.Float_Random; _ with Ada.Numerics.Discrete_Random; _ use Ada.Numerics; _ _ procedure Foo is _ @< >@type Rand_Range is range 0..99; _ @< >@package RandInt is new DiscreteRandom(Rand_Range); _ @< >@IG: Rand_Int.Generator; _ @< >@FG: Float_Random.Generator; _ begin _ @< >@##gray|//use//## Rand_Int.Random(IG) _ @< >@##gray|//use//## Float_Random.Random(FG)||floor(100 * random()) _ random()|| ||# bit-op[#bit-op-note bit operators] _ @< >@||shl shr and or xor not|| ||@@ << >> @@ & | ^ ~|| ||||||||~ # strings[#strings-note strings]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||[#string-literal string literal] _ @< >@||’Don’’t say “foo”’||"Don’t say ““foo”””||’Don’’t say “foo”’|| ||[#fixed-string-type fixed length string type]|| ||##gray|@@–@@ error unless string length is 6## _ String(1..6) _ WideWideString(1..6)||##gray|//pads length to// n //with spaces://## _ char(##gray|//n//##)|| ||[#bounded-string-type bounded length string type]|| || ||##gray|//error if// n //exceeded://## _ varchar(##gray|//n//##)|| ||[#unbounded-string-type unbounded length string type]|| ||with Ada.Strings.Unbounded; _ _ Ada.Strings.Unbounded.Unbounded_String||text|| ||[#character-type character type]|| ||Character ##gray|//1 byte//## _ WideWideCharacter ##gray|//4 bytes//##||char(1)|| ||[#chr-ord chr and ord]||Chr(65) _ Ord(‘A’)|| ||chr(65) _ ascii(‘A’)|| ||[#string-concatenate concatenate] _ @< >@||’hello’ + ‘ world’||"hello” & “ world”||’hello’ @@||@@ ‘ world’|| ||[#string-length length] _ @< >@||Length(‘hello’)|| ||length(‘hello’)|| ||[#extract-substring extract substring] _ @< >@||Copy(S, 1, 4)|| ||substr(‘hello’, 1, 4)|| ||[#index-substring index of substring] _ @< >@||Pos(‘hell’, ‘hello’)|| ||strpos(‘hello’, ‘hell’)|| ||[#case-manipulation case manipulation]||uses SysUtils; _ _ UpperCase(‘hello’) _ LowerCase(‘HELLO’)|| ||upper(‘hello’) _ lower(‘HELLO’)|| ||[#strip strip]||Trim(‘ foo ‘) _ TrimLeft(‘ foo’) _ TrimRight(‘foo ‘)|| ||trim(‘ foo ‘) _ ltrim(‘ foo’) _ rtrim(‘foo ‘)|| ||[#pad pad on left, pad on right]|| || ||lpad(‘foo’, 10) _ rpad(‘foo’, 10)|| ||[#string-number-conversion convert string to number]||uses SysUtils; _ _ 7 + StrToInt(‘12’) _ 73.9 + StrToFloat(‘.037’)|| ||##gray|//arithmetic operators automatically convert strings to numbers//## _ cast(‘12’ as int) _ cast(‘3.14’) as real)|| ||[#number-string-conversion convert number to string]||uses SysUtils; _ _ ‘value: ‘ + IntToStr(8) _ ‘value: ‘ + FloatToStr(3.14)||Integer’Image(8) _ Float’Image(3.14)||##gray|//double pipe operator// @@||@@ //converts numbers operands to strings//## _ cast(8 to text) _ cast(3.14 to text)|| ||||||||~ # regexes[#regexes-note regular expressions]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# match[#match-note match]|| || ||select * _ from pwt _ where name similar to ‘r[a-z]+’;|| ||# substitute[#substitute-note substitute] _ @< >@|| || ||select regexpreplace(‘foo bar’, ‘bar$’, ‘baz’);|| ||# extract-subgroup[#extract-subgroup-note extract subgroup]|| || ||select (regexpmatches(‘foobar’, ‘(f..)bar’))[1];|| ||||||||~ # dates-time[#dates-time-note dates and time]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# current-datetime[#current-datetime-note current datetime]|| ||with Ada.Calendar; _ _ Now: Ada.Calendar.Time; _ _ Now := Ada.Calendar.Clock;||now()|| ||# date-time-to-str[#date-time-to-str-note datetime to string]|| ||with Ada.Calendar.Formating; _ _ ##gray|@@–@@ “2015-03-29 17:46:35”:## _ Ada.Calendar.Formatting.Image(Now);||tochar(now(), ‘YYYY-MM-DD HH24:MI:SS’)|| ||# str-to-date-time[#str-to-date-time-note string to datetime]|| || ||totimestamp(‘2011-09-26 00:00:47’, ‘YYYY-MM-DD HH24:MI:SS’)|| ||||||||~ # arrays[#arrays-note arrays]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||[#declare-array declare array]||A: array[1..5] of Integer;||A: array(1..5) of Integer;||a int[];|| ||[#array-length array length]|| ||A’Last||array_length(a, 1)|| ||[#array-access array element access]|| A[1] := 3;|| A(1) := 3;||a[1] = 3;|| ||[#array-initialization array initialization]|| ||A: array(1..5) of Integer := (1, 3, 5, 2, 4);||a int[] = array[1, 3, 5, 2, 4];|| ||[#array-slice array slice]|| || A(3..4) := A(1..2);||a[1:2] _ ##gray|//can assign to slice in// UPDATE //statement but not in assignment//##|| ||[#array-out-of-bounds array out of bounds behavior]||##gray|//undefined; free pascal permits out of bounds memory access//##||##gray|//compiler warning; raises//## Constraint_Error ##gray|//at runtime//##||NULL|| ||[#declare-multidimensional-array declare multidimensional array]|| || ||a integer[][];|| ||[#multidimensional-array-access multidimensional array access]|| || ||a[2][3] = 7;||¯ ||||||||~ # user-defined-types[#user-defined-types-note user-defined types]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# type-synonym[#type-synonym-note type synonym]||type _ @< >@CustomerId = Integer;|| || || ||# enumerated-type[#enumerated-type-note enumerated type]||type _ @< >@Direction = (North, South, East, West); _ var _ @< >@Wind: Direction = North; _ begin _ @< >@WriteLn(Wind); ##gray|{ prints ‘North’ }##|| ||create type direction as enum ( ‘north’, ‘south’, ‘east’, ‘west’); _ _ create table wind ( origin direction, speed_mph real ); _ _ insert into wind values ( ‘north’, 12 );|| ||[#define-record-type define record type]||##gray|//in type section://## _ Customer = record _ @< >@Id: Integer; _ @< >@Name: String; _ end;||type Customer is record _ @< >@Id: Integer; _ @< >@Name: String(1..4); _ end record;||create type customer as ( _ @< >@id integer, _ @< >@name text _ );|| ||[#declare-record declare record]||C: Customer;||C: Customer := (17, “John”);||declare _ @< >@c customer; _ begin _ @< >@c = (17,’John’); _ @< >@##gray|//code which uses// c##|| ||[#record-member-access record member access]||C.Name := ‘Fred’;||C.Name := ‘Fred’;||c.name = ‘Fred’|| ||[#record-block record block]|| || || || ||||||||~ # generic-types[#generic-types-note generic types]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||||||||~ # functions[#functions-note functions]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# decl-func[#decl-func-note declare]|| ||function Add(M: Integer; N: Integer) return Integer;|| || ||# def-func[#def-func-note define]||function Add(M: Integer; N: Integer): Integer; _ begin _ @< >@Result := M + N; _ end;||function Add(M: Integer; N: Integer) return Integer is _ begin _ @< >@return M + N; _ end Add;||create function add(i int, j int) _ returns int as $$ _ begin _ @< >@return i + j; _ end; _ $$ language plpgsql;|| ||# call-func[#call-func-note call]||Sum := Add(7, 3);||Sum := Add(7, 3);||##gray|@@–@@ in select clause:## _ select add(1, 2); _ _ ##gray|@@–@@ in where clause:## _ select * from cust where id = add(1, 2); _ _ ##gray|@@–@@ inside PL/pgSQL functions can be used wherever _ @@–@@ expressions are permitted. Can be used as a statement _ @@–@@ with perform:## _ perform add(1, 2);|| ||# undef-func[#undef-func-note undefine function]||##gray|//none//##||##gray|//none//##||##gray|@@–@@ parameter types are required:## _ drop function foo(int, int);|| ||# no-retval[#no-retval-note no return value]||procedure Message(Msg: String); _ begin _ @< >@WriteLn(Msg); _ end;||procedure Message(Msg: String) is _ begin _ @< >@TextIO.PutLine(Msg); _ end Message;||##gray|@@–@@ declare return type as void:## _ create or replace function message(msg text) _ returns void as $$ _ begin _ @< >@raise notice ‘%’, msg; _ end; _ $$ language plpgsql;|| ||# pass-by-ref[#pass-by-ref-note pass by reference]||##gray|{ declare parameter with var }## _ procedure Incr(var N: Integer); _ begin _ @< >@N := N + 1; _ end; _ _ var _ @< >@I: Integer; _ begin _ @< >@I := 3; _ @< >@Incr(I); _ @< >@WriteLn(I); ##gray|{ prints 4 }## _ end.||procedure Incr(N: in out Integer) is _ begin _ @< >@N := N + 1; _ end Incr; _ _ I := 3; _ Incr(3);||##gray|//none//##|| ||# pass-uninitialized-by-ref[#pass-uninitialized-by-ref-note pass uninitialized variable by reference]|| ||procedure Get_Pi(Pi: out Float) is _ begin _ @< >@Pi := 3.14; _ end Get_Pi;||##gray|//none//##|| ||# nested-func[#nested-func-note nested function]|| || ||##gray|//none//##|| ||# overloaded-func[#overloaded-func-note overloaded function]|| || ||##gray|@@–@@integer version:## _ create or replace function add(m int, n int) _ returns int as $$ _ begin _ @< >@return m + n; _ end; _ $$ language plpgsql; _ _ ##gray|@@–@@float version:## _ create or replace function add(x real, y real) _ returns real as $$ _ begin _ @< >@return x + y; _ end; _ $$ language plpgsql;|| ||# forward-decl[#forward-decl-note forward declaration]||function Odd(N: Integer): Boolean; _ Forward; _ _ function Even(N: Integer): Boolean; _ begin _ @< >@if N = 0 then _ @< >@@< >@Result := true _ @< >@else _ @< >@@< >@Result := Odd(N - 1); _ end; _ _ function Odd(N: Integer): Boolean; _ begin _ @< >@if N = 0 then _ @< >@@< >@Result := false _ @< >@else _ @< >@@< >@Result := Even(N - 1); _ end;|| ||##gray|//none//##|| ||||||||~ # execution-control[#execution-control-note execution control]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# if[#if-note if]||if I = 0 then _ begin _ @< >@WriteLn(‘no hits’); _ end _ else _ @< >@if I = 1 then _ @< >@begin _ @< >@@< >@WriteLn(‘one hit’); _ @< >@end _ @< >@else _ @< >@@< >@WriteLn(IntToStr(I) + ‘ hits’);||if I = 0 then _ @< >@Put_Line(“no hits”); _ elsif I = 1 then _ @< >@Put_Line(“one hit”); _ else _ @< >@Put_Line(Integer’Image(I) & “ hits”); _ end if;||if i = 0 then _ @< >@return ‘no hits’; _ elsif i = 1 then _ @< >@return ‘one hit’; _ else _ @< >@return i @@||@@ ‘ hits’; _ end if;|| ||# switch[#switch-note switch]|| || || || ||# while[#while-note while]||I := 0; _ while I < 10 do _ begin _ @< >@WriteLn(IntToStr(I)); _ @< >@I := I + 1; _ end||I := 0; _ while I < 10 loop _ @< >@Put_Line(Integer’Image(I)); _ @< >@I := I + 1; _ end loop;||i = 1; _ sum = 0; _ while i <= n loop _ @< >@sum = sum + i; _ @< >@i = i + 1; _ end loop;|| ||# for[#for-note for]||for I := 0 to 9 do _ @< >@WriteLn(IntToStr(I));||for I in 1..9 loop _ @< >@Put_Line(Integer’Image(I)); _ end loop;||sum = 0; _ for i in 1..n loop _ @< >@sum = sum + i; _ end loop;|| ||# break[#break-note break]||break||exit||exit|| ||# continue[#continue-note continue]||continue|| ||continue|| ||# goto[#goto-note goto]|| || || || ||||||||~ # exceptions[#exceptions-note exceptions]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# predefined-exc[#predefined-exc-note predefined exceptions]|| ||Constraint_Error _ Program_Error _ Storage_Error _ Tasking_Error|| || ||# def-exc[#def-exc-note define exception]||{$mode delphi} _ _ type Err42 = class(Exception);||Err42: exception;||##gray|@@–@@ code string must be five digits or uppercase letters:## _ begin _ @< >@raise ‘User defined error 42’ using errcode = ‘ERR42’; _ exception when sqlstate ‘ERR42’ then _ @< >@raise notice ‘caught ERR42’; _ end;|| ||# raise-exc[#raise-exc-note raise exception] _ @< >@||{$mode delphi} _ _ raise Exception.Create(‘bad int: ‘ + IntToStr(7));||raise Err42;||##gray|@@–@@ raises exception with condition raise_exception:## _ raise exception ‘bad int: %’, i; _ _ ##gray|@@–@@ also possible to use predefined condition:## _ raise data_exception using message = ‘bam!|| ||# re-raise-exc[#re-raise-exc-note re-raise exception]||{$mode delphi} _ _ try _ @< >@risky(); _ except on E: Exception do begin _ @< >@WriteLn(‘risky failed’); _ @< >@raise; _ end;|| ||begin _ @< >@perform risky(); _ exception when others then _ @< >@raise notice ‘risky() failed.’; _ @< >@raise; _ end;|| ||# handle-any-exc[#handle-any-exc-note handle any exception] _ @< >@||{$mode delphi} _ _ try _ @< >@raise Exception.Create(‘bad int: ‘ + IntToStr(7)); _ except _ @< >@on E: Exception do WriteLn(E.Message);|| ||begin _ @< >@raise exception ‘bad int: %’, i; _ exception when others then _ @< >@raise notice ‘caught bad int’; _ end;|| ||# handle-exc-by-type[#handle-exc-by-type-note handle exception by type]||{$mode delphi} _ _ try _ @< >@raise Err42.Create(‘bad int: ‘ + IntToStr(7)); _ except _ @< >@on E: Err42 do WriteLn(E.Message);|| ||begin _ @< >@n := 1 / 0; _ exception when divisionbyzero then _ @< >@raise notice ‘ignoring div by zero’; _ end;|| ||# multiple-exc-handlers[#multiple-exc-handlers-note multiple exception handlers]|| || || || ||finally|| || || || ||||||||~ # file-handles[#file-handles-note file handles]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# read-line-stdin[#read-line-stdin-note read line from stdin]|| || ||##gray|//none//##|| ||# write-line-stdout[#write-line-stdout-note write line to stdout]|| ||with Text_IO; _ with Ada.WideWideText_IO; _ _ TextIO.PutLine(“Hello!”); _ Ada.WideWideTextIO.PutLine(“Hello!”);||##gray|//none//##|| ||# write-line-stderr[#write-line-stderr-note write line to stderr]|| || ||##gray|@@–@@ stderr stream goes to both client and server log. _ @@–@@ _ @@–@@ levels are: debug, log, info, notice, warning _ @@–@@## _ raise notice ‘i is %’, i;|| ||||||||~ # files[#files-note files]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||||||||~ # directories[#directories-note directories]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||||||||~ # processes-environment[#processes-environment-note processes and environment]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# cmd-line-arg[#cmd-line-arg-note command line arguments]|| ||with Ada.Command_Line; _ _ Ada.CommandLine.CommandName _ Ada.CommandLine.ArgumentCount _ Ada.Command_Line.Argument(1) _ Ada.CommandLine.Argument(2)|| || ||||||||~ # libraries-namespaces[#libraries-namespaces-note libraries and namespaces]|| ||~ ||~ [#pascal pascal]||~ [#ada ada]||~ [#plpgsql plpgsql]|| ||# load-lib[#load-lib-note load library]|| ||with TextIO;|| || ||# namespace-decl[#namespace-decl-note namespace declaration]|| ||$ cat put_hello.ads _ package Put_Hello is _ @< >@procedure Put_Hello; _ end; _ _ $ cat put_hello.adb _ cat put_hello.adb _ with Text_IO; _ _ package body Put_Hello is _ @< >@procedure Put_Hello is _ @< >@begin _ @< >@@< >@TextIO.PutLine(“Hello World!”); _ @< >@end Put_Hello; _ end;|| || ||# namespace-alias[#namespace-alias-note namespace alias]|| ||with Text_IO; _ _ procedure Hello is _ @< >@package IO renames Text_IO; _ begin _ @< >@IO.Put_Line(“Hello World!”); _ end Hello;|| || ||# unqualified-import[#unqualified-import-note unqualified import of namespace]|| ||use TextIO;|| || ||~ ||~ ##EFEFEF|@@____________________________________________________@@##||~ ##EFEFEF|@@__________________________________________________@@##||~ ##EFEFEF|@@_____________________________________________________@@##||
# general-note + [#top General Notes]
# version-used-note ++ [#version-used version used]
The version used in this reference sheet.
# show-version-note ++ [#show-version show version]
How to get the version.
# grammar-invocation-note + [#grammar-invocation Grammar and Invocation]
# hello-world-note ++ [#hello-world hello word]
A “Hello, World!” example.
ada:
{{gnatmake}} takes care of compiling everything that the final executable depends on and linking the final executable.
It is possible to compile and link in separate steps:
code $ cat hello.adb with TextIO; use TextIO; procedure Hello is begin Put_Line (“Hello World!”); end Hello;
$ gnatgcc -gnat12 -c hello.adb
$ gnatbind -gnat12 hello
$ gnatlink -gnat12 hello
$ ./hello Hello World! /code
{{gnatgcc}} creates the files {{hello.ali}} and {{hello.o}}, and {{gnatlink}} creates the final executable {{hello}}.
{{gnatbind}} creates the files {{b~hello2.adb}} and {{b~hello2.ads}}. It is a necessary precursor to {{gnatlink}}. {{gnatbind}} prevents linking a program in which objects are using incompatible versions of the same header. This is a safety feature not provided by C and C++ linkers.
# file-suffixes-note ++ [#file-suffixes file suffixes]
Customary suffixes for source and object files.
ada:
The {{.adi}} file is generated whenever a {{.o}} file is generated. The {{.o}} file is what goes into the final executable. The {{.adi}} file contains information used by the compiler, including the flags that were used to comile the {{.o}} file and what files it depends on.
# eol-comment-note ++ [#eol-comment end-of-line comment]
The syntax for a comment which ends at the end of the line.
pascal:
The @@//@@ style comment is supported by Borland compilers and Free Pascal.
# multiple-line-comment-note ++ [#multiple-line-comment multiple line comment]
The syntax for a comment which can span multiple lines.
# variables-expressions-note + [#variables-expressions Variables and Expressions]
# case-sensitive-note ++ [#case-sensitive case sensitive]
Are identifiers case sensitive?
pascal:
[http://www.freepascal.org/docs-html/ref/refse3.html Free Pascal Reserved Words and Modifiers]
A word in Pascal is reserved if it cannot be redefined by the programmer. The names for the built-in types: Integer, Boolean, etc. are not reserved. They are defined in the System unit and can be redefined by the programmer.
Although Pascal is case insensitive, reserved words and modifiers are customarily written in lower case. Other identifiers are customarily written in upper camel case, also known as Pascal case.
# declarations-note ++ [#declarations declare constant, type, variable]
How to declare a constant, type, or variable.
# assignment-note ++ [#assignment assignment]
The syntax for assigning a value to a variable.
# pointer-declaration-note ++ [#pointer-declaration pointer declaration]
How to declare a pointer.
# arithmetic-logic-note + [#arithmetic-logic Arithmetic and Logic]
# boolean-type-note ++ [#boolean-type boolean type]
The boolean type.
# true-false-note ++ [#true-false true and false]
The literals for true and false.
# falsehoods-note ++ [#falsehoods falsehoods]
Values which evaluate as false in a boolean context.
# logical-op-note ++ [#logical-op logical operators]
The logical operators for conjunction, disjunction, exclusive or, and negation.
# short-circuit-op-note ++ [#short-circuit-op short circuit operators]
Short circuit versions of the logical operators.
The short circuit version of //and// will not evaluate its second argument if the first is false. The short circuit version of //or// will not evaluate its second argument if the first is true.
# int-type-note ++ [#int-type integer type]
# float-type-note ++ [#float-type float type]
# fixed-type-note ++ [#fixed-type fixed type]
# relational-op-note ++ [#relational-op relational operators]
# min-max-note ++ [#min-max min and max]
# arith-op-note ++ [#arith-op arithmetic operators]
# int-div-note ++ [#int-div integer division]
# int-div-zero-note ++ [#int-div-zero integer division by zero]
# float-div-note ++ [#float-div float division]
# float-div-zero-note ++ [#float-div-zero float division by zero]
# power-note ++ [#power power]
# sqrt-note ++ [#sqrt sqrt]
# sqrt-negative-one-note ++ [#sqrt-negative-one sqrt -1]
# transcendental-func-note ++ [#transcendental-func transcendental functions]
# float-truncation-note ++ [#float-truncation float truncation]
# absolute-val-note ++ [#absolute-val absolute value]
# int-overflow-note ++ [#int-overflow integer overflow]
# float-overflow-note ++ [#float-overflow float overflow]
# random-num-note ++ [#random-num random number]
# bit-op-note ++ [#bit-op bit operators]
# strings-note + [#strings Strings]
# string-literal ++ string literal
# string-concatenate ++ string concatenate
# regexes-note + [#regexes Regular Expressions]
# dates-time-note + [#dates-time Dates and Time]
# arrays-note + [#arrays Arrays]
# user-defined-types-note + [#user-defined-types User-Defined Types]
# generic-types-note + [#generic-types Generic Types]
# functions-note + [#functions Functions]
# def-func-note ++ [#def-func define function]
How to define a function.
postgresql:
If “or replace” is omitted from the function definition and the function already exists, the statement fails with an error.
Before PostgreSQL 8.0 parameters could not be assigned names in the function signature. The following syntax which is still valid was used:
code create or replace function foo(int, int) returns int as $$ declare @< >@i alias for $1; @< >@j alias for $2; begin @< >@return i + j; end; $$ language plpgsql; /code
# invoke-func-note ++ [#invoke-func invoke function]
How to invoke a function.
# undef-func-note ++ [#undef-func undefine function]
How to undefine a function.
# no-retval-note ++ [#no-retval no return value]
How to define a function with no return value. Sometimes such functions are called procedure. Such a function is not useful unless it has a side effect.
# pass-by-ref-note ++ [#pass-by-ref pass by reference]
How to pass a variable by reference. This permits the callee to modify the value in the variable.
# nested-func-note ++ [#nested-func nested function]
How to define a function inside another function.
plpgsql:
Nested functions are a feature of Oracle’s PL/SQL. A nested function has access to the local variables of the containing function; it is not visible or callable from outside of the containing function.
# overloaded-func-note ++ [#overloaded-func overloaded function]
How to define multiple versions of a function, with the correct version chosen by the type of the arguments used at invocation.
# forward-decl-note ++ [#forward-decl forward declaration]
How to declare a function before it is defined.
# execution-control-note + [#execution-control Execution Control]
# if-note ++ [#if if]
The syntax for an if statement.
# while-note ++ [#while while]
The syntax for a while statement.
# for-note ++ [#for for]
The syntax for a for loop.
# break-continue-note ++ [#break-continue break and continue]
How to break from a loop; how to jump to the next iteration of a loop.
# exceptions-note + [#exceptions Exceptions]
# raise-exc-note ++ [#raise-exc raise exception]
How to raise an exception.
postgresql:
The raise statement can also be used to write info and warning messages. See [#write-line-stderr write line to stderr].
# re-raise-exc-note ++ [#re-raise-exc re-raise exception]
How to re-raise a caught exception.
postgresql:
PostgreSQL exceptions do not have a stack trace or preserve the line number at which the error originated, so re-raising an exception just preserves the exception type and message.
# def-exc-note ++ [#def-exc define exception]
How to define a new exception type.
postgresql:
Any 5 character string of digits or uppercase letters may be used as an error code. Many error codes are already in use:
[http://www.postgresql.org/docs/9.1/static/errcodes-appendix.html PostgreSQL Error Codes]
# handle-any-exc-note ++ [#handle-any-exc handle any exception]
How to handle any exception.
# handle-exc-by-type-note ++ [#handle-exc-by-type handle exception by type]
How to handle exceptions of a specific type.
# multiple-exc-handlers-note ++ [#multiple-exc-handlers multiple exception handlers]
# file-handles-note + [#file-handles File Handles]
# read-line-stdin-note ++ [#read-line-stdin read line from stdin]
How to read a line from standard input.
plpgsql:
In a SQL session the stream on which the server receives SQL commands from the client can be regarded as the standard input. Furthermore the COPY command provides a mechanism for providing unquoted data on the input stream for insertion into a table. PL/pgSQL code does not have access to this stream, however; a COPY command cannot be run from within PL/pgSQL. Note that some PL/pgSQL code runs as triggers in which case no such stream exists.
# write-line-stdout-note ++ [#write-line-stdout write line stdout]
How to write a line to stdout.
plpgsql:
In an interactive SQL session the server writes the results of SQL select statements to a stream which can be regarded as standard out. PL/pgSQL code cannot write to this stream, however. The only thing PL/pgSQL code can do with selected data is read it into local variables.
# write-line-stderr-note ++ [#write-line-stderr write line stderr]
How to write a line to stderr.
plpgsql:
The RAISE statement is used to write to stderr. Each message must be assigned one of five log levels: debug, log, info, notice, warning. The message can be reported to the client, written to the server log, or both depending upon how the server is configured.
# files-note + [#files Files]
# directories-note + [#directories Directories]
# processes-environment-note + [#processes-environment Processes and Environment]
# libraries-namespaces-note + [#libraries-namespaces Libraries and Namespaces]
# pascal + [#top Pascal]
[http://www.freepascal.org/docs-html/ref/ref.html Free Pascal: Reference Guide] [http://www.freepascal.org/docs-html/rtl/index.html Free Pascal: Run-Time Language Reference]
# ada + [#top Ada]
[http://www.ada-auth.org/standards/12rm/html/RM-TOC.html Ada Reference Manual] [https://gcc.gnu.org/onlinedocs/gnat_rm/ GNAT Reference Manual] [https://gcc.gnu.org/onlinedocs/gnat_ugn/index.html GNAT User’s Guide for Native Platforms]
Ada has about 60 reserved words; these are written with lower case letters. Other identifiers are written with underscores and capitalization, e.g. {{Put_Hello}}.
Files use lower case letters and underscores, but are otherwise named after the package they contain. The package {{PutHello}} would go in the file {{puthello.adb}}, with its specification in {{put_hello.ads}}.
# plpgsql + [#top PL/SQL]
[http://www.postgresql.org/docs/9.1/interactive/plpgsql.html PostgreSQL 9.1 Documention: PL/pgSQL] [http://www.postgresql.org/docs/9.1/interactive/plpgsql-porting.html Porting from PL/SQL to PL/pgSQL]
Execute statement with no result:
code – create table foo (s text, i int);
create or replace function insert_foo(s text, i int) returns void as $$ begin insert into foo values (s, i); end; ) $$ language plpgsql;
– insert, update, and delete statements raise an – exception if they fail /code
Execute SQL in a string:
code create or replace function droptable(tbl text) returns void as $$ begin execute ‘drop table ‘ || quoteident(tbl); end; $$ language plpgsql; /code
Functions to prevent sql injection:
- quote_literal
- quote_nullable
- quote_ident