Exemplo n.º 1
0
def main():
    args = parse_args()
    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    try:
        print to_sqla(" ".join(args.sql))
    except Exception as e:
        print >> sys.stderr, e
        if args.debug:
            raise

    sys.exit(0)
Exemplo n.º 2
0
    def _run(self, sql, output):
        LOG.debug("sql: '%s'", sql)

        sqla = eval(to_sqla(sql))
        result = self.engine.execute(sqla)
        rows = [list(x) for x in result.fetchall()]
        self.assertEqual(rows, output)
Exemplo n.º 3
0
    def _run(self, sql, output):
        LOG.debug("sql: '%s'", sql)

        sqla = eval(to_sqla(sql))
        result = self.engine.execute(sqla)
        rows = [list(x) for x in result.fetchall()]
        self.assertEqual(rows, output)
Exemplo n.º 4
0
def main():
    args = parse_args()
    if args.version:
        print("sqlitis %s" % VERSION)
        return 0

    if not args.sql:
        print("ERROR: No SQL string provided", file=sys.stderr)
        return 1

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    # log version info
    LOG.debug("Version info")
    for key, val in version_info().items():
        LOG.debug("  %s: %s", key, val)

    sql_string = " ".join(args.sql)
    try:
        result = to_sqla(sql_string)
        print(result)
    except Exception as e:
        print("ERROR: Failed to convert SQL: {}".format(sql_string),
              file=sys.stderr)
        print("{}".format(e), file=sys.stderr)
        if args.debug:
            raise
        return 1

    return 0
Exemplo n.º 5
0
    def _run(self, sql, sqla):
        LOG.debug("sql: '%s'", sql)

        x = sqlparse.parse(sql)[0]
        for x in x.tokens:
            if not x.is_whitespace:
                LOG.debug('  %r %s', x, type(x))

        self.assertEqual(to_sqla(sql), sqla)
Exemplo n.º 6
0
    def test_select(self, sql, data=None, output=None, **kwargs):
        if not data:
            self.skipTest("missing data")
        if not output:
            self.skipTest("missing expected output")

        self._prepare_data(data)
        sqla = eval(to_sqla(sql))
        result = self.engine.execute(sqla)
        rows = [list(x) for x in result.fetchall()]
        self.assertEqual(rows, output)
Exemplo n.º 7
0
def main():
    args = parse_args()
    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    try:
        result = to_sqla(" ".join(args.sql))
        print(result)
    except Exception as e:
        print(e, file=sys.stderr)
        if args.debug:
            raise
        return 1

    return 0
Exemplo n.º 8
0
def main():
    args = parse_args()
    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    try:
        result = to_sqla(" ".join(args.sql))
        print(result)
    except Exception as e:
        print(e, file=sys.stderr)
        if args.debug:
            raise
        return 1

    return 0
Exemplo n.º 9
0
    def _run(self, sql, sqla, exception):
        LOG.debug("sql: '%s'", sql)

        x = sqlparse.parse(sql)[0]
        for x in x.tokens:
            if not x.is_whitespace:
                LOG.debug("  %r %s", x, type(x))

        try:
            actual_sqla = to_sqla(sql)
        except Exception as e:
            if not exception:
                raise
            self.assertRegex(str(e), exception.strip())
        else:
            self.assertEqual(actual_sqla, sqla)
Exemplo n.º 10
0
    def _run(self, sql, sqla, exception):
        LOG.debug("sql: '%s'", sql)

        x = sqlparse.parse(sql)[0]
        for x in x.tokens:
            if not x.is_whitespace:
                LOG.debug('  %r %s', x, type(x))

        try:
            actual_sqla = to_sqla(sql)
        except Exception as e:
            if not exception:
                raise
            self._assertRegexMatches(str(e), exception.strip())
        else:
            self.assertEqual(actual_sqla, sqla)
Exemplo n.º 11
0
from sqlitis.convert import to_sqla
from sqlalchemy import text, func
from sqlalchemy.sql import select, and_, or_
from sqlalchemy.orm import Query
from db import create_session, staff, products, customers, products, Staff, Product, Stock, Customer, Order, OrderItem
from ast import literal_eval

stmt = to_sqla(
    'select * from products where products.store = "Kenya"').replace(
        ".c.", ".")
sess = create_session()
rel_lookup = {
    'staff': Staff,
    'products': Product,
    'stocks': Stock,
    'orders': Order,
    'order_items': OrderItem,
    'customers': Customer
}
exec(f'stmt = {stmt}')
q = sess.query(*[rel_lookup[str(table)] for table in stmt.froms]).add_columns(
    *[col.expression for col in stmt.columns]).from_statement(
        text('select * from products where products.store = \'Kenya\''))
print(q.all())