I need to change the schema name of a query based on the user input.
How can I pass/change the schema name to the JOOQ query at runtime ?
Thx.
A jOOQ based solution:
There's a feature called runtime schema mapping that you want to look into. You can provide pairs of input/output schema names, which will be used to map schema names of all objects in generated SQL:
DSLContext ctx = DSL.using(connection, new Settings().withRenderMapping(
new RenderMapping().withSchemata(
new MappedSchema().withInput("INPUT").withOutput("OUTPUT")
)
));
You can now use the above DSLContext to write queries like this:
ctx.select(INPUT.TABLE.COL).from(INPUT.TABLE).fetch();
The resulting SQL would be:
select OUTPUT.TABLE.COL from OUTPUT.TABLE
A SQL based solution
Instead of fully qualifying the schema, you could also prevent the schema name from being generated using Settings.withRenderSchema(false), and then use DSLContext.setSchema() or some other means to specify the default schema of your database connection.
Related
I am learning SQL now, I would like to know what is meant within the from, what is contained before and after the dot.
For example:
SELECT *
FROM apler.W_WORKED_INVOICES wwi
What does apler stand for?
The W_WORKED_INVOICES is the table, correct?
appler is database schema. Schema is defined in the Oracle documentation as a collection of logical data structures or schema objects.
W_WORKED_INVOICES is a table created in the appler schema
I've done DSN Less 2 different ways, but neither seem to have a way to specify a schema.
I tried specifying to schema like [schema]. but it doesn't work.
Any idea how to get it to link up?
You don't specify the schema in the connection string, but specify that schema in the table name (or view).
So, the default schema is "dbo".
So for table customers and schema "dbo", you use
dbo.Customers.
If the schema is sales, or other? then you go:
sales.Customers.
So the connection to the database is un-changed.
You don't have to (or can) specify the schema in the conneciton - you specifty it in the table name.
Of course the local table name can be ANY table name you want - and you are free to include or not the prefix like this
dbo_Customers
Sales_Contacts
But, you can could use
Customers
Contacts
In fact, in most cases, if you doing a migration from a standard Access data file back end to SQL server?
Then you of course will keep the client side (linked) table name as to what it was before, and the linked table name does not have any special meaning in regards to the schema used.
So only the table prefix (dbo.) is how you select/change/use a database schema, and this ONLY applies to the server side name you use when creating a table link. As noted the client side linked table can be any name you want, and it can "only" include the schema if YOU decide to adopt some naming convention.
So, you specify the schema by prefixing the server side table name when re-linking, or creating a table link.
Essentially, what is the "opposite" of CREATE CLASS (or CREATE anything) in ODB SQL? Most/all other SQL/SQL-like databases provide a way to describe a database object in terms of the SQL command it takes to create it, but I can't find anything about this in the ODB docs. What does Studio use when you click on a class in the Schema Manager? What I'm ultimately looking for is a way to produce the CREATE command from an existing database object, using ODB SQL.
OrientDB schema can be queried to obtain information about classes:
https://orientdb.com/docs/last/SQL.html#query-metadata
Information about classes:
select expand(classes) from metadata:schema
Information about a single class:
select expand(properties) from (
select expand(classes) from metadata:schema
) where name = 'MyClassName'
Metadata about indexed could be obtained as well:
select expand(indexes) from metadata:indexmanager
I need to dynamically create a table using a Java method and tranform all its rows into a list of Mapping class objects. The questions are..
Is there a way to execute CREATE TABLE query dynamically?
I saw some examples using doInHibernate() but it didn't work when I tried it. Can I do this without the particular method?
You could just execute a native sql query: session.createSQLQuery("create table .....").executeUpdate(); where "session" is your Hibernate session.
If you already have the mapping files, though, you can just set the hibernate.hbm2ddl.auto property in your hibernate configuration to generate the schema based on the mapping files.
Try this:
session.createSQLQuery(query).executeUpdate();
another possibility would be:
createStatement().execute(someddl);
Earlier I had asked the question:
Where (or how) should I define the schema in a select statement when using PostgreSQL?
The answer I accepted was to modify the search_path for the connecting user so that the schema need not be specified in the SQL. However, now I wonder if I should always specify the schema in SQL rather than allow the schema to be automatically inferred by the search path. This seems like it would be a safer approach and would be more portable to other databases.
This question is different than the previous one in that I want to know what the best practices are for defining the schema in SQL, rather than how it can be done.
Should the schema always be explicitly defined in the SQL statement?
** Note: I would not hard code the schema name but would allow it to be configurable through the Web.config file so that the schema could change from one installation to another. **
It's a bad practice to hardcode schema into SQL statements.
You should keep it in the application settings and issue SET search_path after connecting to the database.
If your application is used by multiple users with their own schemas, your life will be much easier if you don't hardcode schema name into SQL.
In other words,
string query = "SELECT * FROM " + ConfigurationManager.AppSettings.Get("schema") + ".table";
is a bad way;
SQLCommand("SET search_path = " + ConfigurationManager.AppSettings.Get("schema"), connection).ExecuteNonQuery();
string query = "SELECT * FROM table";
is a good way.
Let's see - in the DB of the app I maintain there are around a dozen schemas. What would be the order if I put them in "search_path"? And would I put in the schema names (not the tables name and not the fully-qualified table names) in the configuration?
As you have guessed by now I do not use "search_path". But maybe you could store the fully-qualified table names in the configuration in case you ever change you mind about the names of the schemas or the tables themselves.