From 5101c63fa0a9f92372c013b49c6da36136d65d1e Mon Sep 17 00:00:00 2001 From: Daniel Espinosa Date: Mon, 2 Oct 2023 19:31:59 -0600 Subject: [PATCH] feat(glib): add Vala VAPI for GADBC 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. --- glib/adbc-glib/meson.build | 6 +++ glib/example/README.md | 32 +++++++++++++++ glib/example/meson.build | 27 +++++++++++++ glib/example/sqlite.c | 75 +++++++++++++++++++++++++++++++++++ glib/example/vala/README.md | 36 +++++++++++++++++ glib/example/vala/meson.build | 42 ++++++++++++++++++++ glib/example/vala/sqlite.vala | 50 +++++++++++++++++++++++ glib/meson.build | 11 ++++- glib/meson_options.txt | 5 +++ 9 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 glib/example/README.md create mode 100644 glib/example/meson.build create mode 100644 glib/example/sqlite.c create mode 100644 glib/example/vala/README.md create mode 100644 glib/example/vala/meson.build create mode 100644 glib/example/vala/sqlite.vala diff --git a/glib/adbc-glib/meson.build b/glib/adbc-glib/meson.build index 1fe245e51b..d41ce8bb30 100644 --- a/glib/adbc-glib/meson.build +++ b/glib/adbc-glib/meson.build @@ -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 diff --git a/glib/example/README.md b/glib/example/README.md new file mode 100644 index 0000000000..f26c37ab5a --- /dev/null +++ b/glib/example/README.md @@ -0,0 +1,32 @@ + + +# 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. diff --git a/glib/example/meson.build b/glib/example/meson.build new file mode 100644 index 0000000000..bc365d7241 --- /dev/null +++ b/glib/example/meson.build @@ -0,0 +1,27 @@ +# -*- 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. + +executable('sqlite', 'sqlite.c', + dependencies: [adbc_glib], + link_language: 'c') + +install_data('README.md', + install_dir: join_paths(data_dir, meson.project_name(), 'example')) + +subdir('vala') diff --git a/glib/example/sqlite.c b/glib/example/sqlite.c new file mode 100644 index 0000000000..254c8ab529 --- /dev/null +++ b/glib/example/sqlite.c @@ -0,0 +1,75 @@ +/* + * 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 + +#include + +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); + 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); + return EXIT_FAILURE; + } + + gadbc_database_set_option(database, "uri", "test.db", &error); + + if (error != NULL) { + g_print("Error initializing the database: %s", error->message); + return EXIT_FAILURE; + } + + gadbc_database_init(database, &error); + + if (error != NULL) { + g_print("Error initializing the database: %s", error->message); + return EXIT_FAILURE; + } + + conn = gadbc_connection_new(&error); + + if (error != NULL) { + g_print("Error creating a Connection: %s", error->message); + return EXIT_FAILURE; + } + + gadbc_connection_init(conn, database, &error); + + if (error != NULL) { + g_print("Error initializing a Connection: %s", error->message); + return EXIT_FAILURE; + } + + g_object_unref(conn); + g_object_unref(database); + + return EXIT_SUCCESS; +} diff --git a/glib/example/vala/README.md b/glib/example/vala/README.md new file mode 100644 index 0000000000..19900ae1a9 --- /dev/null +++ b/glib/example/vala/README.md @@ -0,0 +1,36 @@ + + +# 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. diff --git a/glib/example/vala/meson.build b/glib/example/vala/meson.build new file mode 100644 index 0000000000..f5debcd7b8 --- /dev/null +++ b/glib/example/vala/meson.build @@ -0,0 +1,42 @@ +# -*- 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, + 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')) diff --git a/glib/example/vala/sqlite.vala b/glib/example/vala/sqlite.vala new file mode 100644 index 0000000000..dedb42a365 --- /dev/null +++ b/glib/example/vala/sqlite.vala @@ -0,0 +1,50 @@ +// 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 = "CREATE TABLE IF NOT EXISTS test (id INTEGER, name TEXT)"; + stm.set_sql_query (sql); + stm.execute (false, null, null); + 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; +} diff --git a/glib/meson.build b/glib/meson.build index 190023f095..54e96083b0 100644 --- a/glib/meson.build +++ b/glib/meson.build @@ -30,7 +30,7 @@ version_major = version_numbers[0].to_int() version_minor = version_numbers[1].to_int() version_micro = version_numbers[2].to_int() -api_version = '@0@.0'.format(version_major) +api_version = '1.0' so_version = version_major library_version = '.'.join(version_numbers) @@ -39,6 +39,7 @@ include_dir = get_option('includedir') project_include_sub_dir = meson.project_name() data_dir = get_option('datadir') gir_dir = prefix / data_dir / 'gir-1.0' +vapi_dir = data_dir / 'vala' / 'vapi' gnome = import('gnome') pkgconfig = import('pkgconfig') @@ -67,7 +68,15 @@ else dirs: [adbc_build_dir]) endif +dependency('gobject-introspection-1.0', required: false).found() +generate_vapi = get_option('vapi') +if generate_vapi + pkgconfig_variables += ['vapidir=@0@'.format(vapi_dir)] + add_languages('vala') +endif + subdir('adbc-glib') +subdir('example') install_data('../LICENSE.txt', 'README.md', diff --git a/glib/meson_options.txt b/glib/meson_options.txt index 0ae1e6c341..ee0ae9497f 100644 --- a/glib/meson_options.txt +++ b/glib/meson_options.txt @@ -21,3 +21,8 @@ option('adbc_build_dir', type: 'string', value: '', description: 'Use this option to build with not installed ADBC') + +option('vapi', + type: 'boolean', + value: false, + description: 'Build Vala API')