Newer
Older
# -*- python -*-
# -*- coding: utf-8 -*-
# Started 24/05/2011 by Seblu <seblu@seblu.net>
'''
Database stuff
'''
import os
import installsystems.tools as istools
from installsystems.tarball import Tarball
from installsystems.printer import *
class Database(object):
It needs to be local cause of sqlite3 which need to open a file
'''
def create(cls, path):
arrow("Creating repository database")
if not istools.isfile(path):
raise Exception("Database creation must be local")
path = os.path.abspath(path)
if os.path.exists(path):
raise Exception("Database already exists. Remove it before")
conn = sqlite3.connect(path, isolation_level=None)
conn.execute("PRAGMA foreign_keys = ON")
conn.executescript(istemplate.createdb)
conn.commit()
conn.close()
except Exception as e:
raise Exception("Create database failed: %s" % e)
if not istools.isfile(path):
if not os.path.exists(self.path):
raise Exception("Database not exists")
self.conn = sqlite3.connect(self.path, isolation_level=None)
self.conn.execute("PRAGMA foreign_keys = ON")
def begin(self):
'''
Start a db transaction
'''
self.conn.execute("BEGIN TRANSACTION")
def commit(self):
'''
Commit current db transaction
'''
self.conn.execute("COMMIT TRANSACTION")