Commit dbdd2b26 authored by Jacob Schatz's avatar Jacob Schatz

Initial pypika replacement

parent 0fb855eb
......@@ -18,7 +18,7 @@ if os.environ['FLASK_ENV'] == 'development':
db = SQLAlchemy(app)
# connector = ExternalConnector()
connector = ExternalConnector()
logger = logging.getLogger('melt_logger')
handler = RotatingFileHandler(app.config['LOG_PATH'], maxBytes=2000, backupCount=10)
......@@ -45,6 +45,10 @@ def hello():
@app.route("/drop_it_like_its_hot")
def reset_db():
db.drop_all()
try:
db.drop_all()
except sqlalchemy.exc.OperationalError as err:
logging.error("Failed drop database.")
db.create_all()
return jsonify({"dropped_it":"like_its_hot"})
\ No newline at end of file
......@@ -6,8 +6,9 @@ from collections import OrderedDict
from decimal import Decimal
from datetime import date, datetime
from pypika import Query, Table, Field
from app import db
# from app import connector
from flask import (
Blueprint, jsonify, request
......@@ -20,9 +21,6 @@ from models.data import (
Model, Explore, View, Dimension, Measure, Join
)
# _connections = Settings.query.first().settings['connections']
# connector.add_connections(_connections)
connections = {}
def default(obj):
......@@ -118,6 +116,16 @@ def get_sql(model_name, explore_name):
if incoming_order_desc:
order_by = '{} DESC'.format(order_by)
table = sqlHelper.table(base_table, explore_name)
fields = sqlHelper.fields(incoming_dimensions, explore_name)
q = Query\
.from_(table)\
.select(*fields, explore_name)
print(str(q))
base_sql = 'SELECT\n\t{}\nFROM {} AS {} \n{} {} \n{} \n{} \nLIMIT {};'.format(',\n '.join(to_join), base_table, explore_name, filter_by, join_sql, group_by, order_by, limit);
if to_run:
db_to_connect = model.settings['connection']
......
import re
from sqlalchemy import String, cast
from models.data import View, Dimension, DimensionGroup, Measure, Join
from pypika import Query, Table, Field
class SqlHelper():
def table(self, name, alias):
(schema, name) = name.split('.')
return Table(name, schema=schema, alias=alias)
def field(self, name, table):
table = self.table(table)
return Field(name, table)
def dimension(self, d, table):
d.settings['sql'].replace("${TABLE}", table)
def fields(self, fields, table):
return [self.field(f, table) for f in fields]
def dimension_groups(self, view_name, explore_name, dimension_groups):
base_sqls = []
for dimension_group in dimension_groups:
......
from sqlalchemy import create_engine, MetaData
from sqlalchemy import create_engine, MetaData, inspect
import json
class ExternalConnector:
......@@ -6,16 +7,29 @@ class ExternalConnector:
self.connections = {}
def add_connections(self, connections):
pass
# for connection in connections:
# connection_name = connection['name']
# if connection_name not in connections:
# this_connection = {}
# if connection['dialect'] == 'postgresql':
# connection_url = 'postgresql+psycopg2://{user}:{pw}@{host}:{port}/{db}'.format(user=connection['username'],pw=connection['password'],host=connection['host'],port=connection['port'], db=connection['database'])
# this_connection['connection_url'] = connection_url
# this_connection['engine'] = create_engine(this_connection['connection_url'])
# meta = MetaData()
# meta.reflect(bind=this_connection['engine'], schema='analytics')
# this_connection['meta'] = meta
# self.connections[connection_name] = this_connection
\ No newline at end of file
print('adding connections...')
for connection in connections:
connection_name = connection['name']
if connection_name not in connections:
this_connection = {}
if connection['dialect'] == 'postgresql':
connection_url = 'postgresql+psycopg2://{user}:{pw}@{host}:{port}/{db}'.format(user=connection['username'],pw=connection['password'],host=connection['host'],port=connection['port'], db=connection['database'])
this_connection['connection_url'] = connection_url
this_connection['engine'] = create_engine(this_connection['connection_url'])
# inspection = inspect(this_connection['engine'])
# schemas = inspection.get_schema_names()
# for schema in schemas:
# self.connections[schema] = {}
# tables = inspection.get_table_names(schema)
# for table in tables:
# self.connections[schema][table] = {}
# try:
# columns = inspection.get_columns(table)
# for column in columns:
# self.connections[schema][table][column['name']] = str(column['type'])
# except Exception as e:
# continue
# meta = MetaData()
# meta.reflect(bind=this_connection['engine'], schema='analytics')
# this_connection['meta'] = meta
return self.connections
<template>
<form class="container box">
<h2>New Project</h2>
<div class="field">
<label class="label">Name
<div class="control">
<input class="input" type="text" v-model="name" placeholder="Project Name">
<section class="section">
<form class="container box">
<h2>New Project</h2>
<div class="field">
<label class="label">Name
<div class="control">
<input class="input" type="text" v-model="name" placeholder="Project Name">
</div>
</label>
</div>
</label>
</div>
<div class="field">
<label class="label">URL
<div class="control">
<input class="input" type="text" v-model="git_url" placeholder="Git URL">
<div class="field">
<label class="label">URL
<div class="control">
<input class="input" type="text" v-model="git_url" placeholder="Git URL">
</div>
</label>
</div>
</label>
</div>
<div class="field is-grouped">
<div class="control">
<button class="button is-link" @click.prevent="saveProject">Save</button>
<div class="field is-grouped">
<div class="control">
<button class="button is-link" @click.prevent="saveProject">Save</button>
</div>
<div class="control">
<router-link class="button is-text" to="/projects">Cancel</router-link>
</div>
</div>
<div class="control">
<router-link class="button is-text" to="/projects">Cancel</router-link>
</div>
</div>
</form>
</form>
</section>
</template>
<script>
export default {
......
......@@ -79,7 +79,7 @@
</div>
<div class="field is-grouped">
<p class="control">
<p class="control is-expanded">
<input class="input"
type="text"
placeholder="Database"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment