-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdatabase_orm.py
More file actions
49 lines (39 loc) · 1.28 KB
/
database_orm.py
File metadata and controls
49 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Specter BlockChain Implementation
# Nick Frichette 1/23/2017
import json
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import *
from sqlalchemy import create_engine, exists
from base import Base
from block import *
class Database:
engine = create_engine('sqlite:///blockchain_db')
session = None
def __init__(self):
Base.metadata.create_all(self.engine)
Base.metadata.bind = self.engine
db_session = sessionmaker(bind=self.engine)
self.session = db_session()
def insert_block(self, block):
sql_block = Block(
block.coin_index,
json.dumps(block.transaction_info),
block.previous_hash,
block.current_hash,
block.timestamp,
block.nonce
)
self.session.add(sql_block)
self.session.commit()
def get_all_blocks(self):
return self.session.query(Block).all()
def in_db(self, block):
(ret, ) = self.session.query(exists().where(Block.coin_index == block.coin_index))
return ret[0]
def is_empty(self):
ret = self.session.query(Block).count()
if ret == 0:
return True
else:
return False