I want to supporting where clause for columns which containing XML content in hibernate for some DBs. I've achived this in Oracle by extending org.hibernate.dialect.OraclexDialect class and registering xml functions by registerFunction method. Example of the generated query:
SELECT *
FROM OM_PERSON this_
WHERE xmltype.createxml(this_.config_xml).existsNode('/*[condition/text()="19943"]')=1;
The function which is registered in hibernate (For Oracle):
xmltype.createxml(?1).existsNode(?2)
Now i want to support PostgreSQL (preferred version: 9.6) too, and i can't find any equivalent function for this. So my question is is there any equivalent function/statement to the above Oracle query in PostgreSQL?
PostgreSQL has a function XMLEXISTS, so your query can looks like:
SELECT *
FROM OM_PERSON x
WHERE XMLEXISTS('/*[condition/text()="19943"]' PASSING x.config_xml)
I have not idea, how it can be used in Hibernate.
Related
I am new to writing sql queries in presto and was looking for a function similar to 'starts_with'.
If a string starts with a given substring then the query needs to return that record.
In Postgresql, I am currently doing select * from tableA where name~'^Joh'. Whats the equivalent of this in Presto?
PostgreSQL and presto are RDBMS based on SQL. It is weird to see that you've learned a PostgreSQL proprietary add on (regular expressions) to the language before learning the standard SQL functions. In SQL you use LIKE for pattern matches:
select * from tableA where name like 'Joh%';
You can use Like in SQL. You can go through this link https://www.w3schools.com/sql/sql_like.asp. Using like you can search for a specified pattern.
In presto you can use regexp_like() which runs little faster than other like operators.For your case try below query which should provide you with expected functionality.
select regexp_like('John', '^John')
Here I need to convert common format sql such as:
select * from table where id = 'abc' limit 1
to mybatis supporting format such as:
select * from table where id = #{id} limit #{count}
so that sql could be executed in mybatis to aviod sql injection attack and complete different DB type convert using mybatis.
The reason why I have to use mybatis do that is because following request:
1 the orignal sql is come from user input, so have to insure the safe of that sql
2 mybatis support different DB, so no need to write code to convert field for different DB type
3 and also mybatis interception function should involved.
I have tried using Druid WallVisitorutils.check() to verify the safty of orignal sql, but I don't think that is a good way to fulfill the 3 requests.
I am considering MyBatis Dynamic SQL, but can't find a good way.
is there any suggestion?
Is there a way in PostgreSQL to access fields without explicitly providing the column name? For example, instead of the statement:
select (col1,col2,col3,...col42) from foo_table;
there's a (possible) alternative of:
select (1:42) from foo_table;
There is no such syntax in Postgres' SQL.
You could always resort to having code that dynamically constructs the query for you before executing it, but that's about as good as it will get.
Hellow I'm searching how to create query like this with knex
SELECT product.price/100 AS priceInDollars
and getting error 'price/100 not found'
related question divide the value of a column by another column
Knex seems to wrap the columns in quotes, so such operations cannot be supported using Knex query builder, as the database would interpret that as literals.
knex.column('title', 'author', 'year').select().from('books')
Outputs:
select `title`, `author`, `year` from `books`
However, knex also provides a way to fire raw SQL statements, so you would be able to execute this query.
knex.raw('SELECT product.price/100 AS priceInDollars').then(function(resp) { ... });
Further reading: Knex Raw Queries
This can be done using knex.raw query partially for the columns.
You have two possible solutions:
Raw SQL:
You have possibility to use knex.raw to use full raw SQL query as you would execute it against database (as other answers already indicated). However, if you are using tools like knex, usually this is something you want to avoid (especially when you are using query builder to build more complicated queries and relationships - I assume that this is why you are using knex in the first place).
You can use knex.raw partially for specific column instead.
Lets consider following query:
SELECT id, product.price/100 AS priceInDollars, created_at WHERE id='someUUID';
You can execute this with knex in a following format:
knex
.select([
'id',
knex.raw('products.price::numeric/100 as priceInDollars'),
'created_at'
])
.from('products')
.where({ id: 'someUUID' });
My assumption in the answer is that postgresql is used (hence numeric), but if you want to extract float after the division, you will need to do a casting (in dependency of what kind of types database support)
How can you query a range of timestamped tables with the new syntax? Using TABLE_DATE_RANGE returns the error Unhandled node type in from clause: TVF.
The latest version of BigQuery supports an equivalent of table wildcards with Standard SQL. The documentation is available here: https://cloud.google.com/bigquery/docs/wildcard-tables.
Also please take a look at this post:
Is there an equivalent of table wildcard functions in BigQuery with standard SQL?