How to obtain column names from SQL query (Jooq,Java)












0















Hello I have a problem with obtain values from SQL query (in Java with using library of jooq)?



create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))


or



insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')


What I need to obtain (parse/get) are values: Meno, Priezvisko, Vek.
Is it possible somehow get it from sql query name of columns of table (with some jooq method)?










share|improve this question

























  • Possible duplicate of How can I get column names from a table in SQL Server?

    – J.A.P
    Dec 31 '18 at 19:39











  • I don't think so . My article is about jooq and java... :) (it is about parsing sql in Jooq)

    – Noro96
    Dec 31 '18 at 20:57













  • @Noro98 SQL is a language in it self and it is the same for all implementations. Your question is not dependant on JOOQ nor Java. You will therefor find your answer in that duplicate answer.

    – J.A.P
    Jan 2 at 4:56
















0















Hello I have a problem with obtain values from SQL query (in Java with using library of jooq)?



create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))


or



insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')


What I need to obtain (parse/get) are values: Meno, Priezvisko, Vek.
Is it possible somehow get it from sql query name of columns of table (with some jooq method)?










share|improve this question

























  • Possible duplicate of How can I get column names from a table in SQL Server?

    – J.A.P
    Dec 31 '18 at 19:39











  • I don't think so . My article is about jooq and java... :) (it is about parsing sql in Jooq)

    – Noro96
    Dec 31 '18 at 20:57













  • @Noro98 SQL is a language in it self and it is the same for all implementations. Your question is not dependant on JOOQ nor Java. You will therefor find your answer in that duplicate answer.

    – J.A.P
    Jan 2 at 4:56














0












0








0








Hello I have a problem with obtain values from SQL query (in Java with using library of jooq)?



create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))


or



insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')


What I need to obtain (parse/get) are values: Meno, Priezvisko, Vek.
Is it possible somehow get it from sql query name of columns of table (with some jooq method)?










share|improve this question
















Hello I have a problem with obtain values from SQL query (in Java with using library of jooq)?



create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))


or



insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')


What I need to obtain (parse/get) are values: Meno, Priezvisko, Vek.
Is it possible somehow get it from sql query name of columns of table (with some jooq method)?







java jooq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 21:01







Noro96

















asked Dec 31 '18 at 19:13









Noro96Noro96

438




438













  • Possible duplicate of How can I get column names from a table in SQL Server?

    – J.A.P
    Dec 31 '18 at 19:39











  • I don't think so . My article is about jooq and java... :) (it is about parsing sql in Jooq)

    – Noro96
    Dec 31 '18 at 20:57













  • @Noro98 SQL is a language in it self and it is the same for all implementations. Your question is not dependant on JOOQ nor Java. You will therefor find your answer in that duplicate answer.

    – J.A.P
    Jan 2 at 4:56



















  • Possible duplicate of How can I get column names from a table in SQL Server?

    – J.A.P
    Dec 31 '18 at 19:39











  • I don't think so . My article is about jooq and java... :) (it is about parsing sql in Jooq)

    – Noro96
    Dec 31 '18 at 20:57













  • @Noro98 SQL is a language in it self and it is the same for all implementations. Your question is not dependant on JOOQ nor Java. You will therefor find your answer in that duplicate answer.

    – J.A.P
    Jan 2 at 4:56

















Possible duplicate of How can I get column names from a table in SQL Server?

– J.A.P
Dec 31 '18 at 19:39





Possible duplicate of How can I get column names from a table in SQL Server?

– J.A.P
Dec 31 '18 at 19:39













I don't think so . My article is about jooq and java... :) (it is about parsing sql in Jooq)

– Noro96
Dec 31 '18 at 20:57







I don't think so . My article is about jooq and java... :) (it is about parsing sql in Jooq)

– Noro96
Dec 31 '18 at 20:57















@Noro98 SQL is a language in it self and it is the same for all implementations. Your question is not dependant on JOOQ nor Java. You will therefor find your answer in that duplicate answer.

– J.A.P
Jan 2 at 4:56





