def create_account(initial_balance, db_name): """ :param initial_balance: Initial balance for this account :param db_name: Database name (path) :return: -1 on failure or new account number """ # # For a given initial balance create a new account # try: # Get connection and cursor to database con, cur = conn.open_connection(db_name) # Build sql to insert this user sql = 'INSERT INTO accounts ( initial_limit, current_limit ) VALUES ( {0}, {0} );'.format( initial_balance) # Insert into database and commit cur.execute(sql) # New account number = MAX(accounts.acct_id) new_acct_id = get_last_account_id(cur) insert_trans(cur, new_acct_id, 'create', initial_balance) # Commit all database transactions and close database connection con.commit() conn.close_connection(con, cur) except Exception as e: print(e) new_acct_id = -1 return new_acct_id
def seed_database(db_name): con, cur = conn.open_connection(db_name) cur.execute("""CREATE TABLE accounts ( acct_id INTEGER PRIMARY KEY AUTOINCREMENT, initial_limit NUMERIC(18,6), current_limit NUMERIC(18,6), created_on REAL DEFAULT (datetime('now', 'localtime')) ); """) cur.execute("""CREATE TABLE trans ( trans_id INTEGER PRIMARY KEY AUTOINCREMENT, acct_id INTEGER, trans_type VARCHAR(50), trans_amt NUMERIC(18,6), created_on REAL DEFAULT (datetime('now', 'localtime')) ); """) cur.execute("""CREATE TABLE holds ( hold_id INTEGER PRIMARY KEY AUTOINCREMENT, acct_id INTEGER, vendor_id INTEGER, hold_amt NUMERIC(18,6), created_on REAL DEFAULT (datetime('now', 'localtime')) ); """) # cur_time = time.time() sql = "INSERT INTO accounts ( initial_limit, current_limit ) " \ + "VALUES ( 100000, 100000 );" cur.execute(sql) con.commit() conn.close_connection(con, cur)
def settle_hold(acct_id, vendor_id, new_amt, db_name): try: # Get connection and cursor to database con, cur = conn.open_connection(db_name) # Release hold even if charge is declined release_hold(acct_id, vendor_id, cur) con.commit() # Issue charge to account for this new amount ret_sts = debit_acct(cur, acct_id, new_amt, 'settle') con.commit() return ret_sts except Exception as e: print(e) return -1
def hold(acct_id, vendor_id, amt, db_name): """ :param acct_id: Account id :param vendor_id: Vendor id :param amt: Hold amount :param db_name: Database name (path) :return: True on success """ try: # Get connection and cursor to database con, cur = conn.open_connection(db_name) # Only 1 hold allowed per vendor id per account id sql = 'SELECT COUNT(*) FROM holds WHERE acct_id = {0} AND vendor_id = {1}'.\ format(acct_id, vendor_id) cur.execute(sql) rows = cur.fetchall() num_holds = rows[0][0] if num_holds > 0: return -3 sql = 'INSERT INTO holds (acct_id, vendor_id, hold_amt) VALUES'\ '( {0}, {1}, {2} );'.format(acct_id, vendor_id, amt) # Insert hold into database cur.execute(sql) # Place a hold on this account for the amount specified ret_sts = debit_acct(cur, acct_id, amt, 'hold') if ret_sts < 0: return ret_sts # Commit all database transactions and close database connection con.commit() conn.close_connection(con, cur) return ret_sts except Exception as e: print(e) return -1
def charge(acct_id, amt, db_name): """ :param acct_id: Account number to charge :param amt: Amount to charge :param db_name: Database name (path) :return: 0 = Success -1 = Charge failed (database error) -2 = Insufficient funds """ try: # Get connection and cursor to database con, cur = conn.open_connection(db_name) ret_sts = debit_acct(cur, acct_id, amt, 'charge') # Commit all database transactions and close database connection con.commit() conn.close_connection(con, cur) return ret_sts except Exception as e: print(e) return -1
# coding: utf-8 # # Part of kde-hearts (http://hearts.luispedro.org/) # Copyright 2007 by Luís Pedro Coelho <*****@*****.**> # # Distributed under GNU Public License, version 2 from options import readoptions from connection import open_connection from service import launch_server if __name__ == '__main__': try: options=readoptions() #if options.daemon(): # daemon() listener=open_connection() launch_server(listener) except Exception, e: print 'Sorry', e # vim: set ts=4 sts=4 sw=4 expandtab smartindent: