Example #1
0
        public override void PrimeOutput(int outputs, int[] outputIDs, PipelineBuffer[] buffers)
        {
            //identify buffers
            PipelineBuffer errorBuffer   = null;
            PipelineBuffer defaultBuffer = null;

            for (int x = 0; x < outputs; x++)
            {
                if (outputIDs[x] == errorOutputID)
                {
                    errorBuffer = buffers[x];
                }
                else
                {
                    defaultBuffer = buffers[x];
                }
            }

            //get ogrlayer and ogrlayer feature definition
            Layer OGRLayer;
            bool  isSQLLayer = (!(ComponentMetaData.CustomPropertyCollection["SQL Statement"].Value == null || ComponentMetaData.CustomPropertyCollection["SQL Statement"].Value.ToString() == string.Empty));

            if (isSQLLayer)
            {
                OGRLayer = getSQLLayer();
            }
            else
            {
                OGRLayer = getLayer();
            }
            Feature     OGRFeature;
            FeatureDefn OGRFeatureDef = OGRLayer.GetLayerDefn();

            //initialize columnInfo object
            columnInfo ci = new columnInfo();

            //for each row in ogrlayer add row to output buffer
            while ((OGRFeature = OGRLayer.GetNextFeature()) != null)
            {
                try
                {
                    defaultBuffer.AddRow();

                    //set buffer column values
                    for (int i = 0; i < this.columnInformation.Count; i++)
                    {
                        ci = (columnInfo)this.columnInformation[i];

                        if (ci.geom)
                        {
                            Geometry geom = OGRFeature.GetGeometryRef();
                            if (geom != null)
                            {
                                byte[] geomBytes = new byte[geom.WkbSize()];
                                geom.ExportToWkb(geomBytes);
                                defaultBuffer.AddBlobData(ci.bufferColumnIndex, geomBytes);
                            }
                        }
                        else
                        {
                            int       OGRFieldIndex = OGRFeatureDef.GetFieldIndex(ci.columnName);
                            FieldDefn OGRFieldDef   = OGRFeatureDef.GetFieldDefn(OGRFieldIndex);
                            FieldType OGRFieldType  = OGRFieldDef.GetFieldType();

                            //declare datetime variables
                            int      pnYear, pnMonth, pnDay, pnHour, pnMinute, pnTZFlag;
                            float    pnSecond;
                            DateTime dt;
                            TimeSpan ts;

                            switch (OGRFieldType)
                            {
                            //case FieldType.OFTBinary:
                            //    break;
                            case FieldType.OFTDate:
                                OGRFeature.GetFieldAsDateTime(OGRFieldIndex, out pnYear, out pnMonth, out pnDay, out pnHour, out pnMinute, out pnSecond, out pnTZFlag);
                                dt = new DateTime(pnYear, pnMonth, pnDay);
                                defaultBuffer.SetDate(ci.bufferColumnIndex, dt);
                                break;

                            case FieldType.OFTDateTime:
                                OGRFeature.GetFieldAsDateTime(OGRFieldIndex, out pnYear, out pnMonth, out pnDay, out pnHour, out pnMinute, out pnSecond, out pnTZFlag);
                                dt = new DateTime(pnYear, pnMonth, pnDay, pnHour, pnMinute, (int)pnSecond);
                                //set time zone?
                                defaultBuffer.SetDateTime(ci.bufferColumnIndex, dt);
                                break;

                            case FieldType.OFTInteger:
                                defaultBuffer.SetInt32(ci.bufferColumnIndex, OGRFeature.GetFieldAsInteger(OGRFieldIndex));
                                break;

                            case FieldType.OFTInteger64:
                                defaultBuffer.SetInt64(ci.bufferColumnIndex, OGRFeature.GetFieldAsInteger(OGRFieldIndex));
                                break;

                            case FieldType.OFTReal:
                                defaultBuffer.SetDouble(ci.bufferColumnIndex, OGRFeature.GetFieldAsDouble(OGRFieldIndex));
                                break;

                            case FieldType.OFTTime:
                                OGRFeature.GetFieldAsDateTime(OGRFieldIndex, out pnYear, out pnMonth, out pnDay, out pnHour, out pnMinute, out pnSecond, out pnTZFlag);
                                ts = new TimeSpan(pnHour, pnMinute, (int)pnSecond);
                                defaultBuffer.SetTime(ci.bufferColumnIndex, ts);
                                break;

                            case FieldType.OFTString:
                            default:
                                defaultBuffer.SetString(ci.bufferColumnIndex, OGRFeature.GetFieldAsString(OGRFieldIndex));
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //redirect to error buffer
                    if (ci.errorDisposition == DTSRowDisposition.RD_RedirectRow)
                    {
                        // Add a row to the error buffer.
                        errorBuffer.AddRow();

                        // Set the error information.
                        int errorCode = System.Runtime.InteropServices.Marshal.GetHRForException(ex);
                        errorBuffer.SetErrorInfo(errorOutputID, errorCode, ci.lineageID);

                        // Remove the row that was added to the default buffer.
                        defaultBuffer.RemoveRow();
                    }
                    //fail component
                    else if (ci.errorDisposition == DTSRowDisposition.RD_FailComponent || ci.errorDisposition == DTSRowDisposition.RD_NotUsed)
                    {
                        ComponentMetaData.FireError(0, "primeoutput failure", ex.ToString(), string.Empty, 0, out cancel);
                        throw;
                    }
                }
            }
            //set end of rowset for buffers
            if (defaultBuffer != null)
            {
                defaultBuffer.SetEndOfRowset();
            }

            if (errorBuffer != null)
            {
                errorBuffer.SetEndOfRowset();
            }

            //clean up layer object
            if (isSQLLayer)
            {
                this.OGRDataSource.ReleaseResultSet(OGRLayer);
            }
        }
Example #2
0
        //Checks if output metadata columns match ogrlayer source columns
        public override bool isMetadataValid()
        {
            //get default output
            IDTSOutput100 output = null;

            for (int i = 0; i < ComponentMetaData.OutputCollection.Count; i++)
            {
                if (!ComponentMetaData.OutputCollection[i].IsErrorOut)
                {
                    output = ComponentMetaData.OutputCollection[i];
                }
            }

            //get ogrlayer and layer definition
            Layer OGRLayer;
            bool  isSQLLayer = (!(ComponentMetaData.CustomPropertyCollection["SQL Statement"].Value == null || ComponentMetaData.CustomPropertyCollection["SQL Statement"].Value.ToString() == string.Empty));

            if (isSQLLayer)
            {
                OGRLayer = getSQLLayer();
            }
            else
            {
                OGRLayer = getLayer();
            }

            FeatureDefn OGRFeatureDef = OGRLayer.GetLayerDefn();

            // Get Geometry column name
            string geomtryColumn = (OGRLayer.GetGeometryColumn() != "") ? OGRLayer.GetGeometryColumn() : "GEOMETRY";

            // Begin checks
            bool isValid = true;

            if (OGRFeatureDef.GetFieldCount() + 1 == output.OutputColumnCollection.Count)
            {
                int i = 0;
                do
                {
                    //check if ogr field exists by name
                    IDTSOutputColumn100 col = output.OutputColumnCollection[i];
                    int OGRFieldIndex       = OGRFeatureDef.GetFieldIndex(col.Name);
                    if (OGRFieldIndex == -1)
                    {
                        //set isValid false if not geom column
                        if ((col.Name != geomtryColumn) ||
                            (col.DataType != DataType.DT_IMAGE) ||
                            ((string)ComponentMetaData.CustomPropertyCollection["Geometry Column"].Value != geomtryColumn))
                        {
                            isValid = false;
                        }
                    }
                    else
                    {
                        //is isVaild to false if field types don't match
                        FieldDefn OGRFieldDef  = OGRFeatureDef.GetFieldDefn(OGRFieldIndex);
                        FieldType OGRFieldType = OGRFieldDef.GetFieldType();
                        if ((col.DataType != this.OGRTypeToBufferType(OGRFieldType)) &&
                            (!(OGRFieldType == FieldType.OFTString && col.DataType == DataType.DT_NTEXT)))    // special case where lenth not provided by OFTString
                        {
                            isValid = false;
                        }
                    }
                    i++;
                } while (isValid && i < output.OutputColumnCollection.Count);
            }
            else
            {
                isValid = false;
            }

            //Manualy call release on OGR layer sql result set if needed
            if (isSQLLayer)
            {
                this.OGRDataSource.ReleaseResultSet(OGRLayer);
            }
            return(isValid);
        }
Example #3
0
        public override void ReinitializeMetaData()
        {
            base.ReinitializeMetaData();

            //get default output
            IDTSOutput100 defaultOutput = null;

            this.GetErrorOutputInfo(ref errorOutputID, ref errorOutputIndex);
            foreach (IDTSOutput100 output in ComponentMetaData.OutputCollection)
            {
                if (output.ID != errorOutputID)
                {
                    defaultOutput = output;
                }
            }

            if (this.isConnected)
            {
                defaultOutput.OutputColumnCollection.RemoveAll();
                defaultOutput.ExternalMetadataColumnCollection.RemoveAll();

                //get ogrlayer and layer definition
                Layer OGRLayer;
                bool  isSQLLayer = (!(ComponentMetaData.CustomPropertyCollection["SQL Statement"].Value == null || ComponentMetaData.CustomPropertyCollection["SQL Statement"].Value.ToString() == string.Empty));
                if (isSQLLayer)
                {
                    OGRLayer = getSQLLayer();
                }
                else
                {
                    OGRLayer = getLayer();
                }
                FeatureDefn OGRFeatureDef = OGRLayer.GetLayerDefn();

                //for each field in ogrlayer add output column and external metadata
                int i = 0;
                while (i < OGRFeatureDef.GetFieldCount())
                {
                    //map OGR field type to SSIS data type
                    FieldDefn OGRFieldDef    = OGRFeatureDef.GetFieldDefn(i);
                    DataType  BufferDataType = this.OGRTypeToBufferType(OGRFieldDef.GetFieldType());
                    int       length         = 0;
                    int       precision      = OGRFieldDef.GetWidth();
                    int       scale          = OGRFieldDef.GetPrecision();
                    int       codepage       = 1252;

                    switch (BufferDataType)
                    {
                    case DataType.DT_WSTR:
                        length    = precision;
                        codepage  = 0;
                        precision = 0;
                        scale     = 0;
                        //check for length == 0
                        if (length == 0)
                        {
                            //BufferDataType = DataType.DT_NTEXT;
                            length = 4000;
                        }
                        break;

                    default:
                        length    = 0;
                        precision = 0;
                        codepage  = 0;
                        scale     = 0;
                        break;
                    }

                    //create column metadata
                    IDTSOutputColumn100 col = defaultOutput.OutputColumnCollection.New();
                    col.Name = OGRFieldDef.GetName();
                    col.ErrorRowDisposition      = DTSRowDisposition.RD_FailComponent;
                    col.TruncationRowDisposition = DTSRowDisposition.RD_FailComponent;
                    col.SetDataTypeProperties(BufferDataType, length, precision, scale, codepage);

                    //create external metadata
                    IDTSExternalMetadataColumn100 ecol = defaultOutput.ExternalMetadataColumnCollection.New();
                    ecol.Name      = col.Name;
                    ecol.DataType  = col.DataType;
                    ecol.Precision = col.Precision;
                    ecol.Length    = col.Length;
                    ecol.Scale     = col.Scale;
                    ecol.CodePage  = col.CodePage;

                    col.ExternalMetadataColumnID = ecol.ID;

                    i++;
                }

                //get geometry column
                string geomtryColumn = (OGRLayer.GetGeometryColumn() != "") ? OGRLayer.GetGeometryColumn() : "GEOMETRY";

                //add geom output column
                IDTSOutputColumn100 geomCol = defaultOutput.OutputColumnCollection.New();
                geomCol.Name = geomtryColumn;
                geomCol.ErrorRowDisposition      = DTSRowDisposition.RD_FailComponent;
                geomCol.TruncationRowDisposition = DTSRowDisposition.RD_FailComponent;
                geomCol.SetDataTypeProperties(DataType.DT_IMAGE, 0, 0, 0, 0);

                //add geom external metadata
                IDTSExternalMetadataColumn100 egeomCol = defaultOutput.ExternalMetadataColumnCollection.New();
                egeomCol.Name      = geomCol.Name;
                egeomCol.DataType  = geomCol.DataType;
                egeomCol.Precision = geomCol.Precision;
                egeomCol.Length    = geomCol.Length;
                egeomCol.Scale     = geomCol.Scale;
                egeomCol.CodePage  = geomCol.CodePage;

                //map column metadata to external column metadata
                geomCol.ExternalMetadataColumnID = egeomCol.ID;

                //set geometry column custom property
                ComponentMetaData.CustomPropertyCollection["Geometry Column"].Value = geomtryColumn;

                if (isSQLLayer)
                {
                    this.OGRDataSource.ReleaseResultSet(OGRLayer);
                }
            }
        }