@Noro98 SQL is a language in it self and it is the same for all implementations. Your question is not dependant on JOOQ nor Java. You will therefor find your answer in that duplicate answer.

– J.A.P
Jan 2 at 4:56












1 Answer
1






active

oldest

votes


















1














From your question, I'm assuming you'd like to use the jOOQ parser API to parse your SQL string and then extract the column names from jOOQ's meta model.



Currently (as of jOOQ 3.11), the meta model is not available through a public API. You can access it only by means of using a VisitListener, which is an SPI that is called on every QueryPart (i.e. expression tree element) that is contained in the meta model. This example implementation can give you an idea:



import org.jooq.*;
import org.jooq.impl.*;

public class Columns {
public static void main(String args) {
var parser =
DSL.using(new DefaultConfiguration().set(new DefaultVisitListener() {
@Override
public void visitStart(VisitContext ctx) {
if (ctx.queryPart() instanceof Field
&& !(ctx.queryPart() instanceof Param))
System.out.println(((Named) ctx.queryPart()).getQualifiedName());
}
})).parser();

System.out.println("Query 1");
System.out.println("-------");
parser.parseQuery("create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))").getSQL();

System.out.println();
System.out.println("Query 2");
System.out.println("-------");
parser.parseQuery("insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')").getSQL();
}
}


It will print:



Query 1
-------
"id"
"Meno"
"Priezvisko"
"Vek"
"id" -- Field is referenced again from the constraint

Query 2
-------
"Meno"
"Priezvisko"
"Vek"


Another option is, of course, to use reflection to access jOOQ's internals.






share|improve this answer


























  • Can you please give me an example how it all works ? I am not able to understand it from your page or documentation... and there is not so much examples on internet with using of visitListener to get values from Query

    – Noro96
    Jan 17 at 20:21











  • @Noro96: See updated answer for an example

    – Lukas Eder
    Jan 18 at 10:26











  • I have last question, I want to ignore id columns, it means probably ignore all columns in table that contains classic id with incement + constraint id. I don't know if there is already method for that, but I think about: getting somehow values one by one as here below: id` int not null auto_increment Meno varchar(21) null Priezvisko varchar(24) null Vek int null constraint pk_filetest primary key (id) And then ask if values contains keywod contraint or primary key or auto_incement to ignore those values. Is there some way how to get those values in those full format ?

    – Noro96
    Jan 20 at 10:52













  • @Noro96: I've shown you a tool that can do anything you want with it. I suggest using a debugger to step through it to see how it works. Do note that the tool (VisitListener) is much more powerful than what you immediately need, so you will probably have quite a few additional follow up questions, unless you try to understand how it works, yourself. Or perhaps, it's not the right tool for your task, given its power. A future version of jOOQ will allow you to access the jOOQ expression tree directly. The current one doesn't.

    – Lukas Eder
    Jan 21 at 8:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53990723%2fhow-to-obtain-column-names-from-sql-query-jooq-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














From your question, I'm assuming you'd like to use the jOOQ parser API to parse your SQL string and then extract the column names from jOOQ's meta model.



Currently (as of jOOQ 3.11), the meta model is not available through a public API. You can access it only by means of using a VisitListener, which is an SPI that is called on every QueryPart (i.e. expression tree element) that is contained in the meta model. This example implementation can give you an idea:



import org.jooq.*;
import org.jooq.impl.*;

public class Columns {
public static void main(String args) {
var parser =
DSL.using(new DefaultConfiguration().set(new DefaultVisitListener() {
@Override
public void visitStart(VisitContext ctx) {
if (ctx.queryPart() instanceof Field
&& !(ctx.queryPart() instanceof Param))
System.out.println(((Named) ctx.queryPart()).getQualifiedName());
}
})).parser();

System.out.println("Query 1");
System.out.println("-------");
parser.parseQuery("create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))").getSQL();

System.out.println();
System.out.println("Query 2");
System.out.println("-------");
parser.parseQuery("insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')").getSQL();
}
}


