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);
Related
I am quite new to GBQ and any help is appreciated it.
I have a query below:
#Standard SQL
create or replace table `xxx.xxx.applications`
as select * from `yyy.yyy.applications`
What I need to do is to add today's date at the end of the table name so it is something like xxx.xxx.applications_<todays date>
basically create a filename with Application but add date at the end of the name applications.
I am writing a procedure to create a table every time it runs but need to add the date for audit purposes every time I create the table (as a backup).
I searched everywhere and can't get the exact answer, is this possible in Query Editor as I need to store this as a Proc.
Thanks in advance
BigQuery doesn't support dynamic SQL at the moment which means that this kind of construction is not possible.
Currently BigQuery supports Parameterized Queries but its not possible to use parameters to dynamically change the source table's name as you can see in the provided link.
BigQuery supports query parameters to help prevent SQL injection when
queries are constructed using user input. This feature is only
available with standard SQL syntax. Query parameters can be used as
substitutes for arbitrary expressions. Parameters cannot be used as
substitutes for identifiers, column names, table names, or other parts
of the query.
If you need to build a query based on some variable's value, I suggest that you use some script in SHELL, Python or any other programming language to create the SQL statement and then execute it using the bq command.
Another approach could be using the BigQuery client library in some of the supported languages instead of the bq command.
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.
Im currently building an app to help with day to day development of our app development team.
Im wondering is there any sort of easy way to generate code to generate SQL tables that have already been created for MSSQL ?
I ask this because in MSSQL you can right click a table and choose the generate scripts option and it will create the code neccessary to build that particular table ?
Is there any way via SQL to leverage that function, or anyway within ColdFusion to create this code, without having to write it from scratch ?
I would use something like this SQL server query to get the list of columns in a table along with Data types, NOT NULL, and PRIMARY KEY constraints to get the table names and columns and data types and construct something with the results to write a script for creating the tables.
You can right click and generate the script from SQL. Then in CF, you can have something like this:-
<cfquery name = "query1" dataSource = "ds1">
type in the generated script from SQL here
</cfquery>`
I am writing an application which works with a legacy database (brownfield). I have a couple of tables in which I insert data. These tables have some fields which need values of which I do not want the properties in my domain entities. Is there a way to insert the default value into the field without having to create a property for it my mapping file? I cannot alter the database to create a trigger, so it has to be done via the mapping file/.net application.
Hope someone can help. I hoped I could use a formula, but that doesn't work and I couldn't find any other ways to do it either.
you could use a private / protected property.
That would mean introducing these fields into your domain model / mappings, but they would be limited to those, and not exposed to whoever uses your entities.
seems like a reasonable compromise to me.
You could use EventListeners
in the OnPostInsert / OnPostUpdate event you can get the db connection and ad-hoc execute a sql query.
NH makes it rather easy
using xml see here
using FluentNHibernate see here
the basic idea is to use PropertyAccessor on a non existing property which always has the constant value.
Is there a way that to access my db view as a table for a model?
Yes, you can use views just fine, they behave just like tables in ActiveRecord. I don't know what database you're using, but I use them in Oracle and haven't had a problem.
The only difference is that if you want to have your migrations automatically create them, you'll have to forego the typical create_table and instead execute SQL statements to create it.
With MongoDb, I accessed to the MongoDB Views with the following method.
Firstly you should create a dummy model to be able to use MongoDB connectors. You can manually create new file with the name view_names.rb and append below lines.
class ViewName
include Mongoid::Document
include Mongoid::Timestamps
end
And then to access records;
ViewName.collection.find({})
>> #<Mongo::Collection::View:0x0000000107cd4378>)
find gets MongoDB queries as parameter, so you can pass your logic as hash to find method.
{created_at: { '$lte': Date.todat - 2.week }}
View name on MongoDB should be plural view_names just as same with the others.