feat: Add SQLite database support with MySQL compatibility

This commit is contained in:
Eduardo Leonardo Rosa da Silva
2025-10-20 12:55:20 -03:00
parent 10a4691429
commit 76bcb5bb85
10 changed files with 503 additions and 176 deletions

View File

@@ -20,89 +20,166 @@ namespace WeaponPaints
try
{
await using var connection = await WeaponPaints.Database.GetConnectionAsync();
await using var transaction = await connection.BeginTransactionAsync();
string[] createTableQueries = GetCreateTableQueries();
// Log para debug
WeaponPaints.Instance.Logger.LogInformation($"[WeaponPaints] Creating {createTableQueries.Length} tables for {Config?.DatabaseType} database");
try
foreach (var query in createTableQueries)
{
string[] createTableQueries =
[
@"
CREATE TABLE IF NOT EXISTS `wp_player_skins` (
`steamid` varchar(18) NOT NULL,
`weapon_team` int(1) NOT NULL,
`weapon_defindex` int(6) NOT NULL,
`weapon_paint_id` int(6) NOT NULL,
`weapon_wear` float NOT NULL DEFAULT 0.000001,
`weapon_seed` int(16) NOT NULL DEFAULT 0,
`weapon_nametag` VARCHAR(128) DEFAULT NULL,
`weapon_stattrak` tinyint(1) NOT NULL DEFAULT 0,
`weapon_stattrak_count` int(10) NOT NULL DEFAULT 0,
`weapon_sticker_0` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_sticker_1` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_sticker_2` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_sticker_3` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_sticker_4` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_keychain` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0' COMMENT 'id;x;y;z;seed',
UNIQUE (`steamid`, `weapon_team`, `weapon_defindex`) -- Add unique constraint here
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_knife` (
`steamid` varchar(18) NOT NULL,
`weapon_team` int(1) NOT NULL,
`knife` varchar(64) NOT NULL,
UNIQUE (`steamid`, `weapon_team`) -- Unique constraint
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_gloves` (
`steamid` varchar(18) NOT NULL,
`weapon_team` int(1) NOT NULL,
`weapon_defindex` int(11) NOT NULL,
UNIQUE (`steamid`, `weapon_team`) -- Unique constraint
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_agents` (
`steamid` varchar(18) NOT NULL,
`agent_ct` varchar(64) DEFAULT NULL,
`agent_t` varchar(64) DEFAULT NULL,
UNIQUE (`steamid`) -- Unique constraint
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_music` (
`steamid` varchar(64) NOT NULL,
`weapon_team` int(1) NOT NULL,
`music_id` int(11) NOT NULL,
UNIQUE (`steamid`, `weapon_team`) -- Unique constraint
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_pins` (
`steamid` varchar(64) NOT NULL,
`weapon_team` int(1) NOT NULL,
`id` int(11) NOT NULL,
UNIQUE (`steamid`, `weapon_team`) -- Unique constraint
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;"
];
foreach (var query in createTableQueries)
try
{
await connection.ExecuteAsync(query, transaction: transaction);
await connection.GetConnection().ExecuteAsync(query);
WeaponPaints.Instance.Logger.LogInformation($"[WeaponPaints] Table created successfully");
}
catch (Exception ex)
{
WeaponPaints.Instance.Logger.LogError($"[WeaponPaints] Error creating table: {ex.Message}");
WeaponPaints.Instance.Logger.LogError($"[WeaponPaints] Query: {query}");
throw;
}
}
await transaction.CommitAsync();
}
catch (Exception)
{
await transaction.RollbackAsync();
throw new Exception("[WeaponPaints] Unable to create tables!");
}
WeaponPaints.Instance.Logger.LogInformation("[WeaponPaints] All database tables created successfully");
}
catch (Exception ex)
{
throw new Exception("[WeaponPaints] Unknown MySQL exception! " + ex.Message);
WeaponPaints.Instance.Logger.LogError($"[WeaponPaints] Database exception: {ex.Message}");
throw new Exception("[WeaponPaints] Unknown database exception! " + ex.Message);
}
}
private static string[] GetCreateTableQueries()
{
if (Config?.DatabaseType?.ToLower() == "sqlite")
{
return new string[]
{
@"
CREATE TABLE IF NOT EXISTS wp_player_skins (
steamid TEXT NOT NULL,
weapon_team INTEGER NOT NULL,
weapon_defindex INTEGER NOT NULL,
weapon_paint_id INTEGER NOT NULL,
weapon_wear REAL NOT NULL DEFAULT 0.000001,
weapon_seed INTEGER NOT NULL DEFAULT 0,
weapon_nametag TEXT DEFAULT NULL,
weapon_stattrak INTEGER NOT NULL DEFAULT 0,
weapon_stattrak_count INTEGER NOT NULL DEFAULT 0,
weapon_sticker_0 TEXT NOT NULL DEFAULT '0;0;0;0;0;0;0',
weapon_sticker_1 TEXT NOT NULL DEFAULT '0;0;0;0;0;0;0',
weapon_sticker_2 TEXT NOT NULL DEFAULT '0;0;0;0;0;0;0',
weapon_sticker_3 TEXT NOT NULL DEFAULT '0;0;0;0;0;0;0',
weapon_sticker_4 TEXT NOT NULL DEFAULT '0;0;0;0;0;0;0',
weapon_keychain TEXT NOT NULL DEFAULT '0;0;0;0;0',
UNIQUE (steamid, weapon_team, weapon_defindex)
)",
@"
CREATE TABLE IF NOT EXISTS wp_player_knife (
steamid TEXT NOT NULL,
weapon_team INTEGER NOT NULL,
knife TEXT NOT NULL,
UNIQUE (steamid, weapon_team)
)",
@"
CREATE TABLE IF NOT EXISTS wp_player_gloves (
steamid TEXT NOT NULL,
weapon_team INTEGER NOT NULL,
weapon_defindex INTEGER NOT NULL,
UNIQUE (steamid, weapon_team)
)",
@"
CREATE TABLE IF NOT EXISTS wp_player_agents (
steamid TEXT NOT NULL,
agent_ct TEXT DEFAULT NULL,
agent_t TEXT DEFAULT NULL,
UNIQUE (steamid)
)",
@"
CREATE TABLE IF NOT EXISTS wp_player_music (
steamid TEXT NOT NULL,
weapon_team INTEGER NOT NULL,
music_id INTEGER NOT NULL,
UNIQUE (steamid, weapon_team)
)",
@"
CREATE TABLE IF NOT EXISTS wp_player_pins (
steamid TEXT NOT NULL,
weapon_team INTEGER NOT NULL,
id INTEGER NOT NULL,
UNIQUE (steamid, weapon_team)
)"
};
}
else // MySQL
{
return new string[]
{
@"
CREATE TABLE IF NOT EXISTS `wp_player_skins` (
`steamid` varchar(18) NOT NULL,
`weapon_team` int(1) NOT NULL,
`weapon_defindex` int(6) NOT NULL,
`weapon_paint_id` int(6) NOT NULL,
`weapon_wear` float NOT NULL DEFAULT 0.000001,
`weapon_seed` int(16) NOT NULL DEFAULT 0,
`weapon_nametag` VARCHAR(128) DEFAULT NULL,
`weapon_stattrak` tinyint(1) NOT NULL DEFAULT 0,
`weapon_stattrak_count` int(10) NOT NULL DEFAULT 0,
`weapon_sticker_0` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_sticker_1` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_sticker_2` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_sticker_3` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_sticker_4` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0;0;0' COMMENT 'id;schema;x;y;wear;scale;rotation',
`weapon_keychain` VARCHAR(128) NOT NULL DEFAULT '0;0;0;0;0' COMMENT 'id;x;y;z;seed',
UNIQUE (`steamid`, `weapon_team`, `weapon_defindex`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_knife` (
`steamid` varchar(18) NOT NULL,
`weapon_team` int(1) NOT NULL,
`knife` varchar(64) NOT NULL,
UNIQUE (`steamid`, `weapon_team`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_gloves` (
`steamid` varchar(18) NOT NULL,
`weapon_team` int(1) NOT NULL,
`weapon_defindex` int(11) NOT NULL,
UNIQUE (`steamid`, `weapon_team`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_agents` (
`steamid` varchar(18) NOT NULL,
`agent_ct` varchar(64) DEFAULT NULL,
`agent_t` varchar(64) DEFAULT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_music` (
`steamid` varchar(64) NOT NULL,
`weapon_team` int(1) NOT NULL,
`music_id` int(11) NOT NULL,
UNIQUE (`steamid`, `weapon_team`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
@"
CREATE TABLE IF NOT EXISTS `wp_player_pins` (
`steamid` varchar(64) NOT NULL,
`weapon_team` int(1) NOT NULL,
`id` int(11) NOT NULL,
UNIQUE (`steamid`, `weapon_team`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;"
};
}
}