Ejemplo n.º 1
0
        public static string GetSelectSql(this TableMapping table)
        {
            var sb = new StringBuilder();

            sb.Append("SELECT * FROM ");
            sb.Append(Quote(table.TableName));
            sb.Append(" WHERE ");

            bool first = true;

            foreach (var column in table.PrimaryKey.Columns)
            {
                if (!first)
                {
                    sb.AppendLine(" AND ");
                }
                sb.Append(Quote(column.Name));
                sb.Append(" = ?");
                first = false;
            }

            return(sb.ToString());
        }
        internal IEnumerable ExecuteDeferredQuery(TableMapping map, DbCommand cmd)
        {
            TraceCommand(cmd);

            using (DbDataReader reader = cmd.ExecuteReader())
            {
                var cols = new TableMapping.Column[reader.FieldCount];

                for (int i = 0; i < cols.Length; i++)
                {
                    cols[i] = map.FindColumn(reader.GetName(i));
                }

                while (reader.Read())
                {
                    object obj = Activator.CreateInstance(map.MappedType);
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] != null)
                        {
                            object value = reader.GetValue(i);
                            cols[i].SetValue(obj, value);
                        }
                    }

                    var tracked = obj as ITrackConnection;
                    if (tracked != null)
                    {
                        tracked.Connection = this;
                    }

                    OnInstanceCreated(new InstanceCreatedEventArgs(obj));

                    yield return(obj);
                }
            }
        }
Ejemplo n.º 3
0
        public DbCommand GetUpdateCommand(TableMapping mapping, ConflictResolution extra, object[] args)
        {
            var  key     = new Tuple <TableMapping, ConflictResolution>(mapping, extra);
            bool created = false;
            var  command = updateCommands.GetOrAdd(key, tuple =>
            {
                created         = true;
                DbCommand cmd   = this.Connection.CreateCommand();
                cmd.CommandText = mapping.GetUpdateSql(extra);
                return(cmd);
            });

            if (SqliteSessionBase.Trace)
            {
                Debug.WriteLine("Creating update command: {0}", created);
            }

            if (args != null)
            {
                AddCommandParameters(command, args);
            }

            return(command);
        }
Ejemplo n.º 4
0
        public static string GetCreateSql(this TableMapping table)
        {
            var sb = new StringBuilder();

            sb.Append("CREATE ");
            if (table.Virtual != null)
            {
                sb.Append("VIRTUAL ");
            }
            sb.Append("TABLE ");
            sb.Append(Quote(table.TableName));
            if (table.Virtual != null)
            {
                sb.Append(" USING ");
                sb.Append(table.Virtual.ModuleName);
            }
            sb.Append(" (");
            sb.AppendLine();
            bool first = true;

            foreach (var column in table.Columns)
            {
                if (!first)
                {
                    sb.AppendLine(",");
                }
                sb.Append(column.GetCreateSql(table));
                first = false;
            }
            if (table.Virtual != null && table.Tokenizer != null)
            {
                sb.AppendLine(",");
                sb.Append("tokenize=");
                sb.Append(table.Tokenizer.FullValue);
            }
            if (table.PrimaryKey != null && table.PrimaryKey.Columns.Length > 1)
            {
                sb.AppendLine(",");
                sb.Append(table.PrimaryKey.GetCreateSql());
            }
            if (table.ForeignKeys.Any())
            {
                sb.AppendLine(",");
                first = true;
                foreach (var key in table.ForeignKeys)
                {
                    if (!first)
                    {
                        sb.AppendLine(",");
                    }
                    sb.Append(key.GetCreateSql());
                    first = false;
                }
            }
            if (table.Checks.Any())
            {
                sb.AppendLine(",");
                first = true;
                foreach (var check in table.Checks)
                {
                    if (!first)
                    {
                        sb.AppendLine(",");
                    }
                    sb.Append("CHECK (");
                    sb.Append(check);
                    sb.Append(")");
                    first = false;
                }
            }
            sb.AppendLine();
            sb.Append(");");
            return(sb.ToString());
        }
Ejemplo n.º 5
0
 private TableQuery(SqliteSession conn, TableMapping table)
 {
     Session = conn;
     Table   = table;
 }
Ejemplo n.º 6
0
 private TableQuery(SqliteSessionBase session, TableMapping table)
 {
     this.Session = session;
     this.Table   = table;
 }
 internal IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map, DbCommand cmd)
 {
     return(from object result in this.ExecuteDeferredQuery(map, cmd)
            select(T) result);
 }
 internal IList ExecuteQuery(TableMapping map, DbCommand cmd)
 {
     return(ExecuteDeferredQuery(map, cmd).Cast <object>().ToList());
 }
 internal List <T> ExecuteQuery <T>(TableMapping map, DbCommand cmd)
 {
     return(ExecuteDeferredQuery <T>(map, cmd).ToList());
 }
 /// <summary>
 /// Checks to see if a particular table exists.
 /// </summary>
 /// <param name="map">The table mapping to use.</param>
 /// <returns>
 /// True if the table exists in the database, otherwise False.
 /// </returns>
 private bool TableExists(TableMapping map)
 {
     return(this.TableExists(map.TableName));
 }