init
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
61
kulupu.cs
Normal file
61
kulupu.cs
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
14
kulupu.csproj
Normal file
14
kulupu.csproj
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Npgsql" Version="10.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
86
src/Account.cs
Normal file
86
src/Account.cs
Normal file
@@ -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<string>() { Value = kuid.Text },
|
||||||
|
new NpgsqlParameter<string>() { 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 }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/KUID.cs
Normal file
16
src/KUID.cs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
67
src/Poki.cs
Normal file
67
src/Poki.cs
Normal file
@@ -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[] {};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user