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

Database + table name #93

Open
andybee opened this issue Feb 2, 2018 · 4 comments
Open

Database + table name #93

andybee opened this issue Feb 2, 2018 · 4 comments

Comments

@andybee
Copy link

andybee commented Feb 2, 2018

I was expecting to be able to build a query along the lines of:

$queryBuilder->select()->setTable('users.user');

Where the table user exists within a database users.

Am I missing an undocumented method that lets me set the DB another way, or do I have to apply a USE SQL command ahead of running the query?

@Krinkle
Copy link

Krinkle commented Nov 30, 2018

The library appears to support this through the Table::__construct(.., $schema) parameter and Table::setSchema(), which are also consumed correctly by the Writer implementation.

The only problem is, it seems that the query builder class does not expose this functionality. The $builder->select()->setTable() method only allows a string, or an array with name/alias. It does not allow a schema, nor does it allow a custom Table object to be passed in.

@nilportugues Do I understand correctly? If so, what approach would you prefer for solving this? I would be interested in writing a patch for to fix this.

Krinkle added a commit to Krinkle/php-sql-query-builder that referenced this issue Nov 30, 2018
The Table class supports aliases and schemas, but the query builder
and syntax builder required string parameters instead.

The easiest way to add support for table alias and multiple database
schema queries without changing the API, is to allow Table objects
to be passed to setTable() and related methods.

Also:

- docs: Fix `@return` of AbstractBaseQuery::getTable.

- docs: Fix `@param $table` of AbstractBaseQuery::orderBy.

- minor: Remove redundant `array()` wrap on table names
  from AbstractBaseQuery to SyntaxFactory::createTable, and
  from Column to SyntaxFactory::createTable.

- Column: Fix bug where `(string) $table` caused alias and schema
  to be lost, due to unexpected Table being casted to plain name
  via Table::__toString.

Ref nilportugues#93.
Krinkle added a commit to Krinkle/php-sql-query-builder that referenced this issue Nov 30, 2018
The Table class supports aliases and schemas, but the query builder
and syntax builder required string parameters instead.

The easiest way to add support for table alias and multiple database
schema queries without changing the API, is to allow Table objects
to be passed to setTable() and related methods.

Also:

- docs: Fix `@return` of AbstractBaseQuery::getTable.

- docs: Fix `@param $table` of AbstractBaseQuery::orderBy.

- minor: Remove redundant `array()` wrap on table names
  from AbstractBaseQuery to SyntaxFactory::createTable, and
  from Column to SyntaxFactory::createTable.

- Column: Fix bug where `(string) $table` caused alias and schema
  to be lost, due to unexpected Table being casted to plain name
  via Table::__toString.

Ref nilportugues#93.
@marcus-hiles
Copy link

@andybee we use query builder in association with PDO, hence we sconnect and select the database through PDO, before using query builder. You can try that as well, hope this answer is not too late?

@andybee
Copy link
Author

andybee commented Apr 5, 2019

@marcus-hiles The issue I was experiencing is that in our multi-tenant environment (where we use databases to separate concerns), the same script may need to use multiple different databases and re-establishing the connection to the server just to switch DB would be inefficient.

@marcus-hiles
Copy link

Oh ok, gotcha @andybee
Then I think your best bet here is to mysqli. Back to the basics man :-)

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

3 participants