dblinq-experiment

changeset 0:62f9a07e8d01

Simple LINQ to SQL experiment using DbLinq
author Emanuele Aina <em@nerd.ocracy.org>
date Thu Sep 03 16:17:40 2009 +0200 (11 months ago)
parents
children 248fb7a05b8d
files .hgignore Experiment.cs Makefile db/Db.cs lib/DbLinq.PostgreSql.dll lib/DbLinq.PostgreSql.dll.mdb lib/DbLinq.dll lib/DbLinq.dll.mdb lib/Npgsql.dll lib/Npgsql.dll.mdb sql/experiment.sql
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Thu Sep 03 16:17:40 2009 +0200
     1.3 @@ -0,0 +1,2 @@
     1.4 +build/
     1.5 +.*\.swp
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Experiment.cs	Thu Sep 03 16:17:40 2009 +0200
     2.3 @@ -0,0 +1,30 @@
     2.4 +using System;
     2.5 +using DbLinq.Data.Linq;
     2.6 +using DbLinq.PostgreSql;
     2.7 +using Npgsql;
     2.8 +
     2.9 +namespace Experiment.Db
    2.10 +{
    2.11 +    public class Experiment
    2.12 +    {
    2.13 +        static void Main(string[] args)
    2.14 +        {
    2.15 +            string connString = "server=localhost;user id=test; database=experiments";
    2.16 +
    2.17 +            using (var conn = new NpgsqlConnection(connString))
    2.18 +            using (var db = new DataContext(conn, new PgsqlVendor()))
    2.19 +            {
    2.20 +                var parent = new Parent() { Name = "A" };
    2.21 +                db.GetTable<Parent>().InsertOnSubmit(parent);
    2.22 +
    2.23 +                var numbers = new int[]{ 1, 2, 3 };
    2.24 +                foreach(var i in numbers) {
    2.25 +                    var child = new Child() { Number = i, Parent = parent };
    2.26 +                    db.GetTable<Child>().InsertOnSubmit(child);
    2.27 +                }
    2.28 +                db.SubmitChanges();
    2.29 +            }
    2.30 +        }
    2.31 +
    2.32 +    }
    2.33 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/Makefile	Thu Sep 03 16:17:40 2009 +0200
     3.3 @@ -0,0 +1,33 @@
     3.4 +SOURCES := Experiment.cs db/Db.cs
     3.5 +BUILDDIR := ./build
     3.6 +LIBDIR := ./lib
     3.7 +
     3.8 +EXE := DbLinqExperiment.exe
     3.9 +DLLS := DbLinq.dll DbLinq.PostgreSql.dll Npgsql.dll
    3.10 +PACKAGES := System.Core System.Data System.Data.Linq
    3.11 +
    3.12 +BUILDDLLS := $(addprefix $(BUILDDIR)/, $(DLLS))
    3.13 +REFERENCES := $(addprefix -r:, $(PACKAGES) $(BUILDDLLS))
    3.14 +
    3.15 +MCS := gmcs
    3.16 +MONO := mono --debug
    3.17 +
    3.18 +all: $(BUILDDIR)/$(EXE)
    3.19 +
    3.20 +run: $(BUILDDIR)/$(EXE)
    3.21 +	@${MONO} $(BUILDDIR)/$(EXE)
    3.22 +
    3.23 +clean:
    3.24 +	-rm -Rf $(BUILDDIR)
    3.25 +
    3.26 +$(BUILDDIR):
    3.27 +	mkdir -p $@ 
    3.28 +
    3.29 +$(BUILDDIR)/%.dll: $(LIBDIR)/%.dll $(BUILDDIR)
    3.30 +	cp $< $@ 
    3.31 +	-cp $<.mdb $@.mdb
    3.32 +
    3.33 +$(BUILDDIR)/$(EXE): $(SOURCES) $(BUILDDLLS) $(BUILDDIR)
    3.34 +	$(MCS) $(REFERENCES) -out:"$@" $(SOURCES)
    3.35 +
    3.36 +.PHONY: all run clean
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/db/Db.cs	Thu Sep 03 16:17:40 2009 +0200
     4.3 @@ -0,0 +1,47 @@
     4.4 +using System;
     4.5 +using System.Diagnostics;
     4.6 +using System.Collections.Generic;
     4.7 +using System.Text;
     4.8 +using System.Linq;
     4.9 +//using System.Data.Linq;
    4.10 +using System.Data.Linq.Mapping;
    4.11 +using System.Reflection;
    4.12 +using DbLinq.Data.Linq;
    4.13 +using DbLinq.PostgreSql;
    4.14 +
    4.15 +namespace Experiment.Db
    4.16 +{
    4.17 +
    4.18 +    [Table(Name = "parent")]
    4.19 +    public partial class Parent
    4.20 +    {
    4.21 +        public Parent() { }
    4.22 +
    4.23 +        [Column(Name = "id", DbType = "integer(32,0)", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, Expression = "nextval('parent_id_seq')")]
    4.24 +        public int Id { get; set; }
    4.25 +
    4.26 +        [Column(Name = "name", DbType = "text")]
    4.27 +        public string Name { get; set; }
    4.28 +
    4.29 +        [Association(OtherKey = "ParentId", Name = "child_parent_fkey", IsForeignKey = true)]
    4.30 +        public System.Data.Linq.EntitySet<Child> Children { get; set; }
    4.31 +    }
    4.32 +
    4.33 +    [Table(Name = "child")]
    4.34 +    public partial class Child
    4.35 +    {
    4.36 +        public Child() { }
    4.37 +
    4.38 +        [Column(Name = "id", DbType = "integer(32,0)", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, Expression = "nextval('child_id_seq')")]
    4.39 +        public int Id { get; set; }
    4.40 +
    4.41 +        [Column(Name = "number", DbType = "integer(32,0)")]
    4.42 +        public int Number { get; set; }
    4.43 +
    4.44 +        [Column(Name = "parent", DbType = "integer(32,0)")]
    4.45 +        public int ParentId { get; set; }
    4.46 +
    4.47 +        [Association(ThisKey = "ParentId", Name = "child_parent_fkey")]
    4.48 +        public Parent Parent { get; set; }
    4.49 +    }
    4.50 +}
     5.1 Binary file lib/DbLinq.PostgreSql.dll has changed
     6.1 Binary file lib/DbLinq.PostgreSql.dll.mdb has changed
     7.1 Binary file lib/DbLinq.dll has changed
     8.1 Binary file lib/DbLinq.dll.mdb has changed
     9.1 Binary file lib/Npgsql.dll has changed
    10.1 Binary file lib/Npgsql.dll.mdb has changed
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/sql/experiment.sql	Thu Sep 03 16:17:40 2009 +0200
    11.3 @@ -0,0 +1,20 @@
    11.4 +DROP TABLE IF EXISTS child;
    11.5 +DROP TABLE IF EXISTS parent;
    11.6 +
    11.7 +CREATE TABLE parent
    11.8 +(
    11.9 +    id serial,
   11.10 +    name text,
   11.11 +
   11.12 +    PRIMARY KEY (id)
   11.13 +);
   11.14 +
   11.15 +CREATE TABLE child
   11.16 +(
   11.17 +    id serial,
   11.18 +    number int,
   11.19 +
   11.20 +    parent int REFERENCES parent (id),
   11.21 +
   11.22 +    PRIMARY KEY (id)
   11.23 +);

To download these repositories, get Mercurial and then type something like:

hg clone http://techn.ocracy.org/repository-name/

You can also click the "zip" or "gz" links to get an archive of the latest revision without installing anything.

The change logs of the repositories are aggregated at techn.ocracy.org/planet.

We have also some darcs repositories at techn.ocracy.org/darcs.