Exemplo n.º 1
0
        /// <summary>
        /// Generates a querie list to create tables.
        /// <para/>
        /// SQL query Example: <para/>
        /// CREATE TABLE table1 (id INTEGER PRIMARY KEY, firstColumn TEXT NOT NULL, secondColumn REAL NOT NULL)
        /// </summary>
        /// <returns>Returns a list of queries to create the local tables.</returns>
        private List <string> CreateTableQueries(Dictionary <string, Table> tables)
        {
            List <string> tableQueries = new List <string>();

            foreach (KeyValuePair <string, Table> table in tables)
            {
                SQLQueryBuilder sqb       = new SQLQueryBuilder();
                List <string>   paramList = new List <string>();

                int iter = 0;
                foreach (KeyValuePair <string, Type> column in table.Value.Columns)
                {
                    sqb.AddValue(column.Key);
                    if (column.Value == typeof(int))
                    {
                        sqb.TypeInteger();
                    }
                    else if (column.Value == typeof(string))
                    {
                        sqb.TypeText();
                    }
                    else if (column.Value == typeof(double))
                    {
                        sqb.TypeReal();
                    }
                    else if (column.Value == typeof(float))
                    {
                        sqb.TypeReal();
                    }
                    else if (column.Value == typeof(DateTime))
                    {
                        sqb.TypeText();
                    }
                    else
                    {
                        throw new NotSupportedException(column.Value.Name + " Datatype not supported");
                    }

                    if (iter.Equals(0))
                    {
                        sqb.ParamPrimaryKey();
                    }
                    else
                    {
                        sqb.ParamNot().Null();
                    }

                    paramList.Add(sqb.Flush());
                    iter++;
                }
                string values = sqb.Brackets_Multiple(paramList, false).Flush();
                sqb.Create().Table().IfNotExists().AddValue(table.Value.TableName).AddValue(values);
                tableQueries.Add(sqb.ToString());
                tableQueries.Add(CreateTriggerTable(table.Value.TableName));
                tableQueries.Add(InsertStartValueTriggerTable(table.Value.TableName));
                tableQueries.Add(CreateCounterAddTriger(table.Value.TableName));
                tableQueries.Add(CreateCounterSubTriger(table.Value.TableName));
            }
            return(tableQueries);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Inserts the first and only row of this table. Bascially its set's the rowCOunt to 0.
        /// <para/>
        /// SQL query Example: <para/>
        /// insert into table1Count (rowCount) values (0);
        /// </summary>
        /// <param name="tableName">The name of the table.</param>
        /// <returns>The finished sql string.</returns>
        private string InsertStartValueTriggerTable(string tableName)
        {
            SQLQueryBuilder sqb   = new SQLQueryBuilder();
            string          param = sqb.AddValue("id").Comma().AddValue("rowCount").Flush();

            sqb.Insert().Or().Ignore().Into().AddValue(tableName + "_count").Brackets(param).Values().Brackets("1, 0");
            return(sqb.ToString());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates the trigger table where the row count is automatically update through a custom insert and delete trigger.
        /// <para/>
        /// SQL query Example: <para/>
        /// create table if not exists tableName_count (id integer primary key, number int);
        /// </summary>
        /// <param name="tableName">The name of the table.</param>
        /// <returns>The finished sql string.</returns>
        private string CreateTriggerTable(string tableName)
        {
            SQLQueryBuilder sqb   = new SQLQueryBuilder();
            string          param = sqb.Brackets(sqb.AddValue("id").TypeInteger().ParamPrimaryKey().Comma().AddValue("rowCount").TypeInteger().Flush()).Flush();

            sqb.Create().Table().IfNotExists().AddValue(tableName + "_count").AddValue(param);
            return(sqb.ToString());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Updates the given columns with the given id in the first column.
        /// Each row of rowsToUpdate must have the same size as rowsData.
        /// </summary>
        /// <param name="tableName">The table where rows should be updated.</param>
        /// <param name="rowsToUpdate">The rows with the name and data type to update.</param>
        /// <param name="rowsData">The rows with all column data which should be updated.</param>
        public override void UpdateTable(string tableName, List <Dictionary <string, Type> > rowsToUpdate, List <List <object> > rowsData)
        {
            int           rowIter   = 0;
            List <string> queryList = new List <string>();

            foreach (Dictionary <string, Type> row in rowsToUpdate)
            {
                SQLQueryBuilder sqb        = new SQLQueryBuilder();
                List <object>   columnData = rowsData[rowIter];
                List <string>   columns    = new List <string>();

                if (!row.Count.Equals(columnData.Count))
                {
                    throw new InvalidDataException(rowIter.ToString() + ". row size from rowsToUpdate doesn't match current row size from rowsData");
                }

                int columnIter = 0;
                foreach (KeyValuePair <string, Type> column in row)
                {
                    if (columnIter.Equals(0))
                    {
                        columnIter++;
                        continue;
                    }

                    if (!columnData[columnIter].GetType().Equals(column.Value))
                    {
                        throw new TypeLoadException("Type of the data doesn't match the columns type!");
                    }

                    if (column.Value == typeof(int))
                    {
                        sqb.AddValue(columnData[columnIter].ToString());
                    }
                    else if (column.Value == typeof(string))
                    {
                        sqb.Apostrophe(sqb.AddValue(columnData[columnIter].ToString()).Flush());
                    }
                    else if (column.Value == typeof(double))
                    {
                        sqb.AddValue(columnData[columnIter].ToString().Replace(',', '.'));
                    }
                    else if (column.Value == typeof(float))
                    {
                        sqb.AddValue(columnData[columnIter].ToString().Replace(',', '.'));
                    }
                    else if (column.Value == typeof(DateTime))
                    {
                        DateTime convertTime = (DateTime)columnData[columnIter];
                        sqb.Apostrophe(sqb.AddValue(convertTime.ToString(_stringFormat)).Flush());
                    }
                    else
                    {
                        throw new NotSupportedException(column.Value.Name + " Datatype not supported");
                    }

                    string value = sqb.Flush();

                    sqb.AddValue(column.Key).Equal().AddValue(value);

                    columns.Add(sqb.Flush());
                    columnIter++;
                }
                sqb.Update().AddValue(tableName).Set().AddValues(columns).Where().AddValue(row.First().Key).Equal().AddValue(columnData[0].ToString());
                queryList.Add(sqb.ToString());
                rowIter++;
            }
            CommitBatchQuery(queryList);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Inserts the given objects into the given table. The first object "id" is ignored due to the auto increment,
        /// <para/>
        /// SQL query Example: <para/>
        /// INSERT INTO table3 (Name, Date, value) VALUES ('John Doe', '2018-12-06 12:01:16.767', 22.5);
        /// </summary>
        /// <param name="tableName">The name of the table to insert rows to.</param>
        /// <param name="rows">A list of rows with all column objects to insert.</param>
        /// <param name="tables">All saved tables.</param>
        public override void InsertIntoTable(string tableName, Dictionary <string, Table> tables, List <List <object> > rows)
        {
            List <string> queryList = new List <string>();

            foreach (List <object> row in rows)
            {
                SQLQueryBuilder sqb           = new SQLQueryBuilder();
                List <string>   firstBracket  = new List <string>();
                List <string>   secondBracket = new List <string>();
                int             listIter      = 0;
                foreach (KeyValuePair <string, Type> column in tables[tableName].Columns)
                {
                    if (listIter.Equals(0))
                    {
                        listIter++;
                        continue;
                    }

                    if (!row[listIter].GetType().Equals(column.Value))
                    {
                        throw new TypeLoadException("Type of the data doesn't match the columns type!");
                    }

                    firstBracket.Add(sqb.AddValue(column.Key).Flush());

                    if (column.Value == typeof(int))
                    {
                        sqb.AddValue(row[listIter].ToString());
                    }
                    else if (column.Value == typeof(string))
                    {
                        sqb.Apostrophe(sqb.AddValue(row[listIter].ToString()).Flush());
                    }
                    else if (column.Value == typeof(double))
                    {
                        sqb.AddValue(row[listIter].ToString().Replace(',', '.'));
                    }
                    else if (column.Value == typeof(float))
                    {
                        sqb.AddValue(row[listIter].ToString().Replace(',', '.'));
                    }
                    else if (column.Value == typeof(DateTime))
                    {
                        DateTime convertTime = (DateTime)row[listIter];
                        sqb.Apostrophe(sqb.AddValue(convertTime.ToString(_stringFormat)).Flush());
                    }
                    else
                    {
                        throw new NotSupportedException(column.Value.Name + " Datatype not supported");
                    }

                    secondBracket.Add(sqb.Flush());
                    listIter++;
                }
                string columnNames  = sqb.Brackets_Multiple(firstBracket, false).Flush();
                string columnValues = sqb.Brackets_Multiple(secondBracket, false).Flush();
                sqb.InsertInto().AddValue(tableName).AddValue(columnNames).Values().AddValue(columnValues);
                queryList.Add(sqb.ToString());
            }
            CommitBatchQuery(queryList);
        }
        /// <summary>
        /// Generates a querie list to create tables.
        /// <para/>
        /// </summary>
        /// <returns>Returns a list of queries to create the local tables.</returns>
        private List <string> CreateTableQueries(ConcurrentDictionary <string, Table> tables)
        {
            try
            {
                List <string> tableQueries = new List <string>();
                foreach (KeyValuePair <string, Table> table in tables)
                {
                    SQLQueryBuilder sqb       = new SQLQueryBuilder();
                    List <string>   paramList = new List <string>();

                    int iter = 0;
                    foreach (KeyValuePair <string, Type> column in table.Value.Columns)
                    {
                        sqb.AddValue(column.Key);

                        if (iter.Equals(0))
                        {
                            sqb.AddValue("AUTOINCREMENT").ParamPrimaryKey();
                            paramList.Add(sqb.Flush());
                            iter++;
                            continue;
                        }

                        if (column.Value == typeof(int))
                        {
                            sqb.TypeInteger();
                        }
                        else if (column.Value == typeof(string))
                        {
                            sqb.TypeText();
                        }
                        else if (column.Value == typeof(double))
                        {
                            sqb.TypeReal();
                        }
                        else if (column.Value == typeof(float))
                        {
                            sqb.TypeReal();
                        }
                        else if (column.Value == typeof(DateTime))
                        {
                            sqb.TypeDateTime();
                        }
                        else
                        {
                            throw new NotSupportedException(column.Value.Name + " Datatype not supported");
                        }

                        sqb.ParamNot().Null();

                        paramList.Add(sqb.Flush());
                        iter++;
                    }
                    string values = sqb.Brackets_Multiple(paramList, false).Flush();
                    sqb.Create().Table().AddValue(table.Value.TableName).AddValue(values);
                    tableQueries.Add(sqb.ToString());
                }
                return(tableQueries);
            }
            catch (Exception e)
            {
                throw e;
            }
        }