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

Feature request: allow for run-time table name change. #173

Closed
vrurg opened this issue Jul 30, 2019 · 4 comments
Closed

Feature request: allow for run-time table name change. #173

vrurg opened this issue Jul 30, 2019 · 4 comments

Comments

@vrurg
Copy link
Contributor

vrurg commented Jul 30, 2019

Make it possible for a model to change its table name by a user at run-time. Proposed syntax would be:

Model.^table( "new_name" );

Considering that this approach would change the table name globally conflicts are possible if third-party modules are would be willing to use own tables for the same published model. I.e. lets assume there is a module Red::DBHash providing a hash mapped onto a table. There're modules Foo and Bar published on CPAN too. Both are using Red::DBHash but for different purposes. Both would need their own tables.

To resolve this situation a method new_model could be provided which would return a run-time created model type:

my \Model2 = Model.^new_type(:table<model2>);

Unless undocumented method .^alias does the same.

@vrurg
Copy link
Contributor Author

vrurg commented Jul 30, 2019

BTW, it turns out there is no good way to get model's table name in the user land. Attribute table is public, but it's accessor isn't installed because method table exists. Method table always sets table name from the type supplied. Thus it would override any possible custom table name.

I think the solution is in making table a multi with no params (return only), typed (Mu:U \type), and (Str:D $name) for this request.

FCO added a commit that referenced this issue Aug 3, 2019
@FCO
Copy link
Owner

FCO commented Aug 3, 2019

@vrurg What do you think of this solution?

% perl6 -I. -MRed -e '
my $*RED-DB = database "SQLite"; my $*RED-DEBUG = True;
model Bla { has UInt $.a is id }
Bla.^table = "not_bla";
Bla.^create-table
'
SQL : CREATE TABLE not_bla(
   a integer NOT NULL primary key 
)
BIND: []

@vrurg
Copy link
Contributor Author

vrurg commented Aug 4, 2019

Looks absolutely reasonable. Thanks!

@FCO FCO closed this as completed Aug 4, 2019
@FCO
Copy link
Owner

FCO commented Aug 4, 2019

Thanks!

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