示例#1
0
        public List <T> ExecuteQuery <T>(TableMapping map)
        {
            if (this._conn.Trace)
            {
                Console.WriteLine("Executing Query: " + this);
            }

            var r = new List <T>();

            var stmt = this.Prepare();

            var cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];

            for (int i = 0; i < cols.Length; i++)
            {
                var name = Marshal.PtrToStringUni(SQLite3.ColumnName16(stmt, i));
                cols[i] = map.FindColumn(name);
            }

            while (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                var obj = Activator.CreateInstance(map.MappedType);
                for (int i = 0; i < cols.Length; i++)
                {
                    if (cols[i] == null)
                    {
                        continue;
                    }
                    var colType = SQLite3.ColumnType(stmt, i);
                    var val     = this.ReadCol(stmt, i, colType, cols[i].ColumnType);
                    cols[i].SetValue(obj, val);
                }
                r.Add((T)obj);
            }

            this.Finalize(stmt);
            return(r);
        }
示例#2
0
        public static string SqlDecl(TableMapping.Column p)
        {
            string decl = "\"" + p.Name + "\" " + SqlType(p) + " ";

            if (p.IsPK)
            {
                decl += "primary key ";
            }
            if (p.IsAutoInc)
            {
                decl += "autoincrement ";
            }
            if (!p.IsNullable)
            {
                decl += "not null ";
            }
            if (!string.IsNullOrEmpty(p.Collation))
            {
                decl += "collate " + p.Collation + " ";
            }

            return decl;
        }
示例#3
0
 public static string SqlType(TableMapping.Column p)
 {
     var clrType = p.ColumnType;
     if (clrType == typeof(Boolean) || clrType == typeof(Byte) || clrType == typeof(UInt16) || clrType == typeof(SByte) || clrType == typeof(Int16) || clrType == typeof(Int32))
     {
         return "integer";
     }
     else if (clrType == typeof(UInt32) || clrType == typeof(Int64))
     {
         return "bigint";
     }
     else if (clrType == typeof(Single) || clrType == typeof(Double) || clrType == typeof(Decimal))
     {
         return "float";
     }
     else if (clrType == typeof(String))
     {
         int len = p.MaxStringLength;
         return "varchar(" + len + ")";
     }
     else if (clrType == typeof(DateTime))
     {
         return "datetime";
     }
     else if (clrType.IsEnum)
     {
         return "integer";
     }
     else if (clrType == typeof(byte[]))
     {
         return "blob";
     }
     else
     {
         throw new NotSupportedException("Don't know about " + clrType);
     }
 }
示例#4
0
 public TableQuery(SQLiteConnection conn)
 {
     this.Connection = conn;
     this.Table      = this.Connection.GetMapping(typeof(T));
 }
示例#5
0
 TableQuery(SQLiteConnection conn, TableMapping table)
 {
     this.Connection = conn;
     this.Table      = table;
 }
示例#6
0
        /// <summary>
        /// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?'
        /// in the command text for each of the arguments and then executes that command.
        /// It returns each row of the result using the specified mapping. This function is
        /// only used by libraries in order to query the database via introspection. It is
        /// normally not used.
        /// </summary>
        /// <param name="map">
        /// A <see cref="TableMapping"/> to use to convert the resulting rows
        /// into objects.
        /// </param>
        /// <param name="query">
        /// The fully escaped SQL.
        /// </param>
        /// <param name="args">
        /// Arguments to substitute for the occurences of '?' in the query.
        /// </param>
        /// <returns>
        /// An enumerable with one result for each row returned by the query.
        /// </returns>
        public List <object> Query(TableMapping map, string query, params object[] args)
        {
            var cmd = this.CreateCommand(query, args);

            return(cmd.ExecuteQuery <object>(map));
        }