SQL statement Correspondence Relationship display _set

Source: Internet
Author: User
Tags wrapper

Package

com20161125;

Public

class Sqlelementswrapper {

/*

* Orioutput: {a.*}, {b.*}, A.runid

* Alias:a, B

* Table:tablea, Tabelb

*/

String orioutput;

String alias;

String table;

Public void Setalias () {

if (orioutput. IndexOf ("{") >-1) {

this. alias = orioutput. Replace ("{", ""). Replace (". *}", " " );

return;

}

this. alias = orioutput. substring (0, orioutput. IndexOf ("."));

}

@Override

Public String toString () {

return this. orioutput + "" + this. alias + "" + this. table;

}

}

Package

com20161125;

Import

Java.util.HashSet;

Import

Java.util.Set;

Public

class getsqlquerytest {

Public Static void Main (string[] args) {

set<sqlelementswrapper> ss = Getoutputelemententitymap(

"SELECT {A.*},{b.*},a.runid from TableA A,tableb b where A.runid=b.runid");

for (Sqlelementswrapper obj : SS) {

System. out. println (obj. toString ());

}

}

Private Static Set<sqlelementswrapper> getoutputelemententitymap (String sql) {

Set<sqlelementswrapper> Set = getoutputalias(sql);

for (Sqlelementswrapper wrapper : set) {

Gettablename (SQL, wrapper);

}

return set;

}

Private Static void gettablename (String sql, Sqlelementswrapper wrapper) {

int beginindex = sql. IndexOf ("from") + "from". Length ();

int endIndex = sql. IndexOf ("where") = =-1? SQL. Length (): sql. IndexOf ("where");

String popobjstr = sql. substring (beginindex, endIndex). Trim ();

String[] arr = popobjstr. Split (",");

for (String obj : arr) {

String[] objalias = obj. Split ("");

String alias = Objalias[1];

if (wrapper. alias. Equals (alias)) {

String tableName = Objalias[0];

wrapper. table = TableName;

}

}

}

Private Static Set<sqlelementswrapper> getoutputalias (String sql) {

int beginindex = sql. IndexOf ("select") + "select". Length ();

int endIndex = sql. IndexOf ("from");

String tempobjstr = sql. substring (beginindex, endIndex). Trim ();

String[] arr = tempobjstr. Split (",");

Set<sqlelementswrapper> Set = new hashset<sqlelementswrapper> ();

for (String output : arr) {

Sqlelementswrapper a = new sqlelementswrapper ();

a. orioutput = output;

a. Setalias ();

set. Add (a);

}

return set;

}

}

SQL statement Correspondence Relationship display _set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.