You are on page 1of 13

MyModifyStructure

Declaration of MyModifyStructure
MyModifyStructure is Class

m_sTabela is string
m_nTipo_bd is int
m_ctConection is Connection

m_arrStru_dic is array of 0 by 7 string // array da tabela na analise


m_arrStru_atu is array of 0 by 6 string // array da tabela no Banco de dados
m_arrMyArray is array of 0 string // LINHA DE COMANDO MONTANDO NA ARRAY

PRIVATE

m_arrIndice is array of 0 by 6 string // INDICE QUE IRAO VIR DA ANALISE


m_arrLINK is array of 0 by 6 string // RELACIONACIONAMENTO entre as TABELAS
m_sComando is string

m_arrMyIndice is array of 0 string

sDic_Campo,sAtu_campo are string


sDic_Tipo ,sAtu_tipo are string
sDic_Tipox is string
nDic_Tamanho,nAtu_tamanho are int
//nAtu_decimal is int
nDic_tamx,nAtu_tamx are int
nDic_Decx,nAtu_Decx are int

END

Constructor
PROCEDURE Constructor()

Destructor
PROCEDURE Destructor()

Method Verifica

09/07/2023 1
MyModifyStructure

// Summary: <specify the procedure action>


// Syntax:
//[ <Result> = ] Verifica ()
//
// Parameters:
// None

PROCEDURE Verifica()

:Prepara_Dados_Analise()
:Prepara_Dados_Tabela()
:ChecaEstrura()

RESULT True

Method Prepara_Dados_Analise
// Summary: <specify the procedure action>
// Syntax:
// Prepara_Dados_Analise ()
//
// Parameters:
// None
// Return value:
// None
//
// For instance:
// Indicate an example.
//
PROCEDURE Prepara_Dados_Analise()

sItemList is string = HListItem(m_sTabela, hLstDetailPlus)


sKeyList is string = HListKey(m_sTabela, hLstDetail)
sLinkList is string = HListLink(m_sTabela)

_Field is string
_Type is string
_Typex is string
_size is string
_dec is string
_dec1 is string

09/07/2023 2
MyModifyStructure

_dec2 is string
IF :m_sTabela = "cad_produto" THEN
s is int = 0
END

FOR EACH STRING sItem OF sItemList SEPARATED BY CR


_Field = ExtractString(sItem,1,TAB)
_Type = ExtractString(sItem,2,TAB)
_Typex = ExtractString(sItem,3,TAB)
_size = ExtractString(sItem,4,TAB)
_dec = ExtractString(sItem,5,TAB)
_dec1 = ExtractString(sItem,7,TAB)
_dec2 = ExtractString(sItem,8,TAB)
// 1 2 3 4 5 6 7
ArrayAddLine(m_arrStru_dic , Lower(_Field) ,_Type ,_Typex ,_size ,_dec, _dec1 ,_dec2)
END
FOR EACH STRING sKey OF sKeyList SEPARATED BY CR
_Field = ExtractString(sKey,1,TAB)
_Type = ExtractString(sKey,2,TAB)
_Typex = ExtractString(sKey,3,TAB)
_size = ExtractString(sKey,4,TAB)
_dec = ExtractString(sKey,5,TAB)
// 1 2 3 4 5
ArrayAddLine(m_arrIndice, Lower(_Field) ,_Type ,_Typex ,_size ,_dec)
END

FOR EACH STRING sLink OF sLinkList SEPARATED BY CR


_Field = ExtractString(sLink,1,TAB)
_Type = ExtractString(sLink,2,TAB)
_Typex = ExtractString(sLink,3,TAB)
_size = ExtractString(sLink,4,TAB)
_dec = ExtractString(sLink,5,TAB)
ArrayAddLine(m_arrLINK , Lower(_Field) ,_Type ,_Typex ,_size ,_dec)
END

Method Prepara_Dados_Tabela
// Summary: <specify the procedure action>
// Syntax:
// Prepara_Dados_Tabela ()

09/07/2023 3
MyModifyStructure

//
// Parameters:
// None
// Return value:
// None
//
// For instance:
// Indicate an example.
//
PROCEDURE Prepara_Dados_Tabela()

SWITCH :m_nTipo_bd

