Skip to content

Commit

Permalink
feat(glib): add Vala VAPI for GADBC
Browse files Browse the repository at this point in the history
Vala VAPI file is generated for current GADBC 1.0 API version.

It is disable by default, but recommended to be enable when
a development package is distributed.

This commit force a 1.0 API versioning, replacing the old one
where 0 were used for GIR API version (gir_version), so now
shows '1.0' instead of '0.0'.

Also examples are provided for C and Vala.
  • Loading branch information
esodan committed Oct 16, 2023
1 parent 792f3d2 commit 216784e
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 1 deletion.
6 changes: 6 additions & 0 deletions glib/adbc-glib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ adbc_glib_gir = gnome.generate_gir(libadbc_glib,
nsversion: api_version,
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'],
sources: [adbc_glib_gir[0]])
endif
32 changes: 32 additions & 0 deletions glib/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!---
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# GADBC GLib example

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.

## C example codes

Here are example codes in this directory:

* `sqlite.c`: It shows how to connect to a database using SQLite.
29 changes: 29 additions & 0 deletions glib/example/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- indent-tabs-mode: nil -*-
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# 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')

install_data('README.md',
install_dir: join_paths(data_dir, meson.project_name(), 'example'))

subdir('vala')
159 changes: 159 additions & 0 deletions glib/example/sqlite.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <stdlib.h>

#include <adbc-glib/adbc-glib.h>
#include <arrow-glib/arrow-glib.h>

int main(int argc, char** argv) {
GADBCDatabase* database;
GADBCConnection* conn;
GError* error = NULL;

database = gadbc_database_new(&error);

if (error != NULL) {
g_print("Error creating a Database: %s", error->message);
g_object_unref(database);
return EXIT_FAILURE;
}

gadbc_database_set_option(database, "driver", "adbc_driver_sqlite", &error);

if (error != NULL) {
g_print("Error initializing the database: %s", error->message);
g_error_free(error);
g_object_unref(database);
return EXIT_FAILURE;
}

gadbc_database_set_option(database, "uri", "test.db", &error);

if (error != NULL) {
g_print("Error initializing the database: %s", error->message);
g_error_free(error);
g_object_unref(database);
return EXIT_FAILURE;
}

gadbc_database_init(database, &error);

if (error != NULL) {
g_print("Error initializing the database: %s", error->message);
g_error_free(error);
g_object_unref(database);
return EXIT_FAILURE;
}

conn = gadbc_connection_new(&error);

if (error != NULL) {
g_print("Error creating a Connection: %s", error->message);
g_error_free(error);
g_object_unref(database);
g_object_unref(conn);
return EXIT_FAILURE;
}

gadbc_connection_init(conn, database, &error);

if (error != NULL) {
g_print("Error initializing a Connection: %s", error->message);
g_error_free(error);
g_object_unref(database);
g_object_unref(conn);
return EXIT_FAILURE;
}

GADBCStatement* statement = gadbc_statement_new(conn, &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;
}
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;
}

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;
}

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;
}
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;
}
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("Result:\n%s\n", table_content);
g_free(table_content);
gadbc_statement_release(statement, &error);
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;
}
36 changes: 36 additions & 0 deletions glib/example/vala/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!---
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# ADBC GLib Vala example

There are Vala example codes in this directory.

## How to build

Here is a command line to build an example in this directory:

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

## GLib Vala example codes

Here are example codes in this directory:

* `sqlite.vala`: It shows how to connect to a SQLite database.
43 changes: 43 additions & 0 deletions glib/example/vala/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- indent-tabs-mode: nil -*-
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

if generate_vapi
vala_example_executable_kwargs = {
'c_args': [
'-I' + meson.build_root(),
'-I' + meson.source_root(),
],
'dependencies': [
adbc_glib_vapi,
arrow_glib,
dependency('gobject-2.0'),
],
'vala_args': [
'--pkg', 'posix',
],
}
executable('sqlite', 'sqlite.vala',
kwargs: vala_example_executable_kwargs)
endif

install_data('README.md',
install_dir: join_paths(data_dir,
meson.project_name(),
'example',
'vala'))
59 changes: 59 additions & 0 deletions glib/example/vala/sqlite.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// 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...");
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);
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);
}
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 e) {
GLib.message ("Error initializing the database: %s", e.message);
}

return Posix.EXIT_SUCCESS;
}
Loading

0 comments on commit 216784e

Please sign in to comment.