Browse Source

initial commit

Chris Cromer 4 months ago
commit
0bb81e5dee
Signed by: Chris Cromer <chris@cromer.cl> GPG Key ID: 39CC813FF3C8708A
7 changed files with 196 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 20
    0
      c/meson.build
  3. 40
    0
      c/postgresql_demo.c
  4. 53
    0
      java/PostgresqlDemo.java
  5. 27
    0
      php/postgresql-demo.php
  6. 30
    0
      vala/PostgresqlDemo.vala
  7. 24
    0
      vala/meson.build

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+*.jar
2
+build

+ 20
- 0
c/meson.build View File

@@ -0,0 +1,20 @@
1
+project('postgresqldemo',
2
+	'c',
3
+	version : '1.0.0',
4
+	license : 'BSD-3',
5
+	default_options : [ 'b_ofast=if-release', 'b_march_native=if-release', 'b_ndebug=if-release' ]
6
+)
7
+
8
+#gtk_dep = dependency('gtk+-3.0', version : '>=3.0.0')
9
+# gmodule-export-2.0 is needed to connect the handlers
10
+#gmodule_dep = dependency('gmodule-export-2.0', version : '>=2.0')
11
+pg_dep = dependency('libpq', version : '>=8.0')
12
+
13
+cc = meson.get_compiler('c')
14
+
15
+sources = ['postgresql_demo.c']
16
+
17
+exe = executable('postgresql_demo',
18
+	sources,
19
+	dependencies : [pg_dep],
20
+	install : false)

+ 40
- 0
c/postgresql_demo.c View File

@@ -0,0 +1,40 @@
1
+#include <libpq-fe.h>
2
+
3
+int main(int argc, char *argv[]) {
4
+	PGconn *conn;
5
+	PGresult *res;
6
+	int i;
7
+	int j;
8
+
9
+	conn = PQsetdbLogin("localhost", "5432", "", "", "database", "user", "password");
10
+	if (PQstatus(conn) != CONNECTION_OK) {
11
+		fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));
12
+		PQfinish(conn);
13
+		return 1;
14
+	}
15
+
16
+	res = PQexec(conn, "SELECT * FROM turista");
17
+	if (PQresultStatus(res) != PGRES_TUPLES_OK) {
18
+		fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
19
+		PQclear(res);
20
+		PQfinish(conn);
21
+		return 1;
22
+	}
23
+
24
+	for (i = 0; i < PQnfields(res); i++) {
25
+		printf("%-20s", PQfname(res, i));
26
+	}
27
+	printf("\n\n");
28
+
29
+	for (i = 0; i < PQntuples(res); i++){
30
+		for (j = 0; j < PQnfields(res); j++) {
31
+			printf("%-20s", PQgetvalue(res, i, j));
32
+		}
33
+		printf("\n");
34
+	}
35
+
36
+	PQclear(res);
37
+
38
+	PQfinish(conn);
39
+	return 0;
40
+}

+ 53
- 0
java/PostgresqlDemo.java View File

@@ -0,0 +1,53 @@
1
+import java.sql.*;
2
+import java.util.Properties;
3
+
4
+import static java.lang.System.exit;
5
+
6
+public class PostgresqlDemo {
7
+    private Connection conn;
8
+
9
+    private PostgresqlDemo() {
10
+        String url = "jdbc:postgresql://localhost/databasename";
11
+        Properties props = new Properties();
12
+        props.setProperty("user", "bdd");
13
+        props.setProperty("password", "bdd");
14
+        try {
15
+            conn = DriverManager.getConnection(url, props);
16
+        }
17
+        catch (SQLException e) {
18
+            e.printStackTrace();
19
+            exit(1);
20
+        }
21
+        try {
22
+            Statement statement = conn.createStatement();
23
+            ResultSet resultSet = statement.executeQuery("SELECT * FROM turista");
24
+            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
25
+            for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
26
+                System.out.printf("%-20s", resultSetMetaData.getColumnName(i));
27
+            }
28
+            System.out.print("\n\n");
29
+            while (resultSet.next()) {
30
+                for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
31
+                    Object object = resultSet.getObject(i);
32
+                    System.out.printf("%-20s", object == null ? "NULL" : object.toString());
33
+                }
34
+                System.out.print("\n");
35
+            }
36
+            resultSet.close();
37
+            statement.close();
38
+        }
39
+        catch (SQLException e) {
40
+            e.printStackTrace();
41
+        }
42
+        try {
43
+            conn.close();
44
+        }
45
+        catch (SQLException e) {
46
+            e.printStackTrace();
47
+        }
48
+    }
49
+
50
+    public static void main(String[] args) {
51
+        new PostgresqlDemo();
52
+    }
53
+}