CASE 4 // mysql
dsQRY is Data Source
sCSql is string = [
SHOW FULL COLUMNS FROM '%1'
]
sCSql = StringBuild(sCSql, :m_sTabela)
HExecuteSQLQuery(dsQRY, :m_ctConection , hQueryWithoutCorrection, sCSql)
FOR ALL dsQRY
//1 2 3 4 5 6
ArrayAddLine(m_arrStru_atu ,Lower(dsQRY.field),dsQRY.type,dsQRY.null ,dsQRY.key)
END

CASE 5 // postgres

dsQRY is Data Source


sCSql is string = [
SELECT table_catalog, table_name, column_name, data_type, character_maximum_length, numeric_precision, numeric_scale, ordinal_position
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '%1' and column_name not like 'indkey%'
ORDER BY ordinal_position
]
sCSql = StringBuild(sCSql, :m_sTabela)
HExecuteSQLQuery(dsQRY, :m_ctConection , hQueryWithoutCorrection, sCSql)
FOR ALL dsQRY
//1 2 3 4 5 6
ArrayAddLine(m_arrStru_atu ,Lower(dsQRY.column_name),dsQRY.data_type,dsQRY.character_maximum_length,dsQRY.numeric_precision,dsQRY.
numeric_scale,dsQRY.ordinal_position)
END
OTHER CASE

09/07/2023 4
MyModifyStructure

END

Method ChecaEstrura
// Summary: <specify the procedure action>
// Syntax:
//ChecaEstrura ()
//
// Parameters:
// None

// Return value:
// For instance:
PROCEDURE ChecaEstrura()
npos is int

IF :m_sTabela = "cad_produto" THEN


s is int = 0
END

FOR nIFOR = 1 _TO_ ArrayCount(m_arrStru_dic)

// IF :m_arrStru_dic[nIFOR,1] = "sec_data_delete" THEN


// XXX IS INT
// END

:sDic_Campo = :m_arrStru_dic[nIFOR,1]
:sDic_Tipo = :m_arrStru_dic[nIFOR,3]
:sDic_Tipox = :ConverteTipoDic(:m_arrStru_dic[nIFOR,3] )
:nDic_Tamanho = :m_arrStru_dic[nIFOR,4]
:nDic_tamx = :m_arrStru_dic[nIFOR,6] // Usado Para pegar o Valor Preciso TAMANHO
:nDic_Decx = :m_arrStru_dic[nIFOR,7] // Usado Para pegar o Valor Preciso DECIMAL
npos = ArraySeek(m_arrStru_atu,asLinearFirst,1,:sDic_Campo)

IF npos <= 0 THEN


:MyAddColumn()
ELSE
:sAtu_campo = :m_arrStru_atu[npos,1]

09/07/2023 5
MyModifyStructure

:sAtu_tipo = ConverteTipoAtu( :m_arrStru_atu[npos,2] )


:nAtu_tamanho = :m_arrStru_atu[npos,3]
:nAtu_tamx = :m_arrStru_atu[npos,4]
:nAtu_Decx = :m_arrStru_atu[npos,5]

:MyChangeColumn()
END

END

:MyDropColumn()

Method ConverteTipoDic
// Summary: <specify the procedure action>
// Syntax:
//[ <Result> = ] ConverteTipoDic (<xType> is string)
//
// Parameters:
// xType (ANSI string):

PROCEDURE ConverteTipoDic( xType is string )


cvalue is string
IF xType = "1"
cvalue = "+" // autoIncremento
ELSE IF (xType = "2" OR xType = "29")
cvalue = "C"
ELSE IF (xType = "3" OR xType = "5" OR xType = "8" OR xType = "9" OR xType = "12" OR xType = "19" )
cvalue = "I"
ELSE IF(xType = "4" OR xType = "27") //BOOLEAN
cvalue = "L"
ELSE IF (xType = "6" OR xType = "7") //NUMERICO
cvalue = "N"
ELSE IF (xType = "10" OR xType = "14") // D - DATE
cvalue = "D"
ELSE IF xType = "11" // D - TIME
cvalue = "T"
ELSE IF xType = "13"
cvalue = "N"

09/07/2023 6
MyModifyStructure

ELSE IF (xType = "15" OR xType = "30" )


cvalue = "M"
ELSE IF xType = "17"
cvalue = "$"
ELSE IF xType = "24"
cvalue = "DT"
ELSE IF xType = "31"
cvalue = "N"