It will print:



Query 1
-------
"id"
"Meno"
"Priezvisko"
"Vek"
"id" -- Field is referenced again from the constraint

Query 2
-------
"Meno"
"Priezvisko"
"Vek"


Another option is, of course, to use reflection to access jOOQ's internals.






share|improve this answer


























  • Can you please give me an example how it all works ? I am not able to understand it from your page or documentation... and there is not so much examples on internet with using of visitListener to get values from Query

    – Noro96
    Jan 17 at 20:21











  • @Noro96: See updated answer for an example

    – Lukas Eder
    Jan 18 at 10:26











  • I have last question, I want to ignore id columns, it means probably ignore all columns in table that contains classic id with incement + constraint id. I don't know if there is already method for that, but I think about: getting somehow values one by one as here below: id` int not null auto_increment Meno varchar(21) null Priezvisko varchar(24) null Vek int null constraint pk_filetest primary key (id) And then ask if values contains keywod contraint or primary key or auto_incement to ignore those values. Is there some way how to get those values in those full format ?

    – Noro96
    Jan 20 at 10:52













  • @Noro96: I've shown you a tool that can do anything you want with it. I suggest using a debugger to step through it to see how it works. Do note that the tool (VisitListener) is much more powerful than what you immediately need, so you will probably have quite a few additional follow up questions, unless you try to understand how it works, yourself. Or perhaps, it's not the right tool for your task, given its power. A future version of jOOQ will allow you to access the jOOQ expression tree directly. The current one doesn't.

    – Lukas Eder
    Jan 21 at 8:39
















1














From your question, I'm assuming you'd like to use the jOOQ parser API to parse your SQL string and then extract the column names from jOOQ's meta model.



Currently (as of jOOQ 3.11), the meta model is not available through a public API. You can access it only by means of using a VisitListener, which is an SPI that is called on every QueryPart (i.e. expression tree element) that is contained in the meta model. This example implementation can give you an idea:



import org.jooq.*;
import org.jooq.impl.*;

public class Columns {
public static void main(String args) {
var parser =
DSL.using(new DefaultConfiguration().set(new DefaultVisitListener() {
@Override
public void visitStart(VisitContext ctx) {
if (ctx.queryPart() instanceof Field
&& !(ctx.queryPart() instanceof Param))
System.out.println(((Named) ctx.queryPart()).getQualifiedName());
}
})).parser();

System.out.println("Query 1");
System.out.println("-------");
parser.parseQuery("create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))").getSQL();

System.out.println();
System.out.println("Query 2");
System.out.println("-------");
parser.parseQuery("insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')").getSQL();
}
}


It will print:



Query 1
-------
"id"
"Meno"
"Priezvisko"
"Vek"
"id" -- Field is referenced again from the constraint

Query 2
-------
"Meno"
"Priezvisko"
"Vek"


Another option is, of course, to use reflection to access jOOQ's internals.






share|improve this answer


























  • Can you please give me an example how it all works ? I am not able to understand it from your page or documentation... and there is not so much examples on internet with using of visitListener to get values from Query

    – Noro96
    Jan 17 at 20:21











  • @Noro96: See updated answer for an example

    – Lukas Eder
    Jan 18 at 10:26











  • I have last question, I want to ignore id columns, it means probably ignore all columns in table that contains classic id with incement + constraint id. I don't know if there is already method for that, but I think about: getting somehow values one by one as here below: id` int not null auto_increment Meno varchar(21) null Priezvisko varchar(24) null Vek int null constraint pk_filetest primary key (id) And then ask if values contains keywod contraint or primary key or auto_incement to ignore those values. Is there some way how to get those values in those full format ?

    – Noro96
    Jan 20 at 10:52













  • @Noro96: I've shown you a tool that can do anything you want with it. I suggest using a debugger to step through it to see how it works. Do note that the tool (VisitListener) is much more powerful than what you immediately need, so you will probably have quite a few additional follow up questions, unless you try to understand how it works, yourself. Or perhaps, it's not the right tool for your task, given its power. A future version of jOOQ will allow you to access the jOOQ expression tree directly. The current one doesn't.

    – Lukas Eder
    Jan 21 at 8:39














