location fixed for postgresql client
This commit is contained in:
45
examples/builder/clients/psql.vsh
Executable file
45
examples/builder/clients/psql.vsh
Executable file
@@ -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!')
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
// }
|
||||
@@ -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(
|
||||
|
||||
@@ -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'
|
||||
}
|
||||
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'
|
||||
// Create locations database if it doesn't exist
|
||||
if !db_client.db_exists('locations')! {
|
||||
db_client.db_create('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}') }
|
||||
// Switch to locations database
|
||||
db_client.dbname = 'locations'
|
||||
|
||||
init_db.exec("CREATE DATABASE ${dbname}") or {}
|
||||
init_db.close()
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -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)!
|
||||
|
||||
@@ -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})')
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
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 {
|
||||
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].trim_space()
|
||||
tag_name := parts[1].all_after('refs/tags/').trim_space()
|
||||
|
||||
// Update remote tags info
|
||||
repo.status_remote.tags[tag_name] = commit_hash
|
||||
|
||||
Reference in New Issue
Block a user