I see that createSchema was not written for the MySQLDataStore.

Here is a patch for createSchema and it seems to work fine for
mysql-5.1-wl1326 ( MySQL that includes "Precise spatial operations" ).

Please let me know if you need anything from me inorder to get this
checked into svn.

-Thanks
Steve More
Index: MySQLDataStore.java
===================================================================
--- MySQLDataStore.java (revision 26252)
+++ MySQLDataStore.java (working copy)
@@ -18,6 +18,7 @@
 import java.io.IOException;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Statement;
 import java.sql.Types;
 import java.util.logging.Logger;
 
@@ -37,6 +38,7 @@
 import org.geotools.data.jdbc.datasource.DataSourceUtil;
 import org.geotools.feature.AttributeType;
 import org.geotools.feature.AttributeTypeFactory;
+import org.geotools.feature.FeatureType;
 import org.geotools.filter.Filter;
 import org.geotools.filter.SQLEncoderMySQL;
 
@@ -68,6 +70,8 @@
     /** The logger for the mysql module. */
     private static final Logger LOGGER = 
Logger.getLogger("org.geotools.data.mysql");
 
+    private DataSource MySQLdataSource;
+
     /**
      * Basic constructor for MySQLDataStore.  
      * be done similar to the following:<br>
@@ -78,6 +82,7 @@
      */
     public MySQLDataStore(DataSource dataSource) throws IOException {
         this(dataSource, null);
+        MySQLdataSource = dataSource;
     }
 
     /**
@@ -287,4 +292,44 @@
         return new MySQLFeatureWriter(reader, queryData);
     }
 
+    public void createSchema(FeatureType featureType) throws IOException {
+        String typeName = featureType.getTypeName();
+        String namespace = featureType.getNamespace().toString();
+        String colName = null;
+        Class colClass = null;
+        String colType = null;
+
+        AttributeType[] atts = featureType.getAttributeTypes();
+        try {
+            String sql = "CREATE TABLE " + typeName + "( ";
+
+            //Add fid column right at the start...auto-increment PK
+            sql += "fid varchar(255) PRIMARY KEY";
+
+            for( int i = 0; i < atts.length; i++ ) {
+                sql += ",";
+                colName = atts[i].getName();
+                colClass = atts[i].getType();
+                if (colClass.isAssignableFrom(int.class)
+                        || colClass.isAssignableFrom(Integer.class)) {
+                    colType = "integer";
+                } else if (colClass.isAssignableFrom(String.class)) {
+                    colType = "varchar(255)";
+                } else if (colClass.isAssignableFrom(double.class)
+                        || colClass.isAssignableFrom(Double.class)) {
+                    colType = "double";
+                } else if (colClass.isAssignableFrom(Geometry.class)) {
+                    colType = "geometry";
+                } else if (Geometry.class.isAssignableFrom(colClass)) {
+                    colType = "geometry";
+                }
+                sql += " " + colName + " " + colType;
+            }
+            sql += " )";
+            Statement st = MySQLdataSource.getConnection().createStatement();
+            st.execute( sql );
+        } catch (SQLException e) {
+            // Attempted to re-create typeTable table...OK
+        }
+    }
 }
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to