1












1








1







From your question, I'm assuming you'd like to use the jOOQ parser API to parse your SQL string and then extract the column names from jOOQ's meta model.



Currently (as of jOOQ 3.11), the meta model is not available through a public API. You can access it only by means of using a VisitListener, which is an SPI that is called on every QueryPart (i.e. expression tree element) that is contained in the meta model. This example implementation can give you an idea:



import org.jooq.*;
import org.jooq.impl.*;

public class Columns {
public static void main(String args) {
var parser =
DSL.using(new DefaultConfiguration().set(new DefaultVisitListener() {
@Override
public void visitStart(VisitContext ctx) {
if (ctx.queryPart() instanceof Field
&& !(ctx.queryPart() instanceof Param))
System.out.println(((Named) ctx.queryPart()).getQualifiedName());
}
})).parser();

System.out.println("Query 1");
System.out.println("-------");
parser.parseQuery("create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))").getSQL();

System.out.println();
System.out.println("Query 2");
System.out.println("-------");
parser.parseQuery("insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')").getSQL();
}
}


It will print:



Query 1
-------
"id"
"Meno"
"Priezvisko"
"Vek"
"id" -- Field is referenced again from the constraint

Query 2
-------
"Meno"
"Priezvisko"
"Vek"


Another option is, of course, to use reflection to access jOOQ's internals.






share|improve this answer















From your question, I'm assuming you'd like to use the jOOQ parser API to parse your SQL string and then extract the column names from jOOQ's meta model.



Currently (as of jOOQ 3.11), the meta model is not available through a public API. You can access it only by means of using a VisitListener, which is an SPI that is called on every QueryPart (i.e. expression tree element) that is contained in the meta model. This example implementation can give you an idea:



import org.jooq.*;
import org.jooq.impl.*;

public class Columns {
public static void main(String args) {
var parser =
DSL.using(new DefaultConfiguration().set(new DefaultVisitListener() {
@Override
public void visitStart(VisitContext ctx) {
if (ctx.queryPart() instanceof Field
&& !(ctx.queryPart() instanceof Param))
System.out.println(((Named) ctx.queryPart()).getQualifiedName());
}
})).parser();

System.out.println("Query 1");
System.out.println("-------");
parser.parseQuery("create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))").getSQL();

System.out.println();
System.out.println("Query 2");
System.out.println("-------");
parser.parseQuery("insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')").getSQL();
}
}


It will print:



Query 1
-------
"id"
"Meno"
"Priezvisko"
"Vek"
"id" -- Field is referenced again from the constraint

Query 2
-------
"Meno"
"Priezvisko"
"Vek"


Another option is, of course, to use reflection to access jOOQ's internals.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 18 at 10:26

























answered Jan 2 at 12:47









Lukas EderLukas Eder

134k70436962




