From 573ca15d6f21159e830dacb843f5a91fefb0b6e7 Mon Sep 17 00:00:00 2001 From: ana_rchy Date: Tue, 10 Mar 2026 18:53:58 +0000 Subject: [PATCH] init --- .gitignore | 2 ++ kulupu.cs | 61 +++++++++++++++++++++++++++++++++++ kulupu.csproj | 14 ++++++++ src/Account.cs | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/KUID.cs | 16 ++++++++++ src/Poki.cs | 67 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 246 insertions(+) create mode 100644 .gitignore create mode 100644 kulupu.cs create mode 100644 kulupu.csproj create mode 100644 src/Account.cs create mode 100644 src/KUID.cs create mode 100644 src/Poki.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd42ee3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ diff --git a/kulupu.cs b/kulupu.cs new file mode 100644 index 0000000..45ea65e --- /dev/null +++ b/kulupu.cs @@ -0,0 +1,61 @@ +using Npgsql; + +class kulupu { + static void Main(string[] args) { + NpgsqlConnection connection = ConnectToDatabase(); + + ClearDatabase(connection); + InitDatabase(connection); + + var account = Account.NewAccount(connection, "ion"); + account.AddPoki(new[] { "ana", "kitty" }); + account.AddPoki(new[] { "state 1" }, parentIDs: new[] { 1 }); + } + + + static NpgsqlConnection ConnectToDatabase() { + string connectionString = "Host=/run/postgresql; Username=postgres; Password=; Database=kulupu"; + + NpgsqlDataSource dataSource = NpgsqlDataSource.Create(connectionString); + NpgsqlConnection connection = dataSource.OpenConnection(); + + return connection; + } + + static void InitDatabase(NpgsqlConnection connection) { + NpgsqlCommand command = new(""" + CREATE TABLE IF NOT EXISTS accounts ( + kuid TEXT NOT NULL, + name TEXT NOT NULL + ); + CREATE TABLE IF NOT EXISTS poki ( + account_kuid TEXT NOT NULL, + id SERIAL NOT NULL, + names TEXT[] NOT NULL, + parent_poki INTEGER[], + child_poki INTEGER[], + fronting BOOLEAN NOT NULL DEFAULT false, + frontable BOOLEAN NOT NULL DEFAULT true, + tags TEXT[] + ); + CREATE TABLE IF NOT EXISTS poki_front_history ( + poki_id INTEGER NOT NULL, + switch_in_times TIMESTAMP[], + switch_out_times TIMESTAMP[], + notes TEXT[] + ) + """, connection); + + command.ExecuteNonQuery(); + } + + static void ClearDatabase(NpgsqlConnection connection) { + NpgsqlCommand command = new(""" + DROP TABLE IF EXISTS accounts; + DROP TABLE IF EXISTS poki; + DROP TABLE IF EXISTS poki_front_history + """, connection); + + command.ExecuteNonQuery(); + } +} diff --git a/kulupu.csproj b/kulupu.csproj new file mode 100644 index 0000000..f1df0ba --- /dev/null +++ b/kulupu.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/src/Account.cs b/src/Account.cs new file mode 100644 index 0000000..eb175d7 --- /dev/null +++ b/src/Account.cs @@ -0,0 +1,86 @@ +using Npgsql; + +public class Account { + NpgsqlConnection _connection = new(); + public KUID KUID { get; private set; } = new(); + + + public static Account NewAccount(NpgsqlConnection connection, string name) { + KUID kuid = new("special placeholder"); + + NpgsqlCommand command = new(""" + INSERT INTO accounts VALUES ( + ($1), + ($2) + ) + """, connection) { + Parameters = { + new NpgsqlParameter() { Value = kuid.Text }, + new NpgsqlParameter() { Value = name } + } + }; + + command.ExecuteNonQuery(); + + return AccountFromKUID(connection, kuid); + } + + public static Account AccountFromKUID(NpgsqlConnection connection, KUID kuid) { + return new() { + _connection = connection, + KUID = kuid + }; + } + + + public void AddPoki(string[] names, int[]? parentIDs = null, string[]? tags = null) { + NpgsqlCommand command = new(""" + INSERT INTO poki VALUES ( + ($1), + DEFAULT, + ($2), + ($3), + DEFAULT, DEFAULT, DEFAULT, + ($4) + ) + """, _connection) { + Parameters = { + new() { Value = KUID.Text }, + new() { Value = names }, + new() { Value = (parentIDs == null ? DBNull.Value : parentIDs) }, + new() { Value = (tags == null ? DBNull.Value : tags) }, + } + }; + + command.ExecuteNonQuery(); + + if (parentIDs == null) { + return; + } + + command = new(""" + UPDATE poki + SET child_poki = child_poki || currval('poki_id_seq') + WHERE id = ANY($1) + """, _connection) { + Parameters = { + new() { Value = parentIDs } + } + }; + + command.ExecuteNonQuery(); + } + + public void GetPokiByName(string name) { + NpgsqlCommand command = new(""" + SELECT id + FROM poki + WHERE ($1) IN names + LIMIT 1 + """, _connection) { + Parameters = { + new() { Value = name } + } + }; + } +} diff --git a/src/KUID.cs b/src/KUID.cs new file mode 100644 index 0000000..0483e82 --- /dev/null +++ b/src/KUID.cs @@ -0,0 +1,16 @@ +using Npgsql; + +public class KUID { + public string Text; + + + // for making a new KUID + public KUID() { + Text = "placeholder"; + } + + // for making an existing KUID into an object + public KUID(string text) { + Text = text; + } +} diff --git a/src/Poki.cs b/src/Poki.cs new file mode 100644 index 0000000..0fdbb94 --- /dev/null +++ b/src/Poki.cs @@ -0,0 +1,67 @@ +using Npgsql; + +public class Poki { + NpgsqlConnection _connection; + + Account _account; + int _id; + string[] _names; + Poki[]? _parentPoki, _childPoki; + bool _fronting, _frontable; + string[]? _tags; + + + public Poki( + NpgsqlConnection connection, + Account account, + string[] names, + Poki[]? parentPoki = null, Poki[]? childPoki = null, + bool fronting = false, bool frontable = true, + string[]? tags = null + ) { + _connection = connection; + _account = account; + _names = names; + _parentPoki = parentPoki; _childPoki = childPoki; + _fronting = fronting; _frontable = frontable; + _tags = tags; + + NpgsqlCommand command = new(""" + INSERT INTO poki VALUES ( + ($1), + DEFAULT, + ($2), + ($3), + ($4), + ($5), + ($6), + ($7) + ) + """, connection) { + Parameters = { + new() { Value = account.KUID }, + new() { Value = names }, + new() { Value = parentPoki }, + new() { Value = childPoki }, + new() { Value = fronting }, + new() { Value = frontable }, + new() { Value = tags } + } + }; + + command.ExecuteNonQuery(); + + + command = new(""" + SELECT currval('poki_id_seq') + """, connection); + + #pragma warning disable CS8605 + _id = (int) command.ExecuteScalar(); + #pragma warning disable CS8605 + } + + public static Poki[] GetPokiByName(string name) { + return new Poki[] {}; + } +}