~marcelo-escobal/geined/Alianza

« back to all changes in this revision

Viewing changes to datos.rb

  • Committer: Cualquier Desarrollador
  • Date: 2010-01-29 21:15:22 UTC
  • Revision ID: desarrollo@marte-20100129211522-u97qh9o90mhdap04
Importación inicial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'mysql'
 
2
class Registro
 
3
    def initialize(tab)
 
4
        @tabla = tab
 
5
        @filtro = ''
 
6
        @orden = ''
 
7
        @db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
8
        @registro = []
 
9
    end
 
10
    def leer()
 
11
        fil = ''
 
12
        ord = ''
 
13
        if @filtro!=''
 
14
            fil = "WHERE " + @filtro
 
15
        end
 
16
        if @orden!=''
 
17
            ord = "ORDER BY " + @orden
 
18
        end
 
19
        sql = "SELECT * FROM " + @tabla + " " + ord + " " + fil
 
20
        res = @db.query(sql)
 
21
        @registro = res.fetch_hash
 
22
        res.free
 
23
    end
 
24
    def grabar()
 
25
        pass
 
26
    end
 
27
    def filtro(fil)
 
28
        @filtro = fil
 
29
        leer()
 
30
    end
 
31
    def registro
 
32
        @registro
 
33
    end
 
34
end
 
35
class Tabla
 
36
    def initialize(tab)
 
37
        @nombre = tab
 
38
        @filtro=''
 
39
        @orden=''
 
40
        @db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
41
        @registro = []
 
42
        @tabla= @db.query('SELECT * FROM ' + @nombre)
 
43
    end
 
44
    def leer()
 
45
        fil = ''
 
46
        ord = ''
 
47
        if @filtro!=''
 
48
            fil = " WHERE " + @filtro
 
49
        end
 
50
        if @orden!=''
 
51
            ord = " ORDER BY " + @orden
 
52
        end
 
53
        sql = "SELECT * FROM " + @nombre + fil + ord
 
54
        @tabla = @db.query(sql)
 
55
    end
 
56
    def filtro=(fil)
 
57
        @filtro = fil
 
58
        leer()
 
59
    end
 
60
    def orden=(ord)
 
61
        @orden = ord
 
62
        leer()
 
63
    end
 
64
    def registro
 
65
        @registro
 
66
    end
 
67
    def leer_registro
 
68
        @registro = @tabla.fetch_hash
 
69
    end
 
70
    def tabla
 
71
        @tabla
 
72
    end
 
73
    def actualizar
 
74
        sql = "UPDATE " + @nombre + " SET "
 
75
        @registro.each do |x,y|
 
76
            if y==nil
 
77
                y = ''
 
78
            end
 
79
            if x != 'id'
 
80
                sql = sql + x + '="' + y + '",'
 
81
            end
 
82
        end
 
83
        sql = sql[0..-2] + ' WHERE ' + @filtro
 
84
        @db.query(sql)
 
85
    end
 
86
    def insertar
 
87
        sql = "INSERT INTO " + @nombre + " SET "
 
88
        @registro.each do |x,y|
 
89
            if y==nil
 
90
                y = ''
 
91
            end
 
92
            if x != 'id'
 
93
                sql = sql + x + '="' + y + '",'
 
94
            end
 
95
        end
 
96
        sql = sql[0..-2]
 
97
        puts sql
 
98
        @db.query(sql)
 
99
    end
 
100
end
 
101
class Recordset
 
102
    attr_accessor :table, :where, :order_by, :limit
 
103
    def initialize(tabla)
 
104
        @table = tabla
 
105
        @db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
106
        @where = ""
 
107
        @order_by = ""
 
108
        @limit = ""
 
109
    end
 
110
    def query(sql)
 
111
        res = @db.query(sql)
 
112
        return res
 
113
    end
 
114
    def execute(sql)
 
115
      @db.query(sql)
 
116
    end
 
117
    def get_query()
 
118
        sql = "SELECT * FROM #{@table}"
 
119
        if @where != ""
 
120
          sql = sql + " WHERE #{@where} "
 
121
        end
 
122
        if @order_by != ""
 
123
          sql = sql + " ORDER BY #{@order_by} "
 
124
        end
 
125
        if @limit != ""
 
126
          sql = sql + " LIMIT #{@limit}"
 
127
        end
 
128
        res = @db.query(sql)
 
129
    end
 
130
    def get_record(campo,valor)
 
131
      sql = "SELECT * FROM #{@table} WHERE #{campo}='#{valor}'"
 
132
      res = @db.query(sql)
 
133
      fil = res.fetch_hash()
 
134
      return fil
 
135
    end
 
136
end
 
137
 
 
138
def liquidacion(campo,clave)
 
139
    db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
140
    sql_liq = 'SELECT * FROM liquidacion WHERE '+campo+' = "'+ clave + '"'
 
141
    res_liq = db.query(sql_liq)
 
142
    return res_liq
 
143
end
 
144
def empleados(campo,clave)
 
145
    sql_emp = 'SELECT * FROM empleados WHERE '+ campo+'="'+clave+'"'
 
146
    res_emp = mysql_query(sql_emp);
 
147
    return mysql_fetch_array(res_emp);
 
148
end
 
149
def det_liquidacion(campo,clave)
 
150
    sql_det = 'SELECT * FROM det_liquidacion WHERE '+campo+'="'+clave+'"'
 
151
    res_det = mysql_query(sql_det);
 
152
    return mysql_fetch_array(res_det)
 
153
end
 
154
def tabla(tabla,campo,clave)
 
155
    sql = 'SELECT * FROM ' + tabla + ' WHERE ' + campo + '="' + clave + '"'
 
156
    db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
157
    res = db.query(sql)
 
158
    return res
 
159
end
 
160
def tabla_ordenada(tabla,campo,orden)
 
161
    sql = "SELECT * FROM #{tabla} ORDER BY #{campo} #{orden}"
 
162
    db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
163
    res = db.query(sql)
 
164
    return res
 
165
end
 
166
def consulta(sql)
 
167
    db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
168
    res = db.query(sql)
 
169
    return res
 
170
end
 
171
def comando(sql)
 
172
    db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
173
    db.query(sql)
 
174
    db.close
 
175
end
 
176
def tabla_simple(nombre)
 
177
    db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
178
    res = db.query('SELECT * FROM '+ nombre)
 
179
    return res
 
180
end
 
181
def buscar_registro(tabla,campo,filtro)
 
182
    db = Mysql.real_connect("localhost","mescobal","pacu2000","Alianza")
 
183
    sql = "SELECT * FROM #{tabla} WHERE #{campo} = '#{filtro}'"
 
184
    res = db.query(sql)
 
185
    fil = res.fetch_hash
 
186
    return fil
 
187
end