commit 0bb81e5dee3f0ae7b4231dda0a94d9123d9a9635 Author: Chris Cromer Date: Fri Dec 21 16:03:06 2018 -0300 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86096c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.jar +build diff --git a/c/meson.build b/c/meson.build new file mode 100644 index 0000000..dc772ff --- /dev/null +++ b/c/meson.build @@ -0,0 +1,20 @@ +project('postgresqldemo', + 'c', + version : '1.0.0', + license : 'BSD-3', + default_options : [ 'b_ofast=if-release', 'b_march_native=if-release', 'b_ndebug=if-release' ] +) + +#gtk_dep = dependency('gtk+-3.0', version : '>=3.0.0') +# gmodule-export-2.0 is needed to connect the handlers +#gmodule_dep = dependency('gmodule-export-2.0', version : '>=2.0') +pg_dep = dependency('libpq', version : '>=8.0') + +cc = meson.get_compiler('c') + +sources = ['postgresql_demo.c'] + +exe = executable('postgresql_demo', + sources, + dependencies : [pg_dep], + install : false) diff --git a/c/postgresql_demo.c b/c/postgresql_demo.c new file mode 100644 index 0000000..e0b5f21 --- /dev/null +++ b/c/postgresql_demo.c @@ -0,0 +1,40 @@ +#include + +int main(int argc, char *argv[]) { + PGconn *conn; + PGresult *res; + int i; + int j; + + conn = PQsetdbLogin("localhost", "5432", "", "", "database", "user", "password"); + if (PQstatus(conn) != CONNECTION_OK) { + fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); + PQfinish(conn); + return 1; + } + + res = PQexec(conn, "SELECT * FROM turista"); + if (PQresultStatus(res) != PGRES_TUPLES_OK) { + fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn)); + PQclear(res); + PQfinish(conn); + return 1; + } + + for (i = 0; i < PQnfields(res); i++) { + printf("%-20s", PQfname(res, i)); + } + printf("\n\n"); + + for (i = 0; i < PQntuples(res); i++){ + for (j = 0; j < PQnfields(res); j++) { + printf("%-20s", PQgetvalue(res, i, j)); + } + printf("\n"); + } + + PQclear(res); + + PQfinish(conn); + return 0; +} diff --git a/java/PostgresqlDemo.java b/java/PostgresqlDemo.java new file mode 100644 index 0000000..9e93349 --- /dev/null +++ b/java/PostgresqlDemo.java @@ -0,0 +1,53 @@ +import java.sql.*; +import java.util.Properties; + +import static java.lang.System.exit; + +public class PostgresqlDemo { + private Connection conn; + + private PostgresqlDemo() { + String url = "jdbc:postgresql://localhost/databasename"; + Properties props = new Properties(); + props.setProperty("user", "bdd"); + props.setProperty("password", "bdd"); + try { + conn = DriverManager.getConnection(url, props); + } + catch (SQLException e) { + e.printStackTrace(); + exit(1); + } + try { + Statement statement = conn.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT * FROM turista"); + ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); + for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { + System.out.printf("%-20s", resultSetMetaData.getColumnName(i)); + } + System.out.print("\n\n"); + while (resultSet.next()) { + for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { + Object object = resultSet.getObject(i); + System.out.printf("%-20s", object == null ? "NULL" : object.toString()); + } + System.out.print("\n"); + } + resultSet.close(); + statement.close(); + } + catch (SQLException e) { + e.printStackTrace(); + } + try { + conn.close(); + } + catch (SQLException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + new PostgresqlDemo(); + } +} diff --git a/php/postgresql-demo.php b/php/postgresql-demo.php new file mode 100644 index 0000000..189ad53 --- /dev/null +++ b/php/postgresql-demo.php @@ -0,0 +1,27 @@ +'; +for ($i = 0; $i < pg_num_fields($result); $i++) { + echo ''.pg_field_name($result, $i).''; +} +echo ''; +while ($row = pg_fetch_row($result)) { + echo ''; + for ($i = 0; $i < pg_num_fields($result); $i++) { + echo ''.$row[$i].''; + } + echo ''; +} +echo ''; +pg_close($conn); +?> diff --git a/vala/PostgresqlDemo.vala b/vala/PostgresqlDemo.vala new file mode 100644 index 0000000..835202b --- /dev/null +++ b/vala/PostgresqlDemo.vala @@ -0,0 +1,30 @@ +namespace PostgresqlDemo { + using Postgres; + + public static int main (string[] args) { + Database conn = set_db_login ("localhost", "5432", "", "", "database", "user", "password"); + if (conn.get_status () != ConnectionStatus.OK) { + stderr.printf ("%s\n", conn.get_error_message ()); + return 1; + } + + Result res = conn.exec ("SELECT * FROM turista"); + if (res.get_status () != ExecStatus.TUPLES_OK) { + stderr.printf ("SELECT failed: %s", conn.get_error_message ()); + return 1; + } + + for (int i = 0; i < res.get_n_fields (); i++) { + stdout.printf ("%-20s", res.get_field_name (i)); + } + stdout.printf ("\n\n"); + + for (int i = 0; i < res.get_n_tuples (); i++) { + for (int j = 0; j < res.get_n_fields (); j++) { + stdout.printf ("%-20s", res.get_value (i, j)); + } + stdout.printf ("\n"); + } + return 0; + } +} diff --git a/vala/meson.build b/vala/meson.build new file mode 100644 index 0000000..0159621 --- /dev/null +++ b/vala/meson.build @@ -0,0 +1,24 @@ +project('postgresqldemo', + ['c', 'vala'], + version: '1.0.0', + license: 'BSD-3', + default_options: [ + 'b_ofast=if-release', + 'b_march_native=if-release', + 'b_ndebug=if-release' + ] +) +add_global_arguments('-DGETTEXT_PACKAGE="sernatur"', language: 'c') + +glib_dep = dependency('glib-2.0') +pq_dep = dependency('libpq', version: '>=8.0') + +vala_sources = files('PostgresqlDemo.vala') + +sources = vala_sources + +exe = executable('postgresql_demo', + sources, + dependencies: [glib_dep, pq_dep], + install: false) +