Giter Club home page Giter Club logo

Comments (5)

Gaerzi avatar Gaerzi commented on June 10, 2024

There's already a Lua implementation. It's not SQL, and arguably more involved, but there's little reason to have two different scripting languages.
https://slade.readthedocs.io/en/latest/

You can use it to update stuff from a map, see these examples:
https://slade.readthedocs.io/en/latest/md/Examples/ChangeTextures.html
https://slade.readthedocs.io/en/latest/md/Examples/ArchvileMadness.html

from slade.

sirjuddington avatar sirjuddington commented on June 10, 2024

I'll leave this open in case someone else wants to have a go at implementing something like it, but as @Gaerzi said we already have lua scripting that can do the same thing (and more)

from slade.

PROPHESSOR avatar PROPHESSOR commented on June 10, 2024

I've written a simple parser concept in TypeScript

export const QUERY_ACTIONS = ['select', 'deselect', 'update', 'delete', 'count'] as const;
export type QueryAction = typeof QUERY_ACTIONS[number];
export type QueryConditions = Record<string, string | number>; // TODO: Add typings
export const QUERY_TYPES = ['things', 'lines', 'sectors', 'vertexes'] as const;
export type QueryType = typeof QUERY_TYPES[number];

class Query {
    query: string;
    action: QueryAction;
    type: QueryType;
    conditions: QueryConditions | null;

    constructor(query: string) {
        this.query = query;
        this.action = this.parseQueryAction(query);
        this.type = this.parseQueryType(query);
        this.conditions = this.parseWhereSection(query);
    }

    private parseQueryAction(query: string): QueryAction {
        const tokens = query.toLowerCase().split(' ');

        if (QUERY_ACTIONS.includes(tokens[0] as QueryAction)) return (tokens[0] as QueryAction);

        throw new Error('Unknown query action ' + tokens[0]);
    }

    private parseQueryType(query: string): QueryType {
        const tokens = query.toLowerCase().split(' ');

        if (QUERY_TYPES.includes(tokens[1] as QueryType)) return (tokens[1] as QueryType);

        throw new Error('Unknown query type' + tokens[1]);
    }

    private parseWhereSection(query: string): QueryConditions | null {
        const queryLower = query.toLowerCase();
        if (!queryLower.includes('where')) return null;

        const tokensLower = queryLower.split(' ');
        const tokens = query.replaceAll(',', '').split(' ');
        const whereTokens = tokens.slice(tokensLower.indexOf('where') + 1);

        const conditions: QueryConditions = {};

        for (const condition of whereTokens) {
            if (!condition.includes('=')) continue;

            const [key, value] = condition.split('=');

            conditions[key] = Number(value) ?? value;
        }

        return conditions;
    }
}

class SLQL {
    query: Query;

    constructor(query: string) {
        console.log('Executing the query: ' + query);

        this.query = new Query(query);
    }

    public exec() {
        switch (this.query.action) {
            case 'select': return this.execSelectQuery();
            default:
                throw new Error('Query action not implemented yet ' + this.query.action)
        }
    }

    private execSelectQuery() {
        console.log(`Change selection type to ${this.query.type}`);
        console.log(`Go through ${this.query.type} and filter for such conditions: ${this.query.conditions && Object.entries(this.query.conditions)}`)
        console.log(`${this.query.action} filtered ${this.query.type}`);
    }
}

const query = new SLQL('SELECT things WHERE type=3');
query.exec();

As dasho suggested, I've used TypeScript-to-Lua transpiler to check it.
The transpiled code looks like it should work, but I don't know how to use SLADE API from it:
The playground link with Lua code (wait a few seconds to transpile)

If a way is found to conveniently use the SLADE API, I can write a full SLQL handler in TypeScript and it can be added to SLADE as a Lua script.
But in that case, it would still need to be made convenient to use it. For example, when you press F1, a popup window opens where you can enter the query text with some autosuggestions and examples. This will require changes on the C++ side.

from slade.

eevee avatar eevee commented on June 10, 2024

i can definitely see a gui being useful for arbitrary find/replace (and perhaps meshing with #1618 in some ways), but i'm not sure how SQL would be an improvement over that. also your parser doesn't seem to understand type = 3?

from slade.

PROPHESSOR avatar PROPHESSOR commented on June 10, 2024

your parser doesn't seem to understand type = 3?

Yeah, it's a quick concept, not ready-to-use parser

from slade.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.