by nick spanos
SELECT * FROM sensor_readings
WHERE temperature > 30.0;
SELECT * FROM medical_records
AS OF BLOCK 12345
WHERE patient_id = 'P789';
SELECT * FROM document_registry
WITH VERIFICATION
WHERE document_hash = 'f58920a...';
# Connect to a BlockchainSQL database
from blockchainsql import BlockchainDB
# Connect with standard credentials
db = BlockchainDB(host="db.example.com",
port=5432,
username="app_user",
password="secure_pass")
# Execute a verification-enabled query
result = db.execute_query("""
SELECT * FROM iot_sensor_data
WITH VERIFICATION
WHERE device_id = %s
AND reading_time > %s
""", ["DEVICE-001", "2025-01-01T00:00:00Z"])
# Verify the proof
if result.verify_proof():
print("Data verified successfully")
import { BlockchainSQLClient, VerificationLevel }
from 'blockchainsql';
// Initialize client
const client = new BlockchainSQLClient({
host: 'db.example.com',
port: 5432,
user: 'app_user',
password: 'secure_pass',
database: 'product_registry'
});
// Query with verification
const result = await client.query({
sql: `SELECT current.description AS current_description,
historical.description AS original_description,
current.last_modified_block - historical.last_modified_block
AS blocks_since_change
FROM products current
JOIN products AS OF BRANCH_ROOT historical
ON current.product_id = historical.product_id
WHERE current.product_id = $1
AND current.description <> historical.description`,
params: [productId],
verificationLevel: VerificationLevel.MERKLE_PROOF
});
import com.blockchainsql.BlockchainSQLConnection;
import com.blockchainsql.VerificationOptions;
// Establish connection to BlockchainSQL
try (BlockchainSQLConnection conn =
BlockchainSQLConnection.builder()
.withHost("db.example.com")
.withPort(5432)
.withCredentials("app_user", "secure_pass")
.withDatabase("supply_chain")
.build()) {
// Query with verification
VerifiedResultSet results = conn
.prepareVerifiedStatement(
"SELECT inventory.product_id, " +
"inventory.quantity, " +
"certifications.certification_id, " +
"certifications.expiration_date " +
"FROM BRANCH('inventory') inventory " +
"JOIN BRANCH('certifications') certifications " +
"ON inventory.product_id = certifications.product_id " +
"WHERE inventory.warehouse_id = ? AND " +
"certifications.is_valid = true"
)
.setString(1, "WAREHOUSE-A")
.withVerificationOptions(new VerificationOptions()
.includeBlockHeaders(true)
.includeMerkleProofs(true)
.signResult(true))
.executeQuery();
}
from blockchainsql.lineage import LineageTracker
# Initialize lineage tracker
tracker = LineageTracker(db_connection)
# Track complete history of a data point
lineage = tracker.trace_lineage(
data_id="PATIENT-1234-BLOODWORK-2025-04-01",
include_branches=True,
include_metadata=True
)
# Visualize the lineage graph
lineage.export_graph("patient_data_lineage.svg")
# Verify integrity of entire lineage chain
verification_result = lineage.verify_complete_chain()
if verification_result.is_valid:
print("Complete data lineage verified successfully")
else:
print(f"Lineage verification failed: {verification_result.failure_reason}")
import { SchemaManager } from 'blockchainsql-schema';
// Initialize the schema manager
const schemaManager = new SchemaManager({
connection: dbClient,
schemaName: 'medical_records'
});
// Create a versioned schema change
const migrationPlan = await schemaManager.createMigration({
name: 'add_patient_consent_fields',
changes: [
{
type: 'ADD_COLUMN',
table: 'patient_records',
column: {
name: 'consent_date',
dataType: 'TIMESTAMP',
nullable: false,
defaultValue: 'NOW()'
}
},
{
type: 'ADD_COLUMN',
table: 'patient_records',
column: {
name: 'consent_version',
dataType: 'VARCHAR(50)',
nullable: false,
defaultValue: "'v1.0'"
}
}
]
});
import com.blockchainsql.compliance.ComplianceReporter;
import com.blockchainsql.compliance.ReportFormat;
import com.blockchainsql.compliance.ReportCriteria;
// Initialize compliance reporting engine
ComplianceReporter reporter = new ComplianceReporter(connection);
// Generate GDPR compliance report with verification
ReportResult gdprReport = reporter.generateReport(
new ReportCriteria()
.withReportType("GDPR_DATA_ACCESS")
.withSubjectIdentifier("citizen-id-12345")
.withTimeRange(startDate, endDate)
.withVerificationLevel(VerificationLevel.FULL)
.withFormat(ReportFormat.PDF)
);
// Export the verified report
File reportFile = gdprReport.exportToFile("gdpr_compliance_report.pdf");
String verificationUrl = gdprReport.getVerificationUrl();
import { BlockchainSQLEventProcessor } from 'blockchainsql-events';
// Create event processor for real-time data
const eventProcessor = new BlockchainSQLEventProcessor({
connection: dbClient,
sourceTable: 'iot_sensor_readings',
processingOptions: {
verifyBeforeProcessing: true,
recordProcessingEvents: true,
enableRollback: true
}
});
// Register event handlers
eventProcessor.onData(async (reading) => {
// Process each sensor reading
if (reading.temperature > temperatureThreshold ||
reading.humidity > humidityThreshold) {
// Create alert with verification link
await alertSystem.createAlert({
deviceId: reading.device_id,
alertType: 'THRESHOLD_EXCEEDED',
readingValues: {
temperature: reading.temperature,
humidity: reading.humidity
},
verificationData: {
blockchainRef: reading._blockchainRef,
blockHeight: reading._blockHeight
}
});
}
});
// Start real-time processing
eventProcessor.start();