...
This commit is contained in:
44
examples/hero/db/psql.vsh
Executable file
44
examples/hero/db/psql.vsh
Executable file
@@ -0,0 +1,44 @@
|
||||
#!/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
|
||||
import freeflowuniverse.herolib.core.playbook
|
||||
// import freeflowuniverse.herolib.core.playcmds
|
||||
|
||||
// Configure PostgreSQL client
|
||||
heroscript := "
|
||||
!!postgresql_client.configure
|
||||
name:'test'
|
||||
user: 'postgres'
|
||||
port: 5432
|
||||
host: 'localhost'
|
||||
password: '1234'
|
||||
dbname: 'postgres'
|
||||
"
|
||||
mut plbook := playbook.new(text: heroscript)!
|
||||
postgresql_client.play(mut plbook)!
|
||||
|
||||
// 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,6 +1,7 @@
|
||||
module postgresql_client
|
||||
|
||||
import freeflowuniverse.herolib.data.paramsparser
|
||||
import freeflowuniverse.herolib.data.encoderhero
|
||||
import os
|
||||
import db.pg
|
||||
|
||||
@@ -10,7 +11,7 @@ const default = true
|
||||
|
||||
pub fn heroscript_default() !string {
|
||||
heroscript := "
|
||||
!!postgresql_client.configure
|
||||
!!postgresql_client.configure
|
||||
name:'default'
|
||||
user: 'root'
|
||||
port: 5432
|
||||
@@ -21,6 +22,15 @@ pub fn heroscript_default() !string {
|
||||
return heroscript
|
||||
}
|
||||
|
||||
pub fn heroscript_dumps(obj PostgresClient) !string {
|
||||
return encoderhero.encode[PostgresClient](obj)!
|
||||
}
|
||||
|
||||
pub fn heroscript_loads(heroscript string) !PostgresClient {
|
||||
mut obj := encoderhero.decode[PostgresClient](heroscript)!
|
||||
return obj
|
||||
}
|
||||
|
||||
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
|
||||
|
||||
@[heap]
|
||||
|
||||
@@ -3,7 +3,6 @@ module hero_db
|
||||
import json
|
||||
import freeflowuniverse.herolib.clients.postgresql_client
|
||||
import freeflowuniverse.herolib.core.texttools
|
||||
import time
|
||||
|
||||
// Generic database interface for Hero root objects
|
||||
pub struct HeroDB[T] {
|
||||
@@ -11,7 +10,7 @@ pub struct HeroDB[T] {
|
||||
table_name string
|
||||
}
|
||||
|
||||
// Initialize a new HeroDB instance for a specific type
|
||||
// new creates a new HeroDB instance for a specific type T
|
||||
pub fn new[T](client &postgresql_client.PostgresClient) HeroDB[T] {
|
||||
mut table_name := '${texttools.snake_case(T.name)}s'
|
||||
// Map dirname from module path
|
||||
@@ -27,7 +26,7 @@ pub fn new[T](client &postgresql_client.PostgresClient) HeroDB[T] {
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure table exists with proper schema
|
||||
// ensure_table creates the database table with proper schema for type T
|
||||
pub fn (mut self HeroDB[T]) ensure_table() ! {
|
||||
// Get index fields from struct reflection
|
||||
index_fields := self.get_index_fields()
|
||||
@@ -73,7 +72,7 @@ fn (self HeroDB[T]) get_index_fields() []string {
|
||||
return fields
|
||||
}
|
||||
|
||||
// Save object to database
|
||||
// save stores the object T in the database, updating if it already exists
|
||||
pub fn (mut self HeroDB[T]) save(obj &T) ! {
|
||||
// Get index values from object
|
||||
index_data := self.extract_index_values(obj)
|
||||
@@ -125,7 +124,7 @@ pub fn (mut self HeroDB[T]) save(obj &T) ! {
|
||||
}
|
||||
}
|
||||
|
||||
// Get object by index values
|
||||
// get_by_index retrieves an object T by its index values
|
||||
pub fn (mut self HeroDB[T]) get_by_index(index_values map[string]string) !&T {
|
||||
mut query := 'SELECT data FROM ${self.table_name} WHERE '
|
||||
mut params := []string{}
|
||||
@@ -148,7 +147,7 @@ pub fn (mut self HeroDB[T]) get_by_index(index_values map[string]string) !&T {
|
||||
return &obj
|
||||
}
|
||||
|
||||
// Get all objects
|
||||
// get_all retrieves all objects T from the database
|
||||
pub fn (mut self HeroDB[T]) get_all() ![]&T {
|
||||
query := 'SELECT data FROM ${self.table_name} ORDER BY id DESC'
|
||||
rows := self.db_client.exec(query)!
|
||||
@@ -165,7 +164,7 @@ pub fn (mut self HeroDB[T]) get_all() ![]&T {
|
||||
return results
|
||||
}
|
||||
|
||||
// Search by index field
|
||||
// search_by_index searches for objects T by a specific index field
|
||||
pub fn (mut self HeroDB[T]) search_by_index(field_name string, value string) ![]&T {
|
||||
query := 'SELECT data FROM ${self.table_name} WHERE ${field_name} = \'${value}\' ORDER BY id DESC'
|
||||
rows := self.db_client.exec(query)!
|
||||
@@ -182,7 +181,7 @@ pub fn (mut self HeroDB[T]) search_by_index(field_name string, value string) ![]
|
||||
return results
|
||||
}
|
||||
|
||||
// Delete by index values
|
||||
// delete_by_index removes objects T matching the given index values
|
||||
pub fn (mut self HeroDB[T]) delete_by_index(index_values map[string]string) ! {
|
||||
mut query := 'DELETE FROM ${self.table_name} WHERE '
|
||||
mut params := []string{}
|
||||
|
||||
Reference in New Issue
Block a user