This commit is contained in:
2025-07-30 23:43:41 +02:00
parent 34da4f06e1
commit a7f6548bea
12 changed files with 140 additions and 95 deletions

View File

@@ -1,6 +1,6 @@
!!hero_code.generate_client
name: "postgresql_client"
classname: "PostgresClient"
classname: "PostgresqlClient"
hasconfig: true
singleton: false
default: true

View File

@@ -6,12 +6,12 @@ import freeflowuniverse.herolib.osal.core as osal
import os
import freeflowuniverse.herolib.ui.console
pub fn (mut self PostgresClient) check() ! {
pub fn (mut self PostgresqlClient) check() ! {
mut db := self.db()!
db.exec('SELECT version();') or { return error('can\t select version from database.\n${self}') }
}
pub fn (mut self PostgresClient) exec(c_ string) ![]pg.Row {
pub fn (mut self PostgresqlClient) exec(c_ string) ![]pg.Row {
mut db := self.db()!
mut c := c_
if !(c.trim_space().ends_with(';')) {
@@ -22,7 +22,7 @@ pub fn (mut self PostgresClient) exec(c_ string) ![]pg.Row {
}
}
pub fn (mut self PostgresClient) db_exists(name_ string) !bool {
pub fn (mut self PostgresqlClient) db_exists(name_ string) !bool {
mut db := self.db()!
r := db.exec("SELECT datname FROM pg_database WHERE datname='${name_}';")!
if r.len == 1 {
@@ -35,7 +35,7 @@ pub fn (mut self PostgresClient) db_exists(name_ string) !bool {
return false
}
pub fn (mut self PostgresClient) db_create(name_ string) ! {
pub fn (mut self PostgresqlClient) db_create(name_ string) ! {
name := texttools.name_fix(name_)
mut db := self.db()!
if !self.db_exists(name)! {
@@ -47,7 +47,7 @@ pub fn (mut self PostgresClient) db_create(name_ string) ! {
}
}
pub fn (mut self PostgresClient) db_delete(name_ string) ! {
pub fn (mut self PostgresqlClient) db_delete(name_ string) ! {
mut db := self.db()!
name := texttools.name_fix(name_)
self.check()!
@@ -60,7 +60,7 @@ pub fn (mut self PostgresClient) db_delete(name_ string) ! {
}
}
pub fn (mut self PostgresClient) db_names() ![]string {
pub fn (mut self PostgresqlClient) db_names() ![]string {
mut res := []string{}
sqlstr := "SELECT datname FROM pg_database WHERE datistemplate = false and datname != 'postgres' and datname != 'root';"
for row in self.exec(sqlstr)! {
@@ -77,7 +77,7 @@ pub mut:
dest string
}
pub fn (mut self PostgresClient) backup(args BackupParams) ! {
pub fn (mut self PostgresqlClient) backup(args BackupParams) ! {
if args.dest == '' {
return error('specify the destination please')
}

View File

@@ -5,7 +5,7 @@ import freeflowuniverse.herolib.core.playbook { PlayBook }
import freeflowuniverse.herolib.ui.console
__global (
postgresql_client_global map[string]&PostgresClient
postgresql_client_global map[string]&PostgresqlClient
postgresql_client_default string
)
@@ -25,10 +25,10 @@ fn args_get(args_ ArgsGet) ArgsGet {
return args
}
pub fn get(args_ ArgsGet) !&PostgresClient {
pub fn get(args_ ArgsGet) !&PostgresqlClient {
mut context := base.context()!
mut args := args_get(args_)
mut obj := PostgresClient{
mut obj := PostgresqlClient{
name: args.name
}
if args.name !in postgresql_client_global {
@@ -48,11 +48,10 @@ pub fn get(args_ ArgsGet) !&PostgresClient {
}
// register the config for the future
pub fn set(o PostgresClient) ! {
pub fn set(o PostgresqlClient) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
println(heroscript)
context.hero_config_set('postgresql_client', o.name, heroscript)!
}
@@ -73,7 +72,7 @@ pub fn delete(args_ ArgsGet) ! {
}
// only sets in mem, does not set as config
fn set_in_mem(o PostgresClient) ! {
fn set_in_mem(o PostgresqlClient) ! {
mut o2 := obj_init(o)!
postgresql_client_global[o.name] = &o2
postgresql_client_default = o.name
@@ -84,10 +83,7 @@ pub fn play(mut plbook PlayBook) ! {
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
println(heroscript)
mut obj2 := heroscript_loads(heroscript)!
println('postgresql_client playbook action: ${obj2}')
if true{panic("TODO: implement playbook action for postgresql_client, currently not implemented yet.")}
set(obj2)!
}
}

View File

@@ -9,7 +9,7 @@ pub const version = '0.0.0'
const singleton = false
const default = true
pub struct PostgresClient {
pub struct PostgresqlClient {
mut:
db_ ?pg.DB @[skip]
pub mut:
@@ -17,17 +17,17 @@ pub mut:
user string = 'root'
port int = 5432
host string = 'localhost'
password string
password string = ''
dbname string = 'postgres'
}
fn obj_init(obj_ PostgresClient) !PostgresClient {
fn obj_init(obj_ PostgresqlClient) !PostgresqlClient {
// never call get here, only thing we can do here is work on object itself
mut obj := obj_
return obj
}
pub fn (mut self PostgresClient) db() !pg.DB {
pub fn (mut self PostgresqlClient) db() !pg.DB {
// console.print_debug(args)
mut db := self.db_ or {
mut db_ := pg.connect(
@@ -43,14 +43,13 @@ pub fn (mut self PostgresClient) db() !pg.DB {
return db
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj PostgresClient) !string {
return encoderhero.encode[PostgresClient](obj)!
pub fn heroscript_dumps(obj PostgresqlClient) !string {
return encoderhero.encode[PostgresqlClient](obj)!
}
pub fn heroscript_loads(heroscript string) !PostgresClient {
mut obj := encoderhero.decode[PostgresClient](heroscript)!
pub fn heroscript_loads(heroscript string) !PostgresqlClient {
mut obj := encoderhero.decode[PostgresqlClient](heroscript)!
return obj
}