Query to output content of AllXml column of ELMAH_Error sql server table - asp.net

I am having trouble writing query so that I can query the content of AllXml column inside Elmah_Error table.
How can I list out all the item nodes as an output of the query.
How could I write query to only list for certain item nodes?
I would like to get follow resultset:
item value
===== =====
ALL_HTTP HTTP_CONNECTION:xxxx
ALL_RAW Connection: xxxxx
I would also like to be able to filter the query by ErrorID
Content of AllXml column may look like this.
<error
application="/"
message="hello world"
source="TestWebElmah"
detail="xxxxx">
<serverVariables>
<item
name="ALL_HTTP">
<value
string="HTTP_CONNECTION:xxxx" />
</item>
<item
name="ALL_RAW">
<value
string="Connection: xxxxx" />
</item>
</serverVariables>
</error>

Remote Addr nodes
select T.N.value('(value/#string)[1]', 'varchar(30)') as REMOTE_ADDR
from
(select cast(AllXml as xml) as AllXml from ELMAH_Error) e
cross apply AllXml.nodes('//item[#name="REMOTE_ADDR"]') as T(N)
HTTP User Agents which contain mozilla
select T.N.value('(value/#string)[1]', 'varchar(30)') as HTTP_USER_AGENT
from
(select cast(AllXml as xml) as AllXml from ELMAH_Error) e
cross apply AllXml.nodes('//item[#name="HTTP_USER_AGENT"]') as T(N)
where T.N.value('(value/#string)[1]', 'varchar(30)') like '%mozilla%'
Elmah table stores the AllXml column as nvarchar so it needs to be casted to xml
all tags + values, by error id
select T.N.value('#name', 'varchar(30)') as Name,
T.N.value('(value/#string)[1]', 'varchar(30)') as Value
from
(select cast(AllXml as xml) as AllXml from ELMAH_Error where ErrorId = 'DC82172B-F2C0-48CE-8621-A60B702ECF93') e
cross apply AllXml.nodes('/error/serverVariables/item') as T(N)
Before voting down this answer, because uses most of the part of Mikael Eriksson's answer, I let you know I'll happily accept the downvotes only for this reason, since is mainly true

This query will give you all item nodes
select T.N.value('#name', 'varchar(30)') as Name,
T.N.value('(value/#string)[1]', 'varchar(30)') as Value
from Elmah_Error
cross apply AllXml.nodes('/error/serverVariables/item') as T(N)
If you want to filter on any of the values you can put that in a sub-query apply a regular where clause.
select Name,
Value
from
(
select T.N.value('#name', 'varchar(30)') as Name,
T.N.value('(value/#string)[1]', 'varchar(30)') as Value
from Elmah_Error
cross apply AllXml.nodes('/error/serverVariables/item') as T(N)
) T
where Name = 'ALL_HTTP'

Related

SQLite + Mybatis-guice throw ArrayIndexOutOfBoundsException