END

RESULT cvalue

// https://help.windev.com/?3044230&lang=en-US&productversion=xxF260100j

Method ConverteTipoAtu
// Summary: <specify the procedure action>
// Syntax:
//[ <Result> = ] ConverteTipoAtu (<XType> is string)
//
// Parameters:
// XType (ANSI string):

PROCEDURE ConverteTipoAtu( XType is string )


cvalue is string

SWITCH :m_nTipo_bd
CASE 4 // mysql
CASE 5 // postgres

IF (XType = "bigint" OR XType = "integer") THEN


cvalue = "I"
ELSE IF XType = "character varying"
cvalue = "C"
ELSE IF XType = "character"
cvalue = "C"
ELSE IF XType = "numeric"
cvalue = "N"
ELSE IF XType = "date"
cvalue = "D"

09/07/2023 7
MyModifyStructure

ELSE IF XType = "time without time zone"


cvalue = "T"
ELSE IF XType = "timestamp without time zone"
cvalue = "DT"
END

OTHER CASE

END

RESULT cvalue

Method MyAddColumn
// Summary: <specify the procedure action>
// Syntax:
//MyAddColumn ()
//
// Parameters:
// None

// Return value:
// For instance:
PROCEDURE MyAddColumn()

sTexto is string

SWITCH :m_nTipo_bd
CASE 4 // mysql

CASE 5 // postgres
IF :sDic_Tipox = "C" THEN

IF :nDic_Tamanho > 10
sTexto = "ADD COLUMN "+:sDic_Campo+" character varying("+:nDic_Tamanho+") "
ELSE
sTexto = "ADD COLUMN "+:sDic_Campo+" character("+:nDic_Tamanho+") "
END

ELSE IF :sDic_Tipox = "+" THEN

09/07/2023 8
MyModifyStructure

sTexto = "ADD COLUMN "+:sDic_Campo+" bigint NOT NULL DEFAULT nextval("+ :sDic_Campo +")"

ELSE IF :sDic_Tipox = "N" THEN

sTexto = "ADD COLUMN "+:sDic_Campo+" numeric("+:nDic_tamx+","+:nDic_Decx+") "

ELSE IF :sDic_Tipox = "$" THEN

sTexto = "ADD COLUMN "+:sDic_Campo+" numeric(24,6) "

ELSE IF :sDic_Tipox = "I" THEN

IF :sDic_Tipo = "1" THEN


sTexto = "ADD COLUMN "+:sDic_Campo+" bigint NOT NULL DEFAULT nextval('"+:sDic_Campo+"'::regclass) "
ELSE IF :sDic_Tipo = "3" THEN
sTexto = "ADD COLUMN "+:sDic_Campo+" smallint "
ELSE IF :sDic_Tipo = "5" THEN
sTexto = "ADD COLUMN "+:sDic_Campo+" bigint "
ELSE
sTexto = "ADD COLUMN "+:sDic_Campo+" integer "
END
//smallint

ELSE IF :sDic_Tipox = "D" THEN

sTexto = "ADD COLUMN "+:sDic_Campo+" date "

ELSE IF :sDic_Tipox = "M" THEN


sTexto = "ADD COLUMN "+:sDic_Campo+" text DEFAULT '' "
ELSE IF :sDic_Tipox = "L" THEN
sTexto = "ADD COLUMN "+:sDic_Campo+" bool "

ELSE IF :sDic_Tipox = "T" THEN


sTexto = "ADD COLUMN "+:sDic_Campo+" time without time zone "

ELSE IF :sDic_Tipox = "DT" THEN


sTexto = "ADD COLUMN "+:sDic_Campo+" timestamp without time zone"

END

OTHER CASE

09/07/2023 9
MyModifyStructure

END

IF sTexto <> "" THEN


ArrayAddLine(m_arrMyArray,sTexto)
END

Method MyChangeColumn
// Summary: <specify the procedure action>
// Syntax:
//MyChangeColumn ()
//
// Parameters:
// None

// Return value:
// For instance:
PROCEDURE MyChangeColumn()

sTexto is string

SWITCH :m_nTipo_bd

CASE 4 // mysql
CASE 5 // postgres

IF :sDic_Tipox = "C" THEN


IF :nDic_Tamanho <> :nAtu_tamanho THEN
IF nDic_Tamanho > 10
sTexto = "ALTER COLUMN "+:sDic_Campo+" type character varying("+:nDic_Tamanho+") "
ELSE
sTexto = "ALTER COLUMN "+:sDic_Campo+" type character("+:nDic_Tamanho+") "
END
END
ELSE IF :sDic_Tipox = "N"
IF (:nDic_tamx + :nDic_Decx ) <> :nAtu_tamx OR :nDic_Decx <> :nAtu_Decx THEN
sTexto = "ALTER COLUMN "+:sDic_Campo+" type numeric("+nAtu_tamx+","+:nAtu_Decx+") "
END

ELSE IF :sDic_Tipox = "$"

09/07/2023 10
MyModifyStructure

// AJUSTE PARA CURRENCY NA ANALISYS


:nDic_Decx = 6
:nDic_tamx = 18
IF (:nDic_tamx + :nDic_Decx ) <> :nAtu_tamx OR :nDic_Decx <> :nAtu_Decx THEN
sTexto = "ALTER COLUMN "+:sDic_Campo+" type numeric("+nAtu_tamx+","+:nAtu_Decx+") "
END

ELSE IF :sDic_Tipox = "I"


IF :nDic_tamx <> :nAtu_tamx THEN
IF :nDic_tamx = 19 AND :nAtu_tamx <> 64
sTexto = "ALTER COLUMN "+:sDic_Campo+" type bigint "
END
END

ELSE IF :sDic_Tipox = "+"


IF :nDic_tamx <> 19 THEN // 19 NO DIC 64 NO POSTGRES :nAtu_tamx
sTexto = "ALTER COLUMN "+:sDic_Campo+" type bigint NOT NULL DEFAULT nextval("+ :sDic_Campo +")"
END
ELSE IF :sDic_Tipox = "DT"
IF :sAtu_tipo = "D" THEN
sTexto = "ALTER COLUMN "+:sDic_Campo+" type timestamp without time zone"
END
ELSE IF :sDic_Tipox = "D"
IF :sAtu_tipo = "DT" THEN
sTexto = "ALTER COLUMN "+:sDic_Campo+" type date"
END
ELSE IF :sDic_Tipox = "T"

END

OTHER CASE

END

IF sTexto <> ""


ArrayAddLine(m_arrMyArray,sTexto)
END

Method MyDropColumn

09/07/2023 11
MyModifyStructure

// Summary: <specify the procedure action>


// Syntax:
// MyDropColumn ()
//
// Parameters:
// None
// Return value:
// None
//
// For instance:
// Indicate an example.
//
PROCEDURE MyDropColumn()

npos is int
sTexto is string
nQtd is int = ArrayCount(m_arrStru_atu)

FOR nIFOR = 1 _TO_ nQtd


:sAtu_campo = :m_arrStru_atu[nIFOR,1]
npos = ArraySeek(m_arrStru_dic,asLinearFirst,1,:sAtu_campo)
IF npos <= 0 THEN

//checar o TIPO DO banco de dados SE POSTGRES OU MYSQL EM CASO DE MUDANÇA DE COMANDO


sTexto = "DROP COLUMN "+:sAtu_campo
ArrayAddLine(m_arrMyArray,sTexto)
END
END

Method Executa
// Summary: <specify the procedure action>
// Syntax:
// Executa ()
//
// Parameters:
// None
// Return value:
// None

09/07/2023 12
MyModifyStructure

//
// For instance:
// Indicate an example.
//
PROCEDURE Executa()

IF ArrayCount(m_arrMyArray) > 0
sComando is string
FOR nIFOR = 1 _TO_ ArrayCount(m_arrMyArray)
sComando += :m_arrMyArray[nIFOR]+","
END
dsQRY is Data Source
sCSql is string = "ALTER TABLE "+:m_sTabela +" "+ Left(sComando,Length(sComando)-1)
sCSql = Replace(sCSql,"'","`")

IF HExecuteSQLQuery(dsQRY, :m_ctConection , hQueryWithoutCorrection, sCSql)=False THEN


_MyMessage("Erro ao executar o comando." + CR + HErrorInfo() + CR + sCSql,1)
END
END

09/07/2023 13

You might also like