Db0027; database code for HotBitcoinAddress

This commit is contained in:
Rick Falkvinge
2015-10-07 12:52:05 +02:00
parent db04b2d205
commit b4930a4fcb
9 changed files with 317 additions and 3 deletions
@@ -10,18 +10,19 @@ namespace Swarmops.Basic.Types.Financial
public class BasicHotBitcoinAddress: IHasIdentity
{
public BasicHotBitcoinAddress (int hotBitcoinAddressId, int organizationId, string derivationPath,
Int64 balanceSatoshis, Int64 throughputSatoshis)
string address, Int64 balanceSatoshis, Int64 throughputSatoshis)
{
this.HotBitcoinAddressId = hotBitcoinAddressId;
this.OrganizationId = organizationId;
this.DerivationPath = derivationPath;
this.Address = address;
this.BalanceSatoshis = balanceSatoshis;
this.ThroughputSatoshis = throughputSatoshis;
}
public BasicHotBitcoinAddress (BasicHotBitcoinAddress original)
: this (
original.HotBitcoinAddressId, original.OrganizationId, original.DerivationPath, original.BalanceSatoshis,
original.HotBitcoinAddressId, original.OrganizationId, original.DerivationPath, original.Address, original.BalanceSatoshis,
original.ThroughputSatoshis)
{
// copy ctor
@@ -30,6 +31,7 @@ namespace Swarmops.Basic.Types.Financial
public int HotBitcoinAddressId { get; private set; }
public int OrganizationId { get; private set; }
public string DerivationPath { get; private set; }
public string Address { get; private set; }
public Int64 BalanceSatoshis { get; protected set; }
public Int64 ThroughputSatoshis { get; protected set; }
+1 -1
View File
@@ -14,7 +14,7 @@ namespace Swarmops.Database
/// </summary>
public static int DbVersionExpected
{
get { return 26; }
get { return 27; }
}
+172
View File
@@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Swarmops.Basic.Types.Financial;
using Swarmops.Basic.Types.Governance;
namespace Swarmops.Database
{
public partial class SwarmDb
{
#region Field reading code
private const string hotBitcoinAddressFieldSequence =
" HotBitcoinAddressId,OrganizationId,DerivationPath,AddressString,BalanceSatoshis," + // 0-4
" ThroughputSatoshis " + // 5
" FROM HotBitcoinAddresses ";
private const string hotBitcoinAddressUnspentFieldSequence =
" HotBitcoinAddressUnspentId,TransactionHash,TransactionOutputIndex,AmountSatoshis,ConfirmationCount" + // 0-4
" FROM HotBitcoinAddressUnspents ";
private BasicHotBitcoinAddress ReadHotBitcoinAddressFromDataReader(IDataRecord reader)
{
int hotBitcoinAddressId = reader.GetInt32 (0);
int organizationId = reader.GetInt32 (1);
string derivationPath = reader.GetString (2);
string addressString = reader.GetString (3);
Int64 balanceSatoshis = reader.GetInt64 (4);
Int64 throughputSatoshis = reader.GetInt64 (5);
return new BasicHotBitcoinAddress (hotBitcoinAddressId, organizationId, derivationPath, addressString, balanceSatoshis, throughputSatoshis);
}
private BasicHotBitcoinAddressUnspent ReadHotBitcoinAddressUnspentFromDataReader(IDataRecord reader)
{
int hotBitcoinAddressUnspentId = reader.GetInt32 (0);
string transactionHash = reader.GetString (1);
int transactionOutputIndex = reader.GetInt32 (2);
Int64 amountSatoshis = reader.GetInt64 (3);
int confirmationCount = reader.GetInt32 (4);
return new BasicHotBitcoinAddressUnspent (hotBitcoinAddressUnspentId, transactionHash, transactionOutputIndex, amountSatoshis, confirmationCount);
}
#endregion
#region Record reading code
public BasicHotBitcoinAddress GetHotBitcoinAddress (int hotBitcoinAddressId)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand(
"SELECT" + hotBitcoinAddressFieldSequence + "WHERE HotBitcoinAddressId=" + hotBitcoinAddressId + ";", connection);
using (DbDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return ReadHotBitcoinAddressFromDataReader(reader);
}
throw new ArgumentException("Unknown HotBitcoinAddress Id: " + hotBitcoinAddressId);
}
}
}
public BasicHotBitcoinAddress[] GetHotBitcoinAddresses(params object[] conditions) // the typical condition would be an Organization, here
{
List<BasicHotBitcoinAddress> result = new List<BasicHotBitcoinAddress>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand(
"SELECT" + hotBitcoinAddressFieldSequence + ConstructWhereClause("HotBitcoinAddresses", conditions), connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add(ReadHotBitcoinAddressFromDataReader(reader));
}
return result.ToArray();
}
}
}
public BasicHotBitcoinAddressUnspent[] GetHotBitcoinAddressUnspents(params object[] conditions) // the typical condition would be a HotBitcoinAddress, here
{
List<BasicHotBitcoinAddressUnspent> result = new List<BasicHotBitcoinAddressUnspent>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command =
GetDbCommand(
"SELECT" + hotBitcoinAddressUnspentFieldSequence + ConstructWhereClause("HotBitcoinAddressUnspents", conditions), connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add(ReadHotBitcoinAddressUnspentFromDataReader(reader));
}
return result.ToArray();
}
}
}
#endregion
#region Creation and manipulation
public int CreateHotBitcoinAddressConditional(int organizationId, string derivationPath, string address)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
using (DbCommand command = GetDbCommand("CreateHotBitcoinAddressConditional", connection))
{
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "organizationId", organizationId);
AddParameterWithName(command, "derivationPath", derivationPath);
AddParameterWithName(command, "addressString", address);
return Convert.ToInt32(command.ExecuteScalar());
}
}
}
public int CreateHotBitcoinAddressUnspentConditional(int hotBitcoinAddressId, string transactionHash, int transactionOutputIndex, Int64 amountSatoshis, int confirmationCount)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
using (DbCommand command = GetDbCommand("CreateHotBitcoinAddressUnspentConditional", connection))
{
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "hotBitcoinAddressId", hotBitcoinAddressId);
AddParameterWithName(command, "transactionHash", transactionHash);
AddParameterWithName(command, "transactionOutputIndex", transactionOutputIndex);
AddParameterWithName(command, "amountSatoshis", amountSatoshis);
AddParameterWithName(command, "confirmationCount", confirmationCount);
return Convert.ToInt32(command.ExecuteScalar());
}
}
}
#endregion
}
}
+1
View File
@@ -65,6 +65,7 @@
<Compile Include="Database-FinancialTransactionTags.cs" />
<Compile Include="Database-FinancialTransactionTagSetTypes.cs" />
<Compile Include="Database-FinancialTransactionTagTypes.cs" />
<Compile Include="Database-HotBitcoinAddresses.cs" />
<Compile Include="Database-Initialization.cs" />
<Compile Include="Database-Meetings.cs" />
<Compile Include="Database-CommunicationTurnarounds.cs" />
+1
View File
@@ -24,3 +24,4 @@
0024 - Maint on CreatePayrollItem [RF]
0025 - Bugfix for 0024 [RF]
0026 - Added first tables, procs for hot bitcoin wallets [RF]
0027 - Fixes and additions to HotBitcoinAddresses, -Unspents [RF]
+1
View File
@@ -8,6 +8,7 @@ CREATE TABLE `HotBitcoinAddresses` (
PRIMARY KEY (`HotBitcoinAddressId`),
INDEX `Ix_Organization` (`OrganizationId` ASC),
INDEX `Ix_Balance` (`BalanceSatoshis` ASC),
INDEX `Ix_Address` (`AddressString` ASC),
INDEX `Ix_Used` (`ThroughputSatoshis` ASC))
+121
View File
@@ -0,0 +1,121 @@
ALTER TABLE `HotBitcoinAddresses`
ADD UNIQUE INDEX `Ix_Address` (`AddressString` ASC);
#
ALTER TABLE `activizr-dev`.`HotBitcoinAddressUnspents`
ADD UNIQUE INDEX `Ix_HashIndex` (`TransactionHash` ASC, `TransactionOutputIndex` ASC);
#
DROP procedure IF EXISTS `CreateHotBitcoinAddressConditional`
#
CREATE PROCEDURE `CreateHotBitcoinAddressConditional` (
IN organizationId INT,
IN derivationPath VARCHAR(256),
IN addressString VARCHAR(48)
)
BEGIN
IF ((SELECT COUNT(*) FROM HotBitcoinAddresses WHERE HotBitcoinAddresses.AddressString=addressString) = 0)
THEN
INSERT INTO HotBitcoinAddresses
(OrganizationId,DerivationPath,AddressString,BalanceSatoshis,ThroughputSatoshis)
VALUES
(organizationId,derivationPath,addressString,0,0);
SELECT LAST_INSERT_ID() AS Identity;
ELSE
SELECT HotBitcoinAddresses.HotBitcoinAddressId AS Identity
WHERE HotBitcoinAddresses.AddressString=addressString;
END IF;
END
#
DROP procedure IF EXISTS `CreateHotBitcoinAddressUnspent`
#
CREATE PROCEDURE `CreateHotBitcoinAddressUnspent` (
IN amountSatoshis BIGINT,
IN transactionHash VARCHAR(128),
IN transactionOutputIndex INT,
IN confirmationCount INT
)
BEGIN
INSERT INTO HotBitcoinAddressUnspents
(AmountSatoshis,TransactionHash,TransactionOutputIndex,ConfirmationCount)
VALUES
(amountSatoshis,transactionHash,transactionOutputIndex,confirmationCount);
SELECT LAST_INSERT_ID() AS Identity;
END
#
DROP procedure IF EXISTS `CreateHotBitcoinAddressUnspentConditional`
#
CREATE PROCEDURE `CreateHotBitcoinAddressUnspentConditional` (
IN amountSatoshis BIGINT,
IN transactionHash VARCHAR(128),
IN transactionOutputIndex INT,
IN confirmationCount INT
)
BEGIN
DECLARE unspentId INT;
IF ((SELECT COUNT(*) FROM HotBitcoinAddressUnspents
WHERE HotBitcoinAddressUnspents.TransactionHash=transactionHash
AND HotBitcoinAddressUnspents.TransactionOutputIndex=transactionOutputIndex) = 0)
THEN
INSERT INTO HotBitcoinAddressUnspents
(AmountSatoshis,TransactionHash,TransactionOutputIndex,ConfirmationCount)
VALUES
(amountSatoshis,transactionHash,transactionOutputIndex,confirmationCount);
SELECT LAST_INSERT_ID() AS Identity;
ELSE
SELECT HotBitcionAddressUnspents.HotBitcoinAddressUnspentId INTO unspentId
FROM HotBitcoinAddressUnspents
WHERE HotBitcoinAddressUnspents.TransactionHash=transactionHash
AND HotBitcoinAddressUnspents.TransactionOutputIndex=transactionOutputIndex;
UPDATE HotBitcoinAddressUnspents
SET HotBitcoinAddressUnspents.ConfirmationCount=confirmationCount
WHERE HotBitcoinAddressUnspents.HotBitcoinAddressUnspentId=unspentId;
SELECT unspentId AS Identity;
END IF;
END
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Swarmops.Basic.Types.Financial;
using Swarmops.Logic.Support;
namespace Swarmops.Logic.Financial
{
public class HotBitcoinAddresses: PluralBase<HotBitcoinAddresses, HotBitcoinAddress, BasicHotBitcoinAddress>
{
// to be implemented
}
}
+1
View File
@@ -120,6 +120,7 @@
<ItemGroup>
<Compile Include="Financial\BitcoinUtility.cs" />
<Compile Include="Financial\HotBitcoinAddress.cs" />
<Compile Include="Financial\HotBitcoinAddresses.cs" />
<Compile Include="Financial\HotBitcoinAddressUnspent.cs" />
<Compile Include="Financial\HotBitcoinAddressUnspents.cs" />
<Compile Include="Financial\IPayable.cs" />