Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
* Enable Vala-Lint
* Use gadbc-1.0 for Vala package name
* Require Apache Arrow GLib only when -Dexample=true or -Dvapi=true
* Simplify examples
  • Loading branch information
kou committed Nov 13, 2023
1 parent 11b20f5 commit 94ad68f
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 124 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ repos:
hooks:
- id: isort
types_or: [python]
# TODO: Change this to vala-lang/vala-lint after
# https://github.com/vala-lang/vala-lint/pull/179 is merged.
- repo: https://github.com/kou/vala-lint
rev: pre-commit-hook
hooks:
- id: vala-lint
- repo: local
hooks:
- id: apache-rat
Expand Down
8 changes: 4 additions & 4 deletions glib/adbc-glib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ adbc_glib_gir = gnome.generate_gir(libadbc_glib,
sources: sources + definition_headers + enums,
symbol_prefix: 'gadbc')
if generate_vapi
adbc_glib_vapi = gnome.generate_vapi('adbc-glib',
install: true,
packages: ['gobject-2.0', 'arrow-glib'],
sources: [adbc_glib_gir[0]])
adbc_glib_vapi = gnome.generate_vapi('gadbc-1.0',
install: true,
packages: ['gobject-2.0'],
sources: [adbc_glib_gir[0]])
endif
7 changes: 4 additions & 3 deletions glib/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
There are example codes in this directory.

C example codes exist in this directory. Language bindings example
codes exists in sub directories. For example, Vala example codes exists
in `vala/` sub directory.
codes exists in sub directories:

* `vala/`: Vala examples

## C example codes

Here are example codes in this directory:

* `sqlite.c`: It shows how to connect to a database using SQLite.
* `sqlite.c`: It shows how to connect to a SQLite database.
11 changes: 6 additions & 5 deletions glib/example/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
# specific language governing permissions and limitations
# under the License.

arrow_glib = dependency('arrow-glib')

executable('sqlite', 'sqlite.c',
dependencies: [adbc_glib, arrow_glib],
link_language: 'c')
if build_example
arrow_glib = dependency('arrow-glib')
executable('sqlite', 'sqlite.c',
dependencies: [adbc_glib, arrow_glib],
link_language: 'c')
endif

install_data('README.md',
install_dir: join_paths(data_dir, meson.project_name(), 'example'))
Expand Down
138 changes: 63 additions & 75 deletions glib/example/sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,125 +23,113 @@
#include <arrow-glib/arrow-glib.h>

