Class: SPED2SQL::SQL::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sped2sql/sql/parser.rb

Constant Summary collapse

HEADER =
"/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\r\n"                   \
"/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\r\n"                 \
"/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\r\n"                   \
"/*!40101 SET NAMES utf8 */;\r\n"                                                         \
"/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\r\n"                                         \
"/*!40103 SET TIME_ZONE='+00:00' */;\r\n"                                                 \
"/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\r\n"                \
"/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\r\n" \
"/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\r\n"
"/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\r\n"                         \
"/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\r\n"       \
"/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\r\n"                 \
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\r\n"   \
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\r\n" \
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\r\n"   \
"/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\r\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dados, options = {}) ⇒ Parser

Returns a new instance of Parser.



25
26
27
28
29
30
# File 'lib/sped2sql/sql/parser.rb', line 25

def initialize(dados, options = {})
  @tbl_prefix = options[:tbl_prefix] || ''
  @tbl_sufix  = options[:tbl_sufix]  || ''
  @dados      = dados
  self
end

Instance Attribute Details

#dadosObject (readonly)

Returns the value of attribute dados.



5
6
7
# File 'lib/sped2sql/sql/parser.rb', line 5

def dados
  @dados
end

#tbl_prefixObject (readonly)

Returns the value of attribute tbl_prefix.



5
6
7
# File 'lib/sped2sql/sql/parser.rb', line 5

def tbl_prefix
  @tbl_prefix
end

#tbl_sufixObject (readonly)

Returns the value of attribute tbl_sufix.



5
6
7
# File 'lib/sped2sql/sql/parser.rb', line 5

def tbl_sufix
  @tbl_sufix
end

Class Method Details

.to_sql(dados, options = {}) ⇒ Object



48
49
50
# File 'lib/sped2sql/sql/parser.rb', line 48

def to_sql(dados, options = {})
  new(dados, options).parse!
end

Instance Method Details

#parse!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sped2sql/sql/parser.rb', line 32

def parse!
  output = []
  inserts_agrupados.each do |registro, inserts|
    tabela = nome_tabela(registro)

    output << "LOCK TABLES `#{ tabela }` WRITE;"
    output << "/*!40000 ALTER TABLE `#{ tabela }` DISABLE KEYS */;"
    output << "INSERT INTO #{ tabela } VALUES ('',#{ inserts.join("),('',") });"
    output << "/*!40000 ALTER TABLE `#{ tabela }` ENABLE KEYS */;"
    output << "UNLOCK TABLES;"
  end

  "#{ HEADER }#{ output.join("\r\n") }\r\n#{ FOOTER }"
end