Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.13 KB | None | 0 0
  1. from java import *
  2. from java.sql import *
  3. from java.lang import *
  4.  
  5. from java.sql import DriverManager
  6. from java.lang import Class
  7.  
  8. from com.ziclix.python.sql import zxJDBC
  9. from sqlalchemy import create_engine
  10. from sqlalchemy import Table
  11. from sqlalchemy import *
  12. from sqlalchemy.orm import sessionmaker, mapper, relation
  13. from sqlalchemy.ext.orderinglist import ordering_list
  14.  
  15. from decimal import *
  16.  
  17. target_db = create_engine('postgresql+zxjdbc://#####:######@localhost/otis-bone')
  18. Session = sessionmaker(bind=target_db)
  19. session = Session()
  20.  
  21. meta = MetaData()
  22.  
  23. # figure out how to import rattail into jython
  24.  
  25. #####################################################################
  26. ################## models, classes and mappers ######################
  27. #####################################################################
  28.  
  29. products = Table('products', meta, autoload=True, autoload_with=target_db);
  30. product_barcodes = Table('product_barcodes', meta, autoload=True, autoload_with=target_db);
  31. product_sources = Table('product_sources', meta, autoload=True, autoload_with=target_db);
  32. prices = Table('prices', meta, autoload=True, autoload_with=target_db);
  33. vendors = Table('vendors', meta, autoload=True, autoload_with=target_db);
  34. departments = Table('departments', meta, autoload=True, autoload_with=target_db);
  35. orders_products = Table('orders_products', meta, autoload=True, autoload_with=target_db);
  36. orders = Table('orders', meta, autoload=True, autoload_with=target_db);
  37.  
  38.  
  39. class Product(object):
  40.     def __init__(self, uuid='None', description='None', department_uuid='None'):
  41.         self.description = description
  42.         self.uuid = uuid
  43.         self.department_uuid = department_uuid
  44.     def __repr__(self):
  45.         return self.description
  46.  
  47. class ProductBarcode(object):
  48.     def __init__(self, index='None', barcode='None', product_uuid='None'):
  49.         self.index = index
  50.         self.barcode = barcode
  51.         self.product_uuid = product_uuid
  52.     def __repr__(self):
  53.         return self.barcode
  54.  
  55. class ProductSource(object):
  56.  
  57.     def __init__(self, uuid=None, product=None, vendor=None, index=None, sku=None, quantity=None, suggested_retail=None, unit_cost=None, cost_recorded=None, cost_expires=None):
  58.         self.product = product
  59.         self.uuid = uuid
  60.         self.vendor = vendor
  61.         self.index = index
  62.         self.sku = sku
  63.         self.quantity = quantity
  64.         self.suggested_retail = suggested_retail
  65.         self.unit_cost = unit_cost
  66.         self.cost_recorded = cost_recorded
  67.         self.cost_expires = cost_expires
  68.  
  69. class Price(object):
  70.     def __init__(self, uuid='None', amount='None', product_uuid='None'):
  71.         self.uuid = uuid
  72.         self.amount = amount
  73.         self.product_uuid = product_uuid
  74.     def __repr__(self):
  75.         return self.barcode
  76.  
  77. class Vendor(object):
  78.     def __init__(self, uuid='None', name=None):
  79.         self.name = name
  80.         self.uuid = uuid
  81.     def __repr__(self):
  82.         return self.name
  83.  
  84. class Department(object):
  85.     def __init__(self, uuid='None', name=None, base_department=None, tax_rate=None):
  86.         self.name = name
  87.         self.uuid = uuid
  88.         self.base_department = base_department
  89.         self.tax_rate = tax_rate
  90.     def __str__(self):
  91.         return self.name
  92.  
  93. class OrderProduct(object):
  94.     def __init__(self, order=None, product=None, price=None, delivered=None, purchase_order=None, status=None):
  95.         self.order = order
  96.         self.product = product
  97.         self.price = price
  98.         self.delivered = delivered
  99.         self.purchase_order = purchase_order
  100.         self.status = self.status
  101.     def __repr__(self):
  102.         return "order:%s, product:%s" % (self.order, self.product)
  103.  
  104. class Order(object):
  105.     def __init__(self, uuid=None, status=None, opened=None, closed=None, employee=None, customer=None):
  106.         self.uuid = uuid
  107.         self.status = status
  108.         self.opened = opened
  109.         self.closed = closed
  110.         self.employee = employee
  111.         self.customer = customer
  112.     def __repr__(self):
  113.         return "#%s" % self.uuid
  114.  
  115.  
  116. mapper(
  117.     Product, products,
  118.     properties = dict(
  119.         _barcodes = relation(ProductBarcode, collection_class=ordering_list("index"), order_by=product_barcodes.c.index, cascade="all, delete-orphan", backref="product"),
  120.         sources = relation(ProductSource),
  121.         prices = relation(Price),
  122.         ),
  123.     )
  124. mapper(ProductBarcode,product_barcodes)
  125. mapper(ProductSource,product_sources,
  126.         properties = dict(
  127.             product = relation(Product),
  128.             vendor = relation(Vendor),
  129.             ),
  130.         )
  131. mapper(Price, prices)
  132. mapper(Vendor, vendors)
  133. mapper(Department, departments)
  134. mapper(OrderProduct, orders_products)
  135. mapper(Order, orders)
  136.  
  137.  
  138. ################################################################
  139. ############# ETL ##############################################
  140. ################################################################
  141.  
  142. db = zxJDBC.connect('jdbc:pervasive://##############/DEMODATA', None, None, "com.pervasive.jdbc.v2.Driver")
  143.  
  144. c = db.cursor()
  145.  
  146. # drop data from target db
  147. session.query(ProductBarcode).delete()
  148. session.query(Price).delete()
  149. session.query(ProductSource).delete()
  150. session.query(Vendor).delete()
  151. session.query(OrderProduct).delete()
  152. session.query(Order).delete()
  153. session.query(Product).delete()
  154. session.query(Department).delete()
  155. session.commit()
  156.  
  157. ############## departments ###################################
  158.  
  159. # extract data from source db
  160. c.execute('SELECT Department, Description FROM Department')
  161.  
  162.  
  163. # load data into target db
  164. for record in c.fetchall():
  165.     uuid = record[0]
  166.     name = record[1]
  167.     department = Department(uuid=uuid, name=name)
  168.     session.add(department)
  169. session.commit()
  170.  
  171. ################# products, prices, upcs, vendors, skus ########
  172.  
  173. # extract data from source db
  174. c.execute('SELECT UPC, UnitPrice, VendorNumber, ItemNumber, BOFDescription, Department FROM Item')
  175.  
  176. # load data into target db
  177. i=1
  178. for record in c.fetchall():
  179.     upc_plu = record[0]
  180.     amount = Decimal(record[1]) * Decimal('.01')
  181.     sku = record[2] + record[3]
  182.     description = record[4]
  183.     department = record[5]
  184.  
  185.     existing_vendor = session.query(Vendor).filter(Vendor.name==record[2])
  186.     if existing_vendor.count() < 1:
  187.         vendor = Vendor(uuid=i, name=record[2])
  188.         session.add(vendor)
  189.     else:
  190.         vendor = existing_vendor[0]
  191.  
  192.     product = Product(uuid=i, description=description, department_uuid=department)
  193.     #barcode = ProductBarcode(index=i, product_uuid=i, barcode=upc_plu)
  194.     barcode = ProductBarcode(index=i, barcode=upc_plu)
  195.     product._barcodes.append(barcode)
  196.     #product_source = ProductSource(index=i, uuid=i, quantity=1, product_uuid=i, sku=sku)
  197.     product_source = ProductSource(uuid=i, index=i, quantity=1, sku=sku, product=product, vendor=vendor)
  198.     #price = Price(amount=amount, product_uuid=product.uuid)
  199.     price = Price(amount=amount)
  200.     product.prices.append(price)
  201.  
  202.     session.add(product)
  203.  
  204.     #session.add(barcode)
  205.     session.add(product_source)
  206.     #session.add(price)
  207.     i+=1
  208.     if i==100: break
  209.  
  210. session.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement