Skip to content

Commit 1ee7d10

Browse files
committed
#390 Updating Model.toInsert() to use Dialect for query generation
1 parent aaf3d50 commit 1ee7d10

File tree

7 files changed

+134
-58
lines changed

7 files changed

+134
-58
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/Formatter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
/*
22
Copyright 2009-2014 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
16-
17-
1816
package org.javalite.activejdbc;
1917

2018
/**
2119
* @author Igor Polevoy
20+
* @deprecated Use {@link org.javalite.activejdbc.dialects.Dialect} to format query values.
2221
*/
22+
@Deprecated
2323
public interface Formatter {
2424
String format(Object value);
2525

activejdbc/src/main/java/org/javalite/activejdbc/Model.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.*;
4040
import org.javalite.activejdbc.conversion.BlankToNullConverter;
4141
import org.javalite.activejdbc.conversion.ZeroToNullConverter;
42+
import org.javalite.activejdbc.dialects.Dialect;
4243
import org.javalite.common.Escape;
4344

4445
import static org.javalite.common.Inflector.*;
@@ -2677,21 +2678,36 @@ public void manageTime(boolean manage) {
26772678
}
26782679

26792680
/**
2680-
* Generates INSERT SQL based on this model. Uses single quotes for all string values.
2681+
* Generates INSERT SQL based on this model. Uses the dialect associated with this model database to format the
2682+
* value literals.
26812683
* Example:
26822684
* <pre>
2683-
*
26842685
* String insert = u.toInsert();
26852686
* //yields this output:
26862687
* //INSERT INTO users (id, first_name, email, last_name) VALUES (1, 'Marilyn', 'mmonroe@yahoo.com', 'Monroe');
26872688
* </pre>
26882689
*
26892690
* @return INSERT SQL based on this model.
26902691
*/
2691-
public String toInsert(){
2692-
return toInsert("'", "'");
2692+
public String toInsert() {
2693+
return toInsert(getMetaModelLocal().getDialect());
26932694
}
26942695

2696+
/**
2697+
* Generates INSERT SQL based on this model with the provided dialect.
2698+
* Example:
2699+
* <pre>
2700+
* String insert = u.toInsert(new MySQLDialect());
2701+
* //yields this output:
2702+
* //INSERT INTO users (id, first_name, email, last_name) VALUES (1, 'Marilyn', 'mmonroe@yahoo.com', 'Monroe');
2703+
* </pre>
2704+
*
2705+
* @param dialect dialect to be used to generate the SQL
2706+
* @return INSERT SQL based on this model.
2707+
*/
2708+
public String toInsert(Dialect dialect) {
2709+
return dialect.insert(getMetaModelLocal(), attributes);
2710+
}
26952711

26962712
/**
26972713
* Generates INSERT SQL based on this model.
@@ -2707,20 +2723,21 @@ public String toInsert(){
27072723
* @param leftStringQuote - left quote for a string value, this can be different for different databases.
27082724
* @param rightStringQuote - left quote for a string value, this can be different for different databases.
27092725
* @return SQL INSERT string;
2726+
* @deprecated Use {@link #toInsert(Dialect)} instead.
27102727
*/
2711-
//TODO: review SimpleFormatter (why should the user worry about this?!), using the Dialect instead
2728+
@Deprecated
27122729
public String toInsert(String leftStringQuote, String rightStringQuote){
27132730
return toInsert(new SimpleFormatter(java.sql.Date.class, "'", "'"),
27142731
new SimpleFormatter(Timestamp.class, "'", "'"),
27152732
new SimpleFormatter(String.class, leftStringQuote, rightStringQuote));
27162733
}
27172734

27182735
/**
2719-
* TODO: write good JavaDoc, use code inside method above
2720-
*
27212736
* @param formatters
27222737
* @return
2738+
* @deprecated Use {@link #toInsert(Dialect)} instead.
27232739
*/
2740+
@Deprecated
27242741
public String toInsert(Formatter... formatters){
27252742
HashMap<Class, Formatter> formatterMap = new HashMap<Class, Formatter>();
27262743

activejdbc/src/main/java/org/javalite/activejdbc/SimpleFormatter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
/*
22
Copyright 2009-2014 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
16-
17-
1816
package org.javalite.activejdbc;
1917

2018
import org.javalite.common.Convert;
2119

2220

2321
/**
2422
* @author Igor Polevoy
23+
* @deprecated Use {@link org.javalite.activejdbc.dialects.Dialect} to format query values
2524
*/
25+
@Deprecated
2626
public class SimpleFormatter implements Formatter{
2727

2828
private final Class clazz;

activejdbc/src/main/java/org/javalite/activejdbc/dialects/DefaultDialect.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
package org.javalite.activejdbc.dialects;
1717

1818
import org.javalite.activejdbc.MetaModel;
19+
import org.javalite.activejdbc.associations.Many2ManyAssociation;
20+
import org.javalite.common.Convert;
1921

22+
import java.util.Iterator;
2023
import java.util.List;
24+
import java.util.Map;
2125
import java.util.regex.Pattern;
22-
import org.javalite.activejdbc.associations.Many2ManyAssociation;
2326

2427
import static org.javalite.common.Util.*;
2528

@@ -130,8 +133,9 @@ public String selectCount(String table, String where) {
130133
}
131134

132135
@Override
133-
public String selectExists(MetaModel mm) {
134-
return "SELECT " + mm.getIdName() + " FROM " + mm.getTableName() + " WHERE " + mm.getIdName() + " = ?";
136+
public String selectExists(MetaModel metaModel) {
137+
return "SELECT " + metaModel.getIdName() + " FROM " + metaModel.getTableName()
138+
+ " WHERE " + metaModel.getIdName() + " = ?";
135139
}
136140

137141
@Override
@@ -173,4 +177,46 @@ public String deleteManyToManyAssociation(Many2ManyAssociation association) {
173177
return "DELETE FROM " + association.getJoin()
174178
+ " WHERE " + association.getSourceFkName() + " = ? AND " + association.getTargetFkName() + " = ?";
175179
}
180+
181+
protected void appendValue(StringBuilder query, Object value) {
182+
if (value == null) {
183+
query.append("NULL");
184+
} else if (value instanceof Number) {
185+
query.append(value);
186+
} else if (value instanceof java.sql.Date) {
187+
appendDate(query, (java.sql.Date) value);
188+
} else if (value instanceof java.sql.Timestamp) {
189+
appendTimestamp(query, (java.sql.Timestamp) value);
190+
} else {
191+
query.append('\'').append(Convert.toString(value)).append('\'');
192+
}
193+
}
194+
195+
protected void appendDate(StringBuilder query, java.sql.Date value) {
196+
query.append("DATE ").append('\'').append(value.toString()).append('\'');
197+
}
198+
199+
protected void appendTimestamp(StringBuilder query, java.sql.Timestamp value) {
200+
query.append("TIMESTAMP ").append('\'').append(value.toString()).append('\'');
201+
}
202+
203+
@Override
204+
public String insert(MetaModel metaModel, Map<String, Object> attributes) {
205+
StringBuilder query = new StringBuilder().append("INSERT INTO ").append(metaModel.getTableName()).append(' ');
206+
if (attributes.isEmpty()) {
207+
appendEmptyRow(metaModel, query);
208+
} else {
209+
query.append('(');
210+
join(query, attributes.keySet(), ", ");
211+
query.append(") VALUES (");
212+
Iterator<Object> it = attributes.values().iterator();
213+
appendValue(query, it.next());
214+
while (it.hasNext()) {
215+
query.append(", ");
216+
appendValue(query, it.next());
217+
}
218+
query.append(')');
219+
}
220+
return query.toString();
221+
}
176222
}

activejdbc/src/main/java/org/javalite/activejdbc/dialects/Dialect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.javalite.activejdbc.dialects;
1818

1919
import java.util.List;
20+
import java.util.Map;
2021
import org.javalite.activejdbc.MetaModel;
2122
import org.javalite.activejdbc.associations.Many2ManyAssociation;
2223

@@ -48,4 +49,6 @@ public interface Dialect {
4849
String insertParametrized(MetaModel metaModel, List<String> columns);
4950

5051
String deleteManyToManyAssociation(Many2ManyAssociation association);
52+
53+
String insert(MetaModel metaModel, Map<String, Object> attributes);
5154
}

activejdbc/src/main/java/org/javalite/activejdbc/dialects/OracleDialect.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
/*
2-
Copyright 2009-2014 Igor Polevoy
2+
Copyright 2009-2015 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
16-
17-
1816
package org.javalite.activejdbc.dialects;
1917

2018
import java.util.List;
2119
import org.javalite.activejdbc.MetaModel;
2220

23-
2421
/**
2522
* @author Igor Polevoy
23+
* @author Eric Nielsen
2624
*/
2725
public class OracleDialect extends DefaultDialect {
2826

activejdbc/src/main/java/org/javalite/activejdbc/dialects/SQLiteDialect.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/*
2-
Copyright 2009-2014 Igor Polevoy
2+
Copyright 2009-2015 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
16-
17-
1816
package org.javalite.activejdbc.dialects;
1917

18+
import java.sql.Date;
19+
import java.sql.Timestamp;
2020
import java.util.List;
2121
import org.javalite.activejdbc.MetaModel;
2222
import org.javalite.common.Convert;
2323

2424
/**
2525
* @author Igor Polevoy
26-
* @author ericbn
26+
* @author Eric Nielsen
2727
*/
2828
public class SQLiteDialect extends PostgreSQLDialect {
2929
@Override
@@ -47,4 +47,16 @@ public Object overrideDriverTypeConversion(MetaModel mm, String attributeName, O
4747
}
4848
return value;
4949
}
50+
51+
@Override
52+
protected void appendDate(StringBuilder query, Date value) {
53+
// See https://www.sqlite.org/lang_datefunc.html
54+
query.append("date('").append(value.toString()).append("')");
55+
}
56+
57+
@Override
58+
protected void appendTimestamp(StringBuilder query, Timestamp value) {
59+
// See https://www.sqlite.org/lang_datefunc.html
60+
query.append("datetime('").append(value.toString()).append("')");
61+
}
5062
}

0 commit comments

Comments
 (0)