+ 27
- 0
php/postgresql-demo.php View File

@@ -0,0 +1,27 @@
1
+<?php
2
+$conn = pg_connect('host=localhost port=5432 dbname=bdd user=bdd password=bdd');
3
+if (!$conn) {
4
+	echo 'Database connection failed!';
5
+	exit(1);
6
+}
7
+$result = pg_query($conn, 'SELECT * FROM turista');
8
+if (!$result) {
9
+	echo 'SELECT failed: '.pg_last_error($conn);
10
+	exit(1);
11
+}
12
+
13
+echo '<table border=1><tr>';
14
+for ($i = 0; $i < pg_num_fields($result); $i++) {
15
+	echo '<td>'.pg_field_name($result, $i).'</td>';
16
+}
17
+echo '</tr>';
18
+while ($row = pg_fetch_row($result)) {
19
+	echo '<tr>';
20
+	for ($i = 0; $i < pg_num_fields($result); $i++) {
21
+		echo '<td>'.$row[$i].'</td>';
22
+	}
23
+	echo '</tr>';
24
+}
25
+echo '</table>';
26
+pg_close($conn);
27
+?>

+ 30
- 0
vala/PostgresqlDemo.vala View File

@@ -0,0 +1,30 @@
1
+namespace PostgresqlDemo {
2
+	using Postgres;
3
+
4
+	public static int main (string[] args) {
5
+		Database conn = set_db_login ("localhost", "5432", "", "", "database", "user", "password");
6
+		if (conn.get_status () != ConnectionStatus.OK) {
7
+			stderr.printf ("%s\n", conn.get_error_message ());
8
+			return 1;
9
+		}
10
+
11
+		Result res = conn.exec ("SELECT * FROM turista");
12
+		if (res.get_status () != ExecStatus.TUPLES_OK) {
13
+			stderr.printf ("SELECT failed: %s", conn.get_error_message ());
14
+			return 1;
15
+		}
16
+
17
+		for (int i = 0; i < res.get_n_fields (); i++) {
18
+			stdout.printf ("%-20s", res.get_field_name (i));
19
+		}
20
+		stdout.printf ("\n\n");
21
+
22
+		for (int i = 0; i < res.get_n_tuples (); i++) {
23
+			for (int j = 0; j < res.get_n_fields (); j++) {
24
+				stdout.printf ("%-20s", res.get_value (i, j));
25
+			}
26
+			stdout.printf ("\n");
27
+		}
28
+		return 0;
29
+	}
30
+}

+ 24
- 0
vala/meson.build View File

@@ -0,0 +1,24 @@
1
+project('postgresqldemo',
2
+	['c', 'vala'],
3
+	version: '1.0.0',
4
+	license: 'BSD-3',
5
+	default_options: [
6
+		'b_ofast=if-release',
7
+		'b_march_native=if-release',
8
+		'b_ndebug=if-release'
9
+	]
10
+)
11
+add_global_arguments('-DGETTEXT_PACKAGE="sernatur"', language: 'c')
12
+
13
+glib_dep = dependency('glib-2.0')
14
+pq_dep = dependency('libpq', version: '>=8.0')
15
+
16
+vala_sources = files('PostgresqlDemo.vala')
17
+
18
+sources = vala_sources
19
+
20
+exe = executable('postgresql_demo',
21
+	sources,
22
+	dependencies: [glib_dep, pq_dep],
23
+	install: false)
24
+

Loading…
Cancel
Save