134k70436962













  • Can you please give me an example how it all works ? I am not able to understand it from your page or documentation... and there is not so much examples on internet with using of visitListener to get values from Query

    – Noro96
    Jan 17 at 20:21











  • @Noro96: See updated answer for an example

    – Lukas Eder
    Jan 18 at 10:26











  • I have last question, I want to ignore id columns, it means probably ignore all columns in table that contains classic id with incement + constraint id. I don't know if there is already method for that, but I think about: getting somehow values one by one as here below: id` int not null auto_increment Meno varchar(21) null Priezvisko varchar(24) null Vek int null constraint pk_filetest primary key (id) And then ask if values contains keywod contraint or primary key or auto_incement to ignore those values. Is there some way how to get those values in those full format ?

    – Noro96
    Jan 20 at 10:52













  • @Noro96: I've shown you a tool that can do anything you want with it. I suggest using a debugger to step through it to see how it works. Do note that the tool (VisitListener) is much more powerful than what you immediately need, so you will probably have quite a few additional follow up questions, unless you try to understand how it works, yourself. Or perhaps, it's not the right tool for your task, given its power. A future version of jOOQ will allow you to access the jOOQ expression tree directly. The current one doesn't.

    – Lukas Eder
    Jan 21 at 8:39



















  • Can you please give me an example how it all works ? I am not able to understand it from your page or documentation... and there is not so much examples on internet with using of visitListener to get values from Query

    – Noro96
    Jan 17 at 20:21











  • @Noro96: See updated answer for an example

    – Lukas Eder
    Jan 18 at 10:26











  • I have last question, I want to ignore id columns, it means probably ignore all columns in table that contains classic id with incement + constraint id. I don't know if there is already method for that, but I think about: getting somehow values one by one as here below: id` int not null auto_increment Meno varchar(21) null Priezvisko varchar(24) null Vek int null constraint pk_filetest primary key (id) And then ask if values contains keywod contraint or primary key or auto_incement to ignore those values. Is there some way how to get those values in those full format ?

    – Noro96
    Jan 20 at 10:52













  • @Noro96: I've shown you a tool that can do anything you want with it. I suggest using a debugger to step through it to see how it works. Do note that the tool (VisitListener) is much more powerful than what you immediately need, so you will probably have quite a few additional follow up questions, unless you try to understand how it works, yourself. Or perhaps, it's not the right tool for your task, given its power. A future version of jOOQ will allow you to access the jOOQ expression tree directly. The current one doesn't.

    – Lukas Eder
    Jan 21 at 8:39

















Can you please give me an example how it all works ? I am not able to understand it from your page or documentation... and there is not so much examples on internet with using of visitListener to get values from Query

– Noro96
Jan 17 at 20:21





Can you please give me an example how it all works ? I am not able to understand it from your page or documentation... and there is not so much examples on internet with using of visitListener to get values from Query

– Noro96
Jan 17 at 20:21













@Noro96: See updated answer for an example

– Lukas Eder
Jan 18 at 10:26





@Noro96: See updated answer for an example

– Lukas Eder
Jan 18 at 10:26













I have last question, I want to ignore id columns, it means probably ignore all columns in table that contains classic id with incement + constraint id. I don't know if there is already method for that, but I think about: getting somehow values one by one as here below: id` int not null auto_increment Meno varchar(21) null Priezvisko varchar(24) null Vek int null constraint pk_filetest primary key (id) And then ask if values contains keywod contraint or primary key or auto_incement to ignore those values. Is there some way how to get those values in those full format ?

– Noro96
Jan 20 at 10:52







I have last question, I want to ignore id columns, it means probably ignore all columns in table that contains classic id with incement + constraint id. I don't know if there is already method for that, but I think about: getting somehow values one by one as here below: id` int not null auto_increment Meno varchar(21) null Priezvisko varchar(24) null Vek int null constraint pk_filetest primary key (id) And then ask if values contains keywod contraint or primary key or auto_incement to ignore those values. Is there some way how to get those values in those full format ?

– Noro96
Jan 20 at 10:52















@Noro96: I've shown you a tool that can do anything you want with it. I suggest using a debugger to step through it to see how it works. Do note that the tool (VisitListener) is much more powerful than what you immediately need, so you will probably have quite a few additional follow up questions, unless you try to understand how it works, yourself. Or perhaps, it's not the right tool for your task, given its power. A future version of jOOQ will allow you to access the jOOQ expression tree directly. The current one doesn't.

– Lukas Eder
Jan 21 at 8:39





@Noro96: I've shown you a tool that can do anything you want with it. I suggest using a debugger to step through it to see how it works. Do note that the tool (VisitListener) is much more powerful than what you immediately need, so you will probably have quite a few additional follow up questions, unless you try to understand how it works, yourself. Or perhaps, it's not the right tool for your task, given its power. A future version of jOOQ will allow you to access the jOOQ expression tree directly. The current one doesn't.

– Lukas Eder
Jan 21 at 8:39




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53990723%2fhow-to-obtain-column-names-from-sql-query-jooq-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS