mojoPortal.Data.SQLite - Little improvement suggest

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
4/29/2010 12:46:44 PM
dmo
Gravatar
Total Posts 20

mojoPortal.Data.SQLite - Little improvement suggest

Hi Joe,

When a module is created in a page, in the data layer (SQLite in this case, but some others perhaps too if the code is similar - I think about Postgres), the method DBModuleSettings.CreateDefaultModuleSettings() is called, and a SELECT sqlCommand is performed.

In theses selected fields there is mp_Modules.ModuleID, but in GetTableFromDataReader(), IDataReader.GetSchemaTable() is depending on the implementation, and could return a schema table including an unique constraint for ModuleID (beacause it's a primary key). We observed (by explicitely logging) that behaviour using mojoportal-2-3-4-2-sqlite-deploymentfiles-for-mono for example, but not with mojoportal-2-3-4-2-sqlite-deploymentfiles.

Then, beacause the insterted data are (of course) not unique, an (easily avoidable) constraint exception is raised.

We found by simply deleting the unuseful line sqlCommand.Append("m.ModuleID, "); in DBModuleSettings.CreateDefaultModuleSettings(), we are safe, and implmentation independent.

Why not ?

Michel.

4/29/2010 1:30:12 PM
Gravatar
Total Posts 18439

Re: mojoPortal.Data.SQLite - Little improvement suggest

Hi Michel,

Thanks, I was not aware this was causing a problem. Given what you have found, it is probably best not to use the DBPortal.GetTableFromDataReader method, I changed my code like this:

DataTable dt = new DataTable();
            dt.Columns.Add("ModuleID", typeof(int));
            dt.Columns.Add("ModuleGuid", typeof(string));
            dt.Columns.Add("SettingName", typeof(string));
            dt.Columns.Add("SettingValue", typeof(string));
            dt.Columns.Add("ControlType", typeof(string));
            dt.Columns.Add("ControlSrc", typeof(string));
            dt.Columns.Add("HelpKey", typeof(string));
            dt.Columns.Add("SortOrder", typeof(int));
            dt.Columns.Add("RegexValidationExpression", typeof(string));

            using (IDataReader reader = SqliteHelper.ExecuteReader(
                GetConnectionString(),
                sqlCommand.ToString(),
                arParams))
            {
                while (reader.Read())
                {
                    DataRow row = dt.NewRow();
                    row["ModuleID"] = reader["ModuleID"];
                    row["ModuleGuid"] = reader["ModuleGuid"];
                    row["SettingName"] = reader["SettingName"];
                    row["SettingValue"] = reader["SettingValue"];
                    row["ControlType"] = reader["ControlType"];
                    row["ControlSrc"] = reader["ControlSrc"];
                    row["HelpKey"] = reader["HelpKey"];
                    row["SortOrder"] = reader["SortOrder"];
                    row["RegexValidationExpression"] = reader["RegexValidationExpression"];

                    dt.Rows.Add(row);

                }

            }


            return dt;

Best,

Joe

4/30/2010 4:02:38 AM
dmo
Gravatar
Total Posts 20

Re: mojoPortal.Data.SQLite - Little improvement suggest

Well done,

I've just succesfully tested you're new code ; that works.

By principe, you could make the same modifications for MySql and pgsql, where I detected the use of GetTableFromDataReader() in the same way as SQlite.

Best,

Michel.

4/30/2010 7:28:27 AM
Gravatar
Total Posts 18439

Re: mojoPortal.Data.SQLite - Little improvement suggest

Hi Michel,

After making this change I realized I could have fixed it just as easily from inside the DBPortal.cs GetTableFromReader method by commenting out the line that sets if it is unique like this:

// we don't always want to enforce constraints, it may be fine to have duplicates in a query even if the underlying table has a unique constraint
//column.Unique = Convert.ToBoolean(schemaTable.Rows[i]["IsUnique"]);

We really don't need the constraints, the only reason we are using a DataTable is if we want to be able to loop through the data more than once or if we are going to be calling other data methods while looping over them then we don't want to have an IDataReader which has an open connection while we do that.

Best,

Joe

You must sign in to post in the forums. This thread is closed to new posts.