Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique alias generator #118

Closed
tlaguz opened this issue Aug 2, 2018 · 2 comments
Closed

Unique alias generator #118

tlaguz opened this issue Aug 2, 2018 · 2 comments

Comments

@tlaguz
Copy link
Contributor

tlaguz commented Aug 2, 2018

There is need to generate unique aliases to use inside SQL queries.
This issue was created to continue discussion from #56

It would be best if aliases inside each query was starting from constant number (0 or 1). It would make debugging a lot easier. Solutions 1 and 2 don't meet this.

Few solutions:

1. Random Generator:

public static class UniqueAliasGenerator 
{
    public static string GenerateAlias()
    {
        var rnd = new Random();
        return $"SqlKata_{rnd.Next()}__";
    }
}

2. Cyclic Generator:

public static class UniqueAliasGenerator 
{
    public static uint Counter { get; private set; }
    
    public static string GenerateAlias()
    {
        return $"SqlKata_{Counter++}__";
    }
}

3. Generator in Query class:

    public abstract class AbstractQuery
    {
        protected AbstractQuery Parent;
        
        public uint AliasCounter { get; protected set; }
        public string GetUniqueAlias => Parent != null ? Parent.GetUniqueAlias : $"SqlKata_{AliasCounter++}__";
    }

The problem is that, Parent is not copied by Copy method.

4. SqlResult class

We can't place generator inside SqlResult class, because in nested query scenario it is not shared among all queries.

@ahmad-moussawi
Copy link
Contributor

What is the difference between the 1st and the 2nd ?

@tlaguz
Copy link
Contributor Author

tlaguz commented Aug 3, 2018

I've edited my post.
In the 2nd option we could make Counter thread static to achieve thread-safety and have numbering starting from 0 at least within thread.

In the 4th we could pass current numbering somehow to subquery's SqlResult, but then we would have to update parent SqlResult numbering as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants