Publish date: 2020-02-27

Reading of IsAutoIncrement attribute. (C#, FB3)

////////////////////////////////////////////////////////////////////////////////
//Samples for LCPI ADO.NET Data provider for OLEDB.
//                                                    ibprovider.com. 16.02.2020
using System;
using System.Diagnostics;
using lcpi.data.oledb;

using structure_lib=lcpi.lib.structure;

namespace Sample_0028{
////////////////////////////////////////////////////////////////////////////////
//class Program

class Program
{
 private const string c_cn_str
  ="provider=LCPI.IBProvider.5;"
  +"location=localhost:d:\\database\\ram\\ibp_test_fb30_d3.gdb;"
  +"dbclient_type=fb.direct;"
  +"user id=GAMER;"
  +"password=vermut;";

 //----------------------------------------------------------------------
 static int Main(string[] args)
 {
  int resultCode=0;

  OleDbConnection  cn=null;
  OleDbTransaction tr=null;
  OleDbCommand     cmd=null;
  OleDbDataReader  reader=null;

  try
  {
   cn=new OleDbConnection(c_cn_str);

   cn.Open();

   var dsinfo=cn.GetSchema(OleDbMetaDataCollectionNames.DataSourceInformation);

   Helper__CheckComponents(dsinfo);

   tr=cn.BeginTransaction();

   cmd=new OleDbCommand(null,cn,tr);

   //--------------------
   Console.WriteLine("Recreate TBL_WITH_IDENTITY ...");

   cmd.CommandText
    ="recreate table TBL_WITH_IDENTITY\n"
    +"(ID1 INTEGER GENERATED BY DEFAULT AS IDENTITY,\n"
    +" ID2 INTEGER)";

   cmd.ExecuteNonQuery();

   //--------------------
   Console.WriteLine("Commit retain ...");

   tr.CommitRetaining();

   //--------------------
   cmd.CommandText
    ="select ID1,ID2 from TBL_WITH_IDENTITY";

   reader
    =cmd.ExecuteReader(System.Data.CommandBehavior.SchemaOnly);

   var readerSchema=reader.GetSchemaTable();

   for(int iRow=0;iRow!=readerSchema.Rows.Count;++iRow)
   {
    var readerSchemaRow=readerSchema.Rows[iRow];

    Console.WriteLine
     ("[{0}] \"{1}\" IsAutoIncrement flag: {2}",
      iRow,
      readerSchemaRow[OleDbDataReaderSchemaColumnNames.ColumnName],
      readerSchemaRow[OleDbDataReaderSchemaColumnNames.IsAutoIncrement]);
   }//for i

   //--------------------
   Console.WriteLine("Commit ...");

   tr.Commit();
  }
  catch(Exception e)
  {
   resultCode=1;

   Console.WriteLine("");
   Console.WriteLine("ERROR: {0} - {1}",e.Source,e.Message);
  }//catch
  finally
  {
   structure_lib.DisposeUtils.Exec(ref reader);
   structure_lib.DisposeUtils.Exec(ref cmd);
   structure_lib.DisposeUtils.Exec(ref tr);
   structure_lib.DisposeUtils.Exec(ref cn);
  }//finally

  return resultCode;
 }//Main

 //-----------------------------------------------------------------------
 private static void Helper__CheckComponents(System.Data.DataTable dsinfo)
 {
  Debug.Assert(!Object.ReferenceEquals(dsinfo,null));

  var dsinfoRow=dsinfo.Rows[0];

  Helper__CheckComponents_DBMS(dsinfoRow);

  Helper__CheckComponents_Provider(dsinfoRow);
 }//Helper__CheckComponents

 //-----------------------------------------------------------------------
 private static void Helper__CheckComponents_DBMS(System.Data.DataRow dsinfoRow)
 {
  Debug.Assert(!Object.ReferenceEquals(dsinfoRow,null));

  var dbmsName
   =dsinfoRow[OleDbMetaDataCollectionColumnNames.DataSourceInformation.DataSourceProductName];

  var dbmsVersion
   =dsinfoRow[OleDbMetaDataCollectionColumnNames.DataSourceInformation.DataSourceProductVersion];

  if(DBNull.Value.Equals(dbmsName))
   Helper__ThrowErr("DBMS Name is not defined!");

  if(DBNull.Value.Equals(dbmsVersion))
   Helper__ThrowErr("DBMS Version is not defined!");

  if((string)(dbmsName)!="Firebird")
   Helper__ThrowErr("Wrong DBMS name: {0}. Expected \"Firebird\".",dbmsName);

  if(new Version((string)dbmsVersion)<new Version("3.0"))
   Helper__ThrowErr("Wrong Firebird version: {0}. Expected version not less than 3.0.",dbmsVersion);

  Console.WriteLine("OK. We work with \"{0} v{1}\"",dbmsName,dbmsVersion);
 }//Helper__CheckComponents_DBMS

 //-----------------------------------------------------------------------
 private static void Helper__CheckComponents_Provider(System.Data.DataRow dsinfoRow)
 {
  Debug.Assert(!Object.ReferenceEquals(dsinfoRow,null));

  var providerName
   =dsinfoRow[OleDbMetaDataCollectionColumnNames.DataSourceInformation.LCPI_OleDbProviderFriendlyName];

  if(DBNull.Value.Equals(providerName))
   Helper__ThrowErr("OLE DB Provider Name is not defined!");

  var providerVer
   =dsinfoRow[OleDbMetaDataCollectionColumnNames.DataSourceInformation.LCPI_OleDbProviderVersion];

  if(DBNull.Value.Equals(providerVer))
   Helper__ThrowErr("OLE DB Provider Name is not defined!");

  //----
  if(!((string)providerName).Contains("LCPI OLE DB Provider for InterBase"))
   Helper__ThrowErr("Unexpected OLE DB Provider: [{0}]",providerName);

  if(new Version((string)providerVer)<new Version("5.12"))
   Helper__ThrowErr("Wrong IBProvider version: {0}. Expected version not less than 5.12.",providerVer);

  Console.WriteLine("OK. We work with \"{0} v{1}\"",providerName,providerVer);
 }//Helper__CheckComponents_Provider

 //-----------------------------------------------------------------------
 private static void Helper__ThrowErr(string msgTemplate,params object[] args)
 {
  var msg=string.Format(msgTemplate,args);

  throw new ApplicationException(msg);
 }//Helper__ThrowErr
}//class Program

////////////////////////////////////////////////////////////////////////////////
}//namespace Sample_0028


Output of sample.
Output.