I'm trying to implement a Guice module that to lets Guacamole use SQLite as a backend. The Guacamole project has a generic JDBC base module. This lets you implement modules for specific datastores with less code. Most of the lines of code end up being in mapper XML files. The project provides PostgreSQL and MySQL implementations.
I based this SQLite module off of the MySQL module. For the mapper XML files, SQLite and MySQL are similar enough that I didn't have to make any changes. However, when I try to use the SQLite module, I get this error:
### Error querying database. Cause: java.lang.ArrayIndexOutOfBoundsException: 2
### The error may exist in org/apache/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT guacamole_connection_group.connection_group_id, connection_group_name, parent_id, type, max_connections, max_connections_per_user, enable_session_affinity FROM guacamole_connection_group JOIN guacamole_connection_group_permission ON guacamole_connection_group_permission.connection_group_id = guacamole_connection_group.connection_group_id WHERE guacamole_connection_group.connection_group_id IN ( ? ) AND user_id = ? AND permission = 'READ'; SELECT parent_id, guacamole_connection_group.connection_group_id FROM guacamole_connection_group JOIN guacamole_connection_group_permission ON guacamole_connection_group_permission.connection_group_id = guacamole_connection_group.connection_group_id WHERE parent_id IN ( ? ) AND user_id = ? AND permission = 'READ'; SELECT parent_id, guacamole_connection.connection_id FROM guacamole_connection JOIN guacamole_connection_permission ON guacamole_connection_permission.connection_id = guacamole_connection.connection_id WHERE parent_id IN ( ? ) AND user_id = ? AND permission = 'READ';
### Cause: java.lang.ArrayIndexOutOfBoundsException: 2
It looks like the problem is that two parameters are passed to the query, but each is repeated three times. When MyBatis generates the PreparedStatement, it acts as if there are six parameters that needed to be passed in.
Here's the query it has a problem with:
<!-- Select multiple connection groups by identifier only if readable -->
<select id="selectReadable" resultMap="ConnectionGroupResultMap"
resultSets="connectionGroups,childConnectionGroups,childConnections">
SELECT
guacamole_connection_group.connection_group_id,
connection_group_name,
parent_id,
type,
max_connections,
max_connections_per_user,
enable_session_affinity
FROM guacamole_connection_group
JOIN guacamole_connection_group_permission ON guacamole_connection_group_permission.connection_group_id = guacamole_connection_group.connection_group_id
WHERE guacamole_connection_group.connection_group_id IN
<foreach collection="identifiers" item="identifier"
open="(" separator="," close=")">
#{identifier,jdbcType=VARCHAR}
</foreach>
AND user_id = #{user.objectID,jdbcType=INTEGER}
AND permission = 'READ';
SELECT parent_id, guacamole_connection_group.connection_group_id
FROM guacamole_connection_group
JOIN guacamole_connection_group_permission ON guacamole_connection_group_permission.connection_group_id = guacamole_connection_group.connection_group_id
WHERE parent_id IN
<foreach collection="identifiers" item="identifier"
open="(" separator="," close=")">
#{identifier,jdbcType=VARCHAR}
</foreach>
AND user_id = #{user.objectID,jdbcType=INTEGER}
AND permission = 'READ';
SELECT parent_id, guacamole_connection.connection_id
FROM guacamole_connection
JOIN guacamole_connection_permission ON guacamole_connection_permission.connection_id = guacamole_connection.connection_id
WHERE parent_id IN
<foreach collection="identifiers" item="identifier"
open="(" separator="," close=")">
#{identifier,jdbcType=VARCHAR}
</foreach>
AND user_id = #{user.objectID,jdbcType=INTEGER}
AND permission = 'READ';
</select>
If I manually populate the parameters, I can execute this against the SQLite database. Also, the MySQL version works fine.
What the heck is going on? What can I do to debug this? Is it a MyBatis problem or something with the JDBC connector?
If it helps, you can see the code for the module here.
Here's the method for the parameter the mapper related to this query. The full mapper classes for ConnectionGroup are here and here. The full mapper XML for my SQLite module is here.
Collection<ModelType> selectReadable(#Param("user") UserModel user,
#Param("identifiers") Collection<String> identifiers);
This is what the ConnectionGroupResultMap looks like:
<resultMap id="ConnectionGroupResultMap" type="org.apache.guacamole.auth.jdbc.connectiongroup.ConnectionGroupModel" >
<!-- Connection group properties -->
<id column="connection_group_id" property="objectID" jdbcType="INTEGER"/>
<result column="connection_group_name" property="name" jdbcType="VARCHAR"/>
<result column="parent_id" property="parentIdentifier" jdbcType="INTEGER"/>
<result column="type" property="type" jdbcType="VARCHAR"
javaType="org.apache.guacamole.net.auth.ConnectionGroup$Type"/>
<result column="max_connections" property="maxConnections" jdbcType="INTEGER"/>
<result column="max_connections_per_user" property="maxConnectionsPerUser" jdbcType="INTEGER"/>
<result column="enable_session_affinity" property="sessionAffinityEnabled" jdbcType="BOOLEAN"/>
<!-- Child connection groups -->
<collection property="connectionGroupIdentifiers" resultSet="childConnectionGroups" ofType="java.lang.String"
column="connection_group_id" foreignColumn="parent_id">
<result column="connection_group_id"/>
</collection>
<!-- Child connections -->
<collection property="connectionIdentifiers" resultSet="childConnections" ofType="java.lang.String"
column="connection_group_id" foreignColumn="parent_id">
<result column="connection_id"/>
</collection>
</resultMap>
David,
I know this is a little old, but I'm also trying to implement a SQLite JDBC module, and ran into exactly the same problem. I've managed to track down the source of the issue, and have filed an issue on the JDBC SQLite github page:
https://github.com/xerial/sqlite-jdbc/issues/277
Basically, the SQLite JDBC driver computes one of its array sizes based on the parameter count for a prepared statement. However, in the cases you mentioned, there are multiple SELECT statements which take the same parameters, so the array needs to be x * y (x = parameter count, y = number of select statements) rather than just the number of parameters. When it tries to prepare the statement, it hits the first position beyond the parameter count and generates this exception.
The other JDBC modules - MySQL, PostgreSQL, and SQL Server, as well as Oracle and H2 that I'm messing with - seem to handle this situation correctly (well, Oracle is a little...special...), but the SQLite driver does not.
I was able to work around the issue in a really, really kludgy way, by creating two different result maps, one for the generic select and one for the read that checks for READ permissions, and then break out each of the select queries into its own SELECT block and call those from the collection inside the result map. It's nowhere near as elegant as the existing code, but it works.

ORA-19279 XPTY0004 - Xquery dynamic type mismatch: expected singleton sequence - got multi-item sequence

I have next below xml message in CLOB column:
<Message type="Close Subscription" creationdatetime="15/01/2010 07:48" market="01" xmlns="http://test.org/">
<Customer userId="data" market="01">
<UserAccount>test#hotmail.com</UserAccount>
<ExpireDate>15/02/2016 07:48:11</ExpireDate>
<Member>
<MemberReferency number="000003" digit="85" market="01" name="John Rambo"/>
</Member>
<Email Id="700">
<Address>test#hotmail.com</Address>
<IsConfirmed>True</IsConfirmed>
<Privacies>
<Privacy type="C" value="I"/>
<Privacy type="M" value="I"/>
</Privacies>
</Email>
<Newsletter mailService="NL">
<language>N</language>
<frequency>0</frequency>
<promotion/>
<origin/>
</Newsletter>
</Customer>
</Message>
For this xml message, I have twice the xml tag privacy
<Privacies>
<Privacy type="C" value="I"/>
<Privacy type="M" value="T"/>
</Privacies>
How can I get the value of privacy for only one of them without fetch the error?.
I mean, to get the privacy of the type='C'.
Now I'm using next below query that contain the error:
select Privacy
from (select *
from ta_gen.notification_log a,
XMLTable(XMLNAMESPACES('http://test.org/' AS "XML"),
'/XML:Message' passing xmltype(NTL_MSG) columns
Privacy VARCHAR2(1) path 'XML:Customer/XML:Email/XML:Privacies/XML:Privacy/#value') O
where a.ntl_type = 'Message')
NOTE: When the xml tag privacy is once in the xml message, the error is not raised.
Depending on what you want to happen when there is only an M-type node, you canb either search explicitly for the type attribute being C:
select x.privacy
from notification_log a
cross join XMLTable(XMLNamespaces('http://test.org/' AS "XML"),
'/XML:Message'
passing XMLType(ntl_msg)
columns privacy varchar2(1)
path 'XML:Customer/XML:Email/XML:Privacies/XML:Privacy[#type="C"]/#value') x
where a.ntl_type = 'Message';
PRIVACY
-------
I
Or you can extract the type and value for all privacy nodes via a second level of XMLTable, and then decide which to keep:
select min(x2.privacy) keep (dense_rank first order by x2.type) as privacy
from notification_log a
cross join XMLTable(XMLNamespaces('http://test.org/' AS "XML"),
'/XML:Message' passing XMLType(ntl_msg)
columns privacies XMLType path 'XML:Customer/XML:Email/XML:Privacies') x1
cross join XMLTable(XMLNAMESPACES('http://test.org/' AS "XML"),
'XML:Privacies/XML:Privacy'
passing privacies
columns type varchar2(1) path '#type',
privacy varchar2(1) path '#value') x2
where a.ntl_type = 'Message';
PRIVACY
-------
I
If you ran the same query but instead did select x2.type, x2.value from ... then you'd see:
TYPE PRIVACY
---- -------
C I
M I
.. and the min(x2.privacy) keep (dense_rank first order by x2.type) gets the value type from the 'lower' (according to string comparison) type value; which means it prioritises C over M if both exist, but will use either if there is only one

"Insert if not exists" statement in SQLite

I have an SQLite database. I am trying to insert values (users_id, lessoninfo_id) in table bookmarks, only if both do not exist before in a row.
INSERT INTO bookmarks(users_id,lessoninfo_id)
VALUES(
(SELECT _id FROM Users WHERE User='"+$('#user_lesson').html()+"'),
(SELECT _id FROM lessoninfo
WHERE Lesson="+lesson_no+" AND cast(starttime AS int)="+Math.floor(result_set.rows.item(markerCount-1).starttime)+")
WHERE NOT EXISTS (
SELECT users_id,lessoninfo_id from bookmarks
WHERE users_id=(SELECT _id FROM Users
WHERE User='"+$('#user_lesson').html()+"') AND lessoninfo_id=(
SELECT _id FROM lessoninfo
WHERE Lesson="+lesson_no+")))
This gives an error saying:
db error near where syntax.
If you never want to have duplicates, you should declare this as a table constraint:
CREATE TABLE bookmarks(
users_id INTEGER,
lessoninfo_id INTEGER,
UNIQUE(users_id, lessoninfo_id)
);
(A primary key over both columns would have the same effect.)
It is then possible to tell the database that you want to silently ignore records that would violate such a constraint:
INSERT OR IGNORE INTO bookmarks(users_id, lessoninfo_id) VALUES(123, 456)
If you have a table called memos that has two columns id and text you should be able to do like this:
INSERT INTO memos(id,text)
SELECT 5, 'text to insert'
WHERE NOT EXISTS(SELECT 1 FROM memos WHERE id = 5 AND text = 'text to insert');
If a record already contains a row where text is equal to 'text to insert' and id is equal to 5, then the insert operation will be ignored.
I don't know if this will work for your particular query, but perhaps it give you a hint on how to proceed.
I would advice that you instead design your table so that no duplicates are allowed as explained in #CLs answer below.
For a unique column, use this:
INSERT OR REPLACE INTO tableName (...) values(...);
For more information, see: sqlite.org/lang_insert
insert into bookmarks (users_id, lessoninfo_id)
select 1, 167
EXCEPT
select user_id, lessoninfo_id
from bookmarks
where user_id=1
and lessoninfo_id=167;
This is the fastest way.
For some other SQL engines, you can use a Dummy table containing 1 record.
e.g:
select 1, 167 from ONE_RECORD_DUMMY_TABLE

Inserting data from one table to the next in sql

I have two tables with fields as shown below :-
tbl_emp --> id(auto generate, auto increment, primary key),name, dob
tbl_login --> id(refers to id in tbl_emp, primary key), password
I have a web form which reads the name, password and date of birth(dob), and a submit button. On pressing the submit button, the data is inserted into the first table, and also to the second table. It would be easy to insert data to the second column if I had a way to access the "id" field from the first table.
The query I have is :-
insert into tbl_emp (name,dob) values(#name, #dob);
insert into tbl_login (id,password) values((select id from tbl_emp where id=(select id from tbl_emp where id=id)), #password)
The problem that I encounter with the above coeds is :
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
You could use the SCOPE_IDENTIY function:
insert into tbl_emp (name,dob) values(#name, #dob);
insert into tbl_login (id,password)
select id, #password
from tbl_emp
where id = SCOPE_IDENTITY();

bulk insert using insert...select in sqlite?

I have a list of users who should receive the message. They are in the table subscribe. Now I'd like to insert a message for every one of these users. My query is
insert into message(user, type, theId)
select (select user from subscribe_message), #type, #id
At the moment it is empty. I get the error message.user may not be NULL. Shouldn't it not insert any rows? When I have more than one row it inserts the first row only. How do I write this so it will insert 0 to many rows?
Try this
INSERT INTO message ( user, type, theId )
SELECT subscribe_message.user, #type, #id
FROM subscribe_message

Resources