From f4f5eb06a4825a35116de150112f021f2cf135c9 Mon Sep 17 00:00:00 2001 From: despiegk Date: Thu, 6 Feb 2025 07:15:32 +0300 Subject: [PATCH] location fixed for postgresql client --- examples/builder/clients/psql.vsh | 45 +++++++++++++++ .../data/location/location_example_tcc.vsh | 22 +++++++- lib/clients/postgresql_client/client.v | 56 ------------------- .../postgresql_client_model.v | 2 +- lib/data/location/db.v | 51 ++++------------- lib/data/location/factory.v | 27 +++++++-- lib/develop/gittools/repository.v | 9 ++- lib/develop/gittools/repository_load.v | 24 ++++---- 8 files changed, 116 insertions(+), 120 deletions(-) create mode 100755 examples/builder/clients/psql.vsh delete mode 100644 lib/clients/postgresql_client/client.v diff --git a/examples/builder/clients/psql.vsh b/examples/builder/clients/psql.vsh new file mode 100755 index 00000000..7b64441b --- /dev/null +++ b/examples/builder/clients/psql.vsh @@ -0,0 +1,45 @@ +#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run + +import freeflowuniverse.herolib.core +import freeflowuniverse.herolib.clients.postgresql_client + + +// Configure PostgreSQL client +heroscript := " +!!postgresql_client.configure + name:'test' + user: 'postgres' + port: 5432 + host: 'localhost' + password: '1234' + dbname: 'postgres' +" + +// Process the heroscript configuration +postgresql_client.play(heroscript: heroscript)! + +// Get the configured client +mut db_client := postgresql_client.get(name: "test")! + +// Check if test database exists, create if not +if !db_client.db_exists('test')! { + println('Creating database test...') + db_client.db_create('test')! +} + +// Switch to test database +db_client.dbname = 'test' + +// Create table if not exists +create_table_sql := "CREATE TABLE IF NOT EXISTS users ( + id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + email VARCHAR(255) UNIQUE NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +)" + +println('Creating table users if not exists...') +db_client.exec(create_table_sql)! + +println('Database and table setup completed successfully!') + diff --git a/examples/data/location/location_example_tcc.vsh b/examples/data/location/location_example_tcc.vsh index 5769ba99..ab1a7efe 100755 --- a/examples/data/location/location_example_tcc.vsh +++ b/examples/data/location/location_example_tcc.vsh @@ -1,10 +1,28 @@ #!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run - +import freeflowuniverse.herolib.clients.postgresql_client import freeflowuniverse.herolib.data.location +// Configure PostgreSQL client +heroscript := " +!!postgresql_client.configure + name:'test' + user: 'postgres' + port: 5432 + host: 'localhost' + password: '1234' + dbname: 'postgres' +" + +// Process the heroscript configuration +postgresql_client.play(heroscript: heroscript)! + +// Get the configured client +mut db_client := postgresql_client.get(name: "test")! + + // Create a new location instance -mut loc := location.new(false) or { panic(err) } +mut loc := location.new(mut db_client, false) or { panic(err) } println('Location database initialized') // Initialize the database (downloads and imports data) diff --git a/lib/clients/postgresql_client/client.v b/lib/clients/postgresql_client/client.v deleted file mode 100644 index 7027b20c..00000000 --- a/lib/clients/postgresql_client/client.v +++ /dev/null @@ -1,56 +0,0 @@ -module postgresql_client - -import freeflowuniverse.herolib.core.base -import db.pg -import freeflowuniverse.herolib.core.texttools -import freeflowuniverse.herolib.ui.console - -// pub struct PostgresClient { -// base.BaseConfig -// pub mut: -// config Config -// db pg.DB -// } - -// @[params] -// pub struct ClientArgs { -// pub mut: -// instance string @[required] -// // playargs ?play.PlayArgs -// } - -// pub fn get(clientargs ClientArgs) !PostgresClient { -// // mut plargs := clientargs.playargs or { -// // // play.PlayArgs -// // // { -// // // } -// // } - -// // mut cfg := configurator(clientargs.instance, plargs)! -// // mut args := cfg.get()! - -// args.instance = texttools.name_fix(args.instance) -// if args.instance == '' { -// args.instance = 'default' -// } -// // console.print_debug(args) -// mut db := pg.connect( -// host: args.host -// user: args.user -// port: args.port -// password: args.password -// dbname: args.dbname -// )! -// // console.print_debug(postgres_client) -// return PostgresClient{ -// instance: args.instance -// db: db -// config: args -// } -// } - -// struct LocalConfig { -// name string -// path string -// passwd string -// } diff --git a/lib/clients/postgresql_client/postgresql_client_model.v b/lib/clients/postgresql_client/postgresql_client_model.v index d645fdeb..67a06032 100644 --- a/lib/clients/postgresql_client/postgresql_client_model.v +++ b/lib/clients/postgresql_client/postgresql_client_model.v @@ -55,7 +55,7 @@ fn obj_init(obj_ PostgresClient) !PostgresClient { return obj } -fn (mut self PostgresClient) db() !pg.DB { +pub fn (mut self PostgresClient) db() !pg.DB { // console.print_debug(args) mut db := self.db_ or { mut db_ := pg.connect( diff --git a/lib/data/location/db.v b/lib/data/location/db.v index 8b564ccf..9569eddb 100644 --- a/lib/data/location/db.v +++ b/lib/data/location/db.v @@ -5,62 +5,35 @@ import os import encoding.csv import freeflowuniverse.herolib.osal import freeflowuniverse.herolib.core.pathlib +import freeflowuniverse.herolib.clients.postgresql_client // LocationDB handles all database operations for locations pub struct LocationDB { -mut: +pub mut: db pg.DB + db_client postgresql_client.PostgresClient tmp_dir pathlib.Path db_dir pathlib.Path } // new_location_db creates a new LocationDB instance -pub fn new_location_db(reset bool) !LocationDB { +pub fn new_location_db(mut db_client postgresql_client.PostgresClient, reset bool) !LocationDB { mut db_dir := pathlib.get_dir(path:'${os.home_dir()}/hero/var/db/location.db',create: true)! - // PostgreSQL connection parameters with defaults - mut host := os.getenv('POSTGRES_HOST') - if host == '' { - host = 'localhost' + // Create locations database if it doesn't exist + if !db_client.db_exists('locations')! { + db_client.db_create('locations')! } - port := os.getenv('POSTGRES_PORT') - port_num := if port == '' { 5432 } else { port.int() } - mut user := os.getenv('POSTGRES_USER') - if user == '' { - user = 'postgres' - } - mut password := os.getenv('POSTGRES_PASSWORD') - if password == '' { - password = '1234' - } - mut dbname := os.getenv('POSTGRES_DB') - if dbname == '' { - dbname = 'locations' - } - - // First try to connect to create the database if it doesn't exist - mut init_db := pg.connect( - host: host - port: port_num - user: user - password: password - dbname: 'postgres' - ) or { return error('Failed to connect to PostgreSQL: ${err}') } - init_db.exec("CREATE DATABASE ${dbname}") or {} - init_db.close() + // Switch to locations database + db_client.dbname = 'locations' - // Now connect to our database - db := pg.connect( - host: host - port: port_num - user: user - password: password - dbname: dbname - ) or { return error('Failed to connect to PostgreSQL: ${err}') } + // Get the underlying pg.DB connection + db := db_client.db()! mut loc_db := LocationDB{ db: db + db_client: db_client tmp_dir: pathlib.get_dir(path: '/tmp/location/',create: true)! db_dir: db_dir } diff --git a/lib/data/location/factory.v b/lib/data/location/factory.v index 298aa47a..afe2139f 100644 --- a/lib/data/location/factory.v +++ b/lib/data/location/factory.v @@ -1,16 +1,20 @@ module location +import freeflowuniverse.herolib.clients.postgresql_client + // Location represents the main API for location operations pub struct Location { mut: db LocationDB + db_client postgresql_client.PostgresClient } // new creates a new Location instance -pub fn new(reset bool) !Location { - db := new_location_db(reset)! +pub fn new(mut db_client postgresql_client.PostgresClient, reset bool) !Location { + db := new_location_db(mut db_client, reset)! return Location{ db: db + db_client: db_client } } @@ -22,12 +26,25 @@ pub fn (mut l Location) download_and_import(redownload bool) ! { // Example usage: /* fn main() ! { - // Create a new location instance - mut loc := location.new()! + // Configure and get PostgreSQL client + heroscript := " + !!postgresql_client.configure + name:'test' + user: 'postgres' + port: 5432 + host: 'localhost' + password: '1234' + dbname: 'postgres' + " + postgresql_client.play(heroscript: heroscript)! + mut db_client := postgresql_client.get(name: "test")! + + // Create a new location instance with db_client + mut loc := location.new(db_client, false)! // Initialize the database (downloads and imports data) // Only needs to be done once or when updating data - loc.init_database()! + loc.download_and_import(false)! // Search for a city results := loc.search('London', 'GB', 5, true)! diff --git a/lib/develop/gittools/repository.v b/lib/develop/gittools/repository.v index 99ba46c5..11844d9e 100644 --- a/lib/develop/gittools/repository.v +++ b/lib/develop/gittools/repository.v @@ -310,11 +310,10 @@ fn (mut repo GitRepo) update_submodules() ! { } fn (repo GitRepo) exec(cmd_ string) !string { - import os { quoted_path } - repo_path := quoted_path(repo.path()) - cmd_args := ["sh", "-c", "cd ${repo_path} && ${cmd_}"] - // console.print_debug(cmd_args.join(" ")) - r := os.execute_opt(cmd_args)! + repo_path := repo.path() + cmd := 'cd ${repo_path} && ${cmd_}' + // console.print_debug(cmd) + r := os.execute(cmd) if r.exit_code != 0 { return error('Repo failed to exec cmd: ${cmd}\n${r.output})') } diff --git a/lib/develop/gittools/repository_load.v b/lib/develop/gittools/repository_load.v index 6c0241df..051d57ec 100644 --- a/lib/develop/gittools/repository_load.v +++ b/lib/develop/gittools/repository_load.v @@ -94,20 +94,20 @@ fn (mut repo GitRepo) load_branches() ! { // Helper to load remote tags fn (mut repo GitRepo) load_tags() ! { - tags_result := repo.exec('git show-ref --tags') or { - return error('Failed to list tags: ${err}. Please ensure git is installed and repository is accessible.') + tags_result := repo.exec('git tag --list') or { + return error('Failed to list tags: ${err}. Please ensure git is installed and repository is accessible.') } + //println(tags_result) for line in tags_result.split('\n') { - line_trimmed := line.trim_space() - if line_trimmed == '' { - continue - } - if parts := line_trimmed.split(' refs/tags/') { - if parts.len != 2 { - continue - } - commit_hash := parts[0].trim_space() - tag_name := parts[1].trim_space() + line_trimmed := line.trim_space() + if line_trimmed != '' { + parts := line_trimmed.split(' ') + if parts.len < 2 { + //console.print_debug('Skipping malformed tag line: ${line_trimmed}') + continue + } + commit_hash := parts[0].trim_space() + tag_name := parts[1].all_after('refs/tags/').trim_space() // Update remote tags info repo.status_remote.tags[tag_name] = commit_hash