Beispiel #1
0
        /// <summary>
        /// To Sqlite column type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static SqliteColumnType ToSqliteColumnType(string type)
        {
            var columnType       = type.ToUpper();
            var columnSqliteType = new SqliteColumnType();

            switch (columnType)
            {
            case "INTEGER":
            case "TINYINT":
            case "SMALLINT":
            case "MEDIUMINT":
            case "BIGINT":
            case "UNSIGNED BIG INT":
            case "INT2":
            case "INT8":
            case "INT":
                columnSqliteType = SqliteColumnType.Integer;
                break;

            case "CLOB":
            case "TEXT":
                columnSqliteType = SqliteColumnType.Text;
                break;

            case "BLOB":
                columnSqliteType = SqliteColumnType.Blob;
                break;

            case "REAL":
            case "DOUBLE":
            case "DOUBLE PRECISION":
            case "FLOAT":
                columnSqliteType = SqliteColumnType.Real;
                break;

            case "NUMERIC":
                columnSqliteType = SqliteColumnType.Numeric;
                break;

            case "BOOLEAN":
                columnSqliteType = SqliteColumnType.Boolean;
                break;

            case "DATE":
            case "DATETIME":
                columnSqliteType = SqliteColumnType.Date;
                break;

            default:     // look for fringe cases that need logic
                if (columnType.StartsWith("CHARACTER"))
                {
                    columnSqliteType = SqliteColumnType.Text;
                }
                if (columnType.StartsWith("VARCHAR"))
                {
                    columnSqliteType = SqliteColumnType.Text;
                }
                if (columnType.StartsWith("VARYING CHARACTER"))
                {
                    columnSqliteType = SqliteColumnType.Text;
                }
                if (columnType.StartsWith("NCHAR"))
                {
                    columnSqliteType = SqliteColumnType.Text;
                }
                if (columnType.StartsWith("NATIVE CHARACTER"))
                {
                    columnSqliteType = SqliteColumnType.Text;
                }
                if (columnType.StartsWith("NVARCHAR"))
                {
                    columnSqliteType = SqliteColumnType.Text;
                }
                if (columnType.StartsWith("NVARCHAR"))
                {
                    columnSqliteType = SqliteColumnType.Text;
                }
                if (columnType.StartsWith("DECIMAL"))
                {
                    columnSqliteType = SqliteColumnType.Numeric;
                }
                break;
            }

            return(columnSqliteType);
        }
Beispiel #2
0
        public override IEnumerable <Dictionary <string, object> > ExecuteSqlReader(string sql, QueryParameterCollection parameters)
        {
            var stmt = CreateCommand(sql, parameters);

            try
            {
                for (;;)
                {
                    var returnCode = _sqlite3.step(stmt);

                    if (returnCode == SqliteReturnCode.Busy)
                    {
                        Task.Delay(100).Wait();
                        continue;
                    }

                    if (returnCode == SqliteReturnCode.Done)
                    {
                        break;
                    }

                    if (returnCode != SqliteReturnCode.Row)
                    {
                        throw new SqliteException(returnCode, _sqlite3.extended_errcode(DbHandle), _sqlite3.errmsg(DbHandle));
                    }

                    Dictionary <string, object> record = new Dictionary <string, object>();

                    for (int i = 0; i < _sqlite3.column_count(stmt); i++)
                    {
                        string fieldName = _sqlite3.column_name(stmt, i);

                        SqliteColumnType columnType = _sqlite3.column_type(stmt, i);

                        switch (columnType)
                        {
                        case SqliteColumnType.Blob:
                            record[fieldName] = _sqlite3.column_blob(stmt, i);
                            break;

                        case SqliteColumnType.Text:
                            record[fieldName] = _sqlite3.column_text(stmt, i);
                            break;

                        case SqliteColumnType.Float:
                            record[fieldName] = _sqlite3.column_double(stmt, i);
                            break;

                        case SqliteColumnType.Integer:
                            record[fieldName] = _sqlite3.column_int64(stmt, i);
                            break;

                        case SqliteColumnType.Null:
                            record[fieldName] = null;
                            break;
                        }
                    }

                    yield return(record);
                }
            }
            finally
            {
                _sqlite3.finalize(stmt);
            }
        }