int main(int argc, char** argv) {
GADBCDatabase* database;
GADBCConnection* conn;
int result = EXIT_FAILURE;
GADBCDatabase* database = NULL;
GADBCConnection* connection = NULL;
GADBCStatement* statement = NULL;
GError* error = NULL;

database = gadbc_database_new(&error);
if (!database) {
g_print("Error creating a Database: %s", error->message);
g_error_free(error);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to create a database: %s", error->message);
goto exit;
}

if (!gadbc_database_set_option(database, "driver", "adbc_driver_sqlite", &error)) {
g_print("Error initializing the database: %s", error->message);
g_error_free(error);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to set driver: %s", error->message);
goto exit;
}
if (!gadbc_database_set_option(database, "uri", "test.db", &error)) {
g_print("Error initializing the database: %s", error->message);
g_error_free(error);
g_object_unref(database);
return EXIT_FAILURE;
if (!gadbc_database_set_option(database, "uri", ":memory:", &error)) {
g_print("Failed to set database URI: %s", error->message);
goto exit;
}
if (!gadbc_database_init(database, &error)) {
g_print("Error initializing the database: %s", error->message);
g_error_free(error);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to initialize a database: %s", error->message);
goto exit;
}

conn = gadbc_connection_new(&error);
if (!conn) {
g_print("Error creating a Connection: %s", error->message);
g_error_free(error);
g_object_unref(database);
g_object_unref(conn);
return EXIT_FAILURE;
connection = gadbc_connection_new(&error);
if (!connection) {
g_print("Failed to create a connection: %s", error->message);
goto exit;
}
if (!gadbc_connection_init(conn, database, &error)) {
g_print("Error initializing a Connection: %s", error->message);
g_error_free(error);
g_object_unref(database);
g_object_unref(conn);
return EXIT_FAILURE;
if (!gadbc_connection_init(connection, database, &error)) {
g_print("Failed to initialize a connection: %s", error->message);
goto exit;
}

GADBCStatement* statement = gadbc_statement_new(conn, &error);
statement = gadbc_statement_new(connection, &error);
if (!statement) {
g_print("Error initializing a statement: %s", error->message);
g_error_free(error);
g_object_unref(conn);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to create a statement: %s", error->message);
goto exit;
}
if (!gadbc_statement_set_sql_query(statement, "select sqlite_version() as version",
&error)) {
g_print("Error setting a query: %s", error->message);
g_error_free(error);
g_object_unref(statement);
g_object_unref(conn);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to set a query: %s", error->message);
goto exit;
}

gpointer c_abi_array_stream;
gint64 n_rows_affected;
if (!gadbc_statement_execute(statement, TRUE, &c_abi_array_stream, &n_rows_affected,
&error)) {
g_print("Error executing a query: %s", error->message);
g_error_free(error);
g_object_unref(statement);
g_object_unref(conn);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to execute a query: %s", error->message);
goto exit;
}

GArrowRecordBatchReader* reader =
garrow_record_batch_reader_import(c_abi_array_stream, &error);
g_free(c_abi_array_stream);
if (!reader) {
g_print("Error importing a result: %s", error->message);
g_error_free(error);
g_object_unref(statement);
g_object_unref(conn);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to import a result: %s", error->message);
goto exit;
}

GArrowTable* table = garrow_record_batch_reader_read_all(reader, &error);
g_object_unref(reader);
if (!table) {
g_print("Error reading a result: %s", error->message);
g_error_free(error);
g_object_unref(statement);
g_object_unref(conn);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to read a result: %s", error->message);
goto exit;
}
gchar* table_content = garrow_table_to_string(table, &error);
g_object_unref(table);
if (!table_content) {
g_print("Error stringify a result: %s", error->message);
g_error_free(error);
g_object_unref(statement);
g_object_unref(conn);
g_object_unref(database);
return EXIT_FAILURE;
g_print("Failed to stringify a result: %s", error->message);
goto exit;
}
g_print("Result:\n%s\n", table_content);
g_free(table_content);
gadbc_statement_release(statement, &error);

result = EXIT_SUCCESS;

exit:
if (error) {
g_print("Error releasing a statement: %s", error->message);
g_error_free(error);
error = NULL;
}
g_object_unref(statement);
g_object_unref(conn);
g_object_unref(database);

return EXIT_SUCCESS;
if (statement) {
if (!gadbc_statement_release(statement, &error)) {
g_print("Failed to release a statement: %s", error->message);
g_error_free(error);
error = NULL;
}
g_object_unref(statement);
}
if (connection) {
if (!gadbc_connection_release(connection, &error)) {
g_print("Failed to release a connection: %s", error->message);
g_error_free(error);
error = NULL;
}
g_object_unref(connection);
}
if (database) {
if (!gadbc_database_release(database, &error)) {
g_print("Failed to release a database: %s", error->message);
g_error_free(error);
error = NULL;
}
g_object_unref(database);
}
return result;
}
4 changes: 2 additions & 2 deletions glib/example/vala/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ There are Vala example codes in this directory.
Here is a command line to build an example in this directory:

```console
$ valac --pkg adbc-glib --pkg posix XXX.vala
$ valac --pkg adbc-glib --pkg arrow-glib --pkg posix XXX.vala
```

## GLib Vala example codes
## Vala example codes

Here are example codes in this directory:

Expand Down
3 changes: 2 additions & 1 deletion glib/example/vala/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# specific language governing permissions and limitations
# under the License.

if generate_vapi
if build_example and generate_vapi
vala_example_executable_kwargs = {
'c_args': [
'-I' + meson.build_root(),
Expand All @@ -30,6 +30,7 @@ if generate_vapi
],
'vala_args': [
'--pkg', 'posix',
'--vapidir', arrow_glib.get_variable('vapidir'),
],
}
executable('sqlite', 'sqlite.vala',
Expand Down
71 changes: 37 additions & 34 deletions glib/example/vala/sqlite.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,48 @@
// specific language governing permissions and limitations
// under the License.


int main (string[] args) {
try {
var database = new GADBC.Database ();
database.set_option ("driver", "adbc_driver_sqlite");
database.set_option ("uri", "test.sqlite");
database.init ();
GLib.message ("Database initialization done...");
var exit_code = Posix.EXIT_FAILURE;

try {
var conn = new GADBC.Connection ();
conn.init (database);
GLib.message ("Connection to database initialized...");
try {
var stm = new GADBC.Statement (conn);
string sql = "SELECT sqlite_version() AS version";
stm.set_sql_query (sql);
void *c_abi_array_stream = null;
stm.execute (true, out c_abi_array_stream, null);
var database = new GADBC.Database ();
database.set_option ("driver", "adbc_driver_sqlite");
database.set_option ("uri", ":memory:");
database.init ();
try {
GLib.message ("Statement executed: %s", sql);
var reader = GArrow.RecordBatchReader.import (c_abi_array_stream);
var table = reader.read_all ();
GLib.message ("Executed result: %s", table.to_string ());
} finally {
GLib.free (c_abi_array_stream);
var connection = new GADBC.Connection ();
connection.init (database);
try {
var statement = new GADBC.Statement (connection);
string sql = "SELECT sqlite_version() AS version";
statement.set_sql_query (sql);
try {
void *c_abi_array_stream = null;
int64 n_rows_affected;
statement.execute (true, out c_abi_array_stream, out n_rows_affected);
try {
var reader = GArrow.RecordBatchReader.import (c_abi_array_stream);
var table = reader.read_all ();
stdout.printf ("Result:\n%s", table.to_string ());
} finally {
GLib.free (c_abi_array_stream);
}
exit_code = Posix.EXIT_SUCCESS;
} catch (GLib.Error error) {
GLib.error ("Failed to execute a statement: %s", error.message);
}
}
catch (GLib.Error error) {
GLib.error ("Failed to create a statement: %s", error.message);
}
}
catch (GLib.Error error) {
GLib.error ("Failed to create a connection: %s", error.message);
}
GLib.message ("Statement executed: %s", sql);
}
catch (GLib.Error e) {
GLib.message ("Error executing statement: %s", e.message);
}
}
catch (GLib.Error e) {
GLib.message ("Error initializing the connection: %s", e.message);
catch (GLib.Error error) {
GLib.error ("Failed to create a database: %s", error.message);
}
}
catch (GLib.Error e) {
GLib.message ("Error initializing the database: %s", e.message);
}

return Posix.EXIT_SUCCESS;
return exit_code;
}
2 changes: 2 additions & 0 deletions glib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ else
dirs: [adbc_build_dir])
endif

build_example = get_option('example')

dependency('gobject-introspection-1.0', required: false).found()
generate_vapi = get_option('vapi')
if generate_vapi
Expand Down
5 changes: 5 additions & 0 deletions glib/meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ option('adbc_build_dir',
value: '',
description: 'Use this option to build with not installed ADBC')

option('example',
type: 'boolean',
value: false,
description: 'Build example')

option('vapi',
type: 'boolean',
value: false,
Expand Down

0 comments on commit 94ad68f

Please sign in to comment.