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

Add support for named parameters #5

Closed
terales opened this issue Feb 2, 2018 · 12 comments
Closed

Add support for named parameters #5

terales opened this issue Feb 2, 2018 · 12 comments

Comments

@terales
Copy link
Contributor

terales commented Feb 2, 2018

We have several queries we want to refactor but right now it's scary because of strict order of the parameters. I would love to add support of the names parameters like this:

-- name: getPaymentsExample
SELECT * FROM payments AS p WHERE p.from = :from AND p.to = :to

<?php
// get payments
$repository->getPaymentsExample([':from' => $from. ':to' => $to]);

Do you like the idea?
Would you accept a PR for this?

@terales terales changed the title Add support for names params Add support for named parameters Feb 2, 2018
@nulpunkt
Copy link
Owner

nulpunkt commented Feb 9, 2018

Hey,

Sorry for my late answer :(

I actually thought about this when I started out and I'm not crazy about the idea. Basically you are revealing how thin a wrapper this is, which means you end up with a weird interface.

Check out the very last example in the readme where I use the inFunc, that should solve your problem. If not, please let me know 👍

@terales
Copy link
Contributor Author

terales commented Feb 9, 2018

So the idea is to write a wrapper like this:

function execWithNamedParams(array $params) {
  return $params;
}

// Call it like this:
$repository->getPaymentsExample(['from' => $from, 'to' => $to]);

And use it everywhere like:

-- name: getPaymentsExample inFunc: execWithNamedParams
SELECT * FROM payments AS p WHERE p.from = :from AND p.to = :to

Did I get you right?
Would you like a PR to add this example to the README?

@nulpunkt
Copy link
Owner

Again, sorry of the late reply.

No. the idea is to not use named parameters in the function call. You would (probably) never do that with a normal repository. I'd go something like this:

function nameFromAndTo($from, $to) {
  return ['from' => $from, 'to' => $to];
}
$repository->getPaymentsExample($from, $to);

with a query like this:

-- name: getPaymentsExample inFunc: nameFromAndTo
SELECT * FROM payments AS p WHERE p.from = :from AND p.to = :to

now you can refactor your query all you want, without breaking stuff. And your outside api looks like a normal repository :)

@terales
Copy link
Contributor Author

terales commented Feb 19, 2018 via email

@nulpunkt
Copy link
Owner

Absolutely! :)

Also, good ideas as to how I can improve the documentation is more than welcome. The readme is getting longer, so it would be cool with ideas on how to improve it!

@terales
Copy link
Contributor Author

terales commented Feb 19, 2018

Thanks, I'm busy now, but I'll prepare PR this week.

Also, good ideas as to how I can improve the documentation is more than welcome. The readme is getting longer, so it would be cool with ideas on how to improve it!

Moved this conversation into the separate issue: #6

@nulpunkt
Copy link
Owner

Awesome, thanks!

@nulpunkt nulpunkt closed this as completed Jul 2, 2018
@nulpunkt nulpunkt mentioned this issue Jul 2, 2018
@terales
Copy link
Contributor Author

terales commented Jul 2, 2018

I love the way you referenced static method. In production we have something like this:

class User {
  __constructor($pdo) {
    $this->queries = new Nulpunkt\Yesql\Repository($pdo, "my-queries/queries.sql");
  }

  public static function toAddParams(User $user) {
    return ['id' => $user->id, 'name' => $user->name];
  }

  public function add($user) {
    $this->queries->insertObject($user)
  }
}
-- name: insertObject inFunc: User::toAddParams
insert into test_table (id, something) values (:id, :something)

@nulpunkt
Copy link
Owner

nulpunkt commented Jul 3, 2018

That is awesome! :)

@nulpunkt
Copy link
Owner

nulpunkt commented Nov 2, 2018

@terales Hey man, I got tired of creating inFunc's, so I've added (what I consider) better support for named parameters :)

@terales
Copy link
Contributor Author

terales commented Nov 2, 2018

Awesome! How can I use it? It's not obvious from the PR.

Can you update an example, please?
https://github.com/nulpunkt/yesql-php#mapping-data-on-the-way-in

@terales
Copy link
Contributor Author

terales commented Nov 2, 2018

Ah, found it. It's just a different section. Yeah, awesome!

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