1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2016 Guilherme Mansur <guilhermerpmansur@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 module test_postgres_native
19 import postgresql
::native_postgres
21 var db_suffix
= "NIT_TESTING_ID".environ
22 var db
= new NativePostgres.connectdb
("dbname=postgres")
23 assert postgres_open
: db
.status
.is_ok
else print_error db
.error
25 var result
= db
.exec
("CREATE TABLE IF NOT EXISTS animals_{db_suffix} (aname TEXT PRIMARY KEY, class TEXT NOT NULL, sex INTEGER)")
26 assert postgres_create_table
: result
.status
.is_ok
else print_error db
.error
28 result
= db
.exec
("INSERT INTO animals_{db_suffix} VALUES('Whale', 'mammal', 1)")
29 assert postgres_insert_1
: result
.status
.is_ok
else print_error db
.error
31 result
= db
.exec
("INSERT INTO animals_{db_suffix} VALUES('Snake', 'reptile', 0)")
32 assert postgres_insert_2
: result
.status
.is_ok
else print_error db
.error
34 result
= db
.exec
("SELECT * FROM animals_{db_suffix}")
35 assert postgres_select
: result
.status
.is_ok
else print_error db
.error
37 assert postgres_ntuples
: result
.ntuples
== 2 else print_error db
.error
38 assert postgres_nfields
: result
.nfields
== 3 else print_error db
.error
39 assert postgres_fname
: result
.fname
(0) == "aname" else print_error db
.error
40 assert postgres_isnull
: not result
.is_null
(0,0) else print_error db
.error
41 assert postgres_value
: result
.value
(0,0) == "Whale" else print_error db
.error
43 var cols
: Int = result
.nfields
44 var rows
: Int = result
.ntuples
45 var fields
: String = ""
46 for c
in [0..cols
[ do fields
+= result
.fname
(c
) + " "
50 for j
in [0..cols
[ do fields
+= result
.value
(i
, j
) + " "
54 result
= db
.exec
("DELETE FROM animals_{db_suffix} WHERE aname = 'Lioness'")
55 assert postgres_delete_1
: result
.status
.is_ok
else print_error db
.error
57 result
= db
.exec
("DELETE FROM animals_{db_suffix} WHERE aname = 'Snake'")
58 assert postgres_delete_2
: result
.status
.is_ok
else print_error db
.error
60 result
= db
.prepare
("PREPARED_INSERT", "INSERT INTO animals_{db_suffix}(aname, class, sex) VALUES ($1, $2, $3)", 3)
61 assert postgres_prepare
: result
.status
.is_ok
else print_error db
.error
63 result
= db
.exec
("DELETE FROM animals_{db_suffix} WHERE aname = 'Frog'")
64 assert postgres_delete_3
: result
.status
.is_ok
else print_error db
.error
66 var values
= ["Frog", "Anphibian", "1"]
67 var lengths
= [values
[0].length
, values
[1].length
, values
[2].length
]
69 result
= db
.exec_prepared
("PREPARED_INSERT", 3, values
, lengths
, formats
,0)
70 assert postgres_exec_prepared
: result
.status
.is_ok
else print_error db
.error
72 result
= db
.exec
("DROP TABLE animals_{db_suffix}")
73 assert postgres_drop_table
: result
.status
.is_ok
else print_error db
.error