[SQL]
Wyszukanie wszystkich kluczy obcych:
SELECT obj.name AS FK_NAME,
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id
order by obj.name
programmingmt
Czyli zbiór moich notatek i przykładów które mogą się jeszcze kiedyś komuś przydać...
poniedziałek, 3 marca 2014
poniedziałek, 10 lutego 2014
Java - wyrażenia regularne regex/maski
Przykładowa walidacja/maski za pomocą wyrażeń regularnych:
public class RegExTest {
public static void main(String[] args) {
System.out.print("\\a{5}\\d{15} ");
String path = "abcde123456789012345";
String regularExpression = "([A-Za-z]{5}+[0-9]{15})";
Pattern pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path));
regularExpression = "([A-Za-z]{5}\\d{15})";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\a{2,} ");
path = "ab";
regularExpression = "[A-Za-z]{2,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\w{3,} ");
path = "a0_";
regularExpression = "[A-Za-z0-9_]{3,}";
regularExpression = "\\w{3,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{11} ");
path = "12345678901";
regularExpression = "[0-9]{11}";
pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path)+"/");
regularExpression = "\\d{11}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("[tTnN]{1} ");
path = "t";
regularExpression = "[tTnN]{1}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("[\\w\\d[:punct:]] ");
path = "1";
regularExpression = "[\\w\\d\\p{Punct}]{1,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{2}-\\d{3} ");
path = "21-010";
regularExpression = "\\d{2}-\\d{3}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\w ");
path = "gfasg4wer454351235Fw_";
regularExpression = "[A-Za-z0-9_]{1,}";
pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path)+"/");
regularExpression = "\\w{1,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$ ");
path = "konto@komena.pl";
regularExpression = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{9} ");
path = "123456789";
regularExpression = "\\d{9}";
pattern = Pattern.compile(regularExpression);
System.out.println(": "+pattern.matches(regularExpression, path));
String input = "linea\nlineb\nlinec";
Pattern p = Pattern.compile("(?i)lineb", Pattern.DOTALL);
Matcher m = p.matcher(input);
boolean b = m.find();
System.out.println("match found: " + b);
}
}
public class RegExTest {
public static void main(String[] args) {
System.out.print("\\a{5}\\d{15} ");
String path = "abcde123456789012345";
String regularExpression = "([A-Za-z]{5}+[0-9]{15})";
Pattern pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path));
regularExpression = "([A-Za-z]{5}\\d{15})";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\a{2,} ");
path = "ab";
regularExpression = "[A-Za-z]{2,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\w{3,} ");
path = "a0_";
regularExpression = "[A-Za-z0-9_]{3,}";
regularExpression = "\\w{3,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{11} ");
path = "12345678901";
regularExpression = "[0-9]{11}";
pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path)+"/");
regularExpression = "\\d{11}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("[tTnN]{1} ");
path = "t";
regularExpression = "[tTnN]{1}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("[\\w\\d[:punct:]] ");
path = "1";
regularExpression = "[\\w\\d\\p{Punct}]{1,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{2}-\\d{3} ");
path = "21-010";
regularExpression = "\\d{2}-\\d{3}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\w ");
path = "gfasg4wer454351235Fw_";
regularExpression = "[A-Za-z0-9_]{1,}";
pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path)+"/");
regularExpression = "\\w{1,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$ ");
path = "konto@komena.pl";
regularExpression = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{9} ");
path = "123456789";
regularExpression = "\\d{9}";
pattern = Pattern.compile(regularExpression);
System.out.println(": "+pattern.matches(regularExpression, path));
String input = "linea\nlineb\nlinec";
Pattern p = Pattern.compile("(?i)lineb", Pattern.DOTALL);
Matcher m = p.matcher(input);
boolean b = m.find();
System.out.println("match found: " + b);
}
}
piątek, 15 listopada 2013
Zmiana nazwy ciastka sesyjnego z domyślnego jsessionid
Zmiana nazwy ciastka sesyjnego z domyślnego jsessionid na MYJSESSIONID.
W pliku web.xml należy dodać poniższy wpis:
<session-config>
<cookie-config>
<name>MYJSESSIONID</name>
</cookie-config>
</session-config>
środa, 25 września 2013
SQL - Zapis wyniku SQL do pliku
Poniżej prosty sposób na zapis wyniku zapytania do pliku. Kolejne wywołania będą dopisywały wynik na końcu pliku.
set echo off newpage 0 space 0 pagesize 0 feed off head off trimspool on
set colsep ,
spool C:\export_my_table.txt
select * from my_table;
spool off
set echo off newpage 0 space 0 pagesize 0 feed off head off trimspool on
set colsep ,
spool C:\export_my_table.txt
select * from my_table;
spool off
piątek, 6 września 2013
JAVA/SQL - Wywołanie procedury SQL z poziomu Java/hibernateTemplate
Kiedy niedawno potrzebowałem wywołać kilka procedur/funkcji z poziomu kodu JAVA z u życiem 'HibernateTemplate' napotkałem na problem kiedy funkcja miała zwracać jakąś wartość.
Poniżej kilka przykładów wywołań:
1) Wywołanie funkcji zwracającej liczbę osób o podanym imieniu i nazwisku:
create or replace PACKAGE EXAMPLE_PACKAGE AS
FUNCTION COUNT_USERS ( fname VARCHAR2, lname VARCHAR2 ) RETURN NUMBER;
END EXAMPLE_PACKAGE;
create or replace PACKAGE BODY EXAMPLE_PACKAGE AS
FUNCTION COUNT_USERS ( fname VARCHAR2, lname VARCHAR2 ) RETURN NUMBER AS
users_count NUMBER(12);
BEGIN
begin
select count(id) into users_count from USERS where f_name = fname and l_name = lname;
exception
when NO_DATA_FOUND then
users_count := 0;
end;
return users_count;
END EXAMPLE_PACKAGE;
public long countUsersByName(final String fname,
final String lname) {
Long result = (Long) hibernateTemplate.execute(new HibernateCallback() {
Long count= null;
public Object doInHibernate(final Session session)
throws HibernateException, SQLException {
session.doWork(new Work() {
public void execute(Connection conn) throws SQLException {
CallableStatement stmt = conn.prepareCall("CALL EXAMPLE_PACKAGE.COUNT_USERS (?,?) into ?");
stmt.setString(1, fname);
stmt.setString(2, lname);
stmt.registerOutParameter(3, OracleTypes.VARCHAR);
stmt.execute();
count= Long.valueOf(stmt.getString(3));
}
});
return count;
}
});
return result;
}
2) Wywołanie procedury niezwracającej wyniku:
public void updateUsers(final String oldUsername,final String newUsername) {
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createSQLQuery("CALL pck_example.update_username(" +
":old_username, :new_username)");
query.setParameter("old_usernamename", oldUsername);
query.setParameter("new_username", newUsername);
return query.executeUpdate();
}
});
}
piątek, 9 sierpnia 2013
Apache Camel - obsługa wyjątków
Krótki opis jak można realizować oraz zarządzać obsługą wyjątków w Apache Camel.
1) Definicja ErrorHandler - definiowany na poziomie RouteContext:
public class MyDistributeRouteBuilder extends RouteBuilder {
private static final Logger log = Logger.getLogger(MyDistributeRouteBuilder.class);
public void configure() throws Exception {
errorHandler(deadLetterChannel("direct:distributeError")
// punkt przechowywania
.useOriginalMessage().maximumRedeliveries(2)
// liczba wznowień
.onRedelivery(new MyRedeliverProcessor("MyDistributeRouteBuilder")).redeliveryDelay(3000)
// czas opóźnienia
.backOffMultiplier(2)
// mnożnik opóźnienia między próbami
.logStackTrace(true).loggingLevel(LoggingLevel.INFO)
.logRetryStackTrace(true).logHandled(true));
from("direct:distributeError")
.log(LoggingLevel.INFO, "Forward to a route error handling")
.errorHandler(noErrorHandler())
.to("direct:errorHandling").end();
}
}
2) Definicja doTry - definiowana na wybranym fragmencie trasy:
<route>
<from uri="direct:myroute" />
<log message="myroute - BEGIN"
loggingLevel="INFO" logName="com.blogspot.programmingmt" />
<doTry>
<bean ref="someBean" method="prepareException" />
<doCatch>
<!-- catch multiple exceptions -->
<exception>java.lang.Exception</exception>
<handled>
<constant>true</constant>
</handled>
<camel:setBody>
<!-- <simple resultType="java.lang.Boolean">false</simple> -->
<simple resultType="java.lang.String">Caught exception message: ${exception.message}</simple>
</camel:setBody>
<log message="myroute - EXCEPTION"
loggingLevel="ERROR" logName="com.blogspot.programmingmt" />
</doCatch>
</doTry>
</route>
3) Definicja noErrorHandler - definiowana na poziomie trasy. Wyłącza obsługę błędów.
Java DSL:
from("direct:distributeError")
.log(LoggingLevel.INFO, "Forward to a route error handling")
.errorHandler(noErrorHandler())
.to("direct:errorHandling").end();
Spring DSL:
<route errorHandlerRef="noErrorHandler">
<from uri="direct:myroute" />
<log message="myroute - BEGIN"
loggingLevel="INFO" logName="com.blogspot.programmingmt" />
<from uri="mock:delete" />
</route>
1) Definicja ErrorHandler - definiowany na poziomie RouteContext:
public class MyDistributeRouteBuilder extends RouteBuilder {
private static final Logger log = Logger.getLogger(MyDistributeRouteBuilder.class);
public void configure() throws Exception {
errorHandler(deadLetterChannel("direct:distributeError")
// punkt przechowywania
.useOriginalMessage().maximumRedeliveries(2)
// liczba wznowień
.onRedelivery(new MyRedeliverProcessor("MyDistributeRouteBuilder")).redeliveryDelay(3000)
// czas opóźnienia
.backOffMultiplier(2)
// mnożnik opóźnienia między próbami
.logStackTrace(true).loggingLevel(LoggingLevel.INFO)
.logRetryStackTrace(true).logHandled(true));
from("direct:distributeError")
.log(LoggingLevel.INFO, "Forward to a route error handling")
.errorHandler(noErrorHandler())
.to("direct:errorHandling").end();
}
}
2) Definicja doTry - definiowana na wybranym fragmencie trasy:
<route>
<from uri="direct:myroute" />
<log message="myroute - BEGIN"
loggingLevel="INFO" logName="com.blogspot.programmingmt" />
<doTry>
<bean ref="someBean" method="prepareException" />
<doCatch>
<!-- catch multiple exceptions -->
<exception>java.lang.Exception</exception>
<handled>
<constant>true</constant>
</handled>
<camel:setBody>
<!-- <simple resultType="java.lang.Boolean">false</simple> -->
<simple resultType="java.lang.String">Caught exception message: ${exception.message}</simple>
</camel:setBody>
<log message="myroute - EXCEPTION"
loggingLevel="ERROR" logName="com.blogspot.programmingmt" />
</doCatch>
</doTry>
</route>
3) Definicja noErrorHandler - definiowana na poziomie trasy. Wyłącza obsługę błędów.
Java DSL:
from("direct:distributeError")
.log(LoggingLevel.INFO, "Forward to a route error handling")
.errorHandler(noErrorHandler())
.to("direct:errorHandling").end();
Spring DSL:
<route errorHandlerRef="noErrorHandler">
<from uri="direct:myroute" />
<log message="myroute - BEGIN"
loggingLevel="INFO" logName="com.blogspot.programmingmt" />
<from uri="mock:delete" />
</route>
piątek, 2 sierpnia 2013
Weryfikacja sumy kontrolnej pliku/InputStream - md5
Weryfikacja sumy kontrolnej pliku/InputStream z wykożystaniem md5
//filename - nazwa pliku
//attachmentChecksum - suma kontrolna przekazana z plikiem
String checksum = DigestUtils.md5Hex(inputStream);
if(!checksum.equalsIgnoreCase(attachment.getChecksum()) ) {
Formatter formatter = new Formatter();
formatter.format("Checksum verification for file %s. Expected: '%s' but was '%s'", filename, attachmentChecksum, checksum);
throw new Exception(formatter.toString());
}
//filename - nazwa pliku
//attachmentChecksum - suma kontrolna przekazana z plikiem
String checksum = DigestUtils.md5Hex(inputStream);
if(!checksum.equalsIgnoreCase(attachment.getChecksum()) ) {
Formatter formatter = new Formatter();
formatter.format("Checksum verification for file %s. Expected: '%s' but was '%s'", filename, attachmentChecksum, checksum);
throw new Exception(formatter.toString());
}
Subskrybuj:
Posty (Atom)