Exemplo n.º 1
0
        internal override string generateCreateSql()
        {
            PostgreSqlFieldInfo autoInc = null;
            string ret = generateCreateTableSql(ref autoInc) + ";\r\n";

            if (autoInc != null)
            {
                // postgresql requires sequence before table creation because of how we're making the create table (with nextval('<sequenc_name>') in primary key field)
                ret = generateCreateSequenceSql(autoInc) + ";\r\n" + ret;
            }
            return(ret);
        }
Exemplo n.º 2
0
        private string generateCreateTableSql(ref PostgreSqlFieldInfo autoIncrementField)
        {
            /*
             * create table accessoin
             * ( accession_id       integer   PRIMARY KEY DEFAULT nextval('sq_<tablename>'),
             *   accession_number_part1   varchar(50) NOT NULL,
             *   ...
             * );
             */

            autoIncrementField = null;
            StringBuilder sb    = new StringBuilder();
            StringBuilder sbPKs = new StringBuilder();

            sb.Append("CREATE TABLE ")
            .Append(TableName)
            .AppendLine(" (");

            foreach (PostgreSqlFieldInfo fi in Fields)
            {
                sb.Append(fi.GenerateCreateSql());
                if (fi.IsPrimaryKey)
                {
                    if (sbPKs.Length > 0)
                    {
                        sbPKs.Append(", ");
                    }
                    sbPKs.Append(fi.Name);
                }
                if (fi.IsAutoIncrement && autoIncrementField == null)
                {
                    // remember for later so we can create a sequence to match the primary key (if any)
                    autoIncrementField = fi;
                }
            }
            sb.Length -= 4;
            sb.Append(")");

            string ret = sb.ToString();

            return(